STK++ 0.9.13

Poisson distribution law. More...

#include <STK_Law_Poisson.h>

Inheritance diagram for STK::Law::Poisson:
Inheritance graph

Public Types

typedef IUnivLaw< intBase
 

Public Member Functions

 Poisson (Real const &lambda=1.)
 constructor
 
virtual ~Poisson ()
 destructor
 
Real constlambda () const
 
void setLambda (Real const &lambda)
 
virtual int rand () const
 
virtual Real pdf (int const &x) const
 compute the probability distribution function.
 
virtual Real lpdf (int const &x) const
 compute the log probability distribution function.
 
virtual Real cdf (Real const &t) const
 compute the cumulative distribution function Give the probability that a Poisson random variate is less or equal to t.
 
virtual int icdf (Real const &p) const
 inverse cumulative distribution function The quantile is defined as the smallest value q such that F(q) >= p , where F is the cumulative distribution function.
 
- Public Member Functions inherited from STK::Law::IUnivLaw< int >
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 int rand (Real const &lambda)
 
static Real pdf (int const &x, Real const &lambda)
 compute the probability distribution function Give the value of the pdf at the point x.
 
static Real lpdf (int const &x, Real const &lambda)
 compute the log probability distribution function Give the value of the log-pdf at the point x.
 
static Real cdf (Real const &t, Real const &lambda)
 compute the cumulative distribution function Give the probability that a Poisson random variate is less or equal to t.
 
static int icdf (Real const &p, Real const &lambda)
 inverse cumulative distribution function The quantile is defined as the smallest value x such that F(x) >= p , where F is the cumulative distribution function.
 

Protected Attributes

Real lambda_
 mean of the Poisson distribution
 
- Protected Attributes inherited from STK::Law::ILawBase
String name_
 Name of the Law.
 

Additional Inherited Members

- Protected Member Functions inherited from STK::Law::IUnivLaw< int >
 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

Poisson distribution law.

In probability theory and statistics, the Poisson distribution, named after French mathematician Siméon Denis Poisson, is a discrete probability distribution that expresses the probability of a given number of events occurring in a fixed interval of time and/or space if these events occur with a known average rate and independently of the time since the last event.

The Poisson distribution can be applied to systems with a large number of possible events, each of which is rare. How many such events will occur during a fixed time interval? Under the right circumstances, this is a random number with a Poisson distribution.

A discrete random variable X is said to have a Poisson distribution with parameter $ \lambda >0 $ (the mean) if the probability mass function of X is given by

\[
  f(k; \lambda) = P(X=k) = \frac{\lambda^k e^{-\lambda}}{k!}, \quad k=0,1,2,\ldots,
\]

The positive real number $ \lambda $ is equal to the expected value of X and also to its variance.

Definition at line 69 of file STK_Law_Poisson.h.

Member Typedef Documentation

◆ Base

Constructor & Destructor Documentation

◆ Poisson()

STK::Law::Poisson::Poisson ( Real const lambda = 1.)
inline

constructor

Parameters
lambdamean of a Poisson distribution

Definition at line 76 of file STK_Law_Poisson.h.

77 : Base(_T("Poisson")), lambda_(lambda) {}
#define _T(x)
Let x unmodified.
Real lambda_
mean of the Poisson distribution
IUnivLaw< int > Base
Real const & lambda() const

◆ ~Poisson()

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

destructor

Definition at line 79 of file STK_Law_Poisson.h.

79{}

Member Function Documentation

◆ cdf() [1/2]

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

compute the cumulative distribution function Give the probability that a Poisson random variate is less or equal to t.

Parameters
ta real value
Returns
the value of the cdf

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

Definition at line 202 of file STK_Law_Poisson.cpp.

203{
204 // check trivial values
205 if (t < 0) return 0;
206 if (lambda_ == 0.) return( 1. );
207 // use gamma ratio function
208 return Funct::gammaRatioQ(std::floor(t) + 1, lambda_);
209}
Real gammaRatioQ(Real const &a, Real const &x)
Compute the incomplete gamma function ratio Q(a,x).

References STK::Funct::gammaRatioQ(), and lambda_.

Referenced by icdf(), and icdf().

◆ cdf() [2/2]

Real STK::Law::Poisson::cdf ( Real const t,
Real const lambda 
)
static

compute the cumulative distribution function Give the probability that a Poisson random variate is less or equal to t.

Parameters
ta real value
lambdathe mean
Returns
the value of the cdf at t

Definition at line 297 of file STK_Law_Poisson.cpp.

298{
299 // check trivial values
300 if (t < 0) return 0;
301 if (lambda == 0.) return( 1. );
302 // use gamma ratio function
303 return Funct::gammaRatioQ(std::floor(t) + 1, lambda);
304}

References STK::Funct::gammaRatioQ(), and lambda().

◆ icdf() [1/2]

int STK::Law::Poisson::icdf ( Real const p) const
virtual

inverse cumulative distribution function The quantile is defined as the smallest value q such that F(q) >= p , where F is the cumulative distribution function.

Parameters
pa probability number
Returns
the quantile for p

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

Definition at line 215 of file STK_Law_Poisson.cpp.

216{
217 // check trivial values
218 if (p == 0.) return 0;
219 if (p == 1.) return Arithmetic<int>::max(); // infty does not exists for int
220 // check values 0 and 1
221 Real el = std::exp(-lambda_);
222 if (p<el) return 0;
223 if (p<(1+lambda_)*el) return 1;
224 // othewise search
225 Real q = gauss_icdf_fast(p), sqlambda = std::sqrt(lambda_);
226 int k = std::max(0., round(lambda_ + q * sqlambda + (q-1.)*(q+1.)*(1.-q/(12.*sqlambda))/6));
227 if (cdf(k) >= p)
228 { /* decreasing search */
229 while(1)
230 {
231 if ( cdf(k - 1) < p) return k;
232 k--;
233 }
234 }
235 else
236 { /* increasing search */
237 while(1)
238 {
239 k++;
240 if( cdf(k) >= p) return k;
241 }
242 }
243 return k; // avoid warning at compilation
244}
virtual Real cdf(Real const &t) const
compute the cumulative distribution function Give the probability that a Poisson random variate is le...
double Real
STK fundamental type of Real values.

References cdf(), and lambda_.

◆ icdf() [2/2]

int STK::Law::Poisson::icdf ( Real const p,
Real const lambda 
)
static

inverse cumulative distribution function The quantile is defined as the smallest value x such that F(x) >= p , where F is the cumulative distribution function.

Parameters
pa probability number
lambdathe mean
Returns
the quantile for p

Definition at line 310 of file STK_Law_Poisson.cpp.

311{
312 // check trivial values
313 if (p == 0.) return 0;
314 if (p == 1.) return Arithmetic<int>::max();
315 // check values 0 and 1
316 Real el = std::exp(-lambda);
317 if (p<el) return 0;
318 if (p< (1+lambda)*el) return 1;
319 // othewise search
320 Real q = gauss_icdf_fast(p), sqlambda = std::sqrt(lambda);
321 int k = round(lambda + q * sqlambda + (q-1.)*(q+1.)*(1.-q/(12.*sqlambda))/6);
322 if (cdf(k, lambda) >= p)
323 { /* decreasing search */
324 while(1)
325 {
326 if ( cdf(k - 1,lambda) < p) return k;
327 k--;
328 }
329 }
330 else
331 { /* increasing search */
332 while(1)
333 {
334 k++;
335 if( cdf(k, lambda) >= p) return k;
336 }
337 }
338 return k; // avoid warning at compilation
339}

References cdf(), and lambda().

◆ lambda()

Real const & STK::Law::Poisson::lambda ( ) const
inline
Returns
the mean

Definition at line 81 of file STK_Law_Poisson.h.

81{ return lambda_;}

References lambda_.

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

◆ lpdf() [1/2]

Real STK::Law::Poisson::lpdf ( int const x) const
virtual

compute the log probability distribution function.

Give the value of the log-pdf at the point x.

Parameters
xan integer value
Returns
the value of the log-pdf

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

Definition at line 183 of file STK_Law_Poisson.cpp.

184{
185 // check trivial values
186 if (x<0) return( -Arithmetic<Real>::infinity() );
187 // if lambda is 0, we have P(X=0) = 1
188 if (lambda_==0.) return( (x==0) ? 0 : -Arithmetic<Real>::infinity() );
189 // special value
190 if (x==0) return( -lambda_ );
191 // stirling approximation and deviance
193 -Const::_LNSQRT2PI_-std::log((Real)x)/2.
194 );
195}
Real dev0(Real const &a, Real const &b)
compute the partial deviance .
Real lgammaStirlingError(Real const &z)
Compute the error when we compute using the Stirling's formula.

References STK::Funct::dev0(), lambda_, and STK::Funct::lgammaStirlingError().

Referenced by STK::PoissonBase< Derived >::lnComponentProbability().

◆ lpdf() [2/2]

Real STK::Law::Poisson::lpdf ( int const x,
Real const lambda 
)
static

compute the log probability distribution function Give the value of the log-pdf at the point x.

Parameters
xa binary value
lambdathe mean
Returns
the value of the log-pdf

Definition at line 277 of file STK_Law_Poisson.cpp.

278{
279 // check trivial values
280 if (x<0) return( -Arithmetic<Real>::infinity() );
281 // if lambda is 0, we have P(X=0) = 1
282 if (lambda==0.) return( (x==0) ? 0 : -Arithmetic<Real>::infinity() );
283 // special value
284 if (x==0) return( -lambda );
285 // stirling approximation and deviance
287 -Const::_LNSQRT2PI_-std::log((Real)x)/2.
288 );
289}

References STK::Funct::dev0(), lambda(), and STK::Funct::lgammaStirlingError().

◆ pdf() [1/2]

Real STK::Law::Poisson::pdf ( int const x) const
virtual

compute the probability distribution function.

Give the value of the pdf at the point x.

Parameters
xa binary value
Returns
the value of the pdf

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

Definition at line 164 of file STK_Law_Poisson.cpp.

165{
166 // check trivial values
167 if (x<0) return( 0. );
168 // if lambda is 0, we have P(X=0) = 1
169 if (lambda_==0.) return( (x==0) ? 1. : 0. );
170 // special value
171 if (x==0) return( Real(std::exp(-lambda_)) );
172 // stirling approximation and deviance
173 return( std::exp(-Funct::lgammaStirlingError((Real)x)-Funct::dev0((Real)x, lambda_))
174 /(Const::_SQRT2PI_*std::sqrt(x))
175 );
176}

References STK::Funct::dev0(), lambda_, and STK::Funct::lgammaStirlingError().

◆ pdf() [2/2]

Real STK::Law::Poisson::pdf ( int const x,
Real const lambda 
)
static

compute the probability distribution function Give the value of the pdf at the point x.

Parameters
xa binary value
lambdathe mean
Returns
the value of the pdf

Definition at line 257 of file STK_Law_Poisson.cpp.

258{
259 // check trivial values
260 if (x<0) return( 0. );
261 // if lambda is 0, we have P(X=0) = 1
262 if (lambda==0.) return( (x==0) ? 1. : 0. );
263 // special value
264 if (x==0) return( Real(std::exp(-lambda)) );
265 // stirling approximation and deviance
266 return( std::exp(-Funct::lgammaStirlingError((Real)x)-Funct::dev0((Real)x, lambda))
267 /(Const::_SQRT2PI_*std::sqrt(x))
268 );
269}

References STK::Funct::dev0(), lambda(), and STK::Funct::lgammaStirlingError().

◆ rand() [1/2]

int STK::Law::Poisson::rand ( ) const
virtual
Returns
a Poisson random variate .

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

Definition at line 155 of file STK_Law_Poisson.cpp.

156{
157 return poisson_icd_raw(Law::generator.randUnif(), lambda_);
158}

References lambda_.

Referenced by STK::PoissonBase< Derived >::rand().

◆ rand() [2/2]

int STK::Law::Poisson::rand ( Real const lambda)
static
Parameters
lambdathe mean
Returns
a int random variate.

Definition at line 249 of file STK_Law_Poisson.cpp.

250{ return poisson_icd_raw(Law::generator.randUnif(), lambda);}

References lambda().

◆ setLambda()

void STK::Law::Poisson::setLambda ( Real const lambda)
inline
Parameters
lambdamean to set

Definition at line 83 of file STK_Law_Poisson.h.

84 {
85 if (lambda<0)
88 }
#define STKDOMAIN_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:165
void setLambda(Real const &lambda)

References lambda(), lambda_, setLambda(), and STKDOMAIN_ERROR_1ARG.

Referenced by setLambda().

Member Data Documentation

◆ lambda_

Real STK::Law::Poisson::lambda_
protected

mean of the Poisson distribution

Definition at line 165 of file STK_Law_Poisson.h.

Referenced by cdf(), icdf(), lambda(), lpdf(), pdf(), rand(), and setLambda().


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