X-Git-Url: http://git.tuebingen.mpg.de/?p=libdai.git;a=blobdiff_plain;f=include%2Fdai%2Ffactor.h;h=b4411168cf69ec50ceded8410903a7eb9876dbea;hp=d65937a200160aad10904eabf2ccc159b7fce6f2;hb=ad60c591d618f4a65c638612b32e9306ce0aba09;hpb=83f2623b5af4926236d6aeae2a98d8e068d4424f diff --git a/include/dai/factor.h b/include/dai/factor.h index d65937a..b441116 100644 --- a/include/dai/factor.h +++ b/include/dai/factor.h @@ -41,26 +41,30 @@ namespace dai { -// predefine TFactor class -template class TFactor; - - -/// Represents a factor with probability entries represented as Real -typedef TFactor Factor; - - -/// Represents a probability factor. -/** A \e factor is a function of the Cartesian product of the state - * spaces of some set of variables to the nonnegative real numbers. - * More formally, if \f$x_i \in X_i\f$ for all \f$i\f$, then a factor - * depending on the variables \f$\{x_i\}\f$ is a function defined - * on \f$\prod_i X_i\f$ with values in \f$[0,\infty)\f$. +/// Represents a (probability) factor. +/** 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_l\}_{l\in L}\f$ is + * a function \f$f_L : \prod_{l\in L} X_l \to [0,\infty)\f$. * - * A Factor has two components: a VarSet, defining the set of variables - * that the factor depends on, and a TProb, containing the values of - * the factor for all possible joint states of the variables. + * 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_l\}_{l\in L}\f$ + * that the factor depends on; + * \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_{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 castable from and to double. + * \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: @@ -68,211 +72,240 @@ template class TFactor { TProb _p; public: - /// Construct Factor with empty VarSet + /// 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 Factor from VarSet + /// Constructs TFactor depending on variables in ns, with uniform distribution TFactor( const VarSet& ns ) : _vs(ns), _p(_vs.nrStates()) {} - /// Construct Factor from VarSet and initial value + /// 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 Factor from VarSet and initial array - TFactor( const VarSet& ns, const Real *p ) : _vs(ns), _p(_vs.nrStates(),p) {} - - /// Construct Factor from VarSet and TProb + /// 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()) {} + + /// 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 Factor from Var + /// Constructs 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; - } - return *this; - } - - /// Returns const reference to probability entries + /// Returns const reference to value vector const TProb & p() const { return _p; } - /// Returns reference to probability entries + /// Returns reference to value vector TProb & p() { return _p; } - /// Returns const reference to variables + /// Returns const reference to variable set 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 probability value + /// Returns a copy of the i'th entry of the value vector T operator[] (size_t i) const { return _p[i]; } - /// Returns a reference to the i'th probability value + /// Returns a reference to the i'th entry of the value vector T& operator[] (size_t i) { return _p[i]; } - - /// Sets all probability entries to p + + /// 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); } - /// Fills all probability entries with random values + /// Draws all values i.i.d. from a uniform distribution on [0,1) TFactor & randomize () { _p.randomize(); return(*this); } - /// Returns product of *this with x - TFactor operator* (T x) const { - Factor result = *this; - result.p() *= x; - return result; + + /// Multiplies *this with scalar t + TFactor& operator*= (T t) { + _p *= t; + return *this; } - /// Multiplies each probability entry with x - TFactor& operator*= (T x) { - _p *= x; + /// Divides *this by scalar t + TFactor& operator/= (T t) { + _p /= t; return *this; } - /// Returns quotient of *this with x - TFactor operator/ (T x) const { - Factor result = *this; - result.p() /= x; - return result; + /// Adds scalar t to *this + TFactor& operator+= (T t) { + _p += t; + return *this; } - /// Divides each probability entry by x - TFactor& operator/= (T x) { - _p /= x; + /// Subtracts scalar t from *this + TFactor& operator-= (T t) { + _p -= t; return *this; } - /// Returns product of *this with another Factor - TFactor operator* (const TFactor& Q) const; + /// Raises *this to the power a + TFactor& operator^= (Real a) { _p ^= a; return *this; } - /// Returns quotient of *this with another Factor - TFactor operator/ (const TFactor& Q) const; - /// Multiplies *this with another Factor - TFactor& operator*= (const TFactor& Q) { return( *this = (*this * Q) ); } + /// Returns product of *this with scalar t + TFactor operator* (T t) const { + TFactor result = *this; + result.p() *= t; + return result; + } - /// Divides *this by another Factor - TFactor& operator/= (const TFactor& Q) { return( *this = (*this / Q) ); } + /// 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 another Factor (their vars() should be identical) - TFactor operator+ (const TFactor& Q) const { -#ifdef DAI_DEBUG - assert( Q._vs == _vs ); -#endif - TFactor sum(*this); - sum._p += Q._p; - return sum; + /// Returns sum of *this and scalar t + TFactor operator+ (T t) const { + TFactor result(*this); + result._p += t; + return result; } - /// Returns difference of *this and another Factor (their vars() should be identical) - TFactor operator- (const TFactor& Q) const { -#ifdef DAI_DEBUG - assert( Q._vs == _vs ); -#endif - TFactor sum(*this); - sum._p -= Q._p; - return sum; + /// Returns *this minus scalar t + TFactor operator- (T t) const { + TFactor result(*this); + result._p -= t; + return result; } - /// Adds another Factor to *this (their vars() should be identical) - TFactor& operator+= (const TFactor& Q) { -#ifdef DAI_DEBUG - assert( Q._vs == _vs ); -#endif - _p += Q._p; - return *this; + /// Returns *this raised to the power a + TFactor operator^ (Real a) const { + TFactor x; + x._vs = _vs; + x._p = _p^a; + return x; } - /// Subtracts another Factor from *this (their vars() should be identical) - TFactor& operator-= (const TFactor& Q) { -#ifdef DAI_DEBUG - assert( Q._vs == _vs ); -#endif - _p -= Q._p; + /// 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; } - /// Adds scalar to *this - TFactor& operator+= (T q) { - _p += q; + /// 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; } - /// Subtracts scalar from *this - TFactor& operator-= (T q) { - _p -= q; + /// 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; + + /// 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) const; + + /// Adds the TFactor f to *this + /** \pre this->vars() == f.vars() + */ + TFactor& operator+= (const TFactor& f) { +#ifdef DAI_DEBUG + assert( f._vs == _vs ); +#endif + _p += f._p; return *this; } - /// Returns sum of *this and a scalar - TFactor operator+ (T q) const { - TFactor result(*this); - result._p += q; - return result; + /// Subtracts the TFactor f from *this + /** \pre this->vars() == f.vars() + */ + TFactor& operator-= (const TFactor& f) { +#ifdef DAI_DEBUG + assert( f._vs == _vs ); +#endif + _p -= f._p; + return *this; } - /// Returns difference of *this with a scalar - TFactor operator- (T q) const { - TFactor result(*this); - result._p -= q; - return result; + /// Returns sum of *this and the 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; } - /// Returns *this raised to some power - TFactor operator^ (Real a) const { TFactor x; x._vs = _vs; x._p = _p^a; return x; } + /// Returns *this minus the 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; + } - /// Raises *this to some power - TFactor& operator^= (Real a) { _p ^= a; return *this; } - /// Sets all entries that are smaller than epsilon to zero - TFactor& makeZero( Real epsilon ) { + /// Sets all values that are smaller than epsilon to 0 + TFactor& makeZero( T epsilon ) { _p.makeZero( epsilon ); return *this; } - /// Sets all entries that are smaller than epsilon to epsilon - TFactor& makePositive( Real epsilon ) { + /// Sets all values that are smaller than epsilon to epsilon + TFactor& makePositive( T epsilon ) { _p.makePositive( epsilon ); return *this; } - /// Returns inverse of *this - TFactor inverse() const { + /// Returns pointwise inverse of *this. + /** If zero == true, uses 1 / 0 == 0; otherwise 1 / 0 == Inf. + */ + TFactor inverse(bool zero=true) const { TFactor inv; inv._vs = _vs; - inv._p = _p.inverse(true); // FIXME + inv._p = _p.inverse(zero); return inv; } - /// Returns *this divided by another Factor - TFactor divided_by( const TFactor& denom ) const { -#ifdef DAI_DEBUG - assert( denom._vs == _vs ); -#endif - TFactor quot(*this); - quot._p /= denom._p; - return quot; - } - - /// Divides *this by another Factor - TFactor& divide( const TFactor& denom ) { -#ifdef DAI_DEBUG - assert( denom._vs == _vs ); -#endif - _p /= denom._p; - return *this; - } - - /// Returns exp of *this + /// Returns pointwise exp of *this TFactor exp() const { TFactor e; e._vs = _vs; @@ -280,140 +313,156 @@ template class TFactor { return e; } - /// Returns absolute value of *this - TFactor abs() const { - TFactor e; - e._vs = _vs; - e._p = _p.abs(); - return e; - } - - /// Returns logarithm of *this - TFactor log() const { + /// Returns pointwise logarithm of *this + /** If zero==true, uses log(0)==0; otherwise, log(0)=-Inf. + */ + TFactor log(bool zero=false) const { TFactor l; l._vs = _vs; - l._p = _p.log(); + l._p = _p.log(zero); return l; } - /// Returns logarithm of *this (defining log(0)=0) - TFactor log0() const { - TFactor l0; - l0._vs = _vs; - l0._p = _p.log0(); - return l0; + /// Returns pointwise absolute value of *this + TFactor abs() const { + TFactor e; + e._vs = _vs; + e._p = _p.abs(); + return e; } - /// Normalizes *this Factor - T normalize( typename Prob::NormType norm = Prob::NORMPROB ) { return _p.normalize( norm ); } + /// Normalizes *this TFactor according to the specified norm + T normalize( typename Prob::NormType norm=Prob::NORMPROB ) { return _p.normalize( norm ); } - /// Returns a normalized copy of *this - TFactor normalized( typename Prob::NormType norm = Prob::NORMPROB ) const { + /// Returns a normalized copy of *this, according to the specified norm + TFactor normalized( typename Prob::NormType norm=Prob::NORMPROB ) const { TFactor result; result._vs = _vs; result._p = _p.normalized( norm ); return result; } - /// Returns a slice of this factor, where the subset ns is in state ns_state - Factor slice( const VarSet & ns, size_t ns_state ) const { + /// Returns a slice of this TFactor, where the subset ns is in state nsState + /** \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 ); VarSet nsrem = _vs / ns; - Factor result( nsrem, 0.0 ); + TFactor result( nsrem, T(0) ); // OPTIMIZE ME IndexFor i_ns (ns, _vs); IndexFor i_nsrem (nsrem, _vs); for( size_t i = 0; i < states(); i++, ++i_ns, ++i_nsrem ) - if( (size_t)i_ns == ns_state ) + if( (size_t)i_ns == nsState ) result._p[i_nsrem] = _p[i]; return result; } - /// Returns unnormalized marginal; ns should be a subset of vars() - TFactor partSum(const VarSet & ns) const; - - /// Returns (normalized by default) marginal; ns should be a subset of vars() - TFactor marginal(const VarSet & ns, bool normed = true) const { if(normed) return partSum(ns).normalized(); else return partSum(ns); } - - /// Sums out all variables except those in ns - TFactor notSum(const VarSet & ns) const { return partSum(vars() ^ ns); } + /// 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; /// 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 { - VarSet vs = vars(); - assert( ns >> vs ); - if( vs == ns ) + assert( ns >> _vs ); + if( _vs == ns ) return *this; else - return (*this) * Factor(ns / vs, 1.0); + return (*this) * TFactor(ns / _vs, 1); } - /// Returns true if *this has NANs + /// Returns true if *this has NaN values bool hasNaNs() const { return _p.hasNaNs(); } - /// Returns true if *this has negative entries + /// Returns true if *this has negative values bool hasNegatives() const { return _p.hasNegatives(); } - /// Returns total sum of probability entries - T totalSum() const { return _p.totalSum(); } + /// Returns total sum of values + T sum() const { return _p.sum(); } - /// Returns maximum absolute value of probability entries + /// Returns maximum absolute value T maxAbs() const { return _p.maxAbs(); } - /// Returns maximum value of probability entries - T maxVal() const { return _p.maxVal(); } + /// Returns maximum value + T max() const { return _p.max(); } - /// Returns minimum value of probability entries - T minVal() const { return _p.minVal(); } + /// Returns minimum value + 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, using (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 { -#ifdef DAI_DEBUG - assert( ns << _vs ); -#endif +template TFactor TFactor::marginal(const VarSet & ns, bool normed) const { + VarSet res_ns = ns & _vs; - TFactor res( ns, 0.0 ); + TFactor res( res_ns, 0.0 ); - IndexFor i_res( ns, _vs ); + IndexFor i_res( res_ns, _vs ); 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::operator* (const TFactor& f) const { + if( f._vs == _vs ) { // optimizate special case + TFactor prod(*this); + prod._p *= f._p; + return prod; + } else { + TFactor prod( _vs | f._vs, 0.0 ); - IndexFor i1(_vs, prod._vs); - IndexFor i2(Q._vs, prod._vs); + IndexFor i1(_vs, prod._vs); + IndexFor i2(f._vs, prod._vs); - for( size_t i = 0; i < prod._p.size(); i++, ++i1, ++i2 ) - prod._p[i] += _p[i1] * Q._p[i2]; + for( size_t i = 0; i < prod._p.size(); i++, ++i1, ++i2 ) + prod._p[i] += _p[i1] * f._p[i2]; - return prod; + return prod; + } } -template TFactor TFactor::operator/ (const TFactor& Q) const { - TFactor quot( _vs + Q._vs, 0.0 ); +template TFactor TFactor::operator/ (const TFactor& f) const { + if( f._vs == _vs ) { // optimizate special case + TFactor quot(*this); + quot._p /= f._p; + return quot; + } else { + TFactor quot( _vs | f._vs, 0.0 ); - IndexFor i1(_vs, quot._vs); - IndexFor i2(Q._vs, quot._vs); + IndexFor i1(_vs, quot._vs); + IndexFor i2(f._vs, quot._vs); - 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 < quot._p.size(); i++, ++i1, ++i2 ) + quot._p[i] += _p[i1] / f._p[i2]; - return quot; + return quot; + } } @@ -437,8 +486,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; @@ -448,53 +497,71 @@ template T TFactor::strength( const Var &i, const Var &j ) const } -/// Writes a Factor to an output stream +/// Writes a TFactor to an output stream +/** \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; } -/// Returns distance between two Factors (with identical vars()) -template Real dist( const TFactor & x, const TFactor & y, Prob::DistType dt ) { - if( x.vars().empty() || y.vars().empty() ) +/// Returns distance between two TFactors f and g, according to the distance measure dt +/** \relates TFactor + * \pre f.vars() == g.vars() + */ +template Real dist( const TFactor &f, const TFactor &g, Prob::DistType dt ) { + if( f.vars().empty() || g.vars().empty() ) return -1; else { #ifdef DAI_DEBUG - assert( x.vars() == y.vars() ); + assert( f.vars() == g.vars() ); #endif - return dist( x.p(), y.p(), dt ); + return dist( f.p(), g.p(), dt ); } } -/// Returns the pointwise maximum of two Factors -template TFactor max( const TFactor & P, const TFactor & Q ) { - assert( P._vs == Q._vs ); - return TFactor( P._vs, min( P.p(), Q.p() ) ); +/// Returns the pointwise maximum of two TFactors +/** \relates TFactor + * \pre f.vars() == g.vars() + */ +template TFactor max( const TFactor &f, const TFactor &g ) { + assert( f._vs == g._vs ); + return TFactor( f._vs, min( f.p(), g.p() ) ); } -/// Returns the pointwise minimum of two Factors -template TFactor min( const TFactor & P, const TFactor & Q ) { - assert( P._vs == Q._vs ); - return TFactor( P._vs, max( P.p(), Q.p() ) ); +/// Returns the pointwise minimum of two TFactors +/** \relates TFactor + * \pre f.vars() == g.vars() + */ +template TFactor min( const TFactor &f, const TFactor &g ) { + assert( f._vs == g._vs ); + return TFactor( f._vs, max( f.p(), g.p() ) ); } -/// Calculates the mutual information between the two variables in P -template Real MutualInfo(const TFactor & P) { - assert( P.vars().size() == 2 ); - VarSet::const_iterator it = P.vars().begin(); +/// Calculates the mutual information between the two variables that f depends on, under the distribution given by f +/** \relates TFactor + * \pre f.vars().size() == 2 + */ +template Real MutualInfo(const TFactor &f) { + assert( f.vars().size() == 2 ); + VarSet::const_iterator it = f.vars().begin(); Var i = *it; it++; Var j = *it; - TFactor projection = P.marginal(i) * P.marginal(j); - return real( dist( P.normalized(), projection, Prob::DISTKL ) ); + TFactor projection = f.marginal(i) * f.marginal(j); + return real( dist( f.normalized(), projection, Prob::DISTKL ) ); } +/// Represents a factor with values of type Real. +typedef TFactor Factor; + + } // end of namespace dai