Generalized VarSet to "template<typename T> small_set<T>"
[libdai.git] / include / dai / factor.h
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Copyright (C) 2002 Martijn Leisink [martijn@mbfys.kun.nl]
3 Radboud University Nijmegen, The Netherlands
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #ifndef __defined_libdai_factor_h
24 #define __defined_libdai_factor_h
25
26
27 #include <iostream>
28 #include <cmath>
29 #include <dai/prob.h>
30 #include <dai/varset.h>
31 #include <dai/index.h>
32
33
34 namespace dai {
35
36
37 template<typename T> class TFactor;
38 typedef TFactor<Real> Factor;
39
40
41 // predefine friends
42 template<typename T> Real dist( const TFactor<T> & x, const TFactor<T> & y, Prob::DistType dt );
43 template<typename T> Real KL_dist( const TFactor<T> & p, const TFactor<T> & q );
44 template<typename T> Real MutualInfo( const TFactor<T> & p );
45 template<typename T> TFactor<T> max( const TFactor<T> & P, const TFactor<T> & Q );
46 template<typename T> TFactor<T> min( const TFactor<T> & P, const TFactor<T> & Q );
47 template<typename T> std::ostream& operator<< (std::ostream& os, const TFactor<T>& P);
48
49
50 // T should be castable from and to double
51 template <typename T> class TFactor {
52 private:
53 VarSet _vs;
54 TProb<T> _p;
55
56 public:
57 // Construct Factor with empty VarSet but nonempty _p
58 TFactor ( Real p = 1.0 ) : _vs(), _p(1,p) {}
59
60 // Construct Factor from VarSet
61 TFactor( const VarSet& ns ) : _vs(ns), _p(nrStates(_vs)) {}
62
63 // Construct Factor from VarSet and initial value
64 TFactor( const VarSet& ns, Real p ) : _vs(ns), _p(nrStates(_vs),p) {}
65
66 // Construct Factor from VarSet and initial array
67 TFactor( const VarSet& ns, const Real* p ) : _vs(ns), _p(nrStates(_vs),p) {}
68
69 // Construct Factor from VarSet and TProb<T>
70 TFactor( const VarSet& ns, const TProb<T>& p ) : _vs(ns), _p(p) {
71 #ifdef DAI_DEBUG
72 assert( nrStates(_vs) == _p.size() );
73 #endif
74 }
75
76 // Construct Factor from Var
77 TFactor( const Var& n ) : _vs(n), _p(n.states()) {}
78
79 // Copy constructor
80 TFactor( const TFactor<T> &x ) : _vs(x._vs), _p(x._p) {}
81
82 // Assignment operator
83 TFactor<T> & operator= (const TFactor<T> &x) {
84 if( this != &x ) {
85 _vs = x._vs;
86 _p = x._p;
87 }
88 return *this;
89 }
90
91 const TProb<T> & p() const { return _p; }
92 TProb<T> & p() { return _p; }
93 const VarSet & vars() const { return _vs; }
94 size_t states() const { return _p.size(); }
95
96 T operator[] (size_t i) const { return _p[i]; }
97 T& operator[] (size_t i) { return _p[i]; }
98 TFactor<T> & fill (T p)
99 { _p.fill( p ); return(*this); }
100 TFactor<T> & randomize ()
101 { _p.randomize(); return(*this); }
102 TFactor<T> operator* (T x) const {
103 Factor result = *this;
104 result.p() *= x;
105 return result;
106 }
107 TFactor<T>& operator*= (T x) {
108 _p *= x;
109 return *this;
110 }
111 TFactor<T> operator/ (T x) const {
112 Factor result = *this;
113 result.p() /= x;
114 return result;
115 }
116 TFactor<T>& operator/= (T x) {
117 _p /= x;
118 return *this;
119 }
120 TFactor<T> operator* (const TFactor<T>& Q) const;
121 TFactor<T> operator/ (const TFactor<T>& Q) const;
122 TFactor<T>& operator*= (const TFactor<T>& Q) { return( *this = (*this * Q) ); }
123 TFactor<T>& operator/= (const TFactor<T>& Q) { return( *this = (*this / Q) ); }
124 TFactor<T> operator+ (const TFactor<T>& Q) const {
125 #ifdef DAI_DEBUG
126 assert( Q._vs == _vs );
127 #endif
128 TFactor<T> sum(*this);
129 sum._p += Q._p;
130 return sum;
131 }
132 TFactor<T> operator- (const TFactor<T>& Q) const {
133 #ifdef DAI_DEBUG
134 assert( Q._vs == _vs );
135 #endif
136 TFactor<T> sum(*this);
137 sum._p -= Q._p;
138 return sum;
139 }
140 TFactor<T>& operator+= (const TFactor<T>& Q) {
141 #ifdef DAI_DEBUG
142 assert( Q._vs == _vs );
143 #endif
144 _p += Q._p;
145 return *this;
146 }
147 TFactor<T>& operator-= (const TFactor<T>& Q) {
148 #ifdef DAI_DEBUG
149 assert( Q._vs == _vs );
150 #endif
151 _p -= Q._p;
152 return *this;
153 }
154 TFactor<T>& operator+= (T q) {
155 _p += q;
156 return *this;
157 }
158 TFactor<T>& operator-= (T q) {
159 _p -= q;
160 return *this;
161 }
162 TFactor<T> operator+ (T q) const {
163 TFactor<T> result(*this);
164 result._p += q;
165 return result;
166 }
167 TFactor<T> operator- (T q) const {
168 TFactor<T> result(*this);
169 result._p -= q;
170 return result;
171 }
172
173 TFactor<T> operator^ (Real a) const { TFactor<T> x; x._vs = _vs; x._p = _p^a; return x; }
174 TFactor<T>& operator^= (Real a) { _p ^= a; return *this; }
175
176 TFactor<T>& makeZero( Real epsilon ) {
177 _p.makeZero( epsilon );
178 return *this;
179 }
180
181 TFactor<T>& makePositive( Real epsilon ) {
182 _p.makePositive( epsilon );
183 return *this;
184 }
185
186 TFactor<T> inverse() const {
187 TFactor<T> inv;
188 inv._vs = _vs;
189 inv._p = _p.inverse(true); // FIXME
190 return inv;
191 }
192
193 TFactor<T> divided_by( const TFactor<T>& denom ) const {
194 #ifdef DAI_DEBUG
195 assert( denom._vs == _vs );
196 #endif
197 TFactor<T> quot(*this);
198 quot._p /= denom._p;
199 return quot;
200 }
201
202 TFactor<T>& divide( const TFactor<T>& denom ) {
203 #ifdef DAI_DEBUG
204 assert( denom._vs == _vs );
205 #endif
206 _p /= denom._p;
207 return *this;
208 }
209
210 TFactor<T> exp() const {
211 TFactor<T> e;
212 e._vs = _vs;
213 e._p = _p.exp();
214 return e;
215 }
216
217 TFactor<T> abs() const {
218 TFactor<T> e;
219 e._vs = _vs;
220 e._p = _p.abs();
221 return e;
222 }
223
224 TFactor<T> log() const {
225 TFactor<T> l;
226 l._vs = _vs;
227 l._p = _p.log();
228 return l;
229 }
230
231 TFactor<T> log0() const {
232 TFactor<T> l0;
233 l0._vs = _vs;
234 l0._p = _p.log0();
235 return l0;
236 }
237
238 T normalize( typename Prob::NormType norm = Prob::NORMPROB ) { return _p.normalize( norm ); }
239 TFactor<T> normalized( typename Prob::NormType norm = Prob::NORMPROB ) const {
240 TFactor<T> result;
241 result._vs = _vs;
242 result._p = _p.normalized( norm );
243 return result;
244 }
245
246 // returns slice of this factor where the subset ns is in state ns_state
247 Factor slice( const VarSet & ns, size_t ns_state ) const {
248 assert( ns << _vs );
249 VarSet nsrem = _vs / ns;
250 Factor result( nsrem, 0.0 );
251
252 // OPTIMIZE ME
253 IndexFor i_ns (ns, _vs);
254 IndexFor i_nsrem (nsrem, _vs);
255 for( size_t i = 0; i < states(); i++, ++i_ns, ++i_nsrem )
256 if( (size_t)i_ns == ns_state )
257 result._p[i_nsrem] = _p[i];
258
259 return result;
260 }
261
262 // returns unnormalized marginal; ns should be a subset of vars()
263 TFactor<T> partSum(const VarSet & ns) const;
264 // returns (normalized by default) marginal; ns should be a subset of vars()
265 TFactor<T> marginal(const VarSet & ns, bool normed = true) const { if(normed) return partSum(ns).normalized(); else return partSum(ns); }
266 // sums out all variables except those in ns
267 TFactor<T> notSum(const VarSet & ns) const { return partSum(vars() ^ ns); }
268
269 // embeds this factor in larger varset ns
270 TFactor<T> embed(const VarSet & ns) const {
271 VarSet vs = vars();
272 assert( ns >> vs );
273 if( vs == ns )
274 return *this;
275 else
276 return (*this) * Factor(ns / vs, 1.0);
277 }
278
279 bool hasNaNs() const { return _p.hasNaNs(); }
280 bool hasNegatives() const { return _p.hasNegatives(); }
281 T totalSum() const { return _p.totalSum(); }
282 T maxAbs() const { return _p.maxAbs(); }
283 T maxVal() const { return _p.maxVal(); }
284 T minVal() const { return _p.minVal(); }
285 Real entropy() const { return _p.entropy(); }
286 T strength( const Var &i, const Var &j ) const;
287
288 friend Real dist( const TFactor<T> & x, const TFactor<T> & y, Prob::DistType dt ) {
289 if( x._vs.empty() || y._vs.empty() )
290 return -1;
291 else {
292 #ifdef DAI_DEBUG
293 assert( x._vs == y._vs );
294 #endif
295 return dist( x._p, y._p, dt );
296 }
297 }
298 friend Real KL_dist <> (const TFactor<T> & p, const TFactor<T> & q);
299 friend Real MutualInfo <> ( const TFactor<T> & P );
300 template<class U> friend std::ostream& operator<< (std::ostream& os, const TFactor<U>& P);
301 };
302
303
304 template<typename T> TFactor<T> TFactor<T>::partSum(const VarSet & ns) const {
305 #ifdef DAI_DEBUG
306 assert( ns << _vs );
307 #endif
308
309 TFactor<T> res( ns, 0.0 );
310
311 IndexFor i_res( ns, _vs );
312 for( size_t i = 0; i < _p.size(); i++, ++i_res )
313 res._p[i_res] += _p[i];
314
315 return res;
316 }
317
318
319 template<typename T> std::ostream& operator<< (std::ostream& os, const TFactor<T>& P) {
320 os << "(" << P.vars() << " <";
321 for( size_t i = 0; i < P._p.size(); i++ )
322 os << P._p[i] << " ";
323 os << ">)";
324 return os;
325 }
326
327
328 template<typename T> TFactor<T> TFactor<T>::operator* (const TFactor<T>& Q) const {
329 TFactor<T> prod( _vs | Q._vs, 0.0 );
330
331 IndexFor i1(_vs, prod._vs);
332 IndexFor i2(Q._vs, prod._vs);
333
334 for( size_t i = 0; i < prod._p.size(); i++, ++i1, ++i2 )
335 prod._p[i] += _p[i1] * Q._p[i2];
336
337 return prod;
338 }
339
340
341 template<typename T> TFactor<T> TFactor<T>::operator/ (const TFactor<T>& Q) const {
342 TFactor<T> quot( _vs + Q._vs, 0.0 );
343
344 IndexFor i1(_vs, quot._vs);
345 IndexFor i2(Q._vs, quot._vs);
346
347 for( size_t i = 0; i < quot._p.size(); i++, ++i1, ++i2 )
348 quot._p[i] += _p[i1] / Q._p[i2];
349
350 return quot;
351 }
352
353
354 template<typename T> Real KL_dist(const TFactor<T> & P, const TFactor<T> & Q) {
355 if( P._vs.empty() || Q._vs.empty() )
356 return -1;
357 else {
358 #ifdef DAI_DEBUG
359 assert( P._vs == Q._vs );
360 #endif
361 return KL_dist( P._p, Q._p );
362 }
363 }
364
365
366 // calculate mutual information of x_i and x_j where P.vars() = \{x_i,x_j\}
367 template<typename T> Real MutualInfo(const TFactor<T> & P) {
368 assert( P._vs.size() == 2 );
369 VarSet::const_iterator it = P._vs.begin();
370 Var i = *it; it++; Var j = *it;
371 TFactor<T> projection = P.marginal(i) * P.marginal(j);
372 return real( KL_dist( P.normalized(), projection ) );
373 }
374
375
376 template<typename T> TFactor<T> max( const TFactor<T> & P, const TFactor<T> & Q ) {
377 assert( P._vs == Q._vs );
378 return TFactor<T>( P._vs, min( P.p(), Q.p() ) );
379 }
380
381 template<typename T> TFactor<T> min( const TFactor<T> & P, const TFactor<T> & Q ) {
382 assert( P._vs == Q._vs );
383 return TFactor<T>( P._vs, max( P.p(), Q.p() ) );
384 }
385
386 // calculate N(psi, i, j)
387 template<typename T> T TFactor<T>::strength( const Var &i, const Var &j ) const {
388 #ifdef DAI_DEBUG
389 assert( _vs.contains( i ) );
390 assert( _vs.contains( j ) );
391 assert( i != j );
392 #endif
393 VarSet ij(i, j);
394
395 T max = 0.0;
396 for( size_t alpha1 = 0; alpha1 < i.states(); alpha1++ )
397 for( size_t alpha2 = 0; alpha2 < i.states(); alpha2++ )
398 if( alpha2 != alpha1 )
399 for( size_t beta1 = 0; beta1 < j.states(); beta1++ )
400 for( size_t beta2 = 0; beta2 < j.states(); beta2++ )
401 if( beta2 != beta1 ) {
402 size_t as = 1, bs = 1;
403 if( i < j )
404 bs = i.states();
405 else
406 as = j.states();
407 T f1 = slice( ij, alpha1 * as + beta1 * bs ).p().divide( slice( ij, alpha2 * as + beta1 * bs ).p() ).maxVal();
408 T f2 = slice( ij, alpha2 * as + beta2 * bs ).p().divide( slice( ij, alpha1 * as + beta2 * bs ).p() ).maxVal();
409 T f = f1 * f2;
410 if( f > max )
411 max = f;
412 }
413
414 return std::tanh( 0.25 * std::log( max ) );
415 }
416
417
418 template<typename T> TFactor<T> RemoveFirstOrderInteractions( const TFactor<T> & psi ) {
419 TFactor<T> result = psi;
420
421 VarSet vars = psi.vars();
422 for( size_t iter = 0; iter < 100; iter++ ) {
423 for( VarSet::const_iterator n = vars.begin(); n != vars.end(); n++ )
424 result = result * result.partSum(*n).inverse();
425 result.normalize();
426 }
427
428 return result;
429 }
430
431
432 } // end of namespace dai
433
434
435 #endif