Fixed bug in BipartiteGraph::eraseEdge and improved documentation
[libdai.git] / include / dai / factor.h
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2002 Martijn Leisink [martijn@mbfys.kun.nl]
8 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
9 * Copyright (C) 2002-2007 Radboud University Nijmegen, The Netherlands
10 */
11
12
13 /// \file
14 /// \brief Defines TFactor<T> and Factor classes
15 /// \todo Improve documentation
16
17
18 #ifndef __defined_libdai_factor_h
19 #define __defined_libdai_factor_h
20
21
22 #include <iostream>
23 #include <functional>
24 #include <cmath>
25 #include <dai/prob.h>
26 #include <dai/varset.h>
27 #include <dai/index.h>
28 #include <dai/util.h>
29
30
31 namespace dai {
32
33
34 // Function object similar to std::divides(), but different in that dividing by zero results in zero
35 template<typename T> struct divides0 : public std::binary_function<T, T, T> {
36 // Returns (j == 0 ? 0 : (i/j))
37 T operator()( const T &i, const T &j ) const {
38 if( j == (T)0 )
39 return (T)0;
40 else
41 return i / j;
42 }
43 };
44
45
46 /// Represents a (probability) factor.
47 /** Mathematically, a \e factor is a function mapping joint states of some
48 * variables to the nonnegative real numbers.
49 * More formally, denoting a discrete variable with label \f$l\f$ by
50 * \f$x_l\f$ and its state space by \f$X_l = \{0,1,\dots,S_l-1\}\f$,
51 * then a factor depending on the variables \f$\{x_l\}_{l\in L}\f$ is
52 * a function \f$f_L : \prod_{l\in L} X_l \to [0,\infty)\f$.
53 *
54 * In libDAI, a factor is represented by a TFactor<\a T> object, which has two
55 * components:
56 * \arg a VarSet, corresponding with the set of variables \f$\{x_l\}_{l\in L}\f$
57 * that the factor depends on;
58 * \arg a TProb<\a T>, a vector containing the value of the factor for each possible
59 * joint state of the variables.
60 *
61 * The factor values are stored in the entries of the TProb<\a T> in a particular
62 * ordering, which is defined by the one-to-one correspondence of a joint state
63 * in \f$\prod_{l\in L} X_l\f$ with a linear index in
64 * \f$\{0,1,\dots,\prod_{l\in L} S_l-1\}\f$ according to the mapping \f$\sigma\f$
65 * induced by VarSet::calcState(const std::map<Var,size_t> &).
66 *
67 * \tparam T Should be a scalar that is castable from and to double and should support elementary arithmetic operations.
68 * \todo Define a better fileformat for .fg files (maybe using XML)?
69 * \todo Add support for sparse factors.
70 */
71 template <typename T> class TFactor {
72 private:
73 VarSet _vs;
74 TProb<T> _p;
75
76 public:
77 /// Iterator over factor entries
78 typedef typename TProb<T>::iterator iterator;
79
80 /// Const iterator over factor entries
81 typedef typename TProb<T>::const_iterator const_iterator;
82
83 /// Constructs TFactor depending on no variables, with value p
84 TFactor ( Real p = 1.0 ) : _vs(), _p(1,p) {}
85
86 /// Constructs TFactor depending on variables in vars, with uniform distribution
87 TFactor( const VarSet& vars ) : _vs(vars), _p(_vs.nrStates()) {}
88
89 /// Constructs TFactor depending on variables in vars, with all values set to p
90 TFactor( const VarSet& vars, Real p ) : _vs(vars), _p(_vs.nrStates(),p) {}
91
92 /// Constructs TFactor depending on variables in vars, copying the values from the range starting at begin
93 /** \param vars contains the variables that the new TFactor should depend on.
94 * \tparam Iterator Iterates over instances of type T; should support addition of size_t.
95 * \param begin Points to first element to be added.
96 */
97 template<typename TIterator>
98 TFactor( const VarSet& vars, TIterator begin ) : _vs(vars), _p(begin, begin + _vs.nrStates(), _vs.nrStates()) {}
99
100 /// Constructs TFactor depending on variables in vars, with values set to the TProb p
101 TFactor( const VarSet& vars, const TProb<T> &p ) : _vs(vars), _p(p) {
102 DAI_DEBASSERT( _vs.nrStates() == _p.size() );
103 }
104
105 /// Constructs TFactor depending on variables in vars, permuting the values given in TProb p
106 TFactor( const std::vector<Var> &vars, const std::vector<T> &p ) : _vs(vars.begin(), vars.end(), vars.size()), _p(p.size()) {
107 Permute permindex(vars);
108 for( size_t li = 0; li < p.size(); ++li )
109 _p[permindex.convert_linear_index(li)] = p[li];
110 }
111
112 /// Constructs TFactor depending on the variable v, with uniform distribution
113 TFactor( const Var &v ) : _vs(v), _p(v.states()) {}
114
115 /// Returns const reference to value vector
116 const TProb<T>& p() const { return _p; }
117 /// Returns reference to value vector
118 TProb<T>& p() { return _p; }
119
120 /// Returns const reference to variable set
121 const VarSet& vars() const { return _vs; }
122
123 /// Returns the number of possible joint states of the variables
124 /** \note This is equal to the length of the value vector.
125 */
126 size_t states() const { return _p.size(); }
127
128 /// Returns a copy of the i'th entry of the value vector
129 T operator[] (size_t i) const { return _p[i]; }
130
131 /// Returns a reference to the i'th entry of the value vector
132 T& operator[] (size_t i) { return _p[i]; }
133
134 /// Returns iterator pointing to first entry
135 iterator begin() { return _p.begin(); }
136 /// Returns const iterator pointing to first entry
137 const_iterator begin() const { return _p.begin(); }
138 /// Returns iterator pointing beyond last entry
139 iterator end() { return _p.end(); }
140 /// Returns const iterator pointing beyond last entry
141 const_iterator end() const { return _p.end(); }
142
143 /// Sets all values to p
144 TFactor<T> & fill (T p) { _p.fill( p ); return(*this); }
145
146 /// Draws all values i.i.d. from a uniform distribution on [0,1)
147 TFactor<T> & randomize () { _p.randomize(); return(*this); }
148
149
150 /// Multiplies *this with scalar t
151 TFactor<T>& operator*= (T t) {
152 _p *= t;
153 return *this;
154 }
155
156 /// Divides *this by scalar t
157 TFactor<T>& operator/= (T t) {
158 _p /= t;
159 return *this;
160 }
161
162 /// Adds scalar t to *this
163 TFactor<T>& operator+= (T t) {
164 _p += t;
165 return *this;
166 }
167
168 /// Subtracts scalar t from *this
169 TFactor<T>& operator-= (T t) {
170 _p -= t;
171 return *this;
172 }
173
174 /// Raises *this to the power a
175 TFactor<T>& operator^= (Real a) { _p ^= a; return *this; }
176
177
178 /// Returns product of *this with scalar t
179 TFactor<T> operator* (T t) const {
180 TFactor<T> result = *this;
181 result.p() *= t;
182 return result;
183 }
184
185 /// Returns quotient of *this with scalar t
186 TFactor<T> operator/ (T t) const {
187 TFactor<T> result = *this;
188 result.p() /= t;
189 return result;
190 }
191
192 /// Returns sum of *this and scalar t
193 TFactor<T> operator+ (T t) const {
194 TFactor<T> result(*this);
195 result._p += t;
196 return result;
197 }
198
199 /// Returns *this minus scalar t
200 TFactor<T> operator- (T t) const {
201 TFactor<T> result(*this);
202 result._p -= t;
203 return result;
204 }
205
206 /// Returns *this raised to the power a
207 TFactor<T> operator^ (Real a) const {
208 TFactor<T> x;
209 x._vs = _vs;
210 x._p = _p^a;
211 return x;
212 }
213
214 /// Multiplies *this with the TFactor f
215 TFactor<T>& operator*= (const TFactor<T>& f) {
216 if( f._vs == _vs ) // optimize special case
217 _p *= f._p;
218 else
219 *this = (*this * f);
220 return *this;
221 }
222
223 /// Divides *this by the TFactor f
224 TFactor<T>& operator/= (const TFactor<T>& f) {
225 if( f._vs == _vs ) // optimize special case
226 _p /= f._p;
227 else
228 *this = (*this / f);
229 return *this;
230 }
231
232 /// Adds the TFactor f to *this
233 TFactor<T>& operator+= (const TFactor<T>& f) {
234 if( f._vs == _vs ) // optimize special case
235 _p += f._p;
236 else
237 *this = (*this + f);
238 return *this;
239 }
240
241 /// Subtracts the TFactor f from *this
242 TFactor<T>& operator-= (const TFactor<T>& f) {
243 if( f._vs == _vs ) // optimize special case
244 _p -= f._p;
245 else
246 *this = (*this - f);
247 return *this;
248 }
249
250 /// Returns product of *this with the TFactor f
251 /** The product of two factors is defined as follows: if
252 * \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
253 * \f[fg : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) g(x_M).\f]
254 */
255 TFactor<T> operator* (const TFactor<T>& f) const {
256 return pointwiseOp(*this,f,std::multiplies<T>());
257 }
258
259 /// Returns quotient of *this by the TFactor f
260 /** The quotient of two factors is defined as follows: if
261 * \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
262 * \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]
263 */
264 TFactor<T> operator/ (const TFactor<T>& f) const {
265 return pointwiseOp(*this,f,divides0<T>());
266 }
267
268 /// Returns sum of *this and the TFactor f
269 /** The sum of two factors is defined as follows: if
270 * \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
271 * \f[f+g : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) + g(x_M).\f]
272 */
273 TFactor<T> operator+ (const TFactor<T>& f) const {
274 return pointwiseOp(*this,f,std::plus<T>());
275 }
276
277 /// Returns *this minus the TFactor f
278 /** The difference of two factors is defined as follows: if
279 * \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
280 * \f[f-g : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) - g(x_M).\f]
281 */
282 TFactor<T> operator- (const TFactor<T>& f) const {
283 return pointwiseOp(*this,f,std::minus<T>());
284 }
285
286
287 /// Sets all values that are smaller than epsilon to 0
288 TFactor<T>& makeZero( T epsilon ) {
289 _p.makeZero( epsilon );
290 return *this;
291 }
292
293 /// Sets all values that are smaller than epsilon to epsilon
294 TFactor<T>& makePositive( T epsilon ) {
295 _p.makePositive( epsilon );
296 return *this;
297 }
298
299 /// Returns pointwise inverse of *this.
300 /** If zero == true, uses 1 / 0 == 0; otherwise 1 / 0 == Inf.
301 */
302 TFactor<T> inverse(bool zero=true) const {
303 TFactor<T> inv;
304 inv._vs = _vs;
305 inv._p = _p.inverse(zero);
306 return inv;
307 }
308
309 /// Returns pointwise exp of *this
310 TFactor<T> exp() const {
311 TFactor<T> e;
312 e._vs = _vs;
313 e._p = _p.exp();
314 return e;
315 }
316
317 /// Returns pointwise logarithm of *this
318 /** If zero==true, uses log(0)==0; otherwise, log(0)=-Inf.
319 */
320 TFactor<T> log(bool zero=false) const {
321 TFactor<T> l;
322 l._vs = _vs;
323 l._p = _p.log(zero);
324 return l;
325 }
326
327 /// Returns pointwise absolute value of *this
328 TFactor<T> abs() const {
329 TFactor<T> e;
330 e._vs = _vs;
331 e._p = _p.abs();
332 return e;
333 }
334
335 /// Normalizes *this TFactor according to the specified norm
336 T normalize( typename Prob::NormType norm=Prob::NORMPROB ) { return _p.normalize( norm ); }
337
338 /// Returns a normalized copy of *this, according to the specified norm
339 TFactor<T> normalized( typename Prob::NormType norm=Prob::NORMPROB ) const {
340 TFactor<T> result;
341 result._vs = _vs;
342 result._p = _p.normalized( norm );
343 return result;
344 }
345
346 /// Returns a slice of this TFactor, where the subset ns is in state nsState
347 /** \pre \a ns sould be a subset of vars()
348 * \pre \a nsState < ns.states()
349 *
350 * The result is a TFactor that depends on the variables in this->vars() except those in \a ns,
351 * obtained by setting the variables in \a ns to the joint state specified by the linear index
352 * \a nsState. Formally, if *this corresponds with the factor \f$f : \prod_{l\in L} X_l \to [0,\infty)\f$,
353 * \f$M \subset L\f$ corresponds with \a ns and \a nsState corresponds with a mapping \f$s\f$ that
354 * 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
355 * returned corresponds with the factor \f$g : \prod_{l \in L \setminus M} X_l \to [0,\infty)\f$
356 * 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$.
357 */
358 TFactor<T> slice( const VarSet& ns, size_t nsState ) const {
359 DAI_ASSERT( ns << _vs );
360 VarSet nsrem = _vs / ns;
361 TFactor<T> result( nsrem, T(0) );
362
363 // OPTIMIZE ME
364 IndexFor i_ns (ns, _vs);
365 IndexFor i_nsrem (nsrem, _vs);
366 for( size_t i = 0; i < states(); i++, ++i_ns, ++i_nsrem )
367 if( (size_t)i_ns == nsState )
368 result._p[i_nsrem] = _p[i];
369
370 return result;
371 }
372
373 /// Returns marginal on ns, obtained by summing out all variables except those in ns, and normalizing the result if normed==true
374 TFactor<T> marginal(const VarSet & ns, bool normed=true) const;
375
376 /// Returns max-marginal on ns, obtained by maximizing all variables except those in ns, and normalizing the result if normed==true
377 TFactor<T> maxMarginal(const VarSet & ns, bool normed=true) const;
378
379 /// Embeds this factor in a larger VarSet
380 /** \pre vars() should be a subset of ns
381 *
382 * If *this corresponds with \f$f : \prod_{l\in L} X_l \to [0,\infty)\f$ and \f$L \subset M\f$, then
383 * the embedded factor corresponds with \f$g : \prod_{m\in M} X_m \to [0,\infty) : x \mapsto f(x_L)\f$.
384 */
385 TFactor<T> embed(const VarSet & ns) const {
386 DAI_ASSERT( ns >> _vs );
387 if( _vs == ns )
388 return *this;
389 else
390 return (*this) * TFactor<T>(ns / _vs, (T)1);
391 }
392
393 /// Returns true if *this has NaN values
394 bool hasNaNs() const { return _p.hasNaNs(); }
395
396 /// Returns true if *this has negative values
397 bool hasNegatives() const { return _p.hasNegatives(); }
398
399 /// Returns total sum of values
400 T sum() const { return _p.sum(); }
401
402 /// Returns maximum absolute value
403 T maxAbs() const { return _p.maxAbs(); }
404
405 /// Returns maximum value
406 T max() const { return _p.max(); }
407
408 /// Returns minimum value
409 T min() const { return _p.min(); }
410
411 /// Returns entropy of *this TFactor
412 Real entropy() const { return _p.entropy(); }
413
414 /// Returns strength of *this TFactor (between variables i and j), as defined in eq. (52) of [\ref MoK07b]
415 T strength( const Var &i, const Var &j ) const;
416 };
417
418
419 template<typename T> TFactor<T> TFactor<T>::marginal(const VarSet & ns, bool normed) const {
420 VarSet res_ns = ns & _vs;
421
422 TFactor<T> res( res_ns, 0.0 );
423
424 IndexFor i_res( res_ns, _vs );
425 for( size_t i = 0; i < _p.size(); i++, ++i_res )
426 res._p[i_res] += _p[i];
427
428 if( normed )
429 res.normalize( Prob::NORMPROB );
430
431 return res;
432 }
433
434
435 template<typename T> TFactor<T> TFactor<T>::maxMarginal(const VarSet & ns, bool normed) const {
436 VarSet res_ns = ns & _vs;
437
438 TFactor<T> res( res_ns, 0.0 );
439
440 IndexFor i_res( res_ns, _vs );
441 for( size_t i = 0; i < _p.size(); i++, ++i_res )
442 if( _p[i] > res._p[i_res] )
443 res._p[i_res] = _p[i];
444
445 if( normed )
446 res.normalize( Prob::NORMPROB );
447
448 return res;
449 }
450
451
452 /// Apply binary operator pointwise on two factors
453 template<typename T, typename binaryOp> TFactor<T> pointwiseOp( const TFactor<T> &f, const TFactor<T> &g, binaryOp op ) {
454 if( f.vars() == g.vars() ) { // optimizate special case
455 TFactor<T> result(f);
456 for( size_t i = 0; i < result.states(); i++ )
457 result[i] = op( result[i], g[i] );
458 return result;
459 } else {
460 TFactor<T> result( f.vars() | g.vars(), 0.0 );
461
462 IndexFor i1(f.vars(), result.vars());
463 IndexFor i2(g.vars(), result.vars());
464
465 for( size_t i = 0; i < result.states(); i++, ++i1, ++i2 )
466 result[i] = op( f[i1], g[i2] );
467
468 return result;
469 }
470 }
471
472
473 template<typename T> T TFactor<T>::strength( const Var &i, const Var &j ) const {
474 DAI_DEBASSERT( _vs.contains( i ) );
475 DAI_DEBASSERT( _vs.contains( j ) );
476 DAI_DEBASSERT( i != j );
477 VarSet ij(i, j);
478
479 T max = 0.0;
480 for( size_t alpha1 = 0; alpha1 < i.states(); alpha1++ )
481 for( size_t alpha2 = 0; alpha2 < i.states(); alpha2++ )
482 if( alpha2 != alpha1 )
483 for( size_t beta1 = 0; beta1 < j.states(); beta1++ )
484 for( size_t beta2 = 0; beta2 < j.states(); beta2++ )
485 if( beta2 != beta1 ) {
486 size_t as = 1, bs = 1;
487 if( i < j )
488 bs = i.states();
489 else
490 as = j.states();
491 T f1 = slice( ij, alpha1 * as + beta1 * bs ).p().divide( slice( ij, alpha2 * as + beta1 * bs ).p() ).max();
492 T f2 = slice( ij, alpha2 * as + beta2 * bs ).p().divide( slice( ij, alpha1 * as + beta2 * bs ).p() ).max();
493 T f = f1 * f2;
494 if( f > max )
495 max = f;
496 }
497
498 return std::tanh( 0.25 * std::log( max ) );
499 }
500
501
502 /// Writes a TFactor to an output stream
503 /** \relates TFactor
504 */
505 template<typename T> std::ostream& operator<< (std::ostream& os, const TFactor<T>& P) {
506 os << "(" << P.vars() << ", (";
507 for( size_t i = 0; i < P.states(); i++ )
508 os << (i == 0 ? "" : ", ") << P[i];
509 os << "))";
510 return os;
511 }
512
513
514 /// Returns distance between two TFactors f and g, according to the distance measure dt
515 /** \relates TFactor
516 * \pre f.vars() == g.vars()
517 */
518 template<typename T> Real dist( const TFactor<T> &f, const TFactor<T> &g, Prob::DistType dt ) {
519 if( f.vars().empty() || g.vars().empty() )
520 return -1;
521 else {
522 DAI_DEBASSERT( f.vars() == g.vars() );
523 return dist( f.p(), g.p(), dt );
524 }
525 }
526
527
528 /// Returns the pointwise maximum of two TFactors
529 /** \relates TFactor
530 * \pre f.vars() == g.vars()
531 */
532 template<typename T> TFactor<T> max( const TFactor<T> &f, const TFactor<T> &g ) {
533 DAI_ASSERT( f._vs == g._vs );
534 return TFactor<T>( f._vs, min( f.p(), g.p() ) );
535 }
536
537
538 /// Returns the pointwise minimum of two TFactors
539 /** \relates TFactor
540 * \pre f.vars() == g.vars()
541 */
542 template<typename T> TFactor<T> min( const TFactor<T> &f, const TFactor<T> &g ) {
543 DAI_ASSERT( f._vs == g._vs );
544 return TFactor<T>( f._vs, max( f.p(), g.p() ) );
545 }
546
547
548 /// Calculates the mutual information between the two variables that f depends on, under the distribution given by f
549 /** \relates TFactor
550 * \pre f.vars().size() == 2
551 */
552 template<typename T> Real MutualInfo(const TFactor<T> &f) {
553 DAI_ASSERT( f.vars().size() == 2 );
554 VarSet::const_iterator it = f.vars().begin();
555 Var i = *it; it++; Var j = *it;
556 TFactor<T> projection = f.marginal(i) * f.marginal(j);
557 return dist( f.normalized(), projection, Prob::DISTKL );
558 }
559
560
561 /// Represents a factor with values of type Real.
562 typedef TFactor<Real> Factor;
563
564
565 } // end of namespace dai
566
567
568 #endif