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