add precomputed variable

This commit is contained in:
Young Geun Kim
2025-02-02 16:43:54 +09:00
parent 631f7dfe2b
commit 473d4203c0
3 changed files with 20 additions and 4 deletions
+2
View File
@@ -10,3 +10,5 @@ CMakeFiles/**
# Editor Options
.vscode
.idea
.DS_Store
@@ -16,6 +16,7 @@
#include <boost/config/no_tr1/cmath.hpp>
#include <istream>
#include <iosfwd>
#include <limits>
#include <boost/assert.hpp>
#include <boost/limits.hpp>
#include <boost/random/detail/config.hpp>
@@ -115,11 +116,14 @@ public:
{
BOOST_ASSERT(alpha_arg > 0);
BOOST_ASSERT(beta_arg > 0);
init();
}
/** Constructs an @c inverse_gaussian_distribution from its parameters. */
explicit inverse_gaussian_distribution(const param_type& parm)
: _alpha(parm.alpha()), _beta(parm.beta())
{}
{
init();
}
/**
* Returns a random variate distributed according to the
@@ -131,8 +135,8 @@ public:
#ifndef BOOST_NO_STDC_NAMESPACE
using std::sqrt;
#endif
RealType y = _alpha * chi_squared_distribution<RealType>(RealType(1.0))(urng);
RealType cand = _alpha + _alpha * (y - sqrt(y * (result_type(4) * _beta + y))) / (result_type(2) * _beta);
RealType w = _alpha * chi_squared_distribution<RealType>(result_type(1))(urng);
RealType cand = _alpha + _c * (w - sqrt(w * (result_type(4) * _beta + w)));
RealType u = uniform_01<RealType>()(urng);
if (u < _alpha / (_alpha + cand)) {
return cand;
@@ -169,6 +173,7 @@ public:
{
_alpha = parm.alpha();
_beta = parm.beta();
init();
}
/**
@@ -210,6 +215,16 @@ public:
private:
result_type _alpha;
result_type _beta;
// some data precomputed from the parameters
result_type _c;
void init()
{
#ifndef BOOST_NO_STDC_NAMESPACE
using std::exp;
#endif
_c = _alpha / (result_type(2) * _beta);
}
};
} // namespace random
@@ -1,7 +1,6 @@
/**
* test_inverse_gaussian_distribution.cpp
*
* Copyright Young Geun Kim 2025
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)