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