dai::TProb< T > Class Template Reference

Represents a vector with entries of type T. More...

#include <dai/prob.h>

List of all members.

Public Types

enum  NormType { NORMPROB, NORMLINF }
 Enumerates different ways of normalizing a probability measure. More...
enum  DistType {
  DISTL1, DISTLINF, DISTTV, DISTKL,
  DISTHEL
}
 Enumerates different distance measures between probability measures. More...
typedef std::vector< T >
::const_iterator 
const_iterator
 Constant iterator over the elements.
typedef std::vector< T >::iterator iterator
 Iterator over the elements.
typedef std::vector< T >
::const_reverse_iterator 
const_reverse_iterator
 Constant reverse iterator over the elements.
typedef std::vector< T >
::reverse_iterator 
reverse_iterator
 Reverse iterator over the elements.

Public Member Functions

template<typename binOp1, typename binOp2>
innerProduct (const TProb< T > &q, T init, binOp1 binaryOp1, binOp2 binaryOp2) const
 Performs a generalized inner product, similar to std::inner_product.
Constructors and destructors
 TProb ()
 Default constructor (constructs empty vector).
 TProb (size_t n)
 Construct uniform probability distribution over n outcomes (i.e., a vector of length n with each entry set to $1/n$).
 TProb (size_t n, T p)
 Construct vector of length n with each entry set to p.
template<typename TIterator>
 TProb (TIterator begin, TIterator end, size_t sizeHint=0)
 Construct vector from a range.
template<typename S>
 TProb (const std::vector< S > &v)
 Construct vector from another vector.
Iterator interface
iterator begin ()
 Returns iterator that points to the first element.
const_iterator begin () const
 Returns constant iterator that points to the first element.
iterator end ()
 Returns iterator that points beyond the last element.
const_iterator end () const
 Returns constant iterator that points beyond the last element.
reverse_iterator rbegin ()
 Returns reverse iterator that points to the last element.
const_reverse_iterator rbegin () const
 Returns constant reverse iterator that points to the last element.
reverse_iterator rend ()
 Returns reverse iterator that points beyond the first element.
const_reverse_iterator rend () const
 Returns constant reverse iterator that points beyond the first element.
Queries
const std::vector< T > & p () const
 Returns a const reference to the wrapped vector.
std::vector< T > & p ()
 Returns a reference to the wrapped vector.
operator[] (size_t i) const
 Returns a copy of the i 'th entry.
T & operator[] (size_t i)
 Returns reference to the i 'th entry.
size_t size () const
 Returns length of the vector (i.e., the number of entries).
template<typename binOp, typename unOp>
accumulate (T init, binOp op1, unOp op2) const
 Accumulate over all values, similar to std::accumulate.
entropy () const
 Returns the Shannon entropy of *this, $-\sum_i p_i \log p_i$.
max () const
 Returns maximum value of all entries.
min () const
 Returns minimum value of all entries.
sum () const
 Returns sum of all entries.
sumAbs () const
 Return sum of absolute value of all entries.
maxAbs () const
 Returns maximum absolute value of all entries.
bool hasNaNs () const
 Returns true if one or more entries are NaN.
bool hasNegatives () const
 Returns true if one or more entries are negative.
std::pair< size_t, T > argmax () const
 Returns a pair consisting of the index of the maximum value and the maximum value itself.
size_t draw ()
 Returns a random index, according to the (normalized) distribution described by *this.
bool operator<= (const TProb< T > &q) const
 Lexicographical comparison.
Unary transformations
template<typename unaryOp>
TProb< T > pwUnaryTr (unaryOp op) const
 Returns the result of applying operation op pointwise on *this.
TProb< T > operator- () const
 Returns negative of *this.
TProb< T > abs () const
 Returns pointwise absolute value.
TProb< T > exp () const
 Returns pointwise exponent.
TProb< T > log (bool zero=false) const
 Returns pointwise logarithm.
TProb< T > inverse (bool zero=true) const
 Returns pointwise inverse.
TProb< T > normalized (NormType norm=NORMPROB) const
 Returns normalized copy of *this, using the specified norm.
Unary operations
template<typename unaryOp>
TProb< T > & pwUnaryOp (unaryOp op)
 Applies unary operation op pointwise.
TProb< T > & randomize ()
 Draws all entries i.i.d. from a uniform distribution on [0,1).
TProb< T > & setUniform ()
 Sets all entries to $1/n$ where n is the length of the vector.
const TProb< T > & takeAbs ()
 Applies absolute value pointwise.
const TProb< T > & takeExp ()
 Applies exponent pointwise.
const TProb< T > & takeLog (bool zero=false)
 Applies logarithm pointwise.
normalize (NormType norm=NORMPROB)
 Normalizes vector using the specified norm.
Operations with scalars
TProb< T > & fill (T x)
 Sets all entries to x.
TProb< T > & operator+= (T x)
 Adds scalar x to each entry.
TProb< T > & operator-= (T x)
 Subtracts scalar x from each entry.
TProb< T > & operator*= (T x)
 Multiplies each entry with scalar x.
TProb< T > & operator/= (T x)
 Divides each entry by scalar x.
TProb< T > & operator^= (T x)
 Raises entries to the power x.
Transformations with scalars
TProb< T > operator+ (T x) const
 Returns sum of *this and scalar x.
TProb< T > operator- (T x) const
 Returns difference of *this and scalar x.
TProb< T > operator* (T x) const
 Returns product of *this with scalar x.
TProb< T > operator/ (T x) const
 Returns quotient of *this and scalar x, where division by 0 yields 0.
TProb< T > operator^ (T x) const
 Returns *this raised to the power x.
Operations with other equally-sized vectors
template<typename binaryOp>
TProb< T > & pwBinaryOp (const TProb< T > &q, binaryOp op)
 Applies binary operation pointwise on two vectors.
TProb< T > & operator+= (const TProb< T > &q)
 Pointwise addition with q.
TProb< T > & operator-= (const TProb< T > &q)
 Pointwise subtraction of q.
TProb< T > & operator*= (const TProb< T > &q)
 Pointwise multiplication with q.
TProb< T > & operator/= (const TProb< T > &q)
 Pointwise division by q, where division by 0 yields 0.
TProb< T > & divide (const TProb< T > &q)
 Pointwise division by q, where division by 0 yields +Inf.
TProb< T > & operator^= (const TProb< T > &q)
 Pointwise power.
Transformations with other equally-sized vectors
template<typename binaryOp>
TProb< T > pwBinaryTr (const TProb< T > &q, binaryOp op) const
 Returns the result of applying binary operation op pointwise on *this and q.
TProb< T > operator+ (const TProb< T > &q) const
 Returns sum of *this and q.
TProb< T > operator- (const TProb< T > &q) const
 Return *this minus q.
TProb< T > operator* (const TProb< T > &q) const
 Return product of *this with q.
TProb< T > operator/ (const TProb< T > &q) const
 Returns quotient of *this with q, where division by 0 yields 0.
TProb< T > divided_by (const TProb< T > &q) const
 Pointwise division by q, where division by 0 yields +Inf.
TProb< T > operator^ (const TProb< T > &q) const
 Returns *this to the power q.

Private Attributes

std::vector< T > _p
 The vector.

Related Functions

(Note that these are not member functions.)

template<typename T>
dist (const TProb< T > &p, const TProb< T > &q, typename TProb< T >::DistType dt)
 Returns distance between p and q, measured using distance measure dt.
template<typename T>
std::ostream & operator<< (std::ostream &os, const TProb< T > &p)
 Writes a TProb<T> to an output stream.
template<typename T>
TProb< T > min (const TProb< T > &a, const TProb< T > &b)
 Returns the pointwise minimum of a and b.
template<typename T>
TProb< T > max (const TProb< T > &a, const TProb< T > &b)
 Returns the pointwise maximum of a and b.


Detailed Description

template<typename T>
class dai::TProb< T >

Represents a vector with entries of type T.

It is simply a std::vector<T> with an interface designed for dealing with probability mass functions.

It is mainly used for representing measures on a finite outcome space, for example, the probability distribution of a discrete random variable. However, entries are not necessarily non-negative; it is also used to represent logarithms of probability mass functions.

Template Parameters:
T Should be a scalar that is castable from and to dai::Real and should support elementary arithmetic operations.

Member Typedef Documentation

template<typename T>
typedef std::vector<T>::const_iterator dai::TProb< T >::const_iterator

Constant iterator over the elements.

template<typename T>
typedef std::vector<T>::iterator dai::TProb< T >::iterator

Iterator over the elements.

template<typename T>
typedef std::vector<T>::const_reverse_iterator dai::TProb< T >::const_reverse_iterator

Constant reverse iterator over the elements.

template<typename T>
typedef std::vector<T>::reverse_iterator dai::TProb< T >::reverse_iterator

Reverse iterator over the elements.


Member Enumeration Documentation

template<typename T>
enum dai::TProb::NormType

Enumerates different ways of normalizing a probability measure.

  • NORMPROB means that the sum of all entries should be 1;
  • NORMLINF means that the maximum absolute value of all entries should be 1.

template<typename T>
enum dai::TProb::DistType

Enumerates different distance measures between probability measures.

  • DISTL1 is the $\ell_1$ distance (sum of absolute values of pointwise difference);
  • DISTLINF is the $\ell_\infty$ distance (maximum absolute value of pointwise difference);
  • DISTTV is the total variation distance (half of the $\ell_1$ distance);
  • DISTKL is the Kullback-Leibler distance ($\sum_i p_i (\log p_i - \log q_i)$).
  • DISTHEL is the Hellinger distance ($\frac{1}{2}\sum_i (\sqrt{p_i}-\sqrt{q_i})^2$).


Constructor & Destructor Documentation

template<typename T>
dai::TProb< T >::TProb (  )  [inline]

Default constructor (constructs empty vector).

template<typename T>
dai::TProb< T >::TProb ( size_t  n  )  [inline, explicit]

Construct uniform probability distribution over n outcomes (i.e., a vector of length n with each entry set to $1/n$).

template<typename T>
dai::TProb< T >::TProb ( size_t  n,
p 
) [inline, explicit]

Construct vector of length n with each entry set to p.

template<typename T>
template<typename TIterator>
dai::TProb< T >::TProb ( TIterator  begin,
TIterator  end,
size_t  sizeHint = 0 
) [inline]

Construct vector from a range.

Template Parameters:
TIterator Iterates over instances that can be cast to T
Parameters:
begin Points to first instance to be added.
end Points just beyond last instance to be added.
sizeHint For efficiency, the number of entries can be speficied by sizeHint.

template<typename T>
template<typename S>
dai::TProb< T >::TProb ( const std::vector< S > &  v  )  [inline]

Construct vector from another vector.

Template Parameters:
S type of elements in v (should be castable to type T)
Parameters:
v vector used for initialization


Member Function Documentation

template<typename T>
iterator dai::TProb< T >::begin (  )  [inline]

Returns iterator that points to the first element.

template<typename T>
const_iterator dai::TProb< T >::begin (  )  const [inline]

Returns constant iterator that points to the first element.

template<typename T>
iterator dai::TProb< T >::end (  )  [inline]

Returns iterator that points beyond the last element.

template<typename T>
const_iterator dai::TProb< T >::end (  )  const [inline]

Returns constant iterator that points beyond the last element.

template<typename T>
reverse_iterator dai::TProb< T >::rbegin (  )  [inline]

Returns reverse iterator that points to the last element.

template<typename T>
const_reverse_iterator dai::TProb< T >::rbegin (  )  const [inline]

Returns constant reverse iterator that points to the last element.

template<typename T>
reverse_iterator dai::TProb< T >::rend (  )  [inline]

Returns reverse iterator that points beyond the first element.

template<typename T>
const_reverse_iterator dai::TProb< T >::rend (  )  const [inline]

Returns constant reverse iterator that points beyond the first element.

template<typename T>
const std::vector<T>& dai::TProb< T >::p (  )  const [inline]

Returns a const reference to the wrapped vector.

template<typename T>
std::vector<T>& dai::TProb< T >::p (  )  [inline]

Returns a reference to the wrapped vector.

template<typename T>
T dai::TProb< T >::operator[] ( size_t  i  )  const [inline]

Returns a copy of the i 'th entry.

template<typename T>
T& dai::TProb< T >::operator[] ( size_t  i  )  [inline]

Returns reference to the i 'th entry.

template<typename T>
size_t dai::TProb< T >::size (  )  const [inline]

Returns length of the vector (i.e., the number of entries).

template<typename T>
template<typename binOp, typename unOp>
T dai::TProb< T >::accumulate ( init,
binOp  op1,
unOp  op2 
) const [inline]

Accumulate over all values, similar to std::accumulate.

template<typename T>
T dai::TProb< T >::entropy (  )  const [inline]

Returns the Shannon entropy of *this, $-\sum_i p_i \log p_i$.

template<typename T>
T dai::TProb< T >::max (  )  const [inline]

Returns maximum value of all entries.

template<typename T>
T dai::TProb< T >::min (  )  const [inline]

Returns minimum value of all entries.

template<typename T>
T dai::TProb< T >::sum (  )  const [inline]

Returns sum of all entries.

template<typename T>
T dai::TProb< T >::sumAbs (  )  const [inline]

Return sum of absolute value of all entries.

template<typename T>
T dai::TProb< T >::maxAbs (  )  const [inline]

Returns maximum absolute value of all entries.

template<typename T>
bool dai::TProb< T >::hasNaNs (  )  const [inline]

Returns true if one or more entries are NaN.

template<typename T>
bool dai::TProb< T >::hasNegatives (  )  const [inline]

Returns true if one or more entries are negative.

template<typename T>
std::pair<size_t,T> dai::TProb< T >::argmax (  )  const [inline]

Returns a pair consisting of the index of the maximum value and the maximum value itself.

template<typename T>
size_t dai::TProb< T >::draw (  )  [inline]

Returns a random index, according to the (normalized) distribution described by *this.

template<typename T>
bool dai::TProb< T >::operator<= ( const TProb< T > &  q  )  const [inline]

Lexicographical comparison.

Precondition:
this->size() == q.size()

template<typename T>
template<typename unaryOp>
TProb<T> dai::TProb< T >::pwUnaryTr ( unaryOp  op  )  const [inline]

Returns the result of applying operation op pointwise on *this.

template<typename T>
TProb<T> dai::TProb< T >::operator- (  )  const [inline]

Returns negative of *this.

template<typename T>
TProb<T> dai::TProb< T >::abs (  )  const [inline]

Returns pointwise absolute value.

template<typename T>
TProb<T> dai::TProb< T >::exp (  )  const [inline]

Returns pointwise exponent.

template<typename T>
TProb<T> dai::TProb< T >::log ( bool  zero = false  )  const [inline]

Returns pointwise logarithm.

If zero == true, uses log(0)==0; otherwise, log(0)==-Inf.

template<typename T>
TProb<T> dai::TProb< T >::inverse ( bool  zero = true  )  const [inline]

Returns pointwise inverse.

If zero == true, uses 1/0==0; otherwise, 1/0==Inf.

template<typename T>
TProb<T> dai::TProb< T >::normalized ( NormType  norm = NORMPROB  )  const [inline]

Returns normalized copy of *this, using the specified norm.

Exceptions:
NOT_NORMALIZABLE if the norm is zero

template<typename T>
template<typename unaryOp>
TProb<T>& dai::TProb< T >::pwUnaryOp ( unaryOp  op  )  [inline]

Applies unary operation op pointwise.

template<typename T>
TProb<T>& dai::TProb< T >::randomize (  )  [inline]

Draws all entries i.i.d. from a uniform distribution on [0,1).

template<typename T>
TProb<T>& dai::TProb< T >::setUniform (  )  [inline]

Sets all entries to $1/n$ where n is the length of the vector.

template<typename T>
const TProb<T>& dai::TProb< T >::takeAbs (  )  [inline]

Applies absolute value pointwise.

template<typename T>
const TProb<T>& dai::TProb< T >::takeExp (  )  [inline]

Applies exponent pointwise.

template<typename T>
const TProb<T>& dai::TProb< T >::takeLog ( bool  zero = false  )  [inline]

Applies logarithm pointwise.

If zero == true, uses log(0)==0; otherwise, log(0)==-Inf.

template<typename T>
T dai::TProb< T >::normalize ( NormType  norm = NORMPROB  )  [inline]

Normalizes vector using the specified norm.

Exceptions:
NOT_NORMALIZABLE if the norm is zero

template<typename T>
TProb<T>& dai::TProb< T >::fill ( x  )  [inline]

Sets all entries to x.

template<typename T>
TProb<T>& dai::TProb< T >::operator+= ( x  )  [inline]

Adds scalar x to each entry.

template<typename T>
TProb<T>& dai::TProb< T >::operator-= ( x  )  [inline]

Subtracts scalar x from each entry.

template<typename T>
TProb<T>& dai::TProb< T >::operator*= ( x  )  [inline]

Multiplies each entry with scalar x.

template<typename T>
TProb<T>& dai::TProb< T >::operator/= ( x  )  [inline]

Divides each entry by scalar x.

template<typename T>
TProb<T>& dai::TProb< T >::operator^= ( x  )  [inline]

Raises entries to the power x.

template<typename T>
TProb<T> dai::TProb< T >::operator+ ( x  )  const [inline]

Returns sum of *this and scalar x.

template<typename T>
TProb<T> dai::TProb< T >::operator- ( x  )  const [inline]

Returns difference of *this and scalar x.

template<typename T>
TProb<T> dai::TProb< T >::operator* ( x  )  const [inline]

Returns product of *this with scalar x.

template<typename T>
TProb<T> dai::TProb< T >::operator/ ( x  )  const [inline]

Returns quotient of *this and scalar x, where division by 0 yields 0.

template<typename T>
TProb<T> dai::TProb< T >::operator^ ( x  )  const [inline]

Returns *this raised to the power x.

template<typename T>
template<typename binaryOp>
TProb<T>& dai::TProb< T >::pwBinaryOp ( const TProb< T > &  q,
binaryOp  op 
) [inline]

Applies binary operation pointwise on two vectors.

Template Parameters:
binaryOp Type of function object that accepts two arguments of type T and outputs a type T
Parameters:
q Right operand
op Operation of type binaryOp

template<typename T>
TProb<T>& dai::TProb< T >::operator+= ( const TProb< T > &  q  )  [inline]

Pointwise addition with q.

Precondition:
this->size() == q.size()

template<typename T>
TProb<T>& dai::TProb< T >::operator-= ( const TProb< T > &  q  )  [inline]

Pointwise subtraction of q.

Precondition:
this->size() == q.size()

template<typename T>
TProb<T>& dai::TProb< T >::operator*= ( const TProb< T > &  q  )  [inline]

Pointwise multiplication with q.

Precondition:
this->size() == q.size()

template<typename T>
TProb<T>& dai::TProb< T >::operator/= ( const TProb< T > &  q  )  [inline]

Pointwise division by q, where division by 0 yields 0.

Precondition:
this->size() == q.size()
See also:
divide(const TProb<T> &)

template<typename T>
TProb<T>& dai::TProb< T >::divide ( const TProb< T > &  q  )  [inline]

Pointwise division by q, where division by 0 yields +Inf.

Precondition:
this->size() == q.size()
See also:
operator/=(const TProb<T> &)

template<typename T>
TProb<T>& dai::TProb< T >::operator^= ( const TProb< T > &  q  )  [inline]

Pointwise power.

Precondition:
this->size() == q.size()

template<typename T>
template<typename binaryOp>
TProb<T> dai::TProb< T >::pwBinaryTr ( const TProb< T > &  q,
binaryOp  op 
) const [inline]

Returns the result of applying binary operation op pointwise on *this and q.

Template Parameters:
binaryOp Type of function object that accepts two arguments of type T and outputs a type T
Parameters:
q Right operand
op Operation of type binaryOp

template<typename T>
TProb<T> dai::TProb< T >::operator+ ( const TProb< T > &  q  )  const [inline]

Returns sum of *this and q.

Precondition:
this->size() == q.size()

template<typename T>
TProb<T> dai::TProb< T >::operator- ( const TProb< T > &  q  )  const [inline]

Return *this minus q.

Precondition:
this->size() == q.size()

template<typename T>
TProb<T> dai::TProb< T >::operator* ( const TProb< T > &  q  )  const [inline]

Return product of *this with q.

Precondition:
this->size() == q.size()

template<typename T>
TProb<T> dai::TProb< T >::operator/ ( const TProb< T > &  q  )  const [inline]

Returns quotient of *this with q, where division by 0 yields 0.

Precondition:
this->size() == q.size()
See also:
divided_by(const TProb<T> &)

template<typename T>
TProb<T> dai::TProb< T >::divided_by ( const TProb< T > &  q  )  const [inline]

Pointwise division by q, where division by 0 yields +Inf.

Precondition:
this->size() == q.size()
See also:
operator/(const TProb<T> &)

template<typename T>
TProb<T> dai::TProb< T >::operator^ ( const TProb< T > &  q  )  const [inline]

Returns *this to the power q.

Precondition:
this->size() == q.size()

template<typename T>
template<typename binOp1, typename binOp2>
T dai::TProb< T >::innerProduct ( const TProb< T > &  q,
init,
binOp1  binaryOp1,
binOp2  binaryOp2 
) const [inline]

Performs a generalized inner product, similar to std::inner_product.

Precondition:
this->size() == q.size()


Friends And Related Function Documentation

template<typename T>
T dist ( const TProb< T > &  p,
const TProb< T > &  q,
typename TProb< T >::DistType  dt 
) [related]

Returns distance between p and q, measured using distance measure dt.

Precondition:
this->size() == q.size()

template<typename T>
std::ostream & operator<< ( std::ostream &  os,
const TProb< T > &  p 
) [related]

Writes a TProb<T> to an output stream.

template<typename T>
TProb< T > min ( const TProb< T > &  a,
const TProb< T > &  b 
) [related]

Returns the pointwise minimum of a and b.

Precondition:
this->size() == q.size()

template<typename T>
TProb< T > max ( const TProb< T > &  a,
const TProb< T > &  b 
) [related]

Returns the pointwise maximum of a and b.

Precondition:
this->size() == q.size()


Member Data Documentation

template<typename T>
std::vector<T> dai::TProb< T >::_p [private]

The vector.


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

Generated on Thu Feb 11 12:26:06 2010 for libDAI by  doxygen 1.5.5