22template <
typename RealType>
31 double halfLambda = 1;
40 [[nodiscard]] std::string
Name()
const override;
42 [[nodiscard]] RealType
MinValue()
const override {
return 0; }
43 [[nodiscard]] RealType
MaxValue()
const override {
return INFINITY; }
46 [[nodiscard]]
inline double GetDegree() const noexcept {
return k; }
49 [[nodiscard]]
double f(
const RealType &x)
const override;
50 [[nodiscard]]
double logf(
const RealType &x)
const override;
51 [[nodiscard]]
double F(
const RealType &x)
const override;
52 [[nodiscard]]
double S(
const RealType &x)
const override;
55 template <std::uniform_random_bit_generator G>
56 RealType variateForDegreeEqualOne(G &g)
const;
59 template <std::uniform_random_bit_generator G>
60 [[nodiscard]]
static RealType
Variate(
double degree,
double noncentrality, G &g);
61 template <std::uniform_random_bit_generator G>
64 [[nodiscard]]
long double Mean()
const override;
65 [[nodiscard]]
long double Variance()
const override;
66 [[nodiscard]] RealType
Mode()
const override;
67 [[nodiscard]]
long double Skewness()
const override;
71 std::complex<double> CFImpl(
double t)
const override;
82template <
typename RealType>
85 SetParameters(degree, noncentrality);
88template <
typename RealType>
91 return "Noncentral Chi-Squared(" + this->toStringWithPrecision(GetDegree()) +
", " +
92 this->toStringWithPrecision(GetNoncentrality()) +
")";
95template <
typename RealType>
99 throw std::invalid_argument(
"Noncentral Chi-Squared distribution: degree parameter should be positive");
100 if (!(noncentrality >= 0.0) || !std::isfinite(noncentrality))
101 throw std::invalid_argument(
102 "Noncentral Chi-Squared distribution: noncentrality parameter should be non-negative and finite");
107 lambda = noncentrality;
108 halfLambda = 0.5 * lambda;
109 sqrtLambda = std::sqrt(lambda);
110 logLambda = (lambda == 0.0) ? -INFINITY : std::log(lambda);
112 if (k < 1 && lambda > 0.0)
113 Y.SetRate(halfLambda);
116template <
typename RealType>
123 return 0.5 * std::exp(-halfLambda);
124 return (k > 2) ? 0.0 : INFINITY;
126 return std::exp(logf(x));
129template <
typename RealType>
137 return (k > 2) ? -INFINITY : INFINITY;
140 return -halfK *
randlib::consts::ln2 - std::lgamma(halfK) + (halfK - 1) * std::log(x) - 0.5 * x;
141 double halfKm1 = halfK - 1;
143 double z = halfKm1 * (std::log(x) - logLambda);
148template <
typename RealType>
155 double halfX = 0.5 * x;
157 return math::pgamma(halfK, halfX, std::log(halfK), std::lgamma(halfK));
158 double sqrtHalfX = std::sqrt(halfX);
159 double logHalfX = std::log(halfX);
164template <
typename RealType>
171 double halfX = 0.5 * x;
173 return math::qgamma(halfK, halfX, std::log(halfK), std::lgamma(halfK));
174 double sqrtHalfX = std::sqrt(halfX);
175 double logHalfX = std::log(halfX);
180template <
typename RealType>
181template <std::uniform_random_bit_generator G>
188template <
typename RealType>
189template <std::uniform_random_bit_generator G>
193 throw std::invalid_argument(
"Noncentral Chi-Squared distribution: degree parameter should be positive");
194 if (!(noncentrality >= 0.0) || !std::isfinite(noncentrality))
195 throw std::invalid_argument(
196 "Noncentral Chi-Squared distribution: noncentrality parameter should be non-negative and finite");
198 if (noncentrality == 0.0)
210template <
typename RealType>
211template <std::uniform_random_bit_generator G>
218 double X = variateForDegreeEqualOne(g);
224template <
typename RealType>
228 for (RealType &var : outputData)
233 for (RealType &var : outputData)
234 var = variateForDegreeEqualOne(g);
235 double halfKmHalf = halfK - 0.5;
238 for (RealType &var : outputData)
239 var += 2 * GammaDistribution<RealType>::StandardVariate(halfKmHalf, g);
241 for (RealType &var : outputData)
242 var = 2 * GammaDistribution<RealType>::StandardVariate(halfK + Y(g), g);
246template <
typename RealType>
252template <
typename RealType>
255 return 2 * (k + 2 * lambda);
258template <
typename RealType>
262 return std::max(0.0, k - 2);
268 return (mode > 0 && this->f(mode) > this->f(0)) ? mode : 0.0;
271template <
typename RealType>
274 long double y = k + 2 * lambda;
276 long double z = y * std::sqrt(y);
277 return z * (k + 3 * lambda);
280template <
typename RealType>
283 long double y = k + 2 * lambda;
284 return 12 * (k + 4 * lambda) / (y * y);
287template <
typename RealType>
290 std::complex<double> aux(1, -2 * t);
291 std::complex<double> y(0, lambda * t);
293 y -= halfK * std::log(aux);
The ContinuousDistribution class Abstract class for all continuous distributions.
Definition continuous.hpp:22
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition continuous.hpp:285
The GammaDistribution class Abstract class for Gamma distribution.
Definition continuous/gamma.hpp:31
static RealType StandardVariate(double shape, G &g)
Definition continuous/gamma.hpp:605
The NoncentralChiSquaredRand class Noncentral Chi-Squared distribution.
Definition noncentral_chi_squared.hpp:24
double f(const RealType &x) const override
Definition noncentral_chi_squared.hpp:117
long double ExcessKurtosis() const override
Definition noncentral_chi_squared.hpp:281
long double Mean() const override
Definition noncentral_chi_squared.hpp:247
double logf(const RealType &x) const override
Definition noncentral_chi_squared.hpp:130
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition noncentral_chi_squared.hpp:259
long double Variance() const override
Definition noncentral_chi_squared.hpp:253
NoncentralChiSquaredRand(double degree=1, double noncentrality=0)
Definition noncentral_chi_squared.hpp:83
RealType MinValue() const override
Definition noncentral_chi_squared.hpp:42
long double Skewness() const override
Definition noncentral_chi_squared.hpp:272
double GetNoncentrality() const noexcept
Definition noncentral_chi_squared.hpp:47
double F(const RealType &x) const override
Definition noncentral_chi_squared.hpp:149
RealType MaxValue() const override
Definition noncentral_chi_squared.hpp:43
double S(const RealType &x) const override
Definition noncentral_chi_squared.hpp:165
double GetDegree() const noexcept
Definition noncentral_chi_squared.hpp:46
RealType operator()(G &g) const
Definition noncentral_chi_squared.hpp:212
std::string Name() const override
Definition noncentral_chi_squared.hpp:89
void SetParameters(double degree, double noncentrality)
Definition noncentral_chi_squared.hpp:96
Support SupportType() const override
Definition noncentral_chi_squared.hpp:41
The PoissonRand class Poisson distribution.
Definition poisson.hpp:26
T Variate() const
Uses the engine of this object, so it is not thread-safe for one object; use operator() with one engi...
Definition distribution.hpp:133
The EngineRef class Non-owning, type-erased reference to any uniform random bit generator.
Definition engine/uniform.hpp:116
constexpr long double ln2
Definition constants.hpp:18
constexpr long double sqrt2
Definition constants.hpp:27
Real standardNormal(G &g)
Ziggurat algorithm of Marsaglia and Tsang with 256 layers and an explicit sign bit.
Definition variates.hpp:45
Real qgamma(std::type_identity_t< Real > a, Real x, std::type_identity_t< Real > logA, std::type_identity_t< Real > lgammaA)
Calculate upper regularized incomplete gamma function, accelerated by using precalculated values of l...
Definition math/gamma.hpp:938
Real logBesselI(std::type_identity_t< Real > nu, Real x)
Calculates logarithm of modified Bessel function of the 1st kind.
Definition functions.hpp:577
Real MarcumP(std::type_identity_t< Real > mu, Real x, std::type_identity_t< Real > y, std::type_identity_t< Real > sqrtX, std::type_identity_t< Real > sqrtY, std::type_identity_t< Real > logX, std::type_identity_t< Real > logY)
Definition functions.hpp:1023
Real MarcumQ(std::type_identity_t< Real > mu, Real x, std::type_identity_t< Real > y, std::type_identity_t< Real > sqrtX, std::type_identity_t< Real > sqrtY, std::type_identity_t< Real > logX, std::type_identity_t< Real > logY)
Definition functions.hpp:1079
Real pgamma(std::type_identity_t< Real > a, Real x, std::type_identity_t< Real > logA, std::type_identity_t< Real > lgammaA)
Calculate lower regularized incomplete gamma function, accelerated by using precalculated value of lo...
Definition math/gamma.hpp:896
Definition bivariate_normal.hpp:10
Support
Shape of the support of a univariate distribution.
Definition univariate.hpp:15