1 /* This file is part of libDAI - http://www.libdai.org/
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
10 /// \brief Defines TFactor<> and Factor classes which represent factors in probability distributions.
13 #ifndef __defined_libdai_factor_h
14 #define __defined_libdai_factor_h
21 #include <dai/varset.h>
22 #include <dai/index.h>
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$.
37 * In libDAI, a factor is represented by a TFactor<T> object, which has two
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.
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().
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.
57 /// Stores the variables on which the factor depends
59 /// Stores the factor values
63 /// \name Constructors and destructors
65 /// Constructs factor depending on no variables with value \a p
66 TFactor ( T p
= 1 ) : _vs(), _p(1,p
) {}
68 /// Constructs factor depending on the variable \a v with uniform distribution
69 TFactor( const Var
&v
) : _vs(v
), _p(v
.states()) {}
71 /// Constructs factor depending on variables in \a vars with uniform distribution
72 TFactor( const VarSet
& vars
) : _vs(vars
), _p((size_t)_vs
.nrStates()) {
73 DAI_ASSERT( _vs
.nrStates() <= std::numeric_limits
<std::size_t>::max() );
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((size_t)_vs
.nrStates(),p
) {
78 DAI_ASSERT( _vs
.nrStates() <= std::numeric_limits
<std::size_t>::max() );
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.
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() );
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.
96 TFactor( const VarSet
& vars
, const T
* p
) : _vs(vars
), _p(p
, p
+ (size_t)_vs
.nrStates(), (size_t)_vs
.nrStates()) {
97 DAI_ASSERT( _vs
.nrStates() <= std::numeric_limits
<std::size_t>::max() );
100 /// Constructs factor depending on variables in \a vars, copying the values from \a p
101 TFactor( const VarSet
& vars
, const TProb
<T
> &p
) : _vs(vars
), _p(p
) {
102 DAI_ASSERT( _vs
.nrStates() == _p
.size() );
105 /// Constructs factor depending on variables in \a vars, permuting the values given in \a p accordingly
106 TFactor( const std::vector
<Var
> &vars
, const std::vector
<T
> &p
) : _vs(vars
.begin(), vars
.end(), vars
.size()), _p(p
.size()) {
108 for( size_t i
= 0; i
< vars
.size(); i
++ )
109 nrStates
*= vars
[i
].states();
110 DAI_ASSERT( nrStates
== p
.size() );
111 Permute
permindex(vars
);
112 for( size_t li
= 0; li
< p
.size(); ++li
)
113 _p
.set( permindex
.convertLinearIndex(li
), p
[li
] );
117 /// \name Get/set individual entries
119 /// Sets \a i 'th entry to \a val
120 void set( size_t i
, T val
) { _p
.set( i
, val
); }
122 /// Gets \a i 'th entry
123 T
get( size_t i
) const { return _p
[i
]; }
128 /// Returns constant reference to value vector
129 const TProb
<T
>& p() const { return _p
; }
131 /// Returns reference to value vector
132 TProb
<T
>& p() { return _p
; }
134 /// Returns a copy of the \a i 'th entry of the value vector
135 T
operator[] (size_t i
) const { return _p
[i
]; }
137 /// Returns constant reference to variable set (i.e., the variables on which the factor depends)
138 const VarSet
& vars() const { return _vs
; }
140 /// Returns reference to variable set (i.e., the variables on which the factor depends)
141 VarSet
& vars() { return _vs
; }
143 /// Returns the number of possible joint states of the variables on which the factor depends, \f$\prod_{l\in L} S_l\f$
144 /** \note This is equal to the length of the value vector.
146 size_t nrStates() const { return _p
.size(); }
148 /// Returns the Shannon entropy of \c *this, \f$-\sum_i p_i \log p_i\f$
149 T
entropy() const { return _p
.entropy(); }
151 /// Returns maximum of all values
152 T
max() const { return _p
.max(); }
154 /// Returns minimum of all values
155 T
min() const { return _p
.min(); }
157 /// Returns sum of all values
158 T
sum() const { return _p
.sum(); }
160 /// Returns sum of absolute values
161 T
sumAbs() const { return _p
.sumAbs(); }
163 /// Returns maximum absolute value of all values
164 T
maxAbs() const { return _p
.maxAbs(); }
166 /// Returns \c true if one or more values are NaN
167 bool hasNaNs() const { return _p
.hasNaNs(); }
169 /// Returns \c true if one or more values are negative
170 bool hasNegatives() const { return _p
.hasNegatives(); }
172 /// Returns strength of this factor (between variables \a i and \a j), as defined in eq. (52) of [\ref MoK07b]
173 T
strength( const Var
&i
, const Var
&j
) const;
176 bool operator==( const TFactor
<T
>& y
) const {
177 return (_vs
== y
._vs
) && (_p
== y
._p
);
181 /// \name Unary transformations
183 /// Returns negative of \c *this
184 TFactor
<T
> operator- () const {
185 // Note: the alternative (shorter) way of implementing this,
186 // return TFactor<T>( _vs, _p.abs() );
187 // is slower because it invokes the copy constructor of TProb<T>
194 /// Returns pointwise absolute value
195 TFactor
<T
> abs() const {
202 /// Returns pointwise exponent
203 TFactor
<T
> exp() const {
210 /// Returns pointwise logarithm
211 /** If \a zero == \c true, uses <tt>log(0)==0</tt>; otherwise, <tt>log(0)==-Inf</tt>.
213 TFactor
<T
> log(bool zero
=false) const {
220 /// Returns pointwise inverse
221 /** If \a zero == \c true, uses <tt>1/0==0</tt>; otherwise, <tt>1/0==Inf</tt>.
223 TFactor
<T
> inverse(bool zero
=true) const {
226 x
._p
= _p
.inverse(zero
);
230 /// Returns normalized copy of \c *this, using the specified norm
231 /** \throw NOT_NORMALIZABLE if the norm is zero
233 TFactor
<T
> normalized( ProbNormType norm
=NORMPROB
) const {
236 x
._p
= _p
.normalized( norm
);
241 /// \name Unary operations
243 /// Draws all values i.i.d. from a uniform distribution on [0,1)
244 TFactor
<T
>& randomize() { _p
.randomize(); return *this; }
246 /// Sets all values to \f$1/n\f$ where \a n is the number of states
247 TFactor
<T
>& setUniform() { _p
.setUniform(); return *this; }
249 /// Applies absolute value pointwise
250 TFactor
<T
>& takeAbs() { _p
.takeAbs(); return *this; }
252 /// Applies exponent pointwise
253 TFactor
<T
>& takeExp() { _p
.takeExp(); return *this; }
255 /// Applies logarithm pointwise
256 /** If \a zero == \c true, uses <tt>log(0)==0</tt>; otherwise, <tt>log(0)==-Inf</tt>.
258 TFactor
<T
>& takeLog( bool zero
= false ) { _p
.takeLog(zero
); return *this; }
260 /// Normalizes factor using the specified norm
261 /** \throw NOT_NORMALIZABLE if the norm is zero
263 T
normalize( ProbNormType norm
=NORMPROB
) { return _p
.normalize( norm
); }
266 /// \name Operations with scalars
268 /// Sets all values to \a x
269 TFactor
<T
>& fill (T x
) { _p
.fill( x
); return *this; }
271 /// Adds scalar \a x to each value
272 TFactor
<T
>& operator+= (T x
) { _p
+= x
; return *this; }
274 /// Subtracts scalar \a x from each value
275 TFactor
<T
>& operator-= (T x
) { _p
-= x
; return *this; }
277 /// Multiplies each value with scalar \a x
278 TFactor
<T
>& operator*= (T x
) { _p
*= x
; return *this; }
280 /// Divides each entry by scalar \a x
281 TFactor
<T
>& operator/= (T x
) { _p
/= x
; return *this; }
283 /// Raises values to the power \a x
284 TFactor
<T
>& operator^= (T x
) { _p
^= x
; return *this; }
287 /// \name Transformations with scalars
289 /// Returns sum of \c *this and scalar \a x
290 TFactor
<T
> operator+ (T x
) const {
291 // Note: the alternative (shorter) way of implementing this,
292 // TFactor<T> result(*this);
294 // is slower because it invokes the copy constructor of TFactor<T>
301 /// Returns difference of \c *this and scalar \a x
302 TFactor
<T
> operator- (T x
) const {
309 /// Returns product of \c *this with scalar \a x
310 TFactor
<T
> operator* (T x
) const {
317 /// Returns quotient of \c *this with scalar \a x
318 TFactor
<T
> operator/ (T x
) const {
325 /// Returns \c *this raised to the power \a x
326 TFactor
<T
> operator^ (T x
) const {
334 /// \name Operations with other factors
336 /// Applies binary operation \a op on two factors, \c *this and \a g
337 /** \tparam binOp Type of function object that accepts two arguments of type \a T and outputs a type \a T
338 * \param g Right operand
339 * \param op Operation of type \a binOp
341 template<typename binOp
> TFactor
<T
>& binaryOp( const TFactor
<T
> &g
, binOp op
) {
342 if( _vs
== g
._vs
) // optimize special case
343 _p
.pwBinaryOp( g
._p
, op
);
345 TFactor
<T
> f(*this); // make a copy
347 DAI_ASSERT( _vs
.nrStates() < std::numeric_limits
<std::size_t>::max() );
348 size_t N
= (size_t)_vs
.nrStates();
350 IndexFor
i_f( f
._vs
, _vs
);
351 IndexFor
i_g( g
._vs
, _vs
);
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
] ) );
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]
366 TFactor
<T
>& operator+= (const TFactor
<T
>& g
) { return binaryOp( g
, std::plus
<T
>() ); }
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]
373 TFactor
<T
>& operator-= (const TFactor
<T
>& g
) { return binaryOp( g
, std::minus
<T
>() ); }
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]
380 TFactor
<T
>& operator*= (const TFactor
<T
>& g
) { return binaryOp( g
, std::multiplies
<T
>() ); }
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]
387 TFactor
<T
>& operator/= (const TFactor
<T
>& g
) { return binaryOp( g
, fo_divides0
<T
>() ); }
390 /// \name Transformations with other factors
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
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.
401 if( _vs
== g
._vs
) { // optimize special case
403 result
._p
= _p
.pwBinaryTr( g
._p
, op
);
405 result
._vs
= _vs
| g
._vs
;
406 DAI_ASSERT( result
._vs
.nrStates() < std::numeric_limits
<std::size_t>::max() );
407 size_t N
= (size_t)result
._vs
.nrStates();
409 IndexFor
i_f( _vs
, result
.vars() );
410 IndexFor
i_g( g
._vs
, result
.vars() );
412 result
._p
.p().clear();
413 result
._p
.p().reserve( N
);
414 for( size_t i
= 0; i
< N
; i
++, ++i_f
, ++i_g
)
415 result
._p
.p().push_back( op( _p
[i_f
], g
[i_g
] ) );
420 /// Returns sum of \c *this and \a g
421 /** The sum of two factors is defined as follows: if
422 * \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
423 * \f[f+g : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) + g(x_M).\f]
425 TFactor
<T
> operator+ (const TFactor
<T
>& g
) const {
426 return binaryTr(g
,std::plus
<T
>());
429 /// Returns \c *this minus \a g
430 /** The difference of two factors is defined as follows: if
431 * \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
432 * \f[f-g : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) - g(x_M).\f]
434 TFactor
<T
> operator- (const TFactor
<T
>& g
) const {
435 return binaryTr(g
,std::minus
<T
>());
438 /// Returns product of \c *this with \a g
439 /** The product of two factors is defined as follows: if
440 * \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
441 * \f[fg : \prod_{l\in L\cup M} X_l \to [0,\infty) : x \mapsto f(x_L) g(x_M).\f]
443 TFactor
<T
> operator* (const TFactor
<T
>& g
) const {
444 return binaryTr(g
,std::multiplies
<T
>());
447 /// Returns quotient of \c *this by \a f (where division by zero yields zero)
448 /** The quotient of two factors is defined as follows: if
449 * \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
450 * \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]
452 TFactor
<T
> operator/ (const TFactor
<T
>& g
) const {
453 return binaryTr(g
,fo_divides0
<T
>());
457 /// \name Miscellaneous operations
459 /// Returns a slice of \c *this, where the subset \a vars is in state \a varsState
460 /** \pre \a vars sould be a subset of vars()
461 * \pre \a varsState < vars.nrStates()
463 * The result is a factor that depends on the variables of *this except those in \a vars,
464 * obtained by setting the variables in \a vars to the joint state specified by the linear index
465 * \a varsState. Formally, if \c *this corresponds with the factor \f$f : \prod_{l\in L} X_l \to [0,\infty)\f$,
466 * \f$M \subset L\f$ corresponds with \a vars and \a varsState corresponds with a mapping \f$s\f$ that
467 * 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
468 * returned corresponds with the factor \f$g : \prod_{l \in L \setminus M} X_l \to [0,\infty)\f$
469 * 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$.
471 TFactor
<T
> slice( const VarSet
& vars
, size_t varsState
) const;
473 /// Embeds this factor in a larger VarSet
474 /** \pre vars() should be a subset of \a vars
476 * If *this corresponds with \f$f : \prod_{l\in L} X_l \to [0,\infty)\f$ and \f$L \subset M\f$, then
477 * the embedded factor corresponds with \f$g : \prod_{m\in M} X_m \to [0,\infty) : x \mapsto f(x_L)\f$.
479 TFactor
<T
> embed(const VarSet
& vars
) const {
480 DAI_ASSERT( vars
>> _vs
);
484 return (*this) * TFactor
<T
>(vars
/ _vs
, (T
)1);
487 /// 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
488 TFactor
<T
> marginal(const VarSet
&vars
, bool normed
=true) const;
490 /// 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
491 TFactor
<T
> maxMarginal(const VarSet
&vars
, bool normed
=true) const;
496 template<typename T
> TFactor
<T
> TFactor
<T
>::slice( const VarSet
& vars
, size_t varsState
) const {
497 DAI_ASSERT( vars
<< _vs
);
498 VarSet varsrem
= _vs
/ vars
;
499 TFactor
<T
> result( varsrem
, T(0) );
502 IndexFor
i_vars (vars
, _vs
);
503 IndexFor
i_varsrem (varsrem
, _vs
);
504 for( size_t i
= 0; i
< nrStates(); i
++, ++i_vars
, ++i_varsrem
)
505 if( (size_t)i_vars
== varsState
)
506 result
.set( i_varsrem
, _p
[i
] );
512 template<typename T
> TFactor
<T
> TFactor
<T
>::marginal(const VarSet
&vars
, bool normed
) const {
513 VarSet res_vars
= vars
& _vs
;
515 TFactor
<T
> res( res_vars
, 0.0 );
517 IndexFor
i_res( res_vars
, _vs
);
518 for( size_t i
= 0; i
< _p
.size(); i
++, ++i_res
)
519 res
.set( i_res
, res
[i_res
] + _p
[i
] );
522 res
.normalize( NORMPROB
);
528 template<typename T
> TFactor
<T
> TFactor
<T
>::maxMarginal(const VarSet
&vars
, bool normed
) const {
529 VarSet res_vars
= vars
& _vs
;
531 TFactor
<T
> res( res_vars
, 0.0 );
533 IndexFor
i_res( res_vars
, _vs
);
534 for( size_t i
= 0; i
< _p
.size(); i
++, ++i_res
)
535 if( _p
[i
] > res
._p
[i_res
] )
536 res
.set( i_res
, _p
[i
] );
539 res
.normalize( NORMPROB
);
545 template<typename T
> T TFactor
<T
>::strength( const Var
&i
, const Var
&j
) const {
546 DAI_DEBASSERT( _vs
.contains( i
) );
547 DAI_DEBASSERT( _vs
.contains( j
) );
548 DAI_DEBASSERT( i
!= j
);
552 for( size_t alpha1
= 0; alpha1
< i
.states(); alpha1
++ )
553 for( size_t alpha2
= 0; alpha2
< i
.states(); alpha2
++ )
554 if( alpha2
!= alpha1
)
555 for( size_t beta1
= 0; beta1
< j
.states(); beta1
++ )
556 for( size_t beta2
= 0; beta2
< j
.states(); beta2
++ )
557 if( beta2
!= beta1
) {
558 size_t as
= 1, bs
= 1;
563 T f1
= slice( ij
, alpha1
* as
+ beta1
* bs
).p().divide( slice( ij
, alpha2
* as
+ beta1
* bs
).p() ).max();
564 T f2
= slice( ij
, alpha2
* as
+ beta2
* bs
).p().divide( slice( ij
, alpha1
* as
+ beta2
* bs
).p() ).max();
570 return std::tanh( 0.25 * std::log( max
) );
574 /// Writes a factor to an output stream
577 template<typename T
> std::ostream
& operator<< (std::ostream
& os
, const TFactor
<T
>& f
) {
578 os
<< "(" << f
.vars() << ", (";
579 for( size_t i
= 0; i
< f
.nrStates(); i
++ )
580 os
<< (i
== 0 ? "" : ", ") << f
[i
];
586 /// Returns distance between two factors \a f and \a g, according to the distance measure \a dt
588 * \pre f.vars() == g.vars()
590 template<typename T
> T
dist( const TFactor
<T
> &f
, const TFactor
<T
> &g
, ProbDistType dt
) {
591 if( f
.vars().empty() || g
.vars().empty() )
594 DAI_DEBASSERT( f
.vars() == g
.vars() );
595 return dist( f
.p(), g
.p(), dt
);
600 /// Returns the pointwise maximum of two factors
602 * \pre f.vars() == g.vars()
604 template<typename T
> TFactor
<T
> max( const TFactor
<T
> &f
, const TFactor
<T
> &g
) {
605 DAI_ASSERT( f
.vars() == g
.vars() );
606 return TFactor
<T
>( f
.vars(), max( f
.p(), g
.p() ) );
610 /// Returns the pointwise minimum of two factors
612 * \pre f.vars() == g.vars()
614 template<typename T
> TFactor
<T
> min( const TFactor
<T
> &f
, const TFactor
<T
> &g
) {
615 DAI_ASSERT( f
.vars() == g
.vars() );
616 return TFactor
<T
>( f
.vars(), min( f
.p(), g
.p() ) );
620 /// Calculates the mutual information between the two variables that \a f depends on, under the distribution given by \a f
622 * \pre f.vars().size() == 2
624 template<typename T
> T
MutualInfo(const TFactor
<T
> &f
) {
625 DAI_ASSERT( f
.vars().size() == 2 );
626 VarSet::const_iterator it
= f
.vars().begin();
627 Var i
= *it
; it
++; Var j
= *it
;
628 TFactor
<T
> projection
= f
.marginal(i
) * f
.marginal(j
);
629 return dist( f
.normalized(), projection
, DISTKL
);
633 /// Represents a factor with values of type dai::Real.
634 typedef TFactor
<Real
> Factor
;
637 /// Returns a binary unnormalized single-variable factor \f$ \exp(hx) \f$ where \f$ x = \pm 1 \f$
638 /** \param x Variable (should be binary)
639 * \param h Field strength
641 Factor
createFactorIsing( const Var
&x
, Real h
);
644 /// Returns a binary unnormalized pairwise factor \f$ \exp(J x_1 x_2) \f$ where \f$ x_1, x_2 = \pm 1 \f$
645 /** \param x1 First variable (should be binary)
646 * \param x2 Second variable (should be binary)
647 * \param J Coupling strength
649 Factor
createFactorIsing( const Var
&x1
, const Var
&x2
, Real J
);
652 /// Returns a random factor on the variables \a vs with strength \a beta
653 /** Each entry are set by drawing a normally distributed random with mean
654 * 0 and standard-deviation \a beta, and taking its exponent.
655 * \param vs Variables
656 * \param beta Factor strength (inverse temperature)
658 Factor
createFactorExpGauss( const VarSet
&vs
, Real beta
);
661 /// Returns a pairwise Potts factor \f$ \exp( J \delta_{x_1, x_2} ) \f$
662 /** \param x1 First variable
663 * \param x2 Second variable (should have the same number of states as \a x1)
664 * \param J Factor strength
666 Factor
createFactorPotts( const Var
&x1
, const Var
&x2
, Real J
);
669 /// Returns a Kronecker delta point mass
670 /** \param v Variable
671 * \param state The state of \a v that should get value 1
673 Factor
createFactorDelta( const Var
&v
, size_t state
);
676 /// Returns a Kronecker delta point mass
677 /** \param vs Set of variables
678 * \param state The state of \a vs that should get value 1
680 Factor
createFactorDelta( const VarSet
& vs
, size_t state
);
683 } // end of namespace dai