RandLib 2.0.0
Probability distributions, fitting, and heavy-tail tools for C++20
Loading...
Searching...
No Matches
randlib::NormalRand< RealType > Class Template Referencefinal

The NormalRand class
Normal distribution. More...

#include <normal.hpp>

Inheritance diagram for randlib::NormalRand< RealType >:
Collaboration diagram for randlib::NormalRand< RealType >:

Public Member Functions

 NormalRand (double location=0, double variance=1)
 
std::string Name () const override
 
void SetScale (double scale)
 
void SetVariance (double variance)
 
double GetScale () const noexcept
 
double GetLogScale () const
 
double GetPrecision () const
 
DoublePair SufficientStatistic (RealType x) const override
 
DoublePair SourceParameters () const override
 
DoublePair SourceToNatural (DoublePair sourceParameters) const override
 
double LogNormalizer (DoublePair theta) const override
 
DoublePair LogNormalizerGradient (DoublePair theta) const override
 
double CarrierMeasure (RealType) const override
 
double f (const RealType &x) const override
 
double logf (const RealType &x) const override
 
double F (const RealType &x) const override
 
double S (const RealType &x) const override
 
template<std::uniform_random_bit_generator G>
RealType operator() (G &g) const
 
long double Moment (size_t n) const
 
long double ThirdMoment () const override
 
long double FourthMoment () const override
 
void FitLocation (std::span< const RealType > sample)
 set location, returned by maximium-likelihood estimator
 
void FitLocation (std::span< const RealType > sample, DoublePair &confidenceInterval, double significanceLevel)
 set location, returned by maximium-likelihood estimator and return confidenceInterval for given significance level
 
void FitVariance (std::span< const RealType > sample)
 set variance, returned by maximium-likelihood estimator
 
void FitVariance (std::span< const RealType > sample, DoublePair &confidenceInterval, double significanceLevel, bool unbiased=false)
 
void FitScale (std::span< const RealType > sample, bool unbiased=false)
 set scale, returned via maximum-likelihood estimation or unbiased estimator (which might be different from the unbiased estimator of variance)
 
void Fit (std::span< const RealType > sample, bool unbiased=false)
 set parameters, returned by maximium-likelihood estimator if unbiased = false, otherwise set parameters via UMVU estimator
 
void Fit (std::span< const RealType > sample, DoublePair &confidenceIntervalForMean, DoublePair &confidenceIntervalForVariance, double significanceLevel, bool unbiased=false)
 set parameters, returned by maximium-likelihood estimator if unbiased = false, otherwise set parameters via UMVU estimator, and return confidence intervals for given significance level
 
NormalRand< RealType > FitLocationBayes (std::span< const RealType > sample, const NormalRand< RealType > &priorDistribution, bool MAP=false)
 set location, returned by bayesian estimation
 
InverseGammaRand< RealType > FitVarianceBayes (std::span< const RealType > sample, const InverseGammaRand< RealType > &priorDistribution, bool MAP=false)
 set variance, returned by bayesian estimation
 
NormalInverseGammaRand< RealType > FitBayes (std::span< const RealType > sample, const NormalInverseGammaRand< RealType > &priorDistribution, bool MAP=false)
 set parameters, returned by bayesian estimation
 
- Public Member Functions inherited from randlib::StableDistribution< RealType >
virtual ~StableDistribution ()
 
Support SupportType () const override
 
RealType MinValue () const override
 
RealType MaxValue () const override
 
void SetLocation (double location)
 
void SetScale (double scale)
 
void SetParameterization (Parameterization value) noexcept
 
double GetExponent () const noexcept
 
double GetSkewness () const noexcept
 
double GetScale () const noexcept
 
double GetLocation () const noexcept
 
Parameterization GetParameterization () const noexcept
 
double GetLogScale () const noexcept
 
double f (const RealType &x) const override
 
double logf (const RealType &x) const override
 
double F (const RealType &x) const override
 
double S (const RealType &x) const override
 
template<std::uniform_random_bit_generator G>
RealType operator() (G &g) const
 
long double Mean () const override
 
long double Variance () const override
 
RealType Median () const override
 
RealType Mode () const override
 Mode is the value, which has the largest probability to happen, or, in the case of continuous distribution, has the largest value of density function.
 
long double Skewness () const override
 
long double ExcessKurtosis () const override
 
- Public Member Functions inherited from randlib::ContinuousDistribution< RealType >
virtual ~ContinuousDistribution ()
 
void ProbabilityDensityFunction (std::span< const RealType > x, std::span< double > y) const
 fill vector y with f(x)
 
void LogProbabilityDensityFunction (std::span< const RealType > x, std::span< double > y) const
 fill vector y with logf(x)
 
double Hazard (const RealType &x) const override
 
double LikelihoodFunction (std::span< const RealType > sample) const override
 
double LogLikelihoodFunction (std::span< const RealType > sample) const override
 
bool KolmogorovSmirnovTest (std::span< const RealType > orderStatistic, double alpha) const
 
- Public Member Functions inherited from randlib::UnivariateDistribution< RealType >
virtual ~UnivariateDistribution ()
 
bool isLeftBounded () const
 
bool isRightBounded () const
 
RealType Quantile (double p) const
 
RealType Quantile1m (double p) const
 
void QuantileFunction (std::span< const double > p, std::span< RealType > y) const
 
void QuantileFunction1m (std::span< const double > p, std::span< RealType > y) const
 
std::complex< double > CF (double t) const
 
void CharacteristicFunction (std::span< const double > t, std::span< std::complex< double > > y) const
 
void HazardFunction (std::span< const RealType > x, std::span< double > y) const
 
virtual long double Kurtosis () const
 
virtual long double SecondMoment () const
 
- Public Member Functions inherited from randlib::ProbabilityDistribution< T >
virtual ~ProbabilityDistribution ()=default
 
virtual double F (const T &x) const =0
 
void CumulativeDistributionFunction (std::span< const T > x, std::span< double > y) const
 
virtual double S (const T &x) const
 
void SurvivalFunction (std::span< const T > x, std::span< double > y) const
 
template<std::uniform_random_bit_generator G>
T operator() (G &g) const
 Draws a variate with the given engine through the virtual interface.
 
T Variate () const
 Uses the engine of this object, so it is not thread-safe for one object; use operator() with one engine per thread instead.
 
void Sample (std::span< T > outputData) const
 Fills the output with variates drawn with the engine of this object.
 
template<std::uniform_random_bit_generator G>
void Sample (G &g, std::span< T > outputData) const
 Fills the output with variates drawn with the given engine.
 
void Reseed (std::uint64_t seed)
 
- Public Member Functions inherited from randlib::ExponentialFamily< RealType, DoublePair >
 ExponentialFamily ()
 
virtual ~ExponentialFamily ()
 
virtual DoublePair NaturalParameters () const
 
double ProbabilityMeasure (RealType x) const
 
double LogProbabilityMeasure (RealType x) const
 
double KullbackLeiblerDivergence (DoublePair parameters) const
 
virtual double CrossEntropyAdjusted (DoublePair parameters) const
 
virtual double EntropyAdjusted () const
 

Static Public Member Functions

static NormalRand FromMeanStd (double mean, double standardDeviation)
 
static NormalRand FromMeanVariance (double mean, double variance)
 
template<std::uniform_random_bit_generator G>
static RealType StandardVariate (G &g)
 
- Static Public Member Functions inherited from randlib::UnivariateDistribution< RealType >
static long double GetSampleSum (std::span< const RealType > sample)
 
static long double GetSampleMean (std::span< const RealType > sample)
 
static long double GetSampleLogMean (std::span< const RealType > sample)
 
static long double GetSampleVariance (std::span< const RealType > sample, double mean)
 
static long double GetSampleLogVariance (std::span< const RealType > sample, double logMean)
 
static LongDoublePair GetSampleMeanAndVariance (std::span< const RealType > sample)
 
static LongDoublePair GetSampleLogMeanAndVariance (std::span< const RealType > sample)
 
static std::tuple< long double, long double, long double, long double > GetSampleStatistics (std::span< const RealType > sample)
 

Additional Inherited Members

- Public Types inherited from randlib::ProbabilityDistribution< T >
using value_type = T
 
- Protected Types inherited from randlib::StableDistribution< RealType >
enum class  Parameterization { S0 , S1 }
 Nolan parameterization used for the public location parameter. More...
 
- Protected Member Functions inherited from randlib::StableDistribution< RealType >
 StableDistribution (double exponent, double skewness, double scale=1, double location=0, Parameterization selectedParameterization=Parameterization::S1)
 
void SetParameters (double exponent, double skewness, double scale=1, double location=0)
 
double pdfNormal (RealType x) const
 
double logpdfNormal (RealType x) const
 
double pdfCauchy (RealType x) const
 
double logpdfCauchy (RealType x) const
 
double pdfLevy (RealType x) const
 
double logpdfLevy (RealType x) const
 
double cdfNormal (RealType x) const
 
double cdfNormalCompl (RealType x) const
 
double cdfCauchy (RealType x) const
 
double cdfCauchyCompl (RealType x) const
 
double cdfLevy (RealType x) const
 
double cdfLevyCompl (RealType x) const
 
RealType quantileNormal (double p) const
 
RealType quantileNormal1m (double p) const
 
RealType quantileCauchy (double p) const
 
RealType quantileCauchy1m (double p) const
 
RealType quantileLevy (double p) const
 
RealType quantileLevy1m (double p) const
 
std::complex< double > cfNormal (double t) const
 
std::complex< double > cfCauchy (double t) const
 
std::complex< double > cfLevy (double t) const
 
- Protected Member Functions inherited from randlib::ContinuousDistribution< RealType >
 ContinuousDistribution ()
 
std::complex< double > integrateCharacteristicFunction (double t) const
 
RealType quantileImpl (double p, RealType initValue) const override
 
RealType quantileImpl1m (double p, RealType initValue) const override
 
long double ExpectedValue (const std::function< double(RealType)> &funPtr, RealType minPoint, RealType maxPoint) const override
 
RealType searchQuantile (double p, bool upperTail, RealType guess, double relativeStep=1e-3) const
 Deterministic root search for the quantile: brackets the root, starting from the guess when it is finite, then refines it with the Illinois variant of regula falsi (on a logarithmic scale for small p), falling back to bisection whenever the bracket does not halve.
 
- Protected Member Functions inherited from randlib::UnivariateDistribution< RealType >
 UnivariateDistribution ()
 
- Protected Member Functions inherited from randlib::ProbabilityDistribution< T >
std::string toStringWithPrecision (const double a_value, const int n=6) const
 
 ProbabilityDistribution ()=default
 
DefaultEngine & engine () const
 
virtual void sample (detail::EngineRef g, std::span< T > outputData) const
 Fills the output with variate(g); distributions with a faster bulk algorithm override it.
 
fit_error fitError (FitErrorCode code, const std::string &explanation) const
 
- Static Protected Member Functions inherited from randlib::UnivariateDistribution< RealType >
static bool allElementsAreNotGreaterThan (RealType value, std::span< const RealType > sample)
 
static bool allElementsAreNotSmallerThan (RealType value, std::span< const RealType > sample)
 
static bool allElementsAreNonNegative (std::span< const RealType > sample)
 
static bool allElementsArePositive (std::span< const RealType > sample)
 
- Protected Attributes inherited from randlib::StableDistribution< RealType >
double alpha = 2
 characteristic exponent α
 
double beta = 0
 skewness β
 
double mu = 0
 location μ
 
double gamma = randlib::consts::sqrt2
 scale γ
 
double logGamma = 0.5 * randlib::consts::ln2
 log(γ)
 
double pdfCoef
 hashed coefficient for faster pdf calculations
 
double pdftailBound = INFINITY
 boundary k such that for |x| > k we can use pdf tail approximation
 
double cdftailBound = INFINITY
 boundary k such that for |x| > k we can use cdf tail approximation
 
- Static Protected Attributes inherited from randlib::ProbabilityDistribution< T >
static constexpr size_t MAX_ITER_REJECTION = detail::MAX_ITER_REJECTION
 MAX_ITER_REJECTION upper boundary for maximum amount of iterations in rejection methods one thousand should be enough to be sure there is a bug (or rejection method is too slow to be used)
 
static constexpr char POSITIVITY_VIOLATION [] = "All elements should be positive"
 
static constexpr char NON_NEGATIVITY_VIOLATION [] = "All elements should be non-negative"
 
static constexpr char UPPER_LIMIT_VIOLATION [] = "No element should be greater than "
 
static constexpr char LOWER_LIMIT_VIOLATION [] = "No element should be smaller than "
 

Detailed Description

template<typename RealType>
class randlib::NormalRand< RealType >

The NormalRand class
Normal distribution.

f(x | μ, σ^2) = 1 / ((2 π σ^2)^(1/2) * exp(-(x - μ)^2 / (2 σ^2))

Notation: X ~ N(μ, σ^2)

Related distributions:
X ~ S(2, 0, σ/√2, μ)

Constructor & Destructor Documentation

◆ NormalRand()

template<typename RealType >
randlib::NormalRand< RealType >::NormalRand ( double  location = 0,
double  variance = 1 
)

Member Function Documentation

◆ CarrierMeasure()

template<typename RealType >
double randlib::NormalRand< RealType >::CarrierMeasure ( RealType  ) const
overridevirtual

◆ f()

template<typename RealType >
double randlib::NormalRand< RealType >::f ( const RealType &  x) const
overridevirtual
Parameters
x
Returns
probability density function

Reimplemented from randlib::ContinuousDistribution< RealType >.

◆ F()

template<typename RealType >
double randlib::NormalRand< RealType >::F ( const RealType &  x) const
override

◆ Fit() [1/2]

template<typename RealType >
void randlib::NormalRand< RealType >::Fit ( std::span< const RealType >  sample,
bool  unbiased = false 
)

set parameters, returned by maximium-likelihood estimator if unbiased = false, otherwise set parameters via UMVU estimator

Parameters
sample
unbiased

◆ Fit() [2/2]

template<typename RealType >
void randlib::NormalRand< RealType >::Fit ( std::span< const RealType >  sample,
DoublePair &  confidenceIntervalForMean,
DoublePair &  confidenceIntervalForVariance,
double  significanceLevel,
bool  unbiased = false 
)

set parameters, returned by maximium-likelihood estimator if unbiased = false, otherwise set parameters via UMVU estimator, and return confidence intervals for given significance level

Parameters
sample
confidenceIntervalForMean
confidenceIntervalForVariance
significanceLevel
unbiased

calculate confidence interval for mean

calculate confidence interval for variance

◆ FitBayes()

template<typename RealType >
NormalInverseGammaRand< RealType > randlib::NormalRand< RealType >::FitBayes ( std::span< const RealType >  sample,
const NormalInverseGammaRand< RealType > &  priorDistribution,
bool  MAP = false 
)

set parameters, returned by bayesian estimation

Parameters
sample
priorDistribution
MAPif true, use MAP estimator
Returns
posterior distribution

◆ FitLocation() [1/2]

template<typename RealType >
void randlib::NormalRand< RealType >::FitLocation ( std::span< const RealType >  sample)

set location, returned by maximium-likelihood estimator

Parameters
sample

◆ FitLocation() [2/2]

template<typename RealType >
void randlib::NormalRand< RealType >::FitLocation ( std::span< const RealType >  sample,
DoublePair &  confidenceInterval,
double  significanceLevel 
)

set location, returned by maximium-likelihood estimator and return confidenceInterval for given significance level

Parameters
sample
confidenceInterval
significanceLevel

◆ FitLocationBayes()

template<typename RealType >
NormalRand< RealType > randlib::NormalRand< RealType >::FitLocationBayes ( std::span< const RealType >  sample,
const NormalRand< RealType > &  priorDistribution,
bool  MAP = false 
)

set location, returned by bayesian estimation

Parameters
sample
priorDistribution
MAPif true, use MAP estimator
Returns
posterior distribution

◆ FitScale()

template<typename RealType >
void randlib::NormalRand< RealType >::FitScale ( std::span< const RealType >  sample,
bool  unbiased = false 
)

set scale, returned via maximum-likelihood estimation or unbiased estimator (which might be different from the unbiased estimator of variance)

Parameters
sample
unbiased

◆ FitVariance() [1/2]

template<typename RealType >
void randlib::NormalRand< RealType >::FitVariance ( std::span< const RealType >  sample)

set variance, returned by maximium-likelihood estimator

Parameters
sample

◆ FitVariance() [2/2]

template<typename RealType >
void randlib::NormalRand< RealType >::FitVariance ( std::span< const RealType >  sample,
DoublePair &  confidenceInterval,
double  significanceLevel,
bool  unbiased = false 
)
Parameters
sample
confidenceInterval
significanceLevel
unbiased

◆ FitVarianceBayes()

template<typename RealType >
InverseGammaRand< RealType > randlib::NormalRand< RealType >::FitVarianceBayes ( std::span< const RealType >  sample,
const InverseGammaRand< RealType > &  priorDistribution,
bool  MAP = false 
)

set variance, returned by bayesian estimation

Parameters
sample
priorDistribution
MAPif true, use MAP estimator
Returns
posterior distribution

◆ FourthMoment()

template<typename RealType >
long double randlib::NormalRand< RealType >::FourthMoment ( ) const
inlineoverridevirtual
Returns
E[X^4]

Reimplemented from randlib::UnivariateDistribution< RealType >.

◆ FromMeanStd()

template<typename RealType >
static NormalRand randlib::NormalRand< RealType >::FromMeanStd ( double  mean,
double  standardDeviation 
)
inlinestatic
Parameters
meanμ
standardDeviationσ
Returns
N(μ, σ^2)

◆ FromMeanVariance()

template<typename RealType >
static NormalRand randlib::NormalRand< RealType >::FromMeanVariance ( double  mean,
double  variance 
)
inlinestatic
Parameters
meanμ
varianceσ^2
Returns
N(μ, σ^2)

◆ GetLogScale()

template<typename RealType >
double randlib::NormalRand< RealType >::GetLogScale ( ) const
inline
Returns
log(σ)

◆ GetPrecision()

template<typename RealType >
double randlib::NormalRand< RealType >::GetPrecision ( ) const
inline
Returns
1/σ^2

◆ GetScale()

template<typename RealType >
double randlib::NormalRand< RealType >::GetScale ( ) const
inlinenoexcept
Returns
σ

◆ logf()

template<typename RealType >
double randlib::NormalRand< RealType >::logf ( const RealType &  x) const
overridevirtual
Parameters
x
Returns
logarithm of probability density function

Implements randlib::ContinuousDistribution< RealType >.

◆ LogNormalizer()

template<typename RealType >
double randlib::NormalRand< RealType >::LogNormalizer ( DoublePair  theta) const
overridevirtual

◆ LogNormalizerGradient()

template<typename RealType >
DoublePair randlib::NormalRand< RealType >::LogNormalizerGradient ( DoublePair  theta) const
overridevirtual

◆ Moment()

template<typename RealType >
long double randlib::NormalRand< RealType >::Moment ( size_t  n) const

E[X^n] = Σ_{even k} C(n, k) μ^(n - k) σ^k (k - 1)!!

◆ Name()

template<typename RealType >
std::string randlib::NormalRand< RealType >::Name ( ) const
overridevirtual
Returns
title of distribution, for instance "Normal(0, 1)"

Implements randlib::ProbabilityDistribution< T >.

◆ operator()()

template<typename RealType >
template<std::uniform_random_bit_generator G>
RealType randlib::NormalRand< RealType >::operator() ( G &  g) const

◆ S()

template<typename RealType >
double randlib::NormalRand< RealType >::S ( const RealType &  x) const
override

◆ SetScale()

template<typename RealType >
void randlib::NormalRand< RealType >::SetScale ( double  scale)

◆ SetVariance()

template<typename RealType >
void randlib::NormalRand< RealType >::SetVariance ( double  variance)

◆ SourceParameters()

template<typename RealType >
DoublePair randlib::NormalRand< RealType >::SourceParameters ( ) const
overridevirtual

◆ SourceToNatural()

template<typename RealType >
DoublePair randlib::NormalRand< RealType >::SourceToNatural ( DoublePair  sourceParameters) const
overridevirtual

◆ StandardVariate()

template<typename RealType >
template<std::uniform_random_bit_generator G>
RealType randlib::NormalRand< RealType >::StandardVariate ( G &  g)
static

◆ SufficientStatistic()

template<typename RealType >
DoublePair randlib::NormalRand< RealType >::SufficientStatistic ( RealType  x) const
overridevirtual

◆ ThirdMoment()

template<typename RealType >
long double randlib::NormalRand< RealType >::ThirdMoment ( ) const
inlineoverridevirtual
Returns
E[X^3]

Reimplemented from randlib::UnivariateDistribution< RealType >.


The documentation for this class was generated from the following file: