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