Population
Image processing library in C++
Public Member Functions | Static Public Member Functions | List of all members
pop::Distribution Class Referenceabstract

abstract class for Distribution More...

Inheritance diagram for pop::Distribution:
pop::DistributionBinomial pop::DistributionDirac pop::DistributionExponential pop::DistributionNormal pop::DistributionPoisson pop::DistributionSign pop::DistributionTriangle pop::DistributionUniformInt pop::DistributionUniformReal

Public Member Functions

virtual ~Distribution ()
 
virtual F32 operator() (F32 value) const =0
 
virtual F32 randomVariable () const =0
 
virtual Distributionclone () const =0
 
void display (F32 xmin=0, F32 xmax=255) const
 

Static Public Member Functions

static MTRand_int32 & irand ()
 

Detailed Description

abstract class for Distribution

Author
Tariel Vincent

Basic definition

The class Distribution, not named function to avoid any confusions with the function concept, is a mapping from the real number to the real number where the input element, x, completely determines the output element, y, by this relation: y = f(x). The evaluation of the output element from an input element is a symbolic (implicit) relation for instance, for a function equal to $x \mapsto x^2$, the output element is evaluated by the multiplication of the input element by itself.

Code

I implement a class pop::Distribution and many derived. Because I hide the polymorphism mechanis with an implementation pattern, you have to create objects (and not pointer) of the class pop::Distribution to operate

For instance, this code

DistributionExpression f("x");//f(x)=x
for(int i =0;i<4;i++)
std::cout<<f(i)<<" ";
std::cout<<std::endl;
DistributionArithmeticMultiplication fmult = f*f;//now f(x)=x*x
for(int i =0;i<4;i++)
std::cout<<fmult(i)<<" ";
std::cout<<std::endl;
DistributionRegularStep fderivate = pop::Statistics::derivate(fmult,0,10);//now f(x)=2*x
for(int i =0;i<4;i++)
std::cout<<fderivate(i)<<" ";
std::cout<<std::endl;

produce this output:
0 1 2 3
0 1 4 9
0.01 2.01 4.01 6.01

Constructor & Destructor Documentation

pop::Distribution::~Distribution ( )
inlinevirtual

virtual destructor

Member Function Documentation

virtual Distribution* pop::Distribution::clone ( ) const
pure virtual
virtual F32 pop::Distribution::operator() ( F32  value) const
pure virtual
Parameters
valueinput value
Returns
y

Unary function to call y=f(value)

DistributionExpression f("sqrt(x)");
for(int i =0;i<4;i++)
std::cout<<f(i)<<" ";

Implemented in pop::DistributionTriangle, pop::DistributionDirac, pop::DistributionPoisson, pop::DistributionExponential, pop::DistributionBinomial, pop::DistributionNormal, pop::DistributionUniformInt, pop::DistributionUniformReal, and pop::DistributionSign.

virtual F32 pop::Distribution::randomVariable ( ) const
pure virtual
Returns
X random variable

Generate random variable, X, following the probability distribution f.
However, some distributions do not have a procedure to generate some random variables. In this case, you have to use this algorithm pop::Statistics::toProbabilityDistribution to generate the probability distribution.
For instance, in the following code, we simulate random variables followin an exponential probability distribution in two ways: from analytical or from expression. Because DistributionExpression does not have a generator of random number, we call the procedure pop::Statistics::toProbabilityDistribution .

F32 lambda=1.f/10;
std::string exp = "exp(-"+BasicUtility::Any2String(lambda)+"*x)";
DistributionExpression expression(exp.c_str());
DistributionRegularStep fexponentialfromexpression = pop::Statistics::toProbabilityDistribution(expression,0,100);//DistributionExpression does not have a generator of random number.
DistributionPoisson fexponentialfromanalytical(1./lambda);//DistributionExponential, has a genrator so you don't need to use pop::Statistics::toProbabilityDistribution
int nbr_sampling = 500000;
F32 sum1=0,sum2=0;
for(int i =0;i<nbr_sampling;i++){
sum1+=fexponentialfromexpression.randomVariable();
sum2+=fexponentialfromanalytical.randomVariable();
}
std::cout<<"Mean1: "<< sum1/nbr_sampling<<std::endl;
std::cout<<"Mean2: "<< sum2/nbr_sampling<<std::endl;
return 1;

Implemented in pop::DistributionTriangle, pop::DistributionDirac, pop::DistributionPoisson, pop::DistributionExponential, pop::DistributionBinomial, pop::DistributionNormal, pop::DistributionUniformInt, pop::DistributionUniformReal, and pop::DistributionSign.


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