libDAI
|
Represents a (probability) factor. More...
#include <dai/factor.h>
Public Member Functions | |
Constructors and destructors | |
TFactor (T p=1) | |
Constructs factor depending on no variables with value p. More... | |
TFactor (const Var &v) | |
Constructs factor depending on the variable v with uniform distribution. More... | |
TFactor (const VarSet &vars) | |
Constructs factor depending on variables in vars with uniform distribution. More... | |
TFactor (const VarSet &vars, T p) | |
Constructs factor depending on variables in vars with all values set to p. More... | |
template<typename S > | |
TFactor (const VarSet &vars, const std::vector< S > &x) | |
Constructs factor depending on variables in vars, copying the values from a std::vector<> More... | |
TFactor (const VarSet &vars, const T *p) | |
Constructs factor depending on variables in vars, copying the values from an array. More... | |
TFactor (const VarSet &vars, const TProb< T > &p) | |
Constructs factor depending on variables in vars, copying the values from p. More... | |
TFactor (const std::vector< Var > &vars, const std::vector< T > &p) | |
Constructs factor depending on variables in vars, permuting the values given in p accordingly. More... | |
Get/set individual entries | |
void | set (size_t i, T val) |
Sets i 'th entry to val. More... | |
T | get (size_t i) const |
Gets i 'th entry. More... | |
Queries | |
const TProb< T > & | p () const |
Returns constant reference to value vector. More... | |
TProb< T > & | p () |
Returns reference to value vector. More... | |
T | operator[] (size_t i) const |
Returns a copy of the i 'th entry of the value vector. More... | |
const VarSet & | vars () const |
Returns constant reference to variable set (i.e., the variables on which the factor depends) More... | |
VarSet & | vars () |
Returns reference to variable set (i.e., the variables on which the factor depends) More... | |
size_t | nrStates () const |
Returns the number of possible joint states of the variables on which the factor depends, . More... | |
T | entropy () const |
Returns the Shannon entropy of *this , . More... | |
T | max () const |
Returns maximum of all values. More... | |
T | min () const |
Returns minimum of all values. More... | |
T | sum () const |
Returns sum of all values. More... | |
T | sumAbs () const |
Returns sum of absolute values. More... | |
T | maxAbs () const |
Returns maximum absolute value of all values. More... | |
bool | hasNaNs () const |
Returns true if one or more values are NaN. More... | |
bool | hasNegatives () const |
Returns true if one or more values are negative. More... | |
T | strength (const Var &i, const Var &j) const |
Returns strength of this factor (between variables i and j), as defined in eq. (52) of [MoK07b]. More... | |
bool | operator== (const TFactor< T > &y) const |
Comparison. More... | |
std::string | toString () const |
Formats a factor as a string. More... | |
Unary transformations | |
TFactor< T > | operator- () const |
Returns negative of *this . More... | |
TFactor< T > | abs () const |
Returns pointwise absolute value. More... | |
TFactor< T > | exp () const |
Returns pointwise exponent. More... | |
TFactor< T > | log (bool zero=false) const |
Returns pointwise logarithm. More... | |
TFactor< T > | inverse (bool zero=true) const |
Returns pointwise inverse. More... | |
TFactor< T > | normalized (ProbNormType norm=NORMPROB) const |
Returns normalized copy of *this , using the specified norm. More... | |
Unary operations | |
TFactor< T > & | randomize () |
Draws all values i.i.d. from a uniform distribution on [0,1) More... | |
TFactor< T > & | setUniform () |
Sets all values to where n is the number of states. More... | |
TFactor< T > & | takeAbs () |
Applies absolute value pointwise. More... | |
TFactor< T > & | takeExp () |
Applies exponent pointwise. More... | |
TFactor< T > & | takeLog (bool zero=false) |
Applies logarithm pointwise. More... | |
T | normalize (ProbNormType norm=NORMPROB) |
Normalizes factor using the specified norm. More... | |
Operations with scalars | |
TFactor< T > & | fill (T x) |
Sets all values to x. More... | |
TFactor< T > & | operator+= (T x) |
Adds scalar x to each value. More... | |
TFactor< T > & | operator-= (T x) |
Subtracts scalar x from each value. More... | |
TFactor< T > & | operator*= (T x) |
Multiplies each value with scalar x. More... | |
TFactor< T > & | operator/= (T x) |
Divides each entry by scalar x. More... | |
TFactor< T > & | operator^= (T x) |
Raises values to the power x. More... | |
Transformations with scalars | |
TFactor< T > | operator+ (T x) const |
Returns sum of *this and scalar x. More... | |
TFactor< T > | operator- (T x) const |
Returns difference of *this and scalar x. More... | |
TFactor< T > | operator* (T x) const |
Returns product of *this with scalar x. More... | |
TFactor< T > | operator/ (T x) const |
Returns quotient of *this with scalar x. More... | |
TFactor< T > | operator^ (T x) const |
Returns *this raised to the power x. More... | |
Operations with other factors | |
template<typename binOp > | |
TFactor< T > & | binaryOp (const TFactor< T > &g, binOp op) |
Applies binary operation op on two factors, *this and g. More... | |
TFactor< T > & | operator+= (const TFactor< T > &g) |
Adds g to *this . More... | |
TFactor< T > & | operator-= (const TFactor< T > &g) |
Subtracts g from *this . More... | |
TFactor< T > & | operator*= (const TFactor< T > &g) |
Multiplies *this with g. More... | |
TFactor< T > & | operator/= (const TFactor< T > &g) |
Divides *this by g (where division by zero yields zero) More... | |
Transformations with other factors | |
template<typename binOp > | |
TFactor< T > | binaryTr (const TFactor< T > &g, binOp op) const |
Returns result of applying binary operation op on two factors, *this and g. More... | |
TFactor< T > | operator+ (const TFactor< T > &g) const |
Returns sum of *this and g. More... | |
TFactor< T > | operator- (const TFactor< T > &g) const |
Returns *this minus g. More... | |
TFactor< T > | operator* (const TFactor< T > &g) const |
Returns product of *this with g. More... | |
TFactor< T > | operator/ (const TFactor< T > &g) const |
Returns quotient of *this by f (where division by zero yields zero) More... | |
Miscellaneous operations | |
TFactor< T > | slice (const VarSet &vars, size_t varsState) const |
Returns a slice of *this , where the subset vars is in state varsState. More... | |
TFactor< T > | embed (const VarSet &vars) const |
Embeds this factor in a larger VarSet. More... | |
TFactor< T > | marginal (const VarSet &vars, bool normed=true) const |
Returns marginal on vars, obtained by summing out all variables except those in vars, and normalizing the result if normed == true . More... | |
TFactor< T > | maxMarginal (const VarSet &vars, bool normed=true) const |
Returns max-marginal on vars, obtained by maximizing all variables except those in vars, and normalizing the result if normed == true . More... | |
Private Attributes | |
VarSet | _vs |
Stores the variables on which the factor depends. More... | |
TProb< T > | _p |
Stores the factor values. More... | |
Related Functions | |
(Note that these are not member functions.) | |
template<typename T > | |
std::ostream & | operator<< (std::ostream &os, const TFactor< T > &f) |
Writes a factor to an output stream. More... | |
template<typename T > | |
T | dist (const TFactor< T > &f, const TFactor< T > &g, ProbDistType dt) |
Returns distance between two factors f and g, according to the distance measure dt. More... | |
template<typename T > | |
TFactor< T > | max (const TFactor< T > &f, const TFactor< T > &g) |
Returns the pointwise maximum of two factors. More... | |
template<typename T > | |
TFactor< T > | min (const TFactor< T > &f, const TFactor< T > &g) |
Returns the pointwise minimum of two factors. More... | |
template<typename T > | |
T | MutualInfo (const TFactor< T > &f) |
Calculates the mutual information between the two variables that f depends on, under the distribution given by f. More... | |
Represents a (probability) factor.
Mathematically, a factor is a function mapping joint states of some variables to the nonnegative real numbers. More formally, denoting a discrete variable with label by and its state space by , a factor depending on the variables is a function .
In libDAI, a factor is represented by a TFactor<T> object, which has two components:
The factor values are stored in the entries of the TProb in a particular ordering, which is defined by the one-to-one correspondence of a joint state in with a linear index in according to the mapping induced by dai::calcLinearState().
T | Should be a scalar that is castable from and to double and should support elementary arithmetic operations. |
Define a better fileformat for .fg files (maybe using XML)?
Add support for sparse factors.
|
inline |
Constructs factor depending on no variables with value p.
|
inline |
Constructs factor depending on the variable v with uniform distribution.
|
inline |
Constructs factor depending on variables in vars with uniform distribution.
|
inline |
Constructs factor depending on variables in vars with all values set to p.
|
inline |
Constructs factor depending on variables in vars, copying the values from a std::vector<>
S | Type of values of x |
vars | contains the variables that the new factor should depend on. |
x | Vector with values to be copied. |
|
inline |
Constructs factor depending on variables in vars, copying the values from an array.
vars | contains the variables that the new factor should depend on. |
p | Points to array of values to be added. |
|
inline |
Constructs factor depending on variables in vars, copying the values from p.
|
inline |
Constructs factor depending on variables in vars, permuting the values given in p accordingly.
|
inline |
Sets i 'th entry to val.
|
inline |
Gets i 'th entry.
|
inline |
Returns constant reference to value vector.
|
inline |
Returns reference to value vector.
|
inline |
Returns a copy of the i 'th entry of the value vector.
|
inline |
Returns constant reference to variable set (i.e., the variables on which the factor depends)
|
inline |
Returns reference to variable set (i.e., the variables on which the factor depends)
|
inline |
Returns the number of possible joint states of the variables on which the factor depends, .
|
inline |
Returns the Shannon entropy of *this
, .
|
inline |
Returns maximum of all values.
|
inline |
Returns minimum of all values.
|
inline |
Returns sum of all values.
|
inline |
Returns sum of absolute values.
|
inline |
Returns maximum absolute value of all values.
|
inline |
Returns true
if one or more values are NaN.
|
inline |
Returns true
if one or more values are negative.
T dai::TFactor< T >::strength | ( | const Var & | i, |
const Var & | j | ||
) | const |
Returns strength of this factor (between variables i and j), as defined in eq. (52) of [MoK07b].
|
inline |
Comparison.
|
inline |
Formats a factor as a string.
|
inline |
Returns negative of *this
.
|
inline |
Returns pointwise absolute value.
|
inline |
Returns pointwise exponent.
|
inline |
Returns pointwise logarithm.
If zero == true
, uses log(0)==0
; otherwise, log(0)==-Inf
.
|
inline |
Returns pointwise inverse.
If zero == true
, uses 1/0==0
; otherwise, 1/0==Inf
.
|
inline |
Returns normalized copy of *this
, using the specified norm.
NOT_NORMALIZABLE | if the norm is zero |
|
inline |
Draws all values i.i.d. from a uniform distribution on [0,1)
|
inline |
Sets all values to where n is the number of states.
|
inline |
Applies absolute value pointwise.
|
inline |
Applies exponent pointwise.
|
inline |
Applies logarithm pointwise.
If zero == true
, uses log(0)==0
; otherwise, log(0)==-Inf
.
|
inline |
Normalizes factor using the specified norm.
NOT_NORMALIZABLE | if the norm is zero |
|
inline |
Sets all values to x.
|
inline |
Adds scalar x to each value.
|
inline |
Subtracts scalar x from each value.
|
inline |
Multiplies each value with scalar x.
|
inline |
Divides each entry by scalar x.
|
inline |
Raises values to the power x.
|
inline |
Returns sum of *this
and scalar x.
|
inline |
Returns difference of *this
and scalar x.
|
inline |
Returns product of *this
with scalar x.
|
inline |
Returns quotient of *this
with scalar x.
|
inline |
Returns *this
raised to the power x.
|
inline |
Applies binary operation op on two factors, *this
and g.
binOp | Type of function object that accepts two arguments of type T and outputs a type T |
g | Right operand |
op | Operation of type binOp |
|
inline |
Adds g to *this
.
The sum of two factors is defined as follows: if and , then
|
inline |
Subtracts g from *this
.
The difference of two factors is defined as follows: if and , then
|
inline |
Multiplies *this
with g.
The product of two factors is defined as follows: if and , then
|
inline |
Divides *this
by g (where division by zero yields zero)
The quotient of two factors is defined as follows: if and , then
|
inline |
Returns result of applying binary operation op on two factors, *this
and g.
binOp | Type of function object that accepts two arguments of type T and outputs a type T |
g | Right operand |
op | Operation of type binOp |
|
inline |
Returns sum of *this
and g.
The sum of two factors is defined as follows: if and , then
|
inline |
Returns *this
minus g.
The difference of two factors is defined as follows: if and , then
|
inline |
Returns product of *this
with g.
The product of two factors is defined as follows: if and , then
|
inline |
Returns quotient of *this
by f (where division by zero yields zero)
The quotient of two factors is defined as follows: if and , then
TFactor< T > dai::TFactor< T >::slice | ( | const VarSet & | vars, |
size_t | varsState | ||
) | const |
Returns a slice of *this
, where the subset vars is in state varsState.
The result is a factor that depends on the variables of *this except those in vars, obtained by setting the variables in vars to the joint state specified by the linear index varsState. Formally, if *this
corresponds with the factor , corresponds with vars and varsState corresponds with a mapping that maps a variable with to its state , then the slice returned corresponds with the factor defined by .
|
inline |
TFactor< T > dai::TFactor< T >::marginal | ( | const VarSet & | vars, |
bool | normed = true |
||
) | const |
Returns marginal on vars, obtained by summing out all variables except those in vars, and normalizing the result if normed == true
.
TFactor< T > dai::TFactor< T >::maxMarginal | ( | const VarSet & | vars, |
bool | normed = true |
||
) | const |
Returns max-marginal on vars, obtained by maximizing all variables except those in vars, and normalizing the result if normed == true
.
|
related |
Writes a factor to an output stream.
|
related |
Returns distance between two factors f and g, according to the distance measure dt.
Returns the pointwise maximum of two factors.
Returns the pointwise minimum of two factors.
|
related |
Calculates the mutual information between the two variables that f depends on, under the distribution given by f.
|
private |
Stores the variables on which the factor depends.
|
private |
Stores the factor values.