1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
5 Copyright (C) 2002 Martijn Leisink [martijn@mbfys.kun.nl]
6 Radboud University Nijmegen, The Netherlands
8 This file is part of libDAI.
10 libDAI is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 libDAI is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with libDAI; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 /// \brief Defines TFactor<T> and Factor classes
28 /// \todo Improve documentation
31 #ifndef __defined_libdai_factor_h
32 #define __defined_libdai_factor_h
38 #include <dai/varset.h>
39 #include <dai/index.h>
45 /// Represents a (probability) factor.
46 /** Mathematically, a \e factor is a function from the Cartesian product
47 * of the state spaces of some variables to the nonnegative real numbers.
48 * More formally, denoting a discrete variable with label \f$l\f$ by
49 * \f$x_l\f$ and its state space by \f$X_l = \{0,1,\dots,S_l-1\}\f$,
50 * then a factor depending on the variables \f$\{x_i\}_{i\in I}\f$ is
51 * a function \f$f_I : \prod_{i\in I} X_i \to [0,\infty)\f$.
53 * In libDAI, a factor is represented by a TFactor<\a T> object, which has two
55 * \arg a VarSet, corresponding with the set of variables \f$\{x_i\}_{i\in I}\f$
56 * that the factor depends on;
57 * \arg a TProb<\a T>, a vector containing the values of the factor for each possible
58 * joint state of the variables.
60 * The factor values are stored in the entries of the TProb<\a T> in a particular
61 * ordering, which is defined by the one-to-one correspondence of a joint state
62 * in \f$\prod_{i\in I} X_i\f$ with a linear index in
63 * \f$\{0,1,\dots,\prod_{i\in I} S_i-1\}\f$ according to the mapping \f$\sigma\f$
64 * induced by VarSet::calcState(const std::map<Var,size_t> &).
66 * \tparam T Should be a scalar that is castable from and to double and should support elementary arithmetic operations.
68 template <typename T
> class TFactor
{
74 /// Construct TFactor depending on no variables, with value p
75 TFactor ( Real p
= 1.0 ) : _vs(), _p(1,p
) {}
77 /// Construct TFactor depending on variables in ns, with uniform distribution
78 TFactor( const VarSet
& ns
) : _vs(ns
), _p(_vs
.nrStates()) {}
80 /// Construct TFactor depending on variables in ns, with all values set to p
81 TFactor( const VarSet
& ns
, Real p
) : _vs(ns
), _p(_vs
.nrStates(),p
) {}
83 /// Construct TFactor depending on variables in ns, copying the values from the array p
84 TFactor( const VarSet
& ns
, const Real
*p
) : _vs(ns
), _p(_vs
.nrStates(),p
) {}
86 /// Construct TFactor depending on variables in ns, with values set to the TProb p
87 TFactor( const VarSet
& ns
, const TProb
<T
>& p
) : _vs(ns
), _p(p
) {
89 assert( _vs
.nrStates() == _p
.size() );
93 /// Construct TFactor depending on the variable n, with uniform distribution
94 TFactor( const Var
& n
) : _vs(n
), _p(n
.states()) {}
97 TFactor( const TFactor
<T
> &x
) : _vs(x
._vs
), _p(x
._p
) {}
99 /// Assignment operator
100 TFactor
<T
> & operator= (const TFactor
<T
> &x
) {
108 /// Returns const reference to value vector
109 const TProb
<T
> & p() const { return _p
; }
110 /// Returns reference to value vector
111 TProb
<T
> & p() { return _p
; }
113 /// Returns const reference to variable set
114 const VarSet
& vars() const { return _vs
; }
116 /// Returns the number of possible joint states of the variables
117 size_t states() const { return _p
.size(); }
119 /// Returns a copy of the i'th entry of the value vector
120 T
operator[] (size_t i
) const { return _p
[i
]; }
122 /// Returns a reference to the i'th entry of the value vector
123 T
& operator[] (size_t i
) { return _p
[i
]; }
125 /// Sets all values to p
126 TFactor
<T
> & fill (T p
) { _p
.fill( p
); return(*this); }
128 /// Draws all values i.i.d. from a uniform distribution on [0,1)
129 TFactor
<T
> & randomize () { _p
.randomize(); return(*this); }
131 /// Returns product of *this with scalar t
132 TFactor
<T
> operator* (T t
) const {
133 TFactor
<T
> result
= *this;
138 /// Multiplies with scalar t
139 TFactor
<T
>& operator*= (T t
) {
144 /// Returns quotient of *this with scalar t
145 TFactor
<T
> operator/ (T t
) const {
146 TFactor
<T
> result
= *this;
151 /// Divides by scalar t
152 TFactor
<T
>& operator/= (T t
) {
157 /// Adds scalar t to *this
158 TFactor
<T
>& operator+= (T t
) {
163 /// Subtracts scalar t from *this
164 TFactor
<T
>& operator-= (T t
) {
169 /// Returns sum of *this and scalar t
170 TFactor
<T
> operator+ (T t
) const {
171 TFactor
<T
> result(*this);
176 /// Returns *this minus scalar t
177 TFactor
<T
> operator- (T t
) const {
178 TFactor
<T
> result(*this);
183 /// Returns product of *this with another TFactor f
184 /** The result is a TFactor depending on the union of the variables
185 * on which *this and f depend.
187 TFactor
<T
> operator* (const TFactor
<T
>& f
) const;
189 /// Returns quotient of *this by another TFactor f
190 /** The result is a TFactor depending on the union of the variables
191 * on which *this and f depend.
193 TFactor
<T
> operator/ (const TFactor
<T
>& f
) const;
195 /// Multiplies *this with another TFactor f
196 /** The result is a TFactor depending on the union of the variables
197 * on which *this and f depend.
199 TFactor
<T
>& operator*= (const TFactor
<T
>& f
) { return( *this = (*this * f
) ); }
201 /// Divides *this by another TFactor f
202 /** The result is a TFactor depending on the union of the variables
203 * on which *this and f depend.
205 TFactor
<T
>& operator/= (const TFactor
<T
>& f
) { return( *this = (*this / f
) ); }
207 /// Returns sum of *this and another TFactor f
208 /** \pre this->vars() == f.vars()
210 TFactor
<T
> operator+ (const TFactor
<T
>& f
) const {
212 assert( f
._vs
== _vs
);
214 TFactor
<T
> sum(*this);
219 /// Returns *this minus another TFactor f
220 /** \pre this->vars() == f.vars()
222 TFactor
<T
> operator- (const TFactor
<T
>& f
) const {
224 assert( f
._vs
== _vs
);
226 TFactor
<T
> sum(*this);
231 /// Adds another TFactor f to *this
232 /** \pre this->vars() == f.vars()
234 TFactor
<T
>& operator+= (const TFactor
<T
>& f
) {
236 assert( f
._vs
== _vs
);
242 /// Subtracts another TFactor f from *this
243 /** \pre this->vars() == f.vars()
245 TFactor
<T
>& operator-= (const TFactor
<T
>& f
) {
247 assert( f
._vs
== _vs
);
253 /// Returns *this raised to the power a
254 TFactor
<T
> operator^ (Real a
) const {
261 /// Raises *this to the power a
262 TFactor
<T
>& operator^= (Real a
) { _p
^= a
; return *this; }
264 /// Sets all values that are smaller than epsilon to 0
265 TFactor
<T
>& makeZero( T epsilon
) {
266 _p
.makeZero( epsilon
);
270 /// Sets all values that are smaller than epsilon to epsilon
271 TFactor
<T
>& makePositive( T epsilon
) {
272 _p
.makePositive( epsilon
);
276 /// Returns pointwise inverse of *this.
277 /** If zero == true, uses 1 / 0 == 0; otherwise 1 / 0 == Inf.
279 TFactor
<T
> inverse(bool zero
=true) const {
282 inv
._p
= _p
.inverse(zero
);
286 /// Returns *this divided pointwise by another TFactor f
287 /** \pre this->vars() == f.vars()
289 TFactor
<T
> divided_by( const TFactor
<T
>& f
) const {
291 assert( f
._vs
== _vs
);
293 TFactor
<T
> quot(*this);
298 /// Divides *this pointwise by another TFactor f
299 /** \pre this->vars() == f.vars()
301 TFactor
<T
>& divide( const TFactor
<T
>& f
) {
303 assert( f
._vs
== _vs
);
309 /// Returns pointwise exp of *this
310 TFactor
<T
> exp() const {
317 /// Returns pointwise absolute value of *this
318 TFactor
<T
> abs() const {
325 /// Returns pointwise logarithm of *this
326 /** If zero==true, uses log(0)==0; otherwise, log(0)=-Inf.
328 TFactor
<T
> log(bool zero
=false) const {
335 /// Normalizes *this TFactor according to the specified norm
336 T
normalize( typename
Prob::NormType norm
=Prob::NORMPROB
) { return _p
.normalize( norm
); }
338 /// Returns a normalized copy of *this, according to the specified norm
339 TFactor
<T
> normalized( typename
Prob::NormType norm
=Prob::NORMPROB
) const {
342 result
._p
= _p
.normalized( norm
);
346 /// Returns a slice of this TFactor, where the subset ns is in state nsState
347 /** \pre ns sould be a subset of vars()
348 * \pre nsState < ns.states()
350 TFactor
<T
> slice( const VarSet
& ns
, size_t nsState
) const {
352 VarSet nsrem
= _vs
/ ns
;
353 TFactor
<T
> result( nsrem
, T(0) );
356 IndexFor
i_ns (ns
, _vs
);
357 IndexFor
i_nsrem (nsrem
, _vs
);
358 for( size_t i
= 0; i
< states(); i
++, ++i_ns
, ++i_nsrem
)
359 if( (size_t)i_ns
== nsState
)
360 result
._p
[i_nsrem
] = _p
[i
];
365 /// Returns unnormalized marginal obtained by summing out all variables except those in ns
366 TFactor
<T
> partSum(const VarSet
&ns
) const;
368 /// Returns (normalized by default) marginal on ns, obtained by summing out all variables except those in ns
369 /** If normed==true, the result is normalized.
371 TFactor
<T
> marginal(const VarSet
& ns
, bool normed
=true) const {
373 return partSum(ns
).normalized();
378 /// Embeds this factor in a larger VarSet
379 /** \pre vars() should be a subset of ns
381 TFactor
<T
> embed(const VarSet
& ns
) const {
386 return (*this) * TFactor
<T
>(ns
/ _vs
, 1);
389 /// Returns true if *this has NaN values
390 bool hasNaNs() const { return _p
.hasNaNs(); }
392 /// Returns true if *this has negative values
393 bool hasNegatives() const { return _p
.hasNegatives(); }
395 /// Returns total sum of values
396 T
totalSum() const { return _p
.totalSum(); }
398 /// Returns maximum absolute value
399 T
maxAbs() const { return _p
.maxAbs(); }
401 /// Returns maximum value
402 T
maxVal() const { return _p
.maxVal(); }
404 /// Returns minimum value
405 T
minVal() const { return _p
.minVal(); }
407 /// Returns entropy of *this
408 Real
entropy() const { return _p
.entropy(); }
410 /// Returns strength of *this, between variables i and j, as defined in eq. (52) of [\ref MoK07b]
411 T
strength( const Var
&i
, const Var
&j
) const;
415 template<typename T
> TFactor
<T
> TFactor
<T
>::partSum(const VarSet
& ns
) const {
416 VarSet res_ns
= ns
& _vs
;
418 TFactor
<T
> res( res_ns
, 0.0 );
420 IndexFor
i_res( res_ns
, _vs
);
421 for( size_t i
= 0; i
< _p
.size(); i
++, ++i_res
)
422 res
._p
[i_res
] += _p
[i
];
428 template<typename T
> TFactor
<T
> TFactor
<T
>::operator* (const TFactor
<T
>& Q
) const {
429 TFactor
<T
> prod( _vs
| Q
._vs
, 0.0 );
431 IndexFor
i1(_vs
, prod
._vs
);
432 IndexFor
i2(Q
._vs
, prod
._vs
);
434 for( size_t i
= 0; i
< prod
._p
.size(); i
++, ++i1
, ++i2
)
435 prod
._p
[i
] += _p
[i1
] * Q
._p
[i2
];
441 template<typename T
> TFactor
<T
> TFactor
<T
>::operator/ (const TFactor
<T
>& Q
) const {
442 TFactor
<T
> quot( _vs
| Q
._vs
, 0.0 );
444 IndexFor
i1(_vs
, quot
._vs
);
445 IndexFor
i2(Q
._vs
, quot
._vs
);
447 for( size_t i
= 0; i
< quot
._p
.size(); i
++, ++i1
, ++i2
)
448 quot
._p
[i
] += _p
[i1
] / Q
._p
[i2
];
454 template<typename T
> T TFactor
<T
>::strength( const Var
&i
, const Var
&j
) const {
456 assert( _vs
.contains( i
) );
457 assert( _vs
.contains( j
) );
463 for( size_t alpha1
= 0; alpha1
< i
.states(); alpha1
++ )
464 for( size_t alpha2
= 0; alpha2
< i
.states(); alpha2
++ )
465 if( alpha2
!= alpha1
)
466 for( size_t beta1
= 0; beta1
< j
.states(); beta1
++ )
467 for( size_t beta2
= 0; beta2
< j
.states(); beta2
++ )
468 if( beta2
!= beta1
) {
469 size_t as
= 1, bs
= 1;
474 T f1
= slice( ij
, alpha1
* as
+ beta1
* bs
).p().divide( slice( ij
, alpha2
* as
+ beta1
* bs
).p() ).maxVal();
475 T f2
= slice( ij
, alpha2
* as
+ beta2
* bs
).p().divide( slice( ij
, alpha1
* as
+ beta2
* bs
).p() ).maxVal();
481 return std::tanh( 0.25 * std::log( max
) );
485 /// Writes a TFactor to an output stream
488 template<typename T
> std::ostream
& operator<< (std::ostream
& os
, const TFactor
<T
>& P
) {
489 os
<< "(" << P
.vars() << " <";
490 for( size_t i
= 0; i
< P
.states(); i
++ )
497 /// Returns distance between two TFactors f and g, according to the distance measure dt
499 * \pre f.vars() == g.vars()
501 template<typename T
> Real
dist( const TFactor
<T
> &f
, const TFactor
<T
> &g
, Prob::DistType dt
) {
502 if( f
.vars().empty() || g
.vars().empty() )
506 assert( f
.vars() == g
.vars() );
508 return dist( f
.p(), g
.p(), dt
);
513 /// Returns the pointwise maximum of two TFactors
515 * \pre f.vars() == g.vars()
517 template<typename T
> TFactor
<T
> max( const TFactor
<T
> &f
, const TFactor
<T
> &g
) {
518 assert( f
._vs
== g
._vs
);
519 return TFactor
<T
>( f
._vs
, min( f
.p(), g
.p() ) );
523 /// Returns the pointwise minimum of two TFactors
525 * \pre f.vars() == g.vars()
527 template<typename T
> TFactor
<T
> min( const TFactor
<T
> &f
, const TFactor
<T
> &g
) {
528 assert( f
._vs
== g
._vs
);
529 return TFactor
<T
>( f
._vs
, max( f
.p(), g
.p() ) );
533 /// Calculates the mutual information between the two variables that f depends on, under the distribution given by f
535 * \pre f.vars().size() == 2
537 template<typename T
> Real
MutualInfo(const TFactor
<T
> &f
) {
538 assert( f
.vars().size() == 2 );
539 VarSet::const_iterator it
= f
.vars().begin();
540 Var i
= *it
; it
++; Var j
= *it
;
541 TFactor
<T
> projection
= f
.marginal(i
) * f
.marginal(j
);
542 return real( dist( f
.normalized(), projection
, Prob::DISTKL
) );
546 /// Represents a factor with values of type Real.
547 typedef TFactor
<Real
> Factor
;
550 } // end of namespace dai