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