STK++ 0.9.13

Bernoulli probability law. More...

#include <STK_Law_Bernoulli.h>

Inheritance diagram for STK::Law::Bernoulli:
Inheritance graph

Public Types

typedef IUnivLaw< BinaryBase
 

Public Member Functions

 Bernoulli (Real const &prob=0.5)
 constructor
 
virtual ~Bernoulli ()
 destructor
 
Real constprob () const
 
void setProb (Real const &prob)
 
virtual Binary rand () const
 
virtual Real pdf (Binary const &x) const
 compute the probability distribution function (density) Give the value of the pdf at the point x.
 
virtual Real lpdf (Binary const &x) const
 compute the log probability distribution function Give the value of the log-pdf at the point x.
 
virtual Real cdf (Real const &t) const
 compute the cumulative distribution function Give the probability that a Bernoulli random variate is less or equal to t.
 
virtual Binary icdf (Real const &prob) const
 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.
 
- Public Member Functions inherited from STK::Law::IUnivLaw< Binary >
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 Binary rand (Real const &prob)
 
static Real pdf (Binary const &x, Real const &prob)
 compute the probability distribution function (density) Give the value of the pdf at the point x.
 
static Real lpdf (Binary const &x, Real const &prob)
 compute the log probability distribution function Give the value of the log-pdf at the point x.
 

Protected Attributes

Real prob_
 probability of success in a Bernoulli trial
 
- Protected Attributes inherited from STK::Law::ILawBase
String name_
 Name of the Law.
 

Additional Inherited Members

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

Bernoulli probability law.

In probability theory and statistics, the Bernoulli distribution, named after Swiss scientist Jacob Bernoulli, is a discrete probability distribution, which takes value 1 with success probability p and value 0 with failure probability q=1-p. So if X is a random variable with this distribution, we have:

\[
   \mathbb{P}(X=1) = 1 - \mathbb{P}(X=0) = 1 - q = p.
\]

A classical example of a Bernoulli experiment is a single toss of a coin. The coin might come up heads with probability p and tails with probability 1-p. The experiment is called fair if p=0.5, indicating the origin of the terminology in betting (the bet is fair if both possible outcomes have the same probability).

Definition at line 63 of file STK_Law_Bernoulli.h.

Member Typedef Documentation

◆ Base

Constructor & Destructor Documentation

◆ Bernoulli()

STK::Law::Bernoulli::Bernoulli ( Real const prob = 0.5)
inline

constructor

Parameters
probprobability of success in a Bernoulli trial

Definition at line 70 of file STK_Law_Bernoulli.h.

70 : Base(String(_T("Bernoulli")) ), prob_(prob)
71 {
72 if (prob<0) STKDOMAIN_ERROR_1ARG(Bernoulli,prob,prob must be >= 0);
73 if (prob>1) STKDOMAIN_ERROR_1ARG(Bernoulli,prob,prob must be <= 1);
74 }
#define STKDOMAIN_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:165
#define _T(x)
Let x unmodified.
Real prob_
probability of success in a Bernoulli trial
Real const & prob() const
IUnivLaw< Binary > Base
std::basic_string< Char > String
STK fundamental type of a String.

References prob(), and STKDOMAIN_ERROR_1ARG.

◆ ~Bernoulli()

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

destructor

Definition at line 76 of file STK_Law_Bernoulli.h.

76{}

Member Function Documentation

◆ cdf()

Real STK::Law::Bernoulli::cdf ( Real const t) const
inlinevirtual

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

Parameters
ta real value
Returns
the value of the cdf

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

Definition at line 162 of file STK_Law_Bernoulli.h.

163{ return (t<0.) ? 0. : (t<1.) ? 1.-prob_ : 1.;}

References prob_.

◆ icdf()

Binary STK::Law::Bernoulli::icdf ( Real const prob) const
inlinevirtual

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
proba probability number

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

Definition at line 164 of file STK_Law_Bernoulli.h.

165{
166#ifdef STK_STATISTIK_DEBUG
169#endif
170 return (prob==0) ? zero_ : (prob==1) ? one_ : (prob <= 1.-prob_) ? zero_ : one_;
171}
virtual Binary icdf(Real const &prob) const
inverse cumulative distribution function The quantile is defined as the smallest value x such that F...
@ zero_
0 value
Definition STK_Binary.h:49
@ one_
1 value
Definition STK_Binary.h:50

References icdf(), STK::one_, prob(), prob_, STKDOMAIN_ERROR_1ARG, and STK::zero_.

Referenced by icdf().

◆ lpdf() [1/2]

Real STK::Law::Bernoulli::lpdf ( Binary const x) const
inlinevirtual

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

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

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

Definition at line 152 of file STK_Law_Bernoulli.h.

153{
154 switch (x)
155 {
156 case zero_: return (prob_ == 1) ? -Arithmetic<Real>::infinity() : std::log(1.-prob_);
157 case one_: return (prob_ == 0) ? -Arithmetic<Real>::infinity() : std::log(prob_);
158 default: break;
159 }
160 return Arithmetic<Real>::NA();
161}
static Type NA()
Adding a Non Available (NA) special number.

References STK::Arithmetic< Type >::NA(), STK::one_, prob_, and STK::zero_.

Referenced by lpdf().

◆ lpdf() [2/2]

Real STK::Law::Bernoulli::lpdf ( Binary const x,
Real const prob 
)
inlinestatic

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

Parameters
xa binary value
proba probability number
Returns
the value of the log-pdf

Definition at line 197 of file STK_Law_Bernoulli.h.

198{
199#ifdef STK_STATISTIK_DEBUG
202#endif
203 switch (x)
204 {
205 case zero_: return (prob == 1) ? -Arithmetic<Real>::infinity() : std::log(1.-prob);
206 case one_: return (prob == 0) ? -Arithmetic<Real>::infinity() : std::log(prob);
207 default: break;
208 }
209 return Arithmetic<Real>::NA();
210}
virtual Real lpdf(Binary const &x) const
compute the log probability distribution function Give the value of the log-pdf at the point x.

References lpdf(), STK::Arithmetic< Type >::NA(), STK::one_, prob(), STKDOMAIN_ERROR_1ARG, and STK::zero_.

◆ pdf() [1/2]

Real STK::Law::Bernoulli::pdf ( Binary const x) const
inlinevirtual

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

Parameters
xa binary value
Returns
the value of the pdf

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

Definition at line 142 of file STK_Law_Bernoulli.h.

143{
144 switch (x)
145 {
146 case zero_: return 1.-prob_;
147 case one_: return prob_;
148 default: break;
149 }
150 return Arithmetic<Real>::NA();
151}

References STK::Arithmetic< Type >::NA(), STK::one_, prob_, and STK::zero_.

Referenced by pdf().

◆ pdf() [2/2]

Real STK::Law::Bernoulli::pdf ( Binary const x,
Real const prob 
)
inlinestatic

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

Parameters
xa binary value
proba probability number
Returns
the value of the pdf

Definition at line 183 of file STK_Law_Bernoulli.h.

184{
185#ifdef STK_STATISTIK_DEBUG
186 if (prob<0) STKDOMAIN_ERROR_1ARG(Bernoulli::pdf,prob,prob must be >= 0);
187 if (prob>1) STKDOMAIN_ERROR_1ARG(Bernoulli::pdf,prob,prob must be <= 1);
188#endif
189 switch (x)
190 {
191 case zero_: return 1.-prob;
192 case one_: return prob;
193 default: break;
194 }
195 return Arithmetic<Real>::NA();
196}
virtual Real pdf(Binary const &x) const
compute the probability distribution function (density) Give the value of the pdf at the point x.

References STK::Arithmetic< Type >::NA(), STK::one_, pdf(), prob(), STKDOMAIN_ERROR_1ARG, and STK::zero_.

◆ prob()

Real const & STK::Law::Bernoulli::prob ( ) const
inline
Returns
the probability of success

Definition at line 78 of file STK_Law_Bernoulli.h.

78{ return prob_;}

References prob_.

Referenced by Bernoulli(), icdf(), lpdf(), pdf(), rand(), and setProb().

◆ rand() [1/2]

Binary STK::Law::Bernoulli::rand ( ) const
inlinevirtual
Returns
a Binary random variate .

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

Definition at line 139 of file STK_Law_Bernoulli.h.

140{ return (Law::generator.randUnif()<=prob_) ? one_ : zero_;}

References STK::one_, prob_, and STK::zero_.

Referenced by rand().

◆ rand() [2/2]

Binary STK::Law::Bernoulli::rand ( Real const prob)
inlinestatic
Parameters
proba probability number
Returns
a Binary random variate.

Definition at line 174 of file STK_Law_Bernoulli.h.

175{
176#ifdef STK_STATISTIK_DEBUG
179#endif
180 return (generator.randUnif()<=prob) ? one_ : zero_;
181}
virtual Binary rand() const

References STK::one_, prob(), rand(), STKDOMAIN_ERROR_1ARG, and STK::zero_.

◆ setProb()

void STK::Law::Bernoulli::setProb ( Real const prob)
inline
Parameters
probthe probability of success to set

Definition at line 80 of file STK_Law_Bernoulli.h.

81 {
84 prob_ = prob;
85 }
void setProb(Real const &prob)

References prob(), prob_, setProb(), and STKDOMAIN_ERROR_1ARG.

Referenced by STK::BernoulliModel< Array, WColVector >::computeParameters(), STK::BernoulliModel< Array, WColVector >::computeParameters(), and setProb().

Member Data Documentation

◆ prob_

Real STK::Law::Bernoulli::prob_
protected

probability of success in a Bernoulli trial

Definition at line 136 of file STK_Law_Bernoulli.h.

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


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