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