STK++ 0.9.13

Normal distribution law. More...

#include <STK_Law_Normal.h>

Inheritance diagram for STK::Law::Normal:
Inheritance graph

Public Types

typedef IUnivLaw< RealBase
 

Public Member Functions

 Normal (Real const &mu=0., Real const &sigma=1.)
 Constructor.
 
virtual ~Normal ()
 Destructor.
 
Real constmu () const
 
Real constsigma () const
 
void setMu (Real const &mu)
 
void setSigma (Real const &sigma)
 
Real rand () const
 Generate a pseudo Normal random variate.
 
virtual Real pdf (Real const &x) const
 
virtual Real lpdf (Real const &x) const
 
virtual Real cdf (Real const &t) const
 Compute the cumulative distribution function at t of the standard normal distribution.
 
virtual Real icdf (Real const &p) const
 Compute the inverse cumulative distribution function at p of the standard normal distribution.
 
- Public Member Functions inherited from STK::Law::IUnivLaw< Real >
virtual ~IUnivLaw ()
 Virtual destructor.
 
virtual Real lcdf (Real const &t) const
 compute the lower tail log-cumulative distribution function Give the log-probability that a random variate is less or equal to t.
 
virtual Real cdfc (Real const &t) const
 calculate the complement of cumulative distribution function, called in statistics the survival function.
 
virtual Real lcdfc (Real const &t) const
 calculate the log-complement of cumulative distribution function Give the log-probability that a random variate is greater than t.
 
- Public Member Functions inherited from STK::Law::ILawBase
String constname () const
 

Static Public Member Functions

static Real rand (Real const &mu, Real const &sigma)
 Generate a pseudo Normal random variate.
 
static Real pdf (Real const &x, Real const &mu, Real const &sigma)
 
static Real lpdf (Real const &x, Real const &mu, Real const &sigma)
 
static Real cdf (Real const &t, Real const &mu, Real const &sigma)
 Compute the cumulative distribution function at t of the standard normal distribution.
 
static Real icdf (Real const &p, Real const &mu, Real const &sigma)
 Compute the inverse cumulative distribution function at p of the standard normal distribution.
 

Protected Attributes

Real mu_
 The mu parameter.
 
Real sigma_
 The sigma parameter.
 
- Protected Attributes inherited from STK::Law::ILawBase
String name_
 Name of the Law.
 

Additional Inherited Members

- Protected Member Functions inherited from STK::Law::IUnivLaw< Real >
 IUnivLaw (String const &name)
 Constructor.
 
 IUnivLaw (IUnivLaw const &law)
 copy Constructor.
 
- Protected Member Functions inherited from STK::Law::ILawBase
 ILawBase (String const &name)
 Constructor.
 
 ~ILawBase ()
 destructor.
 

Detailed Description

Normal distribution law.

In probability theory, the normal (or Gaussian) distribution is a very commonly occurring continuous probability distribution. Normal distributions are extremely important in statistics and are often used in the natural and social sciences for real-valued random variables whose distributions are not known.

The normal distribution is immensely useful because of the central limit theorem, which states that, under mild conditions, the mean of many random variables independently drawn from the same distribution is distributed approximately normally, irrespective of the form of the original distribution.

The probability density of normal distribution is:

\[
 f(x;\mu,\sigma) = \frac{1}{\sigma\sqrt{2\pi}}
           \exp\left(-\frac{\left(x-\mu\right)^2}{2\sigma^2} \right)
 \]

where $ \mu \mbox{ and } \sigma$ are the mean and the standard deviation.

Definition at line 71 of file STK_Law_Normal.h.

Member Typedef Documentation

◆ Base

Constructor & Destructor Documentation

◆ Normal()

STK::Law::Normal::Normal ( Real const mu = 0.,
Real const sigma = 1. 
)
inline

Constructor.

Parameters
mumean of the Normal distribution
sigmastandard deviation of the Normal distribution

Definition at line 79 of file STK_Law_Normal.h.

80 : Base(String(_T("Normal")))
81 , mu_(mu), sigma_(sigma)
82 {
84 { STKDOMAIN_ERROR_2ARG(Normal::Normal,mu,sigma,invalid argument);}
85 }
#define STKDOMAIN_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:147
#define _T(x)
Let x unmodified.
Real sigma_
The sigma parameter.
IUnivLaw< Real > Base
Normal(Real const &mu=0., Real const &sigma=1.)
Constructor.
Real const & mu() const
Real const & sigma() const
Real mu_
The mu parameter.
std::basic_string< Char > String
STK fundamental type of a String.
static bool isFinite(Type const &x)

References mu(), Normal(), sigma(), and STKDOMAIN_ERROR_2ARG.

Referenced by Normal().

◆ ~Normal()

virtual STK::Law::Normal::~Normal ( )
inlinevirtual

Destructor.

Definition at line 87 of file STK_Law_Normal.h.

87{}

Member Function Documentation

◆ cdf() [1/2]

Real STK::Law::Normal::cdf ( Real const t) const
virtual

Compute the cumulative distribution function at t of the standard normal distribution.

Author
W. J. Cody
See also
http://www.netlib.org/specfun/erf

This is the erfc() routine only, adapted by the transform cdf(u)=erfc(-u/sqrt(2))/2

Parameters
ta real value
Returns
the cumulative distribution function value at t

Implements STK::Law::IUnivLaw< Real >.

Definition at line 115 of file STK_Law_Normal.cpp.

116{
117 // check parameter
119 STKDOMAIN_ERROR_1ARG(Normal::cdf,t,invalid argument);
120 // trivial case
121 if (sigma_ == 0) return (t < mu_) ? 0. : 1.;
122 // change of variable
123 return (0.5*Funct::erfc_raw(-((t - mu_) / sigma_)*Const::_1_SQRT2_));
124}
#define STKDOMAIN_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:165
virtual Real cdf(Real const &t) const
Compute the cumulative distribution function at t of the standard normal distribution.
Real erfc_raw(Real const &a)
Compute the complementary error function erfc(a) Compute the function.

References cdf(), STK::Funct::erfc_raw(), mu_, sigma_, and STKDOMAIN_ERROR_1ARG.

Referenced by cdf(), cdf(), and main().

◆ cdf() [2/2]

Real STK::Law::Normal::cdf ( Real const t,
Real const mu,
Real const sigma 
)
static

Compute the cumulative distribution function at t of the standard normal distribution.

Parameters
ta real value
mu,sigmamean and standard deviation of the Normal distribution
Returns
the cumulative distribution function value at t

Definition at line 227 of file STK_Law_Normal.cpp.

228{
229 // check parameter
231 STKDOMAIN_ERROR_1ARG(Normal::cdf,t,invalid argument);
232 // trivial case
233 if (sigma == 0) return (t < mu) ? 0. : 1.;
234 // change of variable
235 return (0.5*Funct::erfc_raw(-((t - mu) / sigma)*Const::_1_SQRT2_));
236}

References cdf(), STK::Funct::erfc_raw(), mu(), sigma(), and STKDOMAIN_ERROR_1ARG.

◆ icdf() [1/2]

Real STK::Law::Normal::icdf ( Real const p) const
virtual

Compute the inverse cumulative distribution function at p of the standard normal distribution.

Author
Peter J. Acklam pjack.nosp@m.lam@.nosp@m.onlin.nosp@m.e.no
See also
http://home.online.no/~pjacklam/notes/invnorm/index.html

This function is based on the MATLAB code from the address above.

Parameters
pa probability number.
Returns
the inverse cumulative distribution function value at p.

Implements STK::Law::IUnivLaw< Real >.

Definition at line 127 of file STK_Law_Normal.cpp.

128{
129 // check parameter
130 if ( (!Arithmetic<Real>::isFinite(p)) || (p > 1.) || (p < 0.) )
131 STKDOMAIN_ERROR_1ARG(Normal::icdf,p,invalid argument);
132 // trivial cases
133 if (p == 0.) return -Arithmetic<Real>::infinity();
134 if (p == 1.) return Arithmetic<Real>::infinity();
135 if (p == 0.5) return mu_;
136
137 double p_low = 0.02425, p_high = 1 - p_low;
138 double x = 0.0;
139 //Rational approximation for lower region.
140 if ( p < p_low)
141 {
142 double q = sqrt(-2*log(p));
143 x = (((((c1*q+c2)*q+c3)*q+c4)*q+c5)*q+c6) / ((((d1*q+d2)*q+d3)*q+d4)*q+1);
144 }
145 //Rational approximation for central region.
146 if (p_low <= p && p <= p_high)
147 {
148 double r = (p-0.5)*(p-0.5);
149 x = (((((a1*r+a2)*r+a3)*r+a4)*r+a5)*r+a6)*(p-0.5)/(((((b1*r+b2)*r+b3)*r+b4)*r+b5)*r+1);
150 }
151 //Rational approximation for upper region.
152 if (p_high < p )
153 {
154 double q = sqrt(-2*log(1-p));
155 x = -(((((c1*q+c2)*q+c3)*q+c4)*q+c5)*q+c6) / ((((d1*q+d2)*q+d3)*q+d4)*q+1);
156 }
157
158 /* The relative error of the approximation has absolute value less
159 than 1.15e-9. One iteration of Halley's rational method (third
160 order) gives full machine precision... */
161// if(( 0 < p)&&(p < 1))
162// {
163// double e = cdf(u) - p;
164// double u = e * Const::_SQRT2PI_ * exp(x*x/2);
165// x = x - u/(1 + x*u/2);
166// }
167 return mu_ + sigma_ * x;
168}
#define d4(z)
#define d2(z)
#define d1(z)
#define d3(z)
const double b1
const double b4
const double a4
const double c1
const double a3
const double a6
const double b5
const double c4
const double b2
const double c3
const double a5
const double a2
const double b3
const double c2
const double c6
const double c5
const double a1
virtual Real icdf(Real const &p) const
Compute the inverse cumulative distribution function at p of the standard normal distribution.

References a1, a2, a3, a4, a5, a6, b1, b2, b3, b4, b5, c1, c2, c3, c4, c5, c6, d1, d2, d3, d4, icdf(), mu_, sigma_, and STKDOMAIN_ERROR_1ARG.

Referenced by icdf(), icdf(), and main().

◆ icdf() [2/2]

Real STK::Law::Normal::icdf ( Real const p,
Real const mu,
Real const sigma 
)
static

Compute the inverse cumulative distribution function at p of the standard normal distribution.

Parameters
pa probability number.
mu,sigmamean and standard deviation of the Normal distribution
Returns
the inverse cumulative distribution function value at p.

Definition at line 239 of file STK_Law_Normal.cpp.

240{
241 // check parameter
242 if ( (!Arithmetic<Real>::isFinite(p)) || (p > 1.) || (p < 0.) )
243 STKDOMAIN_ERROR_1ARG(Normal::icdf,p,invalid argument);
244 // trivial cases
245 if (p == 0.) return -Arithmetic<Real>::infinity();
246 if (p == 1.) return Arithmetic<Real>::infinity();
247 if (p == 0.5) return mu;
248
249 double p_low = 0.02425, p_high = 1 - p_low;
250 double x = 0.0;
251 //Rational approximation for lower region.
252 if ( p < p_low)
253 {
254 double q = sqrt(-2*log(p));
255 x = (((((c1*q+c2)*q+c3)*q+c4)*q+c5)*q+c6) / ((((d1*q+d2)*q+d3)*q+d4)*q+1);
256 }
257 //Rational approximation for central region.
258 if (p_low <= p && p <= p_high)
259 {
260 double r = (p-0.5)*(p-0.5);
261 x = (((((a1*r+a2)*r+a3)*r+a4)*r+a5)*r+a6)*(p-0.5) / (((((b1*r+b2)*r+b3)*r+b4)*r+b5)*r+1);
262 }
263 //Rational approximation for upper region.
264 if (p_high < p )
265 {
266 double q = sqrt(-2*log(1-p));
267 x = -(((((c1*q+c2)*q+c3)*q+c4)*q+c5)*q+c6) / ((((d1*q+d2)*q+d3)*q+d4)*q+1);
268 }
269
270 /* The relative error of the approximation has absolute value less
271 than 1.15e-9. One iteration of Halley's rational method (third
272 order) gives full machine precision... */
273 //Pseudo-code algorithm for refinement
274 // if(( 0 < p)&&(p < 1))
275 // {
276 // double e = cdf(u) - p;
277 // double u = e * Const::_SQRT2PI_ * exp(x*x/2);
278 // x = x - u/(1 + x*u/2);
279 // }
280 return mu + sigma * x;
281}

References a1, a2, a3, a4, a5, a6, b1, b2, b3, b4, b5, c1, c2, c3, c4, c5, c6, d1, d2, d3, d4, icdf(), mu(), sigma(), and STKDOMAIN_ERROR_1ARG.

◆ lpdf() [1/2]

Real STK::Law::Normal::lpdf ( Real const x) const
virtual
Returns
Give the value of the log-pdf at x.
Parameters
xa real value

Reimplemented from STK::Law::IUnivLaw< Real >.

Definition at line 99 of file STK_Law_Normal.cpp.

100{
101 // check parameter
103 STKDOMAIN_ERROR_1ARG(Normal::lpdf,x,invalid argument);
104 // trivial case
105 if (sigma_ == 0)
106 return (x==mu_) ? 0. : -Arithmetic<Real>::infinity();
107 // compute lpdf
108 const Real y = (x - mu_)/sigma_;
109 return -(Const::_LNSQRT2PI_ + std::log(double(sigma_)) + 0.5 * y * y);
110}
virtual Real lpdf(Real const &x) const
double Real
STK fundamental type of Real values.

References lpdf(), mu_, sigma_, and STKDOMAIN_ERROR_1ARG.

Referenced by STK::JointGaussianModel< Array, WColVector >::computeLnLikelihood(), STK::ModelDiagGaussian_muj_sj< Data_, WColVector_ >::computeLnLikelihood(), STK::DiagGaussianBase< Derived >::lnComponentProbability(), STK::HDGaussian_AjkBkQkD< Array >::lnComponentProbability(), lpdf(), lpdf(), and main().

◆ lpdf() [2/2]

Real STK::Law::Normal::lpdf ( Real const x,
Real const mu,
Real const sigma 
)
static
Returns
Give the value of the log-pdf at x.
Parameters
xa real value
mu,sigmamean and standard deviation of the Normal distribution

Definition at line 206 of file STK_Law_Normal.cpp.

207{
208#ifdef STK_DEBUG
209 // check parameters
211 STKDOMAIN_ERROR_2ARG(Normal::rand,mu,sigma,invalid argument);
212#endif
213 // check parameter
215 STKDOMAIN_ERROR_1ARG(Normal::lpdf,x,invalid argument);
216 // trivial case
217 if (sigma == 0)
218 return (x==mu) ? 0. : -Arithmetic<Real>::infinity();
219 // compute lpdf
220 const Real y = (x - mu)/sigma;
221 return -(Const::_LNSQRT2PI_ + std::log(double(sigma)) + 0.5 * y * y);
222}
Real rand() const
Generate a pseudo Normal random variate.

References lpdf(), mu(), rand(), sigma(), STKDOMAIN_ERROR_1ARG, and STKDOMAIN_ERROR_2ARG.

◆ mu()

Real const & STK::Law::Normal::mu ( ) const
inline
Returns
the mean

Definition at line 89 of file STK_Law_Normal.h.

89{ return mu_;}

References mu_.

Referenced by cdf(), icdf(), lpdf(), Normal(), pdf(), rand(), and setMu().

◆ pdf() [1/2]

Real STK::Law::Normal::pdf ( Real const x) const
virtual
Parameters
xa real value
Returns
the value of the normal pdf at x

Implements STK::Law::IUnivLaw< Real >.

Definition at line 84 of file STK_Law_Normal.cpp.

85{
86 // check parameter
88 STKDOMAIN_ERROR_1ARG(Normal::pdf,x,invalid argument);
89 // trivial case
90 if (sigma_ == 0.) return (x==mu_) ? 1. : 0.;
91 // compute pdf
92 const Real y = (x - mu_)/sigma_;
93 return Const::_1_SQRT2PI_ * std::exp(-0.5 * y * y) / sigma_;
94}
virtual Real pdf(Real const &x) const

References mu_, pdf(), sigma_, and STKDOMAIN_ERROR_1ARG.

Referenced by main(), pdf(), and pdf().

◆ pdf() [2/2]

Real STK::Law::Normal::pdf ( Real const x,
Real const mu,
Real const sigma 
)
static
Parameters
xa real value
mu,sigmamean and standard deviation of the Normal distribution
Returns
the value of the normal pdf at x

Definition at line 187 of file STK_Law_Normal.cpp.

188{
189#ifdef STK_DEBUG
190 // check parameters
192 STKDOMAIN_ERROR_2ARG(Normal::rand,mu,sigma,invalid argument);
193#endif
194 // check parameter
196 STKDOMAIN_ERROR_1ARG(Normal::pdf,x,invalid argument);
197 // trivial case
198 if (sigma == 0.) return (x==mu) ? 1. : 0.;
199 // compute pdf
200 const Real y = (x - mu)/sigma;
201 return Const::_1_SQRT2PI_ * std::exp(-0.5 * y * y) / sigma;
202}

References mu(), pdf(), rand(), sigma(), STKDOMAIN_ERROR_1ARG, and STKDOMAIN_ERROR_2ARG.

◆ rand() [1/2]

Real STK::Law::Normal::rand ( ) const
virtual

Generate a pseudo Normal random variate.

Generate a pseudo Normal random variate with location parameter mu_ and standard deviation sigma_.

Returns
a pseudo normal random variate

Implements STK::Law::IUnivLaw< Real >.

Definition at line 78 of file STK_Law_Normal.cpp.

79{ return (sigma_ == 0.) ? mu_ : generator.randGauss(mu_, sigma_);}

References mu_, and sigma_.

Referenced by lpdf(), main(), pdf(), STK::DiagGaussianBase< Derived >::rand(), STK::HDGaussianBase< Derived >::rand(), rand(), and STK::HDMatrixGaussianModel< IdRow_, IdCol_, Array_ >::randomInit().

◆ rand() [2/2]

Real STK::Law::Normal::rand ( Real const mu,
Real const sigma 
)
static

Generate a pseudo Normal random variate.

Generate a pseudo Normal random variate with location mu and standard deviation sigma parameters.

Parameters
mu,sigmamean and standard deviation of the Normal distribution
Returns
a pseudo normal random variate, centered in mu and with standard deviation sigma

Definition at line 173 of file STK_Law_Normal.cpp.

174{
175#ifdef STK_DEBUG
176 // check parameters
178 STKDOMAIN_ERROR_2ARG(Normal::rand,mu,sigma,invalid argument);
179#endif
180 // return variate
181 return (sigma == 0.) ? mu : generator.randGauss(mu, sigma);
182}

References mu(), rand(), sigma(), and STKDOMAIN_ERROR_2ARG.

◆ setMu()

void STK::Law::Normal::setMu ( Real const mu)
inline
Parameters
muthe mean to set

Definition at line 93 of file STK_Law_Normal.h.

93{ mu_ = mu;}

References mu(), and mu_.

◆ setSigma()

void STK::Law::Normal::setSigma ( Real const sigma)
inline
Parameters
sigmathe standard deviation to set

Definition at line 95 of file STK_Law_Normal.h.

96 {
98 sigma_ = sigma;
99 }
void setSigma(Real const &sigma)

References setSigma(), sigma(), sigma_, and STKDOMAIN_ERROR_1ARG.

Referenced by setSigma().

◆ sigma()

Real const & STK::Law::Normal::sigma ( ) const
inline
Returns
the standard deviation

Definition at line 91 of file STK_Law_Normal.h.

91{ return sigma_;}

References sigma_.

Referenced by cdf(), icdf(), lpdf(), Normal(), pdf(), rand(), and setSigma().

Member Data Documentation

◆ mu_

Real STK::Law::Normal::mu_
protected

The mu parameter.

Definition at line 185 of file STK_Law_Normal.h.

Referenced by cdf(), icdf(), lpdf(), mu(), pdf(), rand(), and setMu().

◆ sigma_

Real STK::Law::Normal::sigma_
protected

The sigma parameter.

Definition at line 187 of file STK_Law_Normal.h.

Referenced by cdf(), icdf(), lpdf(), pdf(), rand(), setSigma(), and sigma().


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