Replaced VarSet::calcState(),VarSet::calcStates() by non-members calcLinearState...
[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 // OBSOLETE
227 /// Sets values that are smaller (in absolute value) than \a epsilon to 0
228 /** \deprecated Functionality was not widely used
229 */
230 TFactor<T>& makeZero( T epsilon ) { _p.makeZero( epsilon ); return *this; }
231
232 // OBSOLETE
233 /// Sets values that are smaller than \a epsilon to \a epsilon
234 /** \deprecated Functionality was not widely used
235 */
236 TFactor<T>& makePositive( T epsilon ) { _p.makePositive( epsilon ); return *this; }
237
238 /// Adds scalar \a x to each value
239 TFactor<T>& operator+= (T x) { _p += x; return *this; }
240
241 /// Subtracts scalar \a x from each value
242 TFactor<T>& operator-= (T x) { _p -= x; return *this; }
243
244 /// Multiplies each value with scalar \a x
245 TFactor<T>& operator*= (T x) { _p *= x; return *this; }
246
247 /// Divides each entry by scalar \a x
248 TFactor<T>& operator/= (T x) { _p /= x; return *this; }
249
250 /// Raises values to the power \a x
251 TFactor<T>& operator^= (T x) { _p ^= x; return *this; }
252 //@}
253
254 /// \name Transformations with scalars
255 //@{
256 /// Returns sum of \c *this and scalar \a x
257 TFactor<T> operator+ (T x) const {
258 // Note: the alternative (shorter) way of implementing this,
259 // TFactor<T> result(*this);
260 // result._p += x;
261 // is slower because it invokes the copy constructor of TFactor<T>
262 TFactor<T> result;
263 result._vs = _vs;
264 result._p = p() + x;
265 return result;
266 }
267
268 /// Returns difference of \c *this and scalar \a x
269 TFactor<T> operator- (T x) const {
270 TFactor<T> result;
271 result._vs = _vs;
272 result._p = p() - x;
273 return result;
274 }
275
276 /// Returns product of \c *this with scalar \a x
277 TFactor<T> operator* (T x) const {
278 TFactor<T> result;
279 result._vs = _vs;
280 result._p = p() * x;
281 return result;
282 }
283
284 /// Returns quotient of \c *this with scalar \a x
285 TFactor<T> operator/ (T x) const {
286 TFactor<T> result;
287 result._vs = _vs;
288 result._p = p() / x;
289 return result;
290 }
291
292 /// Returns \c *this raised to the power \a x
293 TFactor<T> operator^ (T x) const {
294 TFactor<T> result;
295 result._vs = _vs;
296 result._p = p() ^ x;
297 return result;
298 }
299 //@}
300
301 /// \name Operations with other factors
302 //@{
303 /// Applies binary operation \a op on two factors, \c *this and \a g
304 /** \tparam binOp Type of function object that accepts two arguments of type \a T and outputs a type \a T
305 * \param g Right operand
306 * \param op Operation of type \a binOp
307 */
308 template<typename binOp> TFactor<T>& binaryOp( const TFactor<T> &g, binOp op ) {
309 if( _vs == g._vs ) // optimize special case
310 _p.pwBinaryOp( g._p, op );
311 else {
312 TFactor<T> f(*this); // make a copy
313 _vs |= g._vs;
314 size_t N = _vs.nrStates();
315
316 IndexFor i_f( f._vs, _vs );
317 IndexFor i_g( g._vs, _vs );
318
319 _p.p().clear();
320 _p.p().reserve( N );
321 for( size_t i = 0; i < N; i++, ++i_f, ++i_g )
322 _p.p().push_back( op( f._p[i_f], g._p[i_g] ) );
323 }
324 return *this;
325 }
326
327 /// Adds \a g to \c *this
328 /** The sum of two factors is defined as follows: if
329 * \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
330 * \f[f+g : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) + g(x_M).\f]
331 */
332 TFactor<T>& operator+= (const TFactor<T>& g) { return binaryOp( g, std::plus<T>() ); }
333
334 /// Subtracts \a g from \c *this
335 /** The difference of two factors is defined as follows: if
336 * \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
337 * \f[f-g : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) - g(x_M).\f]
338 */
339 TFactor<T>& operator-= (const TFactor<T>& g) { return binaryOp( g, std::minus<T>() ); }
340
341 /// Multiplies \c *this with \a g
342 /** The product of two factors is defined as follows: if
343 * \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
344 * \f[fg : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) g(x_M).\f]
345 */
346 TFactor<T>& operator*= (const TFactor<T>& g) { return binaryOp( g, std::multiplies<T>() ); }
347
348 /// Divides \c *this by \a g (where division by zero yields zero)
349 /** The quotient of two factors is defined as follows: if
350 * \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
351 * \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]
352 */
353 TFactor<T>& operator/= (const TFactor<T>& g) { return binaryOp( g, fo_divides0<T>() ); }
354 //@}
355
356 /// \name Transformations with other factors
357 //@{
358 /// Returns result of applying binary operation \a op on two factors, \c *this and \a g
359 /** \tparam binOp Type of function object that accepts two arguments of type \a T and outputs a type \a T
360 * \param g Right operand
361 * \param op Operation of type \a binOp
362 */
363 template<typename binOp> TFactor<T> binaryTr( const TFactor<T> &g, binOp op ) const {
364 // Note that to prevent a copy to be made, it is crucial
365 // that the result is declared outside the if-else construct.
366 TFactor<T> result;
367 if( _vs == g._vs ) { // optimize special case
368 result._vs = _vs;
369 result._p = _p.pwBinaryTr( g._p, op );
370 } else {
371 result._vs = _vs | g._vs;
372 size_t N = result._vs.nrStates();
373
374 IndexFor i_f( _vs, result.vars() );
375 IndexFor i_g( g._vs, result.vars() );
376
377 result._p.p().clear();
378 result._p.p().reserve( N );
379 for( size_t i = 0; i < N; i++, ++i_f, ++i_g )
380 result._p.p().push_back( op( _p[i_f], g._p[i_g] ) );
381 }
382 return result;
383 }
384
385 /// Returns sum of \c *this and \a g
386 /** The sum of two factors is defined as follows: if
387 * \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
388 * \f[f+g : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) + g(x_M).\f]
389 */
390 TFactor<T> operator+ (const TFactor<T>& g) const {
391 return binaryTr(g,std::plus<T>());
392 }
393
394 /// Returns \c *this minus \a g
395 /** The difference of two factors is defined as follows: if
396 * \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
397 * \f[f-g : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) - g(x_M).\f]
398 */
399 TFactor<T> operator- (const TFactor<T>& g) const {
400 return binaryTr(g,std::minus<T>());
401 }
402
403 /// Returns product of \c *this with \a g
404 /** The product of two factors is defined as follows: if
405 * \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
406 * \f[fg : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) g(x_M).\f]
407 */
408 TFactor<T> operator* (const TFactor<T>& g) const {
409 return binaryTr(g,std::multiplies<T>());
410 }
411
412 /// Returns quotient of \c *this by \a f (where division by zero yields zero)
413 /** The quotient of two factors is defined as follows: if
414 * \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
415 * \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]
416 */
417 TFactor<T> operator/ (const TFactor<T>& g) const {
418 return binaryTr(g,fo_divides0<T>());
419 }
420 //@}
421
422 /// \name Miscellaneous operations
423 //@{
424 /// Returns a slice of \c *this, where the subset \a vars is in state \a varsState
425 /** \pre \a vars sould be a subset of vars()
426 * \pre \a varsState < vars.states()
427 *
428 * The result is a factor that depends on the variables of *this except those in \a vars,
429 * obtained by setting the variables in \a vars to the joint state specified by the linear index
430 * \a varsState. Formally, if \c *this corresponds with the factor \f$f : \prod_{l\in L} X_l \to [0,\infty)\f$,
431 * \f$M \subset L\f$ corresponds with \a vars and \a varsState corresponds with a mapping \f$s\f$ that
432 * 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
433 * returned corresponds with the factor \f$g : \prod_{l \in L \setminus M} X_l \to [0,\infty)\f$
434 * 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$.
435 */
436 TFactor<T> slice( const VarSet& vars, size_t varsState ) const;
437
438 /// Embeds this factor in a larger VarSet
439 /** \pre vars() should be a subset of \a vars
440 *
441 * If *this corresponds with \f$f : \prod_{l\in L} X_l \to [0,\infty)\f$ and \f$L \subset M\f$, then
442 * the embedded factor corresponds with \f$g : \prod_{m\in M} X_m \to [0,\infty) : x \mapsto f(x_L)\f$.
443 */
444 TFactor<T> embed(const VarSet & vars) const {
445 DAI_ASSERT( vars >> _vs );
446 if( _vs == vars )
447 return *this;
448 else
449 return (*this) * TFactor<T>(vars / _vs, (T)1);
450 }
451
452 /// 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
453 TFactor<T> marginal(const VarSet &vars, bool normed=true) const;
454
455 /// 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
456 TFactor<T> maxMarginal(const VarSet &vars, bool normed=true) const;
457 //@}
458 };
459
460
461 template<typename T> TFactor<T> TFactor<T>::slice( const VarSet& vars, size_t varsState ) const {
462 DAI_ASSERT( vars << _vs );
463 VarSet varsrem = _vs / vars;
464 TFactor<T> result( varsrem, T(0) );
465
466 // OPTIMIZE ME
467 IndexFor i_vars (vars, _vs);
468 IndexFor i_varsrem (varsrem, _vs);
469 for( size_t i = 0; i < states(); i++, ++i_vars, ++i_varsrem )
470 if( (size_t)i_vars == varsState )
471 result._p[i_varsrem] = _p[i];
472
473 return result;
474 }
475
476
477 template<typename T> TFactor<T> TFactor<T>::marginal(const VarSet &vars, bool normed) const {
478 VarSet res_vars = vars & _vs;
479
480 TFactor<T> res( res_vars, 0.0 );
481
482 IndexFor i_res( res_vars, _vs );
483 for( size_t i = 0; i < _p.size(); i++, ++i_res )
484 res._p[i_res] += _p[i];
485
486 if( normed )
487 res.normalize( TProb<T>::NORMPROB );
488
489 return res;
490 }
491
492
493 template<typename T> TFactor<T> TFactor<T>::maxMarginal(const VarSet &vars, bool normed) const {
494 VarSet res_vars = vars & _vs;
495
496 TFactor<T> res( res_vars, 0.0 );
497
498 IndexFor i_res( res_vars, _vs );
499 for( size_t i = 0; i < _p.size(); i++, ++i_res )
500 if( _p[i] > res._p[i_res] )
501 res._p[i_res] = _p[i];
502
503 if( normed )
504 res.normalize( TProb<T>::NORMPROB );
505
506 return res;
507 }
508
509
510 template<typename T> T TFactor<T>::strength( const Var &i, const Var &j ) const {
511 DAI_DEBASSERT( _vs.contains( i ) );
512 DAI_DEBASSERT( _vs.contains( j ) );
513 DAI_DEBASSERT( i != j );
514 VarSet ij(i, j);
515
516 T max = 0.0;
517 for( size_t alpha1 = 0; alpha1 < i.states(); alpha1++ )
518 for( size_t alpha2 = 0; alpha2 < i.states(); alpha2++ )
519 if( alpha2 != alpha1 )
520 for( size_t beta1 = 0; beta1 < j.states(); beta1++ )
521 for( size_t beta2 = 0; beta2 < j.states(); beta2++ )
522 if( beta2 != beta1 ) {
523 size_t as = 1, bs = 1;
524 if( i < j )
525 bs = i.states();
526 else
527 as = j.states();
528 T f1 = slice( ij, alpha1 * as + beta1 * bs ).p().divide( slice( ij, alpha2 * as + beta1 * bs ).p() ).max();
529 T f2 = slice( ij, alpha2 * as + beta2 * bs ).p().divide( slice( ij, alpha1 * as + beta2 * bs ).p() ).max();
530 T f = f1 * f2;
531 if( f > max )
532 max = f;
533 }
534
535 return std::tanh( 0.25 * std::log( max ) );
536 }
537
538
539 /// Writes a factor to an output stream
540 /** \relates TFactor
541 */
542 template<typename T> std::ostream& operator<< (std::ostream& os, const TFactor<T>& f) {
543 os << "(" << f.vars() << ", (";
544 for( size_t i = 0; i < f.states(); i++ )
545 os << (i == 0 ? "" : ", ") << f[i];
546 os << "))";
547 return os;
548 }
549
550
551 /// Returns distance between two factors \a f and \a g, according to the distance measure \a dt
552 /** \relates TFactor
553 * \pre f.vars() == g.vars()
554 */
555 template<typename T> T dist( const TFactor<T> &f, const TFactor<T> &g, typename TProb<T>::DistType dt ) {
556 if( f.vars().empty() || g.vars().empty() )
557 return -1;
558 else {
559 DAI_DEBASSERT( f.vars() == g.vars() );
560 return dist( f.p(), g.p(), dt );
561 }
562 }
563
564
565 /// Returns the pointwise maximum of two factors
566 /** \relates TFactor
567 * \pre f.vars() == g.vars()
568 */
569 template<typename T> TFactor<T> max( const TFactor<T> &f, const TFactor<T> &g ) {
570 DAI_ASSERT( f._vs == g._vs );
571 return TFactor<T>( f._vs, max( f.p(), g.p() ) );
572 }
573
574
575 /// Returns the pointwise minimum of two factors
576 /** \relates TFactor
577 * \pre f.vars() == g.vars()
578 */
579 template<typename T> TFactor<T> min( const TFactor<T> &f, const TFactor<T> &g ) {
580 DAI_ASSERT( f._vs == g._vs );
581 return TFactor<T>( f._vs, min( f.p(), g.p() ) );
582 }
583
584
585 /// Calculates the mutual information between the two variables that \a f depends on, under the distribution given by \a f
586 /** \relates TFactor
587 * \pre f.vars().size() == 2
588 */
589 template<typename T> T MutualInfo(const TFactor<T> &f) {
590 DAI_ASSERT( f.vars().size() == 2 );
591 VarSet::const_iterator it = f.vars().begin();
592 Var i = *it; it++; Var j = *it;
593 TFactor<T> projection = f.marginal(i) * f.marginal(j);
594 return dist( f.normalized(), projection, TProb<T>::DISTKL );
595 }
596
597
598 /// Represents a factor with values of type dai::Real.
599 typedef TFactor<Real> Factor;
600
601
602 } // end of namespace dai
603
604
605 #endif