X-Git-Url: http://git.tuebingen.mpg.de/?p=libdai.git;a=blobdiff_plain;f=include%2Fdai%2Ffactor.h;h=1427002ba5ef6c8990680611dc0d166b81c06250;hp=30871ec482df99c256710b70727a9975e5f19c4f;hb=2d54f16411b402cfafed03b3026562c96be67468;hpb=233c76f3860b061a093bf253adf0b4877589df8e diff --git a/include/dai/factor.h b/include/dai/factor.h index 30871ec..1427002 100644 --- a/include/dai/factor.h +++ b/include/dai/factor.h @@ -25,7 +25,6 @@ /// \file /// \brief Defines TFactor and Factor classes -/// \todo Improve documentation #ifndef __defined_libdai_factor_h @@ -33,6 +32,7 @@ #include +#include #include #include #include @@ -42,28 +42,41 @@ namespace dai { +/// Function object similar to std::divides(), but different in that dividing by zero results in zero +template struct divides0 : public std::binary_function { + T operator()(const T& i, const T& j) const { + if( j == (T)0 ) + return (T)0; + else + return i / j; + } +}; + + /// Represents a (probability) factor. -/** Mathematically, a \e factor is a function from the Cartesian product - * of the state spaces of some variables to the nonnegative real numbers. +/** Mathematically, a \e factor is a function mapping joint states of some + * variables to the nonnegative real numbers. * More formally, denoting a discrete variable with label \f$l\f$ by * \f$x_l\f$ and its state space by \f$X_l = \{0,1,\dots,S_l-1\}\f$, - * then a factor depending on the variables \f$\{x_i\}_{i\in I}\f$ is - * a function \f$f_I : \prod_{i\in I} X_i \to [0,\infty)\f$. + * then a factor depending on the variables \f$\{x_l\}_{l\in L}\f$ is + * a function \f$f_L : \prod_{l\in L} X_l \to [0,\infty)\f$. * * In libDAI, a factor is represented by a TFactor<\a T> object, which has two * components: - * \arg a VarSet, corresponding with the set of variables \f$\{x_i\}_{i\in I}\f$ + * \arg a VarSet, corresponding with the set of variables \f$\{x_l\}_{l\in L}\f$ * that the factor depends on; - * \arg a TProb<\a T>, a vector containing the values of the factor for each possible + * \arg a TProb<\a T>, a vector containing the value of the factor for each possible * joint state of the variables. * * The factor values are stored in the entries of the TProb<\a T> in a particular * ordering, which is defined by the one-to-one correspondence of a joint state - * in \f$\prod_{i\in I} X_i\f$ with a linear index in - * \f$\{0,1,\dots,\prod_{i\in I} S_i-1\}\f$ according to the mapping \f$\sigma\f$ + * in \f$\prod_{l\in L} X_l\f$ with a linear index in + * \f$\{0,1,\dots,\prod_{l\in L} S_l-1\}\f$ according to the mapping \f$\sigma\f$ * induced by VarSet::calcState(const std::map &). * * \tparam T Should be a scalar that is castable from and to double and should support elementary arithmetic operations. + * \todo Define a better fileformat for .fg files (maybe using XML)? + * \todo Add support for sparse factors. */ template class TFactor { private: @@ -71,39 +84,44 @@ template class TFactor { TProb _p; public: - /// Construct TFactor depending on no variables, with value p + /// Iterator over factor entries + typedef typename TProb::iterator iterator; + + /// Const iterator over factor entries + typedef typename TProb::const_iterator const_iterator; + + /// Constructs TFactor depending on no variables, with value p TFactor ( Real p = 1.0 ) : _vs(), _p(1,p) {} - /// Construct TFactor depending on variables in ns, with uniform distribution + /// Constructs TFactor depending on variables in ns, with uniform distribution TFactor( const VarSet& ns ) : _vs(ns), _p(_vs.nrStates()) {} - /// Construct TFactor depending on variables in ns, with all values set to p + /// Constructs TFactor depending on variables in ns, with all values set to p TFactor( const VarSet& ns, Real p ) : _vs(ns), _p(_vs.nrStates(),p) {} - /// Construct TFactor depending on variables in ns, copying the values from the array p - TFactor( const VarSet& ns, const Real *p ) : _vs(ns), _p(_vs.nrStates(),p) {} + /// Constructs TFactor depending on variables in ns, copying the values from the range starting at begin + /** \param ns contains the variables that the new TFactor should depend on. + * \tparam Iterator Iterates over instances of type T; should support addition of size_t. + * \param begin Points to first element to be added. + */ + template + TFactor( const VarSet& ns, TIterator begin ) : _vs(ns), _p(begin, begin + _vs.nrStates(), _vs.nrStates()) {} - /// Construct TFactor depending on variables in ns, with values set to the TProb p + /// Constructs TFactor depending on variables in ns, with values set to the TProb p TFactor( const VarSet& ns, const TProb& p ) : _vs(ns), _p(p) { #ifdef DAI_DEBUG assert( _vs.nrStates() == _p.size() ); #endif } - - /// Construct TFactor depending on the variable n, with uniform distribution - TFactor( const Var& n ) : _vs(n), _p(n.states()) {} - - /// Copy constructor - TFactor( const TFactor &x ) : _vs(x._vs), _p(x._p) {} - - /// Assignment operator - TFactor & operator= (const TFactor &x) { - if( this != &x ) { - _vs = x._vs; - _p = x._p; + TFactor( const std::vector< Var >& vars, const std::vector< T >& p ) : _vs(vars.begin(), vars.end(), vars.size()), _p(p.size()) { + Permute permindex(vars); + for (size_t li = 0; li < p.size(); ++li) { + _p[permindex.convert_linear_index(li)] = p[li]; } - return *this; } + + /// Constructs TFactor depending on the variable n, with uniform distribution + TFactor( const Var& n ) : _vs(n), _p(n.states()) {} /// Returns const reference to value vector const TProb & p() const { return _p; } @@ -114,6 +132,8 @@ template class TFactor { const VarSet & vars() const { return _vs; } /// Returns the number of possible joint states of the variables + /** \note This is equal to the length of the value vector. + */ size_t states() const { return _p.size(); } /// Returns a copy of the i'th entry of the value vector @@ -121,6 +141,15 @@ template class TFactor { /// Returns a reference to the i'th entry of the value vector T& operator[] (size_t i) { return _p[i]; } + + /// Returns iterator pointing to first entry + iterator begin() { return _p.begin(); } + /// Returns const iterator pointing to first entry + const_iterator begin() const { return _p.begin(); } + /// Returns iterator pointing beyond last entry + iterator end() { return _p.end(); } + /// Returns const iterator pointing beyond last entry + const_iterator end() const { return _p.end(); } /// Sets all values to p TFactor & fill (T p) { _p.fill( p ); return(*this); } @@ -128,27 +157,14 @@ template class TFactor { /// Draws all values i.i.d. from a uniform distribution on [0,1) TFactor & randomize () { _p.randomize(); return(*this); } - /// Returns product of *this with scalar t - TFactor operator* (T t) const { - TFactor result = *this; - result.p() *= t; - return result; - } - /// Multiplies with scalar t + /// Multiplies *this with scalar t TFactor& operator*= (T t) { _p *= t; return *this; } - /// Returns quotient of *this with scalar t - TFactor operator/ (T t) const { - TFactor result = *this; - result.p() /= t; - return result; - } - - /// Divides by scalar t + /// Divides *this by scalar t TFactor& operator/= (T t) { _p /= t; return *this; @@ -166,6 +182,24 @@ template class TFactor { return *this; } + /// Raises *this to the power a + TFactor& operator^= (Real a) { _p ^= a; return *this; } + + + /// Returns product of *this with scalar t + TFactor operator* (T t) const { + TFactor result = *this; + result.p() *= t; + return result; + } + + /// Returns quotient of *this with scalar t + TFactor operator/ (T t) const { + TFactor result = *this; + result.p() /= t; + return result; + } + /// Returns sum of *this and scalar t TFactor operator+ (T t) const { TFactor result(*this); @@ -180,86 +214,86 @@ template class TFactor { return result; } - /// Returns product of *this with another TFactor f - /** The result is a TFactor depending on the union of the variables - * on which *this and f depend. - */ - TFactor operator* (const TFactor& f) const; + /// Returns *this raised to the power a + TFactor operator^ (Real a) const { + TFactor x; + x._vs = _vs; + x._p = _p^a; + return x; + } - /// Returns quotient of *this by another TFactor f - /** The result is a TFactor depending on the union of the variables - * on which *this and f depend. - */ - TFactor operator/ (const TFactor& f) const; + /// Multiplies *this with the TFactor f + TFactor& operator*= (const TFactor& f) { + if( f._vs == _vs ) // optimize special case + _p *= f._p; + else + *this = (*this * f); + return *this; + } - /// Multiplies *this with another TFactor f - /** The result is a TFactor depending on the union of the variables - * on which *this and f depend. - */ - TFactor& operator*= (const TFactor& f) { return( *this = (*this * f) ); } + /// Divides *this by the TFactor f + TFactor& operator/= (const TFactor& f) { + if( f._vs == _vs ) // optimize special case + _p /= f._p; + else + *this = (*this / f); + return *this; + } - /// Divides *this by another TFactor f - /** The result is a TFactor depending on the union of the variables - * on which *this and f depend. - */ - TFactor& operator/= (const TFactor& f) { return( *this = (*this / f) ); } + /// Adds the TFactor f to *this + TFactor& operator+= (const TFactor& f) { + if( f._vs == _vs ) // optimize special case + _p += f._p; + else + *this = (*this + f); + return *this; + } - /// Returns sum of *this and another TFactor f - /** \pre this->vars() == f.vars() - */ - TFactor operator+ (const TFactor& f) const { -#ifdef DAI_DEBUG - assert( f._vs == _vs ); -#endif - TFactor sum(*this); - sum._p += f._p; - return sum; + /// Subtracts the TFactor f from *this + TFactor& operator-= (const TFactor& f) { + if( f._vs == _vs ) // optimize special case + _p -= f._p; + else + *this = (*this - f); + return *this; } - /// Returns *this minus another TFactor f - /** \pre this->vars() == f.vars() + /// Returns product of *this with the TFactor f + /** The product of two factors is defined as follows: if + * \f$f : \prod_{l\in L} X_l \to [0,\infty)\f$ and \f$g : \prod_{m\in M} X_m \to [0,\infty)\f$, then + * \f[fg : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) g(x_M).\f] */ - TFactor operator- (const TFactor& f) const { -#ifdef DAI_DEBUG - assert( f._vs == _vs ); -#endif - TFactor sum(*this); - sum._p -= f._p; - return sum; + TFactor operator* (const TFactor& f) const { + return pointwiseOp(*this,f,std::multiplies()); } - /// Adds another TFactor f to *this - /** \pre this->vars() == f.vars() + /// Returns quotient of *this by the TFactor f + /** The quotient of two factors is defined as follows: if + * \f$f : \prod_{l\in L} X_l \to [0,\infty)\f$ and \f$g : \prod_{m\in M} X_m \to [0,\infty)\f$, then + * \f[\frac{f}{g} : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto \frac{f(x_L)}{g(x_M)}.\f] */ - TFactor& operator+= (const TFactor& f) { -#ifdef DAI_DEBUG - assert( f._vs == _vs ); -#endif - _p += f._p; - return *this; + TFactor operator/ (const TFactor& f) const { + return pointwiseOp(*this,f,divides0()); } - /// Subtracts another TFactor f from *this - /** \pre this->vars() == f.vars() + /// Returns sum of *this and the TFactor f + /** The sum of two factors is defined as follows: if + * \f$f : \prod_{l\in L} X_l \to [0,\infty)\f$ and \f$g : \prod_{m\in M} X_m \to [0,\infty)\f$, then + * \f[f+g : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) + g(x_M).\f] */ - TFactor& operator-= (const TFactor& f) { -#ifdef DAI_DEBUG - assert( f._vs == _vs ); -#endif - _p -= f._p; - return *this; + TFactor operator+ (const TFactor& f) const { + return pointwiseOp(*this,f,std::plus()); } - /// Returns *this raised to the power a - TFactor operator^ (Real a) const { - TFactor x; - x._vs = _vs; - x._p = _p^a; - return x; + /// Returns *this minus the TFactor f + /** The difference of two factors is defined as follows: if + * \f$f : \prod_{l\in L} X_l \to [0,\infty)\f$ and \f$g : \prod_{m\in M} X_m \to [0,\infty)\f$, then + * \f[f-g : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) - g(x_M).\f] + */ + TFactor operator- (const TFactor& f) const { + return pointwiseOp(*this,f,std::minus()); } - /// Raises *this to the power a - TFactor& operator^= (Real a) { _p ^= a; return *this; } /// Sets all values that are smaller than epsilon to 0 TFactor& makeZero( T epsilon ) { @@ -283,29 +317,6 @@ template class TFactor { return inv; } - /// Returns *this divided pointwise by another TFactor f - /** \pre this->vars() == f.vars() - */ - TFactor divided_by( const TFactor& f ) const { -#ifdef DAI_DEBUG - assert( f._vs == _vs ); -#endif - TFactor quot(*this); - quot._p /= f._p; - return quot; - } - - /// Divides *this pointwise by another TFactor f - /** \pre this->vars() == f.vars() - */ - TFactor& divide( const TFactor& f ) { -#ifdef DAI_DEBUG - assert( f._vs == _vs ); -#endif - _p /= f._p; - return *this; - } - /// Returns pointwise exp of *this TFactor exp() const { TFactor e; @@ -314,14 +325,6 @@ template class TFactor { return e; } - /// Returns pointwise absolute value of *this - TFactor abs() const { - TFactor e; - e._vs = _vs; - e._p = _p.abs(); - return e; - } - /// Returns pointwise logarithm of *this /** If zero==true, uses log(0)==0; otherwise, log(0)=-Inf. */ @@ -332,6 +335,14 @@ template class TFactor { return l; } + /// Returns pointwise absolute value of *this + TFactor abs() const { + TFactor e; + e._vs = _vs; + e._p = _p.abs(); + return e; + } + /// Normalizes *this TFactor according to the specified norm T normalize( typename Prob::NormType norm=Prob::NORMPROB ) { return _p.normalize( norm ); } @@ -344,8 +355,16 @@ template class TFactor { } /// Returns a slice of this TFactor, where the subset ns is in state nsState - /** \pre ns sould be a subset of vars() - * \pre nsState < ns.states() + /** \pre \a ns sould be a subset of vars() + * \pre \a nsState < ns.states() + * + * The result is a TFactor that depends on the variables in this->vars() except those in \a ns, + * obtained by setting the variables in \a ns to the joint state specified by the linear index + * \a nsState. Formally, if *this corresponds with the factor \f$f : \prod_{l\in L} X_l \to [0,\infty)\f$, + * \f$M \subset L\f$ corresponds with \a ns and \a nsState corresponds with a mapping \f$s\f$ that + * maps a variable \f$x_m\f$ with \f$m\in M\f$ to its state \f$s(x_m) \in X_m\f$, then the slice + * returned corresponds with the factor \f$g : \prod_{l \in L \setminus M} X_l \to [0,\infty)\f$ + * defined by \f$g(\{x_l\}_{l\in L \setminus M}) = f(\{x_l\}_{l\in L \setminus M}, \{s(x_m)\}_{m\in M})\f$. */ TFactor slice( const VarSet& ns, size_t nsState ) const { assert( ns << _vs ); @@ -362,28 +381,24 @@ template class TFactor { return result; } - /// Returns unnormalized marginal obtained by summing out all variables except those in ns - TFactor partSum(const VarSet &ns) const; + /// Returns marginal on ns, obtained by summing out all variables except those in ns, and normalizing the result if normed==true + TFactor marginal(const VarSet & ns, bool normed=true) const; - /// Returns (normalized by default) marginal on ns, obtained by summing out all variables except those in ns - /** If normed==true, the result is normalized. - */ - TFactor marginal(const VarSet & ns, bool normed=true) const { - if( normed ) - return partSum(ns).normalized(); - else - return partSum(ns); - } + /// Returns max-marginal on ns, obtained by maximizing all variables except those in ns, and normalizing the result if normed==true + TFactor maxMarginal(const VarSet & ns, bool normed=true) const; /// Embeds this factor in a larger VarSet /** \pre vars() should be a subset of ns + * + * If *this corresponds with \f$f : \prod_{l\in L} X_l \to [0,\infty)\f$ and \f$L \subset M\f$, then + * the embedded factor corresponds with \f$g : \prod_{m\in M} X_m \to [0,\infty) : x \mapsto f(x_L)\f$. */ TFactor embed(const VarSet & ns) const { assert( ns >> _vs ); if( _vs == ns ) return *this; else - return (*this) * TFactor(ns / _vs, 1); + return (*this) * TFactor(ns / _vs, (T)1); } /// Returns true if *this has NaN values @@ -393,26 +408,26 @@ template class TFactor { bool hasNegatives() const { return _p.hasNegatives(); } /// Returns total sum of values - T totalSum() const { return _p.totalSum(); } + T sum() const { return _p.sum(); } /// Returns maximum absolute value T maxAbs() const { return _p.maxAbs(); } /// Returns maximum value - T maxVal() const { return _p.maxVal(); } + T max() const { return _p.max(); } /// Returns minimum value - T minVal() const { return _p.minVal(); } + T min() const { return _p.min(); } - /// Returns entropy of *this + /// Returns entropy of *this TFactor Real entropy() const { return _p.entropy(); } - /// Returns strength of *this, between variables i and j, as defined in eq. (52) of [\ref MoK07b] + /// Returns strength of *this TFactor (between variables i and j), as defined in eq. (52) of [\ref MoK07b] T strength( const Var &i, const Var &j ) const; }; -template TFactor TFactor::partSum(const VarSet & ns) const { +template TFactor TFactor::marginal(const VarSet & ns, bool normed) const { VarSet res_ns = ns & _vs; TFactor res( res_ns, 0.0 ); @@ -421,33 +436,47 @@ template TFactor TFactor::partSum(const VarSet & ns) const { for( size_t i = 0; i < _p.size(); i++, ++i_res ) res._p[i_res] += _p[i]; + if( normed ) + res.normalize( Prob::NORMPROB ); + return res; } -template TFactor TFactor::operator* (const TFactor& Q) const { - TFactor prod( _vs | Q._vs, 0.0 ); +template TFactor TFactor::maxMarginal(const VarSet & ns, bool normed) const { + VarSet res_ns = ns & _vs; + + TFactor res( res_ns, 0.0 ); - IndexFor i1(_vs, prod._vs); - IndexFor i2(Q._vs, prod._vs); + IndexFor i_res( res_ns, _vs ); + for( size_t i = 0; i < _p.size(); i++, ++i_res ) + if( _p[i] > res._p[i_res] ) + res._p[i_res] = _p[i]; - for( size_t i = 0; i < prod._p.size(); i++, ++i1, ++i2 ) - prod._p[i] += _p[i1] * Q._p[i2]; + if( normed ) + res.normalize( Prob::NORMPROB ); - return prod; + return res; } -template TFactor TFactor::operator/ (const TFactor& Q) const { - TFactor quot( _vs | Q._vs, 0.0 ); +template TFactor pointwiseOp( const TFactor &f, const TFactor &g, binaryOp op ) { + if( f.vars() == g.vars() ) { // optimizate special case + TFactor result(f); + for( size_t i = 0; i < result.states(); i++ ) + result[i] = op( result[i], g[i] ); + return result; + } else { + TFactor result( f.vars() | g.vars(), 0.0 ); - IndexFor i1(_vs, quot._vs); - IndexFor i2(Q._vs, quot._vs); + IndexFor i1(f.vars(), result.vars()); + IndexFor i2(g.vars(), result.vars()); - for( size_t i = 0; i < quot._p.size(); i++, ++i1, ++i2 ) - quot._p[i] += _p[i1] / Q._p[i2]; + for( size_t i = 0; i < result.states(); i++, ++i1, ++i2 ) + result[i] = op( f[i1], g[i2] ); - return quot; + return result; + } } @@ -471,8 +500,8 @@ template T TFactor::strength( const Var &i, const Var &j ) const bs = i.states(); else as = j.states(); - T f1 = slice( ij, alpha1 * as + beta1 * bs ).p().divide( slice( ij, alpha2 * as + beta1 * bs ).p() ).maxVal(); - T f2 = slice( ij, alpha2 * as + beta2 * bs ).p().divide( slice( ij, alpha1 * as + beta2 * bs ).p() ).maxVal(); + T f1 = slice( ij, alpha1 * as + beta1 * bs ).p().divide( slice( ij, alpha2 * as + beta1 * bs ).p() ).max(); + T f2 = slice( ij, alpha2 * as + beta2 * bs ).p().divide( slice( ij, alpha1 * as + beta2 * bs ).p() ).max(); T f = f1 * f2; if( f > max ) max = f; @@ -486,10 +515,10 @@ template T TFactor::strength( const Var &i, const Var &j ) const /** \relates TFactor */ template std::ostream& operator<< (std::ostream& os, const TFactor& P) { - os << "(" << P.vars() << " <"; + os << "(" << P.vars() << ", ("; for( size_t i = 0; i < P.states(); i++ ) - os << P[i] << " "; - os << ">)"; + os << (i == 0 ? "" : ", ") << P[i]; + os << "))"; return os; } @@ -539,7 +568,7 @@ template Real MutualInfo(const TFactor &f) { VarSet::const_iterator it = f.vars().begin(); Var i = *it; it++; Var j = *it; TFactor projection = f.marginal(i) * f.marginal(j); - return real( dist( f.normalized(), projection, Prob::DISTKL ) ); + return dist( f.normalized(), projection, Prob::DISTKL ); }