29template <
typename RealType>
37 NormalRand(
double location = 0,
double variance = 1);
60 [[nodiscard]] std::string
Name()
const override;
67 [[nodiscard]]
inline double GetScale() const noexcept {
return sigma; }
87 [[nodiscard]]
double f(
const RealType &x)
const override;
88 [[nodiscard]]
double logf(
const RealType &x)
const override;
89 [[nodiscard]]
double F(
const RealType &x)
const override;
90 [[nodiscard]]
double S(
const RealType &x)
const override;
91 template <std::uniform_random_bit_generator G>
94 template <std::uniform_random_bit_generator G>
98 RealType quantileImpl(
double p)
const override;
99 RealType quantileImpl1m(
double p)
const override;
101 std::complex<double> CFImpl(
double t)
const override;
104 [[nodiscard]]
long double Moment(
size_t n)
const;
122 double significanceLevel);
137 double significanceLevel,
bool unbiased =
false);
165 DoublePair &confidenceIntervalForVariance,
double significanceLevel,
166 bool unbiased =
false);
213template <
typename RealType>
220template <
typename RealType>
223 return "Normal(" + this->toStringWithPrecision(this->GetLocation()) +
", " +
224 this->toStringWithPrecision(this->Variance()) +
")";
227template <
typename RealType>
231 throw std::invalid_argument(
"Normal distribution: scale should be positive, but it's equal to " +
232 std::to_string(scale));
237template <
typename RealType>
240 if (!(variance > 0.0))
241 throw std::invalid_argument(
"Variance of Normal distribution should be positive, but it's equal to " +
242 std::to_string(variance));
243 SetScale(std::sqrt(variance));
246template <
typename RealType>
252template <
typename RealType>
255 return {this->mu, this->sigma * this->sigma};
258template <
typename RealType>
261 double mean = sourceParameters.first;
262 double scaleSq = sourceParameters.second;
263 return {mean / scaleSq, -0.5 / scaleSq};
266template <
typename RealType>
269 double theta1 = theta.first, theta2 = theta.second;
270 double F = -0.25 * theta1 * theta1 / theta2;
275template <
typename RealType>
278 double theta1 = theta.first, theta2 = theta.second;
279 double grad1 = -0.5 * theta1 / theta2;
280 double grad2 = -0.5 / theta2 + grad1 * grad1;
281 return {grad1, grad2};
284template <
typename RealType>
290template <
typename RealType>
293 return this->pdfNormal(x);
296template <
typename RealType>
299 return this->logpdfNormal(x);
302template <
typename RealType>
305 return this->cdfNormal(x);
308template <
typename RealType>
311 return this->cdfNormalCompl(x);
314template <
typename RealType>
315template <std::uniform_random_bit_generator G>
318 return this->mu + sigma * StandardVariate(g);
321template <
typename RealType>
322template <std::uniform_random_bit_generator G>
328template <
typename RealType>
331 return this->cfNormal(t);
334template <
typename RealType>
335RealType NormalRand<RealType>::quantileImpl(
double p)
const
337 return this->quantileNormal(p);
340template <
typename RealType>
341RealType NormalRand<RealType>::quantileImpl1m(
double p)
const
343 return this->quantileNormal1m(p);
346template <
typename RealType>
350 long double sum = 0.0;
351 for (
size_t k = 0; k <= n; k += 2) {
352 long double centralMoment = (k == 0) ? 1.0 : std::exp(k * std::log(sigma) +
math::ldfact(k - 1));
353 sum +=
math::binom(n, k) * std::pow(
static_cast<long double>(this->mu),
static_cast<long double>(n - k)) *
359template <
typename RealType>
362 this->SetLocation(this->GetSampleMean(sample));
365template <
typename RealType>
367 double significanceLevel)
369 if (significanceLevel <= 0 || significanceLevel > 1)
371 "Input level is equal to " + this->toStringWithPrecision(significanceLevel));
375 int n = sample.size();
377 double halfAlpha = 0.5 * significanceLevel;
378 double interval = NormalRV.
Quantile1m(halfAlpha) * sigma / std::sqrt(n);
379 confidenceInterval.first = this->mu - interval;
380 confidenceInterval.second = this->mu + interval;
383template <
typename RealType>
386 this->SetVariance(this->GetSampleVariance(sample, this->mu));
389template <
typename RealType>
391 double significanceLevel,
bool unbiased)
393 if (significanceLevel <= 0 || significanceLevel > 1)
395 "Input level is equal to " + this->toStringWithPrecision(significanceLevel));
399 size_t n = sample.size();
400 double halfAlpha = 0.5 * significanceLevel;
402 double numerator = sigma * sigma * (unbiased ? n : (n - 1));
403 confidenceInterval.first = numerator / ChiSqRV.
Quantile1m(halfAlpha);
404 confidenceInterval.second = numerator / ChiSqRV.
Quantile(halfAlpha);
407template <
typename RealType>
410 if (unbiased ==
false)
411 return FitVariance(sample);
412 size_t n = sample.size();
413 double halfN = 0.5 * n;
414 double s = this->GetSampleVariance(sample, this->mu);
416 s = 0.5 * std::log(s);
417 s -= std::lgammal(halfN + 0.5);
418 s += std::lgammal(halfN);
419 SetScale(std::exp(s));
422template <
typename RealType>
425 double adjustment = 1.0;
426 if (unbiased ==
true) {
427 size_t n = sample.size();
430 adjustment =
static_cast<double>(n) / (n - 1);
432 DoublePair stats = this->GetSampleMeanAndVariance(sample);
433 this->SetLocation(stats.first);
434 this->SetVariance(stats.second * adjustment);
437template <
typename RealType>
439 DoublePair &confidenceIntervalForVariance,
double significanceLevel,
bool unbiased)
441 if (significanceLevel <= 0 || significanceLevel > 1)
443 "Input level is equal to " + this->toStringWithPrecision(significanceLevel));
445 Fit(sample, unbiased);
447 size_t n = sample.size();
448 double sigmaAdj = unbiased ? sigma : (sigma * n) / (n - 1);
450 double halfAlpha = 0.5 * significanceLevel;
452 double interval = tRV.
Quantile1m(halfAlpha) * sigmaAdj / std::sqrt(n);
453 confidenceIntervalForMean.first = this->mu - interval;
454 confidenceIntervalForMean.second = this->mu + interval;
458 double numerator = (n - 1) * sigmaAdj * sigmaAdj;
459 confidenceIntervalForVariance.first = numerator / ChiSqRV.
Quantile1m(halfAlpha);
460 confidenceIntervalForVariance.second = numerator / ChiSqRV.
Quantile(halfAlpha);
463template <
typename RealType>
469 double tau = GetPrecision();
470 double numerator = this->GetSampleSum(sample) * tau + tau0 * mu0;
471 double denominator = sample.size() * tau + tau0;
473 this->SetLocation(MAP ? posteriorDistribution.
Mode() : posteriorDistribution.
Mean());
474 return posteriorDistribution;
477template <
typename RealType>
482 double halfN = 0.5 * sample.size();
483 double alphaPrior = priorDistribution.
GetShape();
484 double betaPrior = priorDistribution.
GetRate();
485 double alphaPosterior = alphaPrior + halfN;
486 double betaPosterior = betaPrior + halfN * this->GetSampleVariance(sample, this->mu);
488 SetVariance(MAP ? posteriorDistribution.
Mode() : posteriorDistribution.
Mean());
489 return posteriorDistribution;
492template <
typename RealType>
497 size_t n = sample.size();
498 double alphaPrior = priorDistribution.
GetShape();
499 double betaPrior = priorDistribution.
GetRate();
502 DoublePair stats = this->GetSampleMeanAndVariance(sample);
503 double lambdaPosterior = lambdaPrior + n;
504 double muPosterior = (lambdaPrior * muPrior + n * stats.first) / lambdaPosterior;
505 double halfN = 0.5 * n;
506 double alphaPosterior = alphaPrior + halfN;
507 double aux = muPrior - stats.first;
508 double betaPosterior = betaPrior + halfN * (stats.second + lambdaPrior / lambdaPosterior * aux * aux);
511 :
static_cast<DoublePair>(posteriorDistribution.Mean());
512 this->SetLocation(newParams.first);
513 this->SetVariance(newParams.second);
514 return posteriorDistribution;
The ChiSquaredRand class Chi-squared distribution.
Definition continuous/gamma.hpp:355
Definition exponential_family.hpp:14
The InverseGammaRand class Inverse-Gamma distribution.
Definition inverse_gamma.hpp:23
double GetRate() const noexcept
Definition inverse_gamma.hpp:49
double GetShape() const noexcept
Definition inverse_gamma.hpp:48
long double Mean() const override
Definition inverse_gamma.hpp:146
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition inverse_gamma.hpp:168
The NormalInverseGammaRand class Normal-inverse-gamma distribution.
Definition normal_inverse_gamma.hpp:24
double GetLocation() const noexcept
Definition normal_inverse_gamma.hpp:38
double GetPrecision() const noexcept
Definition normal_inverse_gamma.hpp:39
double GetShape() const noexcept
Definition normal_inverse_gamma.hpp:40
double GetRate() const noexcept
Definition normal_inverse_gamma.hpp:41
The NormalRand class Normal distribution.
Definition normal.hpp:31
DoublePair SourceToNatural(DoublePair sourceParameters) const override
Definition normal.hpp:259
double CarrierMeasure(RealType) const override
Definition normal.hpp:285
DoublePair LogNormalizerGradient(DoublePair theta) const override
Definition normal.hpp:276
static RealType StandardVariate(G &g)
Definition normal.hpp:323
std::string Name() const override
Definition normal.hpp:221
DoublePair SourceParameters() const override
Definition normal.hpp:253
DoublePair SufficientStatistic(RealType x) const override
Definition normal.hpp:247
void FitVariance(std::span< const RealType > sample)
set variance, returned by maximium-likelihood estimator
Definition normal.hpp:384
double GetLogScale() const
Definition normal.hpp:71
NormalInverseGammaRand< RealType > FitBayes(std::span< const RealType > sample, const NormalInverseGammaRand< RealType > &priorDistribution, bool MAP=false)
set parameters, returned by bayesian estimation
Definition normal.hpp:494
void FitScale(std::span< const RealType > sample, bool unbiased=false)
set scale, returned via maximum-likelihood estimation or unbiased estimator (which might be different...
Definition normal.hpp:408
double LogNormalizer(DoublePair theta) const override
Definition normal.hpp:267
double GetScale() const noexcept
Definition normal.hpp:67
void FitLocation(std::span< const RealType > sample)
set location, returned by maximium-likelihood estimator
Definition normal.hpp:360
static NormalRand FromMeanStd(double mean, double standardDeviation)
Definition normal.hpp:44
double F(const RealType &x) const override
Definition normal.hpp:303
void SetScale(double scale)
Definition normal.hpp:228
NormalRand< RealType > FitLocationBayes(std::span< const RealType > sample, const NormalRand< RealType > &priorDistribution, bool MAP=false)
set location, returned by bayesian estimation
Definition normal.hpp:464
double GetPrecision() const
Definition normal.hpp:78
long double FourthMoment() const override
Definition normal.hpp:106
long double Moment(size_t n) const
Definition normal.hpp:347
InverseGammaRand< RealType > FitVarianceBayes(std::span< const RealType > sample, const InverseGammaRand< RealType > &priorDistribution, bool MAP=false)
set variance, returned by bayesian estimation
Definition normal.hpp:478
double S(const RealType &x) const override
Definition normal.hpp:309
void SetVariance(double variance)
Definition normal.hpp:238
void Fit(std::span< const RealType > sample, bool unbiased=false)
set parameters, returned by maximium-likelihood estimator if unbiased = false, otherwise set paramete...
Definition normal.hpp:423
RealType operator()(G &g) const
Definition normal.hpp:316
double logf(const RealType &x) const override
Definition normal.hpp:297
NormalRand(double location=0, double variance=1)
Definition normal.hpp:214
long double ThirdMoment() const override
Definition normal.hpp:105
static NormalRand FromMeanVariance(double mean, double variance)
Definition normal.hpp:56
double f(const RealType &x) const override
Definition normal.hpp:291
The StableDistribution class Abstract class for Stable distribution.
Definition stable.hpp:74
double GetLogScale() const noexcept
Definition stable.hpp:176
long double Variance() const override
Definition stable.hpp:1800
RealType Mode() const override
Mode is the value, which has the largest probability to happen, or, in the case of continuous distrib...
Definition stable.hpp:1806
void SetScale(double scale)
Definition stable.hpp:730
double GetLocation() const noexcept
Definition stable.hpp:168
long double Mean() const override
Definition stable.hpp:1790
The StudentTRand class Student's t-distribution.
Definition student_t.hpp:22
T Quantile(double p) const
Definition univariate.hpp:307
T Quantile1m(double p) const
Definition univariate.hpp:324
The EngineRef class Non-owning, type-erased reference to any uniform random bit generator.
Definition engine/uniform.hpp:116
#define RANDLIB_DETAIL_SUPPRESS_DEPRECATED_END
Definition fwd.hpp:26
#define RANDLIB_DETAIL_SUPPRESS_DEPRECATED_BEGIN
Definition fwd.hpp:25
#define RANDLIB_DEPRECATED_FIT_MEMBER
Definition fwd.hpp:9
constexpr long double ln2
Definition constants.hpp:18
constexpr long double pi
Definition constants.hpp:22
constexpr long double inv_sqrt2
Definition constants.hpp:28
Real standardNormal(G &g)
Ziggurat algorithm of Marsaglia and Tsang with 256 layers and an explicit sign bit.
Definition variates.hpp:45
Real ldfact(size_t n)
ldoublefact
Definition math/gamma.hpp:639
Real binom(size_t n, size_t k)
Calculate binomial coefficient.
Definition math/gamma.hpp:648
Definition bivariate_normal.hpp:10
@ WrongLevel
the significance level is not in (0, 1)
@ TooFewElements
the sample is too small
Pair< double > DoublePair
Definition fwd.hpp:39