STK++ 0.9.13

Categorical probability law. More...

#include <STK_Law_Categorical.h>

Inheritance diagram for STK::Law::Categorical:
Inheritance graph

Public Types

typedef IUnivLaw< intBase
 

Public Member Functions

 Categorical ()
 Default constructor.
 
 Categorical (Array2DVector< Real > const &prob)
 constructor with given probabilities.
 
template<class OtherArray >
 Categorical (OtherArray const &prob)
 constructor with given probabilities.
 
virtual ~Categorical ()
 destructor
 
Array2DVector< Real > constprob () const
 
Array2DVector< Real > constcumProb () const
 
template<class OtherArray >
void setProb (OtherArray const &prob)
 
virtual int rand () const
 
virtual Real cdf (Real const &t) const
 compute the cumulative distribution function Give the probability that a Categorical random variate is less or equal to t.
 
virtual Real pdf (int const &x) const
 compute the probability distribution function (density) Give the value of the pdf at the point x.
 
virtual Real lpdf (int const &x) const
 compute the log probability distribution function Give the value of the log-pdf at the point x.
 
virtual int 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< 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

template<class OtherArray >
static int rand (OtherArray const &prob)
 
template<class OtherArray >
static Real lpdf (int const &x, OtherArray const &prob)
 compute the log probability distribution function Give the value of the log-pdf at the point x.
 

Protected Attributes

Array2DVector< Realprob_
 probabilities in a Categorical trial
 
Array2DVector< RealcumProb_
 cumulative probabilities in a Categorical trial
 
- Protected Attributes inherited from STK::Law::ILawBase
String name_
 Name of the Law.
 

Private Member Functions

void computeCumProb ()
 

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

Categorical probability law.

In probability theory and statistics, a categorical distribution (also called a "generalized Bernoulli distribution" or, less precisely, a "discrete distribution") is a probability distribution that describes the result of a random event that can take on one of K possible outcomes, with the probability of each outcome separately specified. There is not necessarily an underlying ordering of these outcomes, but numerical labels are attached for convenience in describing the distribution, often in the range 1 to K. Note that the K-dimensional categorical distribution is the most general distribution over a K-way event; any other discrete distribution over a size-K sample space is a special case. The parameters specifying the probabilities of each possible outcome are constrained only by the fact that each must be in the range 0 to 1, and all must sum to 1.

Note that, in some fields, such as machine learning and natural language processing, the categorical and multinomial distributions are conflated, and it is common to speak of a "multinomial distribution" when a categorical distribution is actually meant.

Definition at line 68 of file STK_Law_Categorical.h.

Member Typedef Documentation

◆ Base

Constructor & Destructor Documentation

◆ Categorical() [1/3]

STK::Law::Categorical::Categorical ( )
inline

Default constructor.

Only one category

Definition at line 73 of file STK_Law_Categorical.h.

73: Base(_T("Categorical")), prob_(1,1) { computeCumProb();}
#define _T(x)
Let x unmodified.
Array2DVector< Real > prob_
probabilities in a Categorical trial

References computeCumProb().

◆ Categorical() [2/3]

STK::Law::Categorical::Categorical ( Array2DVector< Real > const prob)
inline

constructor with given probabilities.

The probabilities will be normalized in order to have an overall sum of 1

Parameters
probprobabilities of success in a Categorical trial

Definition at line 78 of file STK_Law_Categorical.h.

78 : Base(_T("Categorical")), prob_(prob)
79 { computeCumProb();}
Array2DVector< Real > const & prob() const

References computeCumProb().

◆ Categorical() [3/3]

template<class OtherArray >
STK::Law::Categorical::Categorical ( OtherArray const prob)
inline

constructor with given probabilities.

The probabilities will be normalized in order to have an overall sum of 1

Parameters
probprobabilities of success in a Categorical trial

Definition at line 85 of file STK_Law_Categorical.h.

85 : Base(_T("Categorical"))

References computeCumProb(), prob(), and prob_.

◆ ~Categorical()

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

destructor

Definition at line 88 of file STK_Law_Categorical.h.

88{}

Member Function Documentation

◆ cdf()

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

compute the cumulative distribution function Give the probability that a Categorical 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 189 of file STK_Law_Categorical.h.

190{ return (t<prob_.begin()) ? 0. : (t>=prob_.lastIdx()) ? 1. : cumProb_[std::floor(t)];}
Array2DVector< Real > cumProb_
cumulative probabilities in a Categorical trial

References cumProb_, and prob_.

◆ computeCumProb()

void STK::Law::Categorical::computeCumProb ( )
inlineprivate

Definition at line 207 of file STK_Law_Categorical.h.

208{
209 cumProb_.resize(prob_.range());
210 Real sum=0.;
211 for (int k=prob_.begin(); k< prob_.end(); ++k)
212 { cumProb_[k] = (sum+=prob_[k]);}
213 // normalize
214 if (sum) {cumProb_/=sum; prob_ /=sum;}
215 else {STKINVALIDARGUMENT_ERROR_NO_ARG(Categorical::computeCumProb,sum of the probabilities is zero);}
216}
#define STKINVALIDARGUMENT_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:201
Derived & resize(Range const &I, Range const &J)
resize the array.
Arrays::SumOp< Lhs, Rhs >::result_type sum(Lhs const &lhs, Rhs const &rhs)
convenience function for summing two arrays
double Real
STK fundamental type of Real values.

References computeCumProb(), cumProb_, prob_, STK::IArray2D< Derived >::resize(), STKINVALIDARGUMENT_ERROR_NO_ARG, and STK::sum().

Referenced by Categorical(), Categorical(), Categorical(), computeCumProb(), and setProb().

◆ cumProb()

Array2DVector< Real > const & STK::Law::Categorical::cumProb ( ) const
inline
Returns
the cumulative probabilities of success

Definition at line 93 of file STK_Law_Categorical.h.

93{ return cumProb_;}

References cumProb_.

◆ icdf()

int STK::Law::Categorical::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< int >.

Definition at line 197 of file STK_Law_Categorical.h.

198{
201 int k;
202 for (k = cumProb_.begin(); k< cumProb_.lastIdx(); ++k)
203 { if (cumProb_[k] >= prob) return k;}
204 return k;
205}
#define STKDOMAIN_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:165
virtual int icdf(Real const &prob) const
inverse cumulative distribution function The quantile is defined as the smallest value x such that F...

References cumProb_, icdf(), prob(), and STKDOMAIN_ERROR_1ARG.

Referenced by icdf().

◆ lpdf() [1/2]

Real STK::Law::Categorical::lpdf ( int 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< int >.

Definition at line 181 of file STK_Law_Categorical.h.

182{ return (prob_[x] == 0) ? -Arithmetic<Real>::infinity() : std::log(prob_[x]);}

References prob_.

◆ lpdf() [2/2]

template<class OtherArray >
static Real STK::Law::Categorical::lpdf ( int const x,
OtherArray const prob 
)
inlinestatic

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

Parameters
xthe value to compute the lpdf.
probthe probability of each value
Returns
the value of the log-pdf

Definition at line 146 of file STK_Law_Categorical.h.

147 { return (prob[x] == 0) ? -Arithmetic<Real>::infinity() : std::log(prob[x]);}

References prob().

◆ pdf()

Real STK::Law::Categorical::pdf ( int const x) const
inlinevirtual

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

Parameters
xan integer value
Returns
the value of the pdf

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

Definition at line 174 of file STK_Law_Categorical.h.

175{ return prob_[x];}

References prob_.

◆ prob()

Array2DVector< Real > const & STK::Law::Categorical::prob ( ) const
inline
Returns
the probabilities of success

Definition at line 91 of file STK_Law_Categorical.h.

91{ return prob_;}

References prob_.

Referenced by Categorical(), icdf(), lpdf(), rand(), and setProb().

◆ rand() [1/2]

int STK::Law::Categorical::rand ( ) const
inlinevirtual
Returns
a categorical random variate .

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

Definition at line 160 of file STK_Law_Categorical.h.

161{
162 Real u = Law::generator.randUnif();
163 int k;
164 for(k = cumProb_.begin(); k< cumProb_.end(); k++)
165 { if (u<=cumProb_[k]) return k;}
166 return k;
167}

References cumProb_.

Referenced by STK::CategoricalBase< Derived >::rand(), STK::MixtureSemiLearner::randomZi(), STK::IMixtureDensity< Derived >::sample(), STK::IMixtureModel< Derived >::sample(), and STK::IMixtureComposer::sStep().

◆ rand() [2/2]

template<class OtherArray >
static int STK::Law::Categorical::rand ( OtherArray const prob)
inlinestatic
Returns
a categorical random variate .

Definition at line 129 of file STK_Law_Categorical.h.

130 {
131 Real u = Law::generator.randUnif(), cum = 0.;
132 for(int k = prob.begin(); k< prob.lastIdx(); k++)
133 {
134 cum += prob[k];
135 if (u<=cum) return k;
136 }
137 return prob.lastIdx();
138 }

References prob().

◆ setProb()

template<class OtherArray >
void STK::Law::Categorical::setProb ( OtherArray const prob)
inline
Parameters
probthe probability of success to set

Definition at line 97 of file STK_Law_Categorical.h.

References computeCumProb(), prob(), and prob_.

Member Data Documentation

◆ cumProb_

Array2DVector<Real> STK::Law::Categorical::cumProb_
protected

cumulative probabilities in a Categorical trial

Definition at line 153 of file STK_Law_Categorical.h.

Referenced by cdf(), computeCumProb(), cumProb(), icdf(), and rand().

◆ prob_

Array2DVector<Real> STK::Law::Categorical::prob_
protected

probabilities in a Categorical trial

Definition at line 151 of file STK_Law_Categorical.h.

Referenced by Categorical(), cdf(), computeCumProb(), lpdf(), pdf(), prob(), and setProb().


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