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