/// \file
/// \brief Defines TFactor<T> and Factor classes
-/// \todo Improve documentation
#ifndef __defined_libdai_factor_h
/// 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<Var,size_t> &).
*
* \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 <typename T> class TFactor {
private:
TProb<T> _p;
public:
- /// Construct TFactor depending on no variables, with value p
+ /// Iterator over factor entries
+ typedef typename TProb<T>::iterator iterator;
+
+ /// Const iterator over factor entries
+ typedef typename TProb<T>::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<typename TIterator>
+ 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<T>& p ) : _vs(ns), _p(p) {
#ifdef DAI_DEBUG
assert( _vs.nrStates() == _p.size() );
#endif
}
- /// Construct TFactor depending on the variable n, with uniform distribution
+ /// Constructs TFactor depending on the variable n, with uniform distribution
TFactor( const Var& n ) : _vs(n), _p(n.states()) {}
- /// Copy constructor
- TFactor( const TFactor<T> &x ) : _vs(x._vs), _p(x._p) {}
-
- /// Assignment operator
- TFactor<T> & operator= (const TFactor<T> &x) {
- if( this != &x ) {
- _vs = x._vs;
- _p = x._p;
- }
- return *this;
- }
-
/// Returns const reference to value vector
const TProb<T> & p() const { return _p; }
/// Returns reference to value vector
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
/// 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<T> & fill (T p) { _p.fill( p ); return(*this); }
/// Draws all values i.i.d. from a uniform distribution on [0,1)
TFactor<T> & randomize () { _p.randomize(); return(*this); }
- /// Returns product of *this with scalar t
- TFactor<T> operator* (T t) const {
- TFactor<T> result = *this;
- result.p() *= t;
- return result;
- }
- /// Multiplies with scalar t
+ /// Multiplies *this with scalar t
TFactor<T>& operator*= (T t) {
_p *= t;
return *this;
}
- /// Returns quotient of *this with scalar t
- TFactor<T> operator/ (T t) const {
- TFactor<T> result = *this;
- result.p() /= t;
- return result;
- }
-
- /// Divides by scalar t
+ /// Divides *this by scalar t
TFactor<T>& operator/= (T t) {
_p /= t;
return *this;
return *this;
}
+ /// Raises *this to the power a
+ TFactor<T>& operator^= (Real a) { _p ^= a; return *this; }
+
+
+ /// Returns product of *this with scalar t
+ TFactor<T> operator* (T t) const {
+ TFactor<T> result = *this;
+ result.p() *= t;
+ return result;
+ }
+
+ /// Returns quotient of *this with scalar t
+ TFactor<T> operator/ (T t) const {
+ TFactor<T> result = *this;
+ result.p() /= t;
+ return result;
+ }
+
/// Returns sum of *this and scalar t
TFactor<T> operator+ (T t) const {
TFactor<T> result(*this);
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<T> operator* (const TFactor<T>& f) const;
+ /// Returns *this raised to the power a
+ TFactor<T> operator^ (Real a) const {
+ TFactor<T> 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<T> operator/ (const TFactor<T>& f) const;
+ /// Multiplies *this with the TFactor f
+ TFactor<T>& operator*= (const TFactor<T>& 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.
+ /// Divides *this by the TFactor f
+ TFactor<T>& operator/= (const TFactor<T>& f) {
+ if( f._vs == _vs ) // optimize special case
+ _p /= f._p;
+ else
+ *this = (*this / f);
+ return *this;
+ }
+
+ /// 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<T>& operator*= (const TFactor<T>& f) { return( *this = (*this * f) ); }
+ TFactor<T> operator* (const TFactor<T>& f) const;
- /// Divides *this by another TFactor f
- /** The result is a TFactor depending on the union of the variables
- * on which *this and f depend.
+ /// 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<T>& operator/= (const TFactor<T>& f) { return( *this = (*this / f) ); }
+ TFactor<T> operator/ (const TFactor<T>& f) const;
- /// Returns sum of *this and another TFactor f
+ /// Adds the TFactor f to *this
/** \pre this->vars() == f.vars()
*/
- TFactor<T> operator+ (const TFactor<T>& f) const {
+ TFactor<T>& operator+= (const TFactor<T>& f) {
#ifdef DAI_DEBUG
assert( f._vs == _vs );
#endif
- TFactor<T> sum(*this);
- sum._p += f._p;
- return sum;
+ _p += f._p;
+ return *this;
}
- /// Returns *this minus another TFactor f
+ /// Subtracts the TFactor f from *this
/** \pre this->vars() == f.vars()
*/
- TFactor<T> operator- (const TFactor<T>& f) const {
+ TFactor<T>& operator-= (const TFactor<T>& f) {
#ifdef DAI_DEBUG
assert( f._vs == _vs );
#endif
- TFactor<T> sum(*this);
- sum._p -= f._p;
- return sum;
+ _p -= f._p;
+ return *this;
}
- /// Adds another TFactor f to *this
+ /// Returns sum of *this and the TFactor f
/** \pre this->vars() == f.vars()
*/
- TFactor<T>& operator+= (const TFactor<T>& f) {
+ TFactor<T> operator+ (const TFactor<T>& f) const {
#ifdef DAI_DEBUG
assert( f._vs == _vs );
#endif
- _p += f._p;
- return *this;
+ TFactor<T> sum(*this);
+ sum._p += f._p;
+ return sum;
}
- /// Subtracts another TFactor f from *this
+ /// Returns *this minus the TFactor f
/** \pre this->vars() == f.vars()
*/
- TFactor<T>& operator-= (const TFactor<T>& f) {
+ TFactor<T> operator- (const TFactor<T>& f) const {
#ifdef DAI_DEBUG
assert( f._vs == _vs );
#endif
- _p -= f._p;
- return *this;
- }
-
- /// Returns *this raised to the power a
- TFactor<T> operator^ (Real a) const {
- TFactor<T> x;
- x._vs = _vs;
- x._p = _p^a;
- return x;
+ TFactor<T> sum(*this);
+ sum._p -= f._p;
+ return sum;
}
- /// Raises *this to the power a
- TFactor<T>& operator^= (Real a) { _p ^= a; return *this; }
/// Sets all values that are smaller than epsilon to 0
TFactor<T>& makeZero( T epsilon ) {
return inv;
}
- /// Returns *this divided pointwise by another TFactor f
- /** \pre this->vars() == f.vars()
- */
- TFactor<T> divided_by( const TFactor<T>& f ) const {
-#ifdef DAI_DEBUG
- assert( f._vs == _vs );
-#endif
- TFactor<T> quot(*this);
- quot._p /= f._p;
- return quot;
- }
-
- /// Divides *this pointwise by another TFactor f
- /** \pre this->vars() == f.vars()
- */
- TFactor<T>& divide( const TFactor<T>& f ) {
-#ifdef DAI_DEBUG
- assert( f._vs == _vs );
-#endif
- _p /= f._p;
- return *this;
- }
-
/// Returns pointwise exp of *this
TFactor<T> exp() const {
TFactor<T> e;
return e;
}
- /// Returns pointwise absolute value of *this
- TFactor<T> abs() const {
- TFactor<T> 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.
*/
return l;
}
+ /// Returns pointwise absolute value of *this
+ TFactor<T> abs() const {
+ TFactor<T> 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 ); }
}
/// 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<T> slice( const VarSet& ns, size_t nsState ) const {
assert( ns << _vs );
return result;
}
- /// Returns unnormalized marginal obtained by summing out all variables except those in ns
- TFactor<T> partSum(const VarSet &ns) 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<T> marginal(const VarSet & ns, bool normed=true) const {
- if( normed )
- return partSum(ns).normalized();
- else
- return partSum(ns);
- }
+ /// Returns marginal on ns, obtained by summing out all variables except those in ns, and normalizing the result if normed==true
+ TFactor<T> 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<T> embed(const VarSet & ns) const {
assert( ns >> _vs );
if( _vs == ns )
return *this;
else
- return (*this) * TFactor<T>(ns / _vs, 1);
+ return (*this) * TFactor<T>(ns / _vs, (T)1);
}
/// Returns true if *this has NaN values
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<typename T> TFactor<T> TFactor<T>::partSum(const VarSet & ns) const {
- ns &= _vs;
+template<typename T> TFactor<T> TFactor<T>::marginal(const VarSet & ns, bool normed) const {
+ VarSet res_ns = ns & _vs;
- TFactor<T> res( ns, 0.0 );
+ TFactor<T> 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<typename T> TFactor<T> TFactor<T>::operator* (const TFactor<T>& Q) const {
- TFactor<T> prod( _vs | Q._vs, 0.0 );
+template<typename T> TFactor<T> TFactor<T>::operator* (const TFactor<T>& f) const {
+ if( f._vs == _vs ) { // optimizate special case
+ TFactor<T> prod(*this);
+ prod._p *= f._p;
+ return prod;
+ } else {
+ TFactor<T> 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<typename T> TFactor<T> TFactor<T>::operator/ (const TFactor<T>& Q) const {
- TFactor<T> quot( _vs | Q._vs, 0.0 );
+template<typename T> TFactor<T> TFactor<T>::operator/ (const TFactor<T>& f) const {
+ if( f._vs == _vs ) { // optimizate special case
+ TFactor<T> quot(*this);
+ quot._p /= f._p;
+ return quot;
+ } else {
+ TFactor<T> 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;
+ }
}
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;
/** \relates TFactor
*/
template<typename T> std::ostream& operator<< (std::ostream& os, const TFactor<T>& 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;
}