KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Member Functions | Static Public Member Functions | Friends | List of all members
kjb::Categorical_distribution< T > Class Template Reference

A categorical distribution. More...

#include <prob_distribution.h>

Public Member Functions

 Categorical_distribution ()
 Construct an empty distribution. More...
 
 Categorical_distribution (const std::vector< double > &ps, const T &t1=T(1))
 Constructs a categorical distribution over the integers {t1, ..., t1 + K}, where K is the length of the given vector. More...
 
 Categorical_distribution (const Vector &ps, const T &=T(1))
 Constructs a categorical distribution over the integers {1, ..., K}, where K is the length of the given vector. More...
 
 Categorical_distribution (const std::vector< T > &values, const std::vector< double > &probabilities)
 Constructs a categorical distribution over a set of values. More...
 
 Categorical_distribution (const std::map< T, double > &d)
 Constructs a categorical distribution over a set of values. More...
 
 Categorical_distribution (const std::map< T, size_t > &m)
 Constructs an empirical distribution from a map of vals to counts. More...
 
 Categorical_distribution (const T &min, const T &max, const T &step)
 Constructs a categorical distribution limits. More...
 
 Categorical_distribution (const T &x1, const T &x2, double p1, double p2)
 Creates a two-element categorical distribution. More...
 
 Categorical_distribution (const Categorical_distribution< T > &cd)
 Copy-ctor: needed to make sure init_cdf_ gets called. More...
 
Categorical_distribution< T > & operator= (const Categorical_distribution< T > &cd)
 Assignment: needed to make sure init_cdf_ gets called. More...
 
size_t size () const
 Size of this distribution. More...
 
void erase (const T &key)
 
void remove (const T &key)
 
void insert (const T &key, double weight=1.0)
 
void add (const T &key, double weight=1.0)
 
 ~Categorical_distribution ()
 

Static Public Member Functions

static
Categorical_distribution< T > 
construct_from_log_map (const std::map< T, double > &log_map)
 

Friends

template<class U >
double pdf (const Categorical_distribution< U > &dist, const U &x)
 
template<class U >
double cdf (const Categorical_distribution< U > &dist, const U &x)
 
template<class U >
sample (const Categorical_distribution< U > &dist)
 

Detailed Description

template<class T = int>
class kjb::Categorical_distribution< T >

A categorical distribution.

This class implements a categorical distribution. This is a discrete probability distribution that assigns a probability to each of a finite number of numbers. It stores an STL map that relates values with probabilities.

Template Parameters
TThe type over which the distribution is over. Must be be a RealType (i.e., a field).

Constructor & Destructor Documentation

template<class T = int>
kjb::Categorical_distribution< T >::Categorical_distribution ( )
inline

Construct an empty distribution.

template<class T>
kjb::Categorical_distribution< T >::Categorical_distribution ( const std::vector< double > &  ps,
const T &  t1 = T(1) 
)

Constructs a categorical distribution over the integers {t1, ..., t1 + K}, where K is the length of the given vector.

Parameters
psA std::vector of probabilities
template<class T>
kjb::Categorical_distribution< T >::Categorical_distribution ( const Vector ps,
const T &  t1 = T(1) 
)

Constructs a categorical distribution over the integers {1, ..., K}, where K is the length of the given vector.

Parameters
psA kjb::Vector of probabilities.
template<class T>
kjb::Categorical_distribution< T >::Categorical_distribution ( const std::vector< T > &  values,
const std::vector< double > &  probabilities 
)

Constructs a categorical distribution over a set of values.

Parameters
valuesA set of values to define probabilities over
probabilitiesThe probabilities corresponding to the values.
template<class T = int>
kjb::Categorical_distribution< T >::Categorical_distribution ( const std::map< T, double > &  d)
inline

Constructs a categorical distribution over a set of values.

Parameters
values_beginBeginning of a sequence of values to define probabilities over
values_endEnd of a sequence of values to define probabilities over
probabilities_beginThe beginning of a sequence of probabilities corresponding to the values.
Precondition
probability sequence has at least std::distance(values_begin, values_end) elements Constructs a categorical distribution from the given map.
Parameters
dA map containing the distribution. It must be such that P(X = x) = d[x].
template<class T = int>
kjb::Categorical_distribution< T >::Categorical_distribution ( const std::map< T, size_t > &  m)
inline

Constructs an empirical distribution from a map of vals to counts.

Parameters
mA map containing the distribution. It must be such that P(X = x) is proportional to m[x].
template<class T>
kjb::Categorical_distribution< T >::Categorical_distribution ( const T &  min,
const T &  max,
const T &  step 
)

Constructs a categorical distribution limits.

Creates a uniform distribution over the set {min, min + step, ..., max}, where each element has probability step / (max - min + 1).

Parameters
minsmallest element of the support
maxlargest element of the support
stepgap between elements of the support
template<class T = int>
kjb::Categorical_distribution< T >::Categorical_distribution ( const T &  x1,
const T &  x2,
double  p1,
double  p2 
)
inline

Creates a two-element categorical distribution.

This constructore creates a two-element distribution, with P(X = x1) = p1 and P(X = x2) = p2. Remember that p1 + p2 = 1.

(wait, so this is just a bernoulli distribution? suggest removing this constructor –kyle, March 13, 2012)

template<class T = int>
kjb::Categorical_distribution< T >::Categorical_distribution ( const Categorical_distribution< T > &  cd)
inline

Copy-ctor: needed to make sure init_cdf_ gets called.

template<class T = int>
kjb::Categorical_distribution< T >::~Categorical_distribution ( )
inline

Member Function Documentation

template<class T = int>
void kjb::Categorical_distribution< T >::add ( const T &  key,
double  weight = 1.0 
)
inline

Alias of insert.

Deprecated in favor of the more STL-esque name, insert().

template<class T = int>
static Categorical_distribution<T> kjb::Categorical_distribution< T >::construct_from_log_map ( const std::map< T, double > &  log_map)
inlinestatic

Named constructor

Similar to the constructor that recieves a map of value-weight pairs, but in this case, the weights are given in log-space.

This constructor converts to normalized non-log weights, without losing precision even when the unnormalized weights are extremely small.

Author
Kyle Simek
template<class T = int>
void kjb::Categorical_distribution< T >::erase ( const T &  key)
inline
Parameters
weightRemove an item from the collection.

Cumulative distrbution will be re-constructed from scratch, requiring O(n) time.

Exceptions
Illegal_argumentif key does not exist in the collection.
template<class T = int>
void kjb::Categorical_distribution< T >::insert ( const T &  key,
double  weight = 1.0 
)
inline
Parameters
weightUnnormalized weight of new atom. This is added to the total of atom weights, and the normalization constant is updated.

Running time is O(log n)

template<class T = int>
Categorical_distribution<T>& kjb::Categorical_distribution< T >::operator= ( const Categorical_distribution< T > &  cd)
inline

Assignment: needed to make sure init_cdf_ gets called.

template<class T = int>
void kjb::Categorical_distribution< T >::remove ( const T &  key)
inline

Alias of erase().

Deprecated in favor of the more STL-esque erase().

template<class T = int>
size_t kjb::Categorical_distribution< T >::size ( ) const
inline

Size of this distribution.

Returns the size of the support of this distribution

Friends And Related Function Documentation

template<class T = int>
template<class U >
double cdf ( const Categorical_distribution< U > &  dist,
const U &  x 
)
friend
template<class T = int>
template<class U >
double pdf ( const Categorical_distribution< U > &  dist,
const U &  x 
)
friend
template<class T = int>
template<class U >
U sample ( const Categorical_distribution< U > &  dist)
friend

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