30871ec482df99c256710b70727a9975e5f19c4f
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
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
23 */
26 /// \file
27 /// \brief Defines TFactor<T> and Factor classes
28 /// \todo Improve documentation
31 #ifndef __defined_libdai_factor_h
32 #define __defined_libdai_factor_h
35 #include <iostream>
36 #include <cmath>
37 #include <dai/prob.h>
38 #include <dai/varset.h>
39 #include <dai/index.h>
42 namespace dai {
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$.
52 *
53 * In libDAI, a factor is represented by a TFactor<\a T> object, which has two
54 * components:
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.
59 *
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> &).
65 *
66 * \tparam T Should be a scalar that is castable from and to double and should support elementary arithmetic operations.
67 */
68 template <typename T> class TFactor {
69 private:
70 VarSet _vs;
71 TProb<T> _p;
73 public:
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) {
88 #ifdef DAI_DEBUG
89 assert( _vs.nrStates() == _p.size() );
90 #endif
91 }
93 /// Construct TFactor depending on the variable n, with uniform distribution
94 TFactor( const Var& n ) : _vs(n), _p(n.states()) {}
96 /// Copy constructor
97 TFactor( const TFactor<T> &x ) : _vs(x._vs), _p(x._p) {}
99 /// Assignment operator
100 TFactor<T> & operator= (const TFactor<T> &x) {
101 if( this != &x ) {
102 _vs = x._vs;
103 _p = x._p;
104 }
105 return *this;
106 }
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;
134 result.p() *= t;
135 return result;
136 }
138 /// Multiplies with scalar t
139 TFactor<T>& operator*= (T t) {
140 _p *= t;
141 return *this;
142 }
144 /// Returns quotient of *this with scalar t
145 TFactor<T> operator/ (T t) const {
146 TFactor<T> result = *this;
147 result.p() /= t;
148 return result;
149 }
151 /// Divides by scalar t
152 TFactor<T>& operator/= (T t) {
153 _p /= t;
154 return *this;
155 }
157 /// Adds scalar t to *this
158 TFactor<T>& operator+= (T t) {
159 _p += t;
160 return *this;
161 }
163 /// Subtracts scalar t from *this
164 TFactor<T>& operator-= (T t) {
165 _p -= t;
166 return *this;
167 }
169 /// Returns sum of *this and scalar t
170 TFactor<T> operator+ (T t) const {
171 TFactor<T> result(*this);
172 result._p += t;
173 return result;
174 }
176 /// Returns *this minus scalar t
177 TFactor<T> operator- (T t) const {
178 TFactor<T> result(*this);
179 result._p -= t;
180 return result;
181 }
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.
186 */
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.
192 */
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.
198 */
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.
204 */
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()
209 */
210 TFactor<T> operator+ (const TFactor<T>& f) const {
211 #ifdef DAI_DEBUG
212 assert( f._vs == _vs );
213 #endif
214 TFactor<T> sum(*this);
215 sum._p += f._p;
216 return sum;
217 }
219 /// Returns *this minus another TFactor f
220 /** \pre this->vars() == f.vars()
221 */
222 TFactor<T> operator- (const TFactor<T>& f) const {
223 #ifdef DAI_DEBUG
224 assert( f._vs == _vs );
225 #endif
226 TFactor<T> sum(*this);
227 sum._p -= f._p;
228 return sum;
229 }
231 /// Adds another TFactor f to *this
232 /** \pre this->vars() == f.vars()
233 */
234 TFactor<T>& operator+= (const TFactor<T>& f) {
235 #ifdef DAI_DEBUG
236 assert( f._vs == _vs );
237 #endif
238 _p += f._p;
239 return *this;
240 }
242 /// Subtracts another TFactor f from *this
243 /** \pre this->vars() == f.vars()
244 */
245 TFactor<T>& operator-= (const TFactor<T>& f) {
246 #ifdef DAI_DEBUG
247 assert( f._vs == _vs );
248 #endif
249 _p -= f._p;
250 return *this;
251 }
253 /// Returns *this raised to the power a
254 TFactor<T> operator^ (Real a) const {
255 TFactor<T> x;
256 x._vs = _vs;
257 x._p = _p^a;
258 return x;
259 }
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 );
267 return *this;
268 }
270 /// Sets all values that are smaller than epsilon to epsilon
271 TFactor<T>& makePositive( T epsilon ) {
272 _p.makePositive( epsilon );
273 return *this;
274 }
276 /// Returns pointwise inverse of *this.
277 /** If zero == true, uses 1 / 0 == 0; otherwise 1 / 0 == Inf.
278 */
279 TFactor<T> inverse(bool zero=true) const {
280 TFactor<T> inv;
281 inv._vs = _vs;
282 inv._p = _p.inverse(zero);
283 return inv;
284 }
286 /// Returns *this divided pointwise by another TFactor f
287 /** \pre this->vars() == f.vars()
288 */
289 TFactor<T> divided_by( const TFactor<T>& f ) const {
290 #ifdef DAI_DEBUG
291 assert( f._vs == _vs );
292 #endif
293 TFactor<T> quot(*this);
294 quot._p /= f._p;
295 return quot;
296 }
298 /// Divides *this pointwise by another TFactor f
299 /** \pre this->vars() == f.vars()
300 */
301 TFactor<T>& divide( const TFactor<T>& f ) {
302 #ifdef DAI_DEBUG
303 assert( f._vs == _vs );
304 #endif
305 _p /= f._p;
306 return *this;
307 }
309 /// Returns pointwise exp of *this
310 TFactor<T> exp() const {
311 TFactor<T> e;
312 e._vs = _vs;
313 e._p = _p.exp();
314 return e;
315 }
317 /// Returns pointwise absolute value of *this
318 TFactor<T> abs() const {
319 TFactor<T> e;
320 e._vs = _vs;
321 e._p = _p.abs();
322 return e;
323 }
325 /// Returns pointwise logarithm of *this
326 /** If zero==true, uses log(0)==0; otherwise, log(0)=-Inf.
327 */
328 TFactor<T> log(bool zero=false) const {
329 TFactor<T> l;
330 l._vs = _vs;
331 l._p = _p.log(zero);
332 return l;
333 }
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 {
340 TFactor<T> result;
341 result._vs = _vs;
342 result._p = _p.normalized( norm );
343 return result;
344 }
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()
349 */
350 TFactor<T> slice( const VarSet& ns, size_t nsState ) const {
351 assert( ns << _vs );
352 VarSet nsrem = _vs / ns;
353 TFactor<T> result( nsrem, T(0) );
355 // OPTIMIZE ME
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];
362 return result;
363 }
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.
370 */
371 TFactor<T> marginal(const VarSet & ns, bool normed=true) const {
372 if( normed )
373 return partSum(ns).normalized();
374 else
375 return partSum(ns);
376 }
378 /// Embeds this factor in a larger VarSet
379 /** \pre vars() should be a subset of ns
380 */
381 TFactor<T> embed(const VarSet & ns) const {
382 assert( ns >> _vs );
383 if( _vs == ns )
384 return *this;
385 else
386 return (*this) * TFactor<T>(ns / _vs, 1);
387 }
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;
412 };
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];
424 return res;
425 }
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];
437 return prod;
438 }
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];
450 return quot;
451 }
454 template<typename T> T TFactor<T>::strength( const Var &i, const Var &j ) const {
455 #ifdef DAI_DEBUG
456 assert( _vs.contains( i ) );
457 assert( _vs.contains( j ) );
458 assert( i != j );
459 #endif
460 VarSet ij(i, j);
462 T max = 0.0;
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;
470 if( i < j )
471 bs = i.states();
472 else
473 as = j.states();
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();
476 T f = f1 * f2;
477 if( f > max )
478 max = f;
479 }
481 return std::tanh( 0.25 * std::log( max ) );
482 }
485 /// Writes a TFactor to an output stream
486 /** \relates TFactor
487 */
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++ )
491 os << P[i] << " ";
492 os << ">)";
493 return os;
494 }
497 /// Returns distance between two TFactors f and g, according to the distance measure dt
498 /** \relates TFactor
499 * \pre f.vars() == g.vars()
500 */
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() )
503 return -1;
504 else {
505 #ifdef DAI_DEBUG
506 assert( f.vars() == g.vars() );
507 #endif
508 return dist( f.p(), g.p(), dt );
509 }
510 }
513 /// Returns the pointwise maximum of two TFactors
514 /** \relates TFactor
515 * \pre f.vars() == g.vars()
516 */
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() ) );
520 }
523 /// Returns the pointwise minimum of two TFactors
524 /** \relates TFactor
525 * \pre f.vars() == g.vars()
526 */
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() ) );
530 }
533 /// Calculates the mutual information between the two variables that f depends on, under the distribution given by f
534 /** \relates TFactor
535 * \pre f.vars().size() == 2
536 */
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 ) );
543 }
546 /// Represents a factor with values of type Real.
547 typedef TFactor<Real> Factor;
550 } // end of namespace dai
553 #endif