Binomial Class Reference

This class simulates a "Binomial distribution". More...

#include <Binomial.h>

Inheritance diagram for Binomial:

RandomVar< long >

List of all members.

Public Member Functions

 Binomial (unsigned n=1, double p=0.5)
 Creates a new instance of the binomial random number generator and initializes values for the Bernoulli formula.
 Binomial (unsigned n, double p, RNG &r)
 Creates a new binomial random generator instance by using the pseudo random number generator "r" for the determination of random values and initializes values for the Bernoulli formula.
double pp () const
 Returns the probability pP for a hit.
void pp (double newP)
 Sets the probability pP for a hit to the new value "newP".
unsigned n () const
 Returns the number of trials pN.
void n (unsigned newN)
 Sets the number of trials pN to the new value "newN".
long operator() (unsigned n, double p)
 Returns a binomial random number, i.e. the number of hits in "n" trials, with a probability of "p" for a single hit.
long operator() ()
 Returns a binomial random number, i.e. the number of hits in pN trials, with a probability of pP for a single hit.
double p (const long &) const
 Just a dummy method that is needed for instantiation of class Binomial.
double p (const unsigned x) const
 Returns the probability for "x" hits out of pN trials, when the probability for a single hit is pP.
double p (const unsigned n, const double p, const unsigned x) const
 Returns the probability for "x" hits out of "n" trials, when the probability for a single hit is "p".
unsigned binominalCoefficient (const unsigned n, const unsigned x) const
 Returns the binomial coefficient "n choose x".
unsigned factorial (const unsigned tt) const
 Returns the factorial "tt!".

Protected Attributes

unsigned pN
double pP


Detailed Description

This class simulates a "Binomial distribution".

This class is derived from class RandomVar and the uniformally distributed pseudo random number values of the interval (0,1) are transformed to type "long", indicating the number of "hits" out of a given number of trials with a given probability for a single hit.
The probability for a special number $x$ of hits can also be calculated by using the Bernoulli formula given as

$p_x^{(n)} = P(X = x) = {n \choose x} \cdot p^x \cdot q^{n-x}$

where $p$ is the probability for a single hit, $q = 1 - p$ and $n$ is the total number of trials.
Taking the "urn model", the Binomial distribution simulates the drawing of $n$ balls from the urn with returning the drawn balls to the urn.
Below you can see the distribution for the parameter values $n = 8$ and $p = 0.3$ and $p = 0.5$, respectively:

binomial.png

Author:
M. Kreutz
Date:
1995-01-01
Changes:
none
Status:
stable

Constructor & Destructor Documentation

Binomial::Binomial ( unsigned  n = 1,
double  p = 0.5 
)

Creates a new instance of the binomial random number generator and initializes values for the Bernoulli formula.

Creates a new instance of the binomial random number generator and initializes values for the Bernoulli formula.

The number of trials pN and the probability for a hit pP for the Bernoulli formula used by the random number generator are initialized.
For this instance, the default pseudo random number generator as member of class RandomVar is used.

Parameters:
n the number of trials pN, set to "1" by default
p the probability pP for a hit, set to "0.5" by default
Returns:
none
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Binomial::Binomial ( unsigned  n,
double  p,
RNG r 
)

Creates a new binomial random generator instance by using the pseudo random number generator "r" for the determination of random values and initializes values for the Bernoulli formula.

Creates a new binomial random generator instance by using the pseudo random number generator "r" for the determination of random values and initializes values for the Bernoulli formula.

Each instance of a binomial random number generator is based on a generator, that is defined in class RNG and returns uniformally pseudo random numbers of the interval (0,1). By default, this is a global generator named RNG::globalRng and included as member in class RandomVar.
Here another pseudo random number generator r is used instead.
Additionally to defining the used pseudo random number generator, the number of trials pN and the probability for a hit pP for the Bernoulli formula used by the random number generator are initialized.

Parameters:
n the number of trials pN
p the probability pP for a hit
r the pseudo random number generator that is used
Returns:
none
Author:
M. Kreutz
Date:
1998-08-17
Changes
none
Status
stable


Member Function Documentation

double Binomial::pp (  )  const [inline]

Returns the probability pP for a hit.

Returns:
the probability pP for a hit
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

void Binomial::pp ( double  newP  )  [inline]

Sets the probability pP for a hit to the new value "newP".

Parameters:
newP the new value for the probability pP
Returns:
none
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

unsigned Binomial::n (  )  const [inline]

Returns the number of trials pN.

Returns:
the number of trials pN
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

void Binomial::n ( unsigned  newN  )  [inline]

Sets the number of trials pN to the new value "newN".

Parameters:
newN the new value for the number of trials pN
Returns:
none
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

long Binomial::operator() ( unsigned  n,
double  p 
)

Returns a binomial random number, i.e. the number of hits in "n" trials, with a probability of "p" for a single hit.

Returns a binomial random number, i.e. the number of hits in "n" trials, with a probability of "p" for a single hit.

This method performs the inverse transformation of the original uniformally distributed random numbers of the interval (0,1) created by the used pseudo random number generator to the type of the binomial distribution.

Parameters:
n the number of trials
p the probability for a single hit
Returns:
the number of hits
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

long Binomial::operator() (  )  [virtual]

Returns a binomial random number, i.e. the number of hits in pN trials, with a probability of pP for a single hit.

Returns a binomial random number, i.e. the number of hits in pN trials, with a probability of pP for a single hit.

Returns:
the number of hits
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Implements RandomVar< long >.

double Binomial::p ( const long &  x  )  const [virtual]

Just a dummy method that is needed for instantiation of class Binomial.

Just a dummy method that is needed for instantiation of class Binomial.

To instantiate this class, this dummy method that is declared as purely virtual in class RandomVar, has to be implemented.

Parameters:
x not used
Returns:
always "0"
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

Implements RandomVar< long >.

double Binomial::p ( const unsigned  x  )  const

Returns the probability for "x" hits out of pN trials, when the probability for a single hit is pP.

Returns the probability for "x" hits out of pN trials, when the probability for a single hit is pP.

The Bernoulli formula, that calculates the probability for x hits out of n trials is here defined as

$ p_x^{(n)} = P(X = x) = {n \choose x} \cdot p^x \cdot q^{n-x} $

Here, for $n$ and $p$, the values of pN and pP are used.

Parameters:
x the number of hits, i.e. the value of $x$ in the Bernoulli formula
Returns:
the probability $p_x^{(n)}$
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

double Binomial::p ( const unsigned  n,
const double  p,
const unsigned  x 
) const

Returns the probability for "x" hits out of "n" trials, when the probability for a single hit is "p".

Returns the probability for "x" hits out of "n" trials, when the probability for a single hit is "p".

The Bernoulli formula, that calculates the probability for x hits out of n trials is here defined as

$ p_x^{(n)} = P(X = x) = {n \choose x} \cdot p^x \cdot q^{n-x} $

Parameters:
n the number of trials, i.e. the value of $n$ in the Bernoulli formula
p the probability for a single hit, i.e. the value of $p$ ($q = 1 -p$) in the Bernoulli formula
x the number of hits, i.e. the value of $x$ in the Bernoulli formula
Returns:
the probability $p_x^{(n)}$
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

unsigned Binomial::binominalCoefficient ( const unsigned  n,
const unsigned  x 
) const

Returns the binomial coefficient "n choose x".

The binomial coefficient is here defined as

$ {n \choose x} = \frac{n \cdot (n-1) \cdot (n-2) \cdot \dots \cdot (n-x+1)}{1 \cdot 2 \cdot \dots \cdot x} = \prod_{i=1}^x \frac{n+1-i}{i} $

Parameters:
n the value of $n$ in ${n \choose x}$
x the value of $x$ in ${n \choose x}$
Returns:
the binomial coefficient ${n \choose x}$
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable

unsigned Binomial::factorial ( const unsigned  tt  )  const

Returns the factorial "tt!".

The factorial of tt defined as

$ tt! = tt \cdot (tt - 1) \cdot (tt - 2) \cdot \dots \cdot 2 \cdot 1 = \prod_{i=1}^{tt} i $

Parameters:
tt the value of which the factorial is calculated
Returns:
$tt!$
Author:
M. Kreutz
Date:
1995-01-01
Changes
none
Status
stable


Member Data Documentation

unsigned Binomial::pN [protected]

The number of trials, i.e. the value $n$ in the Bernoulli formula $p_x^{(n)} = P(X = x) = {n \choose x} \cdot p^x \cdot q^{n-x}$.

double Binomial::pP [protected]

The probability for a "hit", i.e. the value $p$ in the Bernoulli formula $p_x^{(n)} = P(X = x) = {n \choose x} \cdot p^x \cdot q^{n-x}$.


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

Generated on Thu Aug 28 18:34:39 2008 for Dolphin by  doxygen 1.5.6