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

typedef std::vector< T > container_type
 Type of data structure used for storing the values.
typedef TProb< T > this_type
 Shorthand.
typedef
container_type::const_iterator 
const_iterator
 Constant iterator over the elements.
typedef container_type::iterator iterator
 Iterator over the elements.
typedef
container_type::const_reverse_iterator 
const_reverse_iterator
 Constant reverse iterator over the elements.
typedef
container_type::reverse_iterator 
reverse_iterator
 Reverse iterator over the elements.

Public Member Functions

template<typename binOp1, typename binOp2>
innerProduct (const this_type &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)
 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.
Miscellaneous operations
void resize (size_t sz)
Get/set individual entries
get (size_t i) const
 Gets i 'th entry.
void set (size_t i, T val)
 Sets i 'th entry to val.
Queries
const container_typep () const
 Returns a const reference to the wrapped container.
container_typep ()
 Returns a reference to the wrapped container.
operator[] (size_t i) const
 Returns a copy of the i 'th entry.
size_t size () const
 Returns length of the vector (i.e., the number of entries).
template<typename unOp>
accumulateSum (T init, unOp op) const
 Accumulate all values (similar to std::accumulate) by summing.
template<typename unOp>
accumulateMax (T init, unOp op, bool minimize) const
 Accumulate all values (similar to std::accumulate) by maximization/minimization.
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 this_type &q) const
 Lexicographical comparison.
bool operator== (const this_type &q) const
 Comparison.
Unary transformations
template<typename unaryOp>
this_type pwUnaryTr (unaryOp op) const
 Returns the result of applying operation op pointwise on *this.
this_type operator- () const
 Returns negative of *this.
this_type abs () const
 Returns pointwise absolute value.
this_type exp () const
 Returns pointwise exponent.
this_type log (bool zero=false) const
 Returns pointwise logarithm.
this_type inverse (bool zero=true) const
 Returns pointwise inverse.
this_type normalized (ProbNormType norm=dai::NORMPROB) const
 Returns normalized copy of *this, using the specified norm.
Unary operations
template<typename unaryOp>
this_typepwUnaryOp (unaryOp op)
 Applies unary operation op pointwise.
this_typerandomize ()
 Draws all entries i.i.d. from a uniform distribution on [0,1).
this_typesetUniform ()
 Sets all entries to $1/n$ where n is the length of the vector.
this_typetakeAbs ()
 Applies absolute value pointwise.
this_typetakeExp ()
 Applies exponent pointwise.
this_typetakeLog (bool zero=false)
 Applies logarithm pointwise.
normalize (ProbNormType norm=dai::NORMPROB)
 Normalizes vector using the specified norm.
Operations with scalars
this_typefill (T x)
 Sets all entries to x.
this_typeoperator+= (T x)
 Adds scalar x to each entry.
this_typeoperator-= (T x)
 Subtracts scalar x from each entry.
this_typeoperator*= (T x)
 Multiplies each entry with scalar x.
this_typeoperator/= (T x)
 Divides each entry by scalar x, where division by 0 yields 0.
this_typeoperator^= (T x)
 Raises entries to the power x.
Transformations with scalars
this_type operator+ (T x) const
 Returns sum of *this and scalar x.
this_type operator- (T x) const
 Returns difference of *this and scalar x.
this_type operator* (T x) const
 Returns product of *this with scalar x.
this_type operator/ (T x) const
 Returns quotient of *this and scalar x, where division by 0 yields 0.
this_type operator^ (T x) const
 Returns *this raised to the power x.
Operations with other equally-sized vectors
template<typename binaryOp>
this_typepwBinaryOp (const this_type &q, binaryOp op)
 Applies binary operation pointwise on two vectors.
this_typeoperator+= (const this_type &q)
 Pointwise addition with q.
this_typeoperator-= (const this_type &q)
 Pointwise subtraction of q.
this_typeoperator*= (const this_type &q)
 Pointwise multiplication with q.
this_typeoperator/= (const this_type &q)
 Pointwise division by q, where division by 0 yields 0.
this_typedivide (const this_type &q)
 Pointwise division by q, where division by 0 yields +Inf.
this_typeoperator^= (const this_type &q)
 Pointwise power.
Transformations with other equally-sized vectors
template<typename binaryOp>
this_type pwBinaryTr (const this_type &q, binaryOp op) const
 Returns the result of applying binary operation op pointwise on *this and q.
this_type operator+ (const this_type &q) const
 Returns sum of *this and q.
this_type operator- (const this_type &q) const
 Return *this minus q.
this_type operator* (const this_type &q) const
 Return product of *this with q.
this_type operator/ (const this_type &q) const
 Returns quotient of *this with q, where division by 0 yields 0.
this_type divided_by (const this_type &q) const
 Pointwise division by q, where division by 0 yields +Inf.
this_type operator^ (const this_type &q) const
 Returns *this to the power q.

Private Attributes

container_type _p
 The data structure that stores the values.

Related Functions

(Note that these are not member functions.)

template<typename T>
dist (const TProb< T > &p, const TProb< T > &q, ProbDistType 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> dai::TProb< T >::container_type

Type of data structure used for storing the values.

template<typename T>
typedef TProb<T> dai::TProb< T >::this_type

Shorthand.

template<typename T>
typedef container_type::const_iterator dai::TProb< T >::const_iterator

Constant iterator over the elements.

template<typename T>
typedef container_type::iterator dai::TProb< T >::iterator

Iterator over the elements.

template<typename T>
typedef container_type::const_reverse_iterator dai::TProb< T >::const_reverse_iterator

Constant reverse iterator over the elements.

template<typename T>
typedef container_type::reverse_iterator dai::TProb< T >::reverse_iterator

Reverse iterator over the elements.


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 
) [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; the value 0 can be given if the size is unknown, but this will result in a performance penalty.

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>
T dai::TProb< T >::get ( size_t  i  )  const [inline]

Gets i 'th entry.

template<typename T>
void dai::TProb< T >::set ( size_t  i,
val 
) [inline]

Sets i 'th entry to val.

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

Returns a const reference to the wrapped container.

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

Returns a reference to the wrapped container.

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

Returns a copy of 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 unOp>
T dai::TProb< T >::accumulateSum ( init,
unOp  op 
) const [inline]

Accumulate all values (similar to std::accumulate) by summing.

The following calculation is done:

  T t = op(init);
  for( const_iterator it = begin(); it != end(); it++ )
      t += op(*it);
  return t;

template<typename T>
template<typename unOp>
T dai::TProb< T >::accumulateMax ( init,
unOp  op,
bool  minimize 
) const [inline]

Accumulate all values (similar to std::accumulate) by maximization/minimization.

The following calculation is done (with "max" replaced by "min" if minimize == true):

  T t = op(init);
  for( const_iterator it = begin(); it != end(); it++ )
      t = std::max( t, op(*it) );
  return t;

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 this_type q  )  const [inline]

Lexicographical comparison.

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

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

Comparison.

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

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

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

Returns negative of *this.

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

Returns pointwise absolute value.

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

Returns pointwise exponent.

template<typename T>
this_type 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>
this_type 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>
this_type dai::TProb< T >::normalized ( ProbNormType  norm = dai::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>
this_type& dai::TProb< T >::pwUnaryOp ( unaryOp  op  )  [inline]

Applies unary operation op pointwise.

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

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

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

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

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

Applies absolute value pointwise.

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

Applies exponent pointwise.

template<typename T>
this_type& 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 ( ProbNormType  norm = dai::NORMPROB  )  [inline]

Normalizes vector using the specified norm.

Exceptions:
NOT_NORMALIZABLE if the norm is zero

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

Sets all entries to x.

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

Adds scalar x to each entry.

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

Subtracts scalar x from each entry.

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

Multiplies each entry with scalar x.

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

Divides each entry by scalar x, where division by 0 yields 0.

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

Raises entries to the power x.

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

Returns sum of *this and scalar x.

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

Returns difference of *this and scalar x.

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

Returns product of *this with scalar x.

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

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

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

Returns *this raised to the power x.

template<typename T>
template<typename binaryOp>
this_type& dai::TProb< T >::pwBinaryOp ( const this_type 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>
this_type& dai::TProb< T >::operator+= ( const this_type q  )  [inline]

Pointwise addition with q.

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

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

Pointwise subtraction of q.

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

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

Pointwise multiplication with q.

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

template<typename T>
this_type& dai::TProb< T >::operator/= ( const this_type 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>
this_type& dai::TProb< T >::divide ( const this_type 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>
this_type& dai::TProb< T >::operator^= ( const this_type q  )  [inline]

Pointwise power.

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

template<typename T>
template<typename binaryOp>
this_type dai::TProb< T >::pwBinaryTr ( const this_type 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>
this_type dai::TProb< T >::operator+ ( const this_type q  )  const [inline]

Returns sum of *this and q.

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

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

Return *this minus q.

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

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

Return product of *this with q.

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

template<typename T>
this_type dai::TProb< T >::operator/ ( const this_type 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>
this_type dai::TProb< T >::divided_by ( const this_type 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>
this_type dai::TProb< T >::operator^ ( const this_type 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 this_type 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,
ProbDistType  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>
container_type dai::TProb< T >::_p [private]

The data structure that stores the values.


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

Generated on Thu Aug 5 19:04:07 2010 for libDAI by  doxygen 1.5.5