Significant improvement of documentation
[libdai.git] / src / mr.cpp
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
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 #include <cstdio>
24 #include <ctime>
25 #include <cmath>
26 #include <cstdlib>
27 #include <dai/mr.h>
28 #include <dai/bp.h>
29 #include <dai/jtree.h>
30 #include <dai/util.h>
31
32
33 namespace dai {
34
35
36 using namespace std;
37
38
39 const char *MR::Name = "MR";
40
41
42 void MR::setProperties( const PropertySet &opts ) {
43 assert( opts.hasKey("tol") );
44 assert( opts.hasKey("verbose") );
45 assert( opts.hasKey("updates") );
46 assert( opts.hasKey("inits") );
47
48 props.tol = opts.getStringAs<double>("tol");
49 props.verbose = opts.getStringAs<size_t>("verbose");
50 props.updates = opts.getStringAs<Properties::UpdateType>("updates");
51 props.inits = opts.getStringAs<Properties::InitType>("inits");
52 }
53
54
55 PropertySet MR::getProperties() const {
56 PropertySet opts;
57 opts.Set( "tol", props.tol );
58 opts.Set( "verbose", props.verbose );
59 opts.Set( "updates", props.updates );
60 opts.Set( "inits", props.inits );
61 return opts;
62 }
63
64
65 string MR::printProperties() const {
66 stringstream s( stringstream::out );
67 s << "[";
68 s << "tol=" << props.tol << ",";
69 s << "verbose=" << props.verbose << ",";
70 s << "updates=" << props.updates << ",";
71 s << "inits=" << props.inits << "]";
72 return s.str();
73 }
74
75
76 // init N, con, nb, tJ, theta
77 void MR::init(size_t Nin, double *_w, double *_th) {
78 size_t i,j;
79
80 N = Nin;
81
82 con.resize(N);
83 nb.resize(N);
84 tJ.resize(N);
85 for(i=0; i<N; i++ ) {
86 nb[i].resize(kmax);
87 tJ[i].resize(kmax);
88 con[i]=0;
89 for(j=0; j<N; j++ )
90 if( _w[i*N+j] != 0.0 ) {
91 nb[i][con[i]] = j;
92 tJ[i][con[i]] = tanh(_w[i*N+j]);
93 con[i]++;
94 }
95 }
96
97 theta.resize(N);
98 for(i=0; i<N; i++)
99 theta[i] = _th[i];
100 }
101
102
103 // calculate cors
104 double MR::init_cor_resp() {
105 size_t j,k,l, runx,i2;
106 double variab1, variab2;
107 double md, maxdev;
108 double thbJsite[kmax];
109 double xinter;
110 double rinter;
111 double res[kmax];
112 size_t s2;
113 size_t flag;
114 size_t concav;
115 size_t runs = 3000;
116 double eps = 0.2;
117 size_t cavity;
118
119 vector<vector<double> > tJ_org;
120 vector<vector<size_t> > nb_org;
121 vector<size_t> con_org;
122 vector<double> theta_org;
123
124 vector<double> xfield(N*kmax,0.0);
125 vector<double> rfield(N*kmax,0.0);
126 vector<double> Hfield(N,0.0);
127 vector<double> devs(N*kmax,0.0);
128 vector<double> devs2(N*kmax,0.0);
129 vector<double> dev(N,0.0);
130 vector<double> avmag(N,0.0);
131
132 // save original tJ, nb
133 nb_org = nb;
134 tJ_org = tJ;
135 con_org = con;
136 theta_org = theta;
137
138 maxdev = 0.0;
139 for(cavity=0; cavity<N; cavity++){ // for each spin to be removed
140 con = con_org;
141 concav=con[cavity];
142
143 nb = nb_org;
144 tJ = tJ_org;
145
146 // Adapt the graph variables nb[], tJ[] and con[]
147 for(size_t i=0; i<con[cavity]; i++) {
148 size_t ij = nb[cavity][i];
149 flag=0;
150 j=0;
151 do{
152 if(nb[ij][j]==cavity){
153 while(j<(con[ij]-1)){
154 nb[ij][j]=nb[ij][j+1];
155 tJ[ij][j] = tJ[ij][j+1];
156 j++;
157 }
158 flag=1;
159 }
160 j++;
161 } while(flag==0);
162 }
163 for(size_t i=0; i<con[cavity]; i++)
164 con[nb[cavity][i]]--;
165 con[cavity] = 0;
166
167
168 // Do everything starting from the new graph********
169
170 makekindex();
171 theta = theta_org;
172
173 for(size_t i=0; i<kmax*N; i++)
174 xfield[i] = 3.0*(2*rnd_uniform()-1.);
175
176 for(i2=0; i2<concav; i2++){ // Subsequently apply a field to each cavity spin ****************
177
178 s2 = nb[cavity][i2]; // identify the index of the cavity spin
179 for(size_t i=0; i<con[s2]; i++)
180 rfield[kmax*s2+i] = 1.;
181
182 runx=0;
183 do { // From here start the response and belief propagation
184 runx++;
185 md=0.0;
186 for(k=0; k<N; k++){
187 if(k!=cavity) {
188 for(size_t i=0; i<con[k]; i++)
189 thbJsite[i] = tJ[k][i];
190 for(l=0; l<con[k]; l++){
191 xinter = 1.;
192 rinter = 0.;
193 if(k==s2) rinter += 1.;
194 for(j=0; j<con[k]; j++)
195 if(j!=l){
196 variab2 = tanh(xfield[kmax*nb[k][j]+kindex[k][j]]);
197 variab1 = thbJsite[j]*variab2;
198 xinter *= (1.+variab1)/(1.-variab1);
199
200 rinter += thbJsite[j]*rfield[kmax*nb[k][j]+kindex[k][j]]*(1-variab2*variab2)/(1-variab1*variab1);
201 }
202
203 variab1 = 0.5*log(xinter);
204 xinter = variab1 + theta[k];
205 devs[kmax*k+l] = xinter-xfield[kmax*k+l];
206 xfield[kmax*k+l] = xfield[kmax*k+l]+devs[kmax*k+l]*eps;
207 if( fabs(devs[kmax*k+l]) > md )
208 md = fabs(devs[kmax*k+l]);
209
210 devs2[kmax*k+l] = rinter-rfield[kmax*k+l];
211 rfield[kmax*k+l] = rfield[kmax*k+l]+devs2[kmax*k+l]*eps;
212 if( fabs(devs2[kmax*k+l]) > md )
213 md = fabs(devs2[kmax*k+l]);
214 }
215 }
216 }
217 } while((md > props.tol)&&(runx<runs)); // Precision condition reached -> BP and RP finished
218 if(runx==runs)
219 if( props.verbose >= 2 )
220 cout << "init_cor_resp: Convergence not reached (md=" << md << ")..." << endl;
221 if(md > maxdev)
222 maxdev = md;
223
224 // compute the observables (i.e. magnetizations and responses)******
225
226 for(size_t i=0; i<concav; i++){
227 rinter = 0.;
228 xinter = 1.;
229 if(i!=i2)
230 for(j=0; j<con[nb[cavity][i]]; j++){
231 variab2 = tanh(xfield[kmax*nb[nb[cavity][i]][j]+kindex[nb[cavity][i]][j]]);
232 variab1 = tJ[nb[cavity][i]][j]*variab2;
233 rinter += tJ[nb[cavity][i]][j]*rfield[kmax*nb[nb[cavity][i]][j]+kindex[nb[cavity][i]][j]]*(1-variab2*variab2)/(1-variab1*variab1);
234 xinter *= (1.+variab1)/(1.-variab1);
235 }
236 xinter = tanh(0.5*log(xinter)+theta[nb[cavity][i]]);
237 res[i] = rinter*(1-xinter*xinter);
238 }
239
240 // *******************
241
242 for(size_t i=0; i<concav; i++)
243 if(nb[cavity][i]!=s2)
244 // if(i!=i2)
245 cors[cavity][i2][i] = res[i];
246 else
247 cors[cavity][i2][i] = 0;
248 } // close for i2 = 0...concav
249 }
250
251 // restore nb, tJ, con
252 tJ = tJ_org;
253 nb = nb_org;
254 con = con_org;
255 theta = theta_org;
256
257 return maxdev;
258 }
259
260
261 double MR::T(size_t i, sub_nb A) {
262 // i is a variable index
263 // A is a subset of nb[i]
264 //
265 // calculate T{(i)}_A as defined in Rizzo&Montanari e-print (2.17)
266
267 sub_nb _nbi_min_A(con[i]);
268 _nbi_min_A.set();
269 _nbi_min_A &= ~A;
270
271 double res = theta[i];
272 for( size_t _j = 0; _j < _nbi_min_A.size(); _j++ )
273 if( _nbi_min_A.test(_j) )
274 res += atanh(tJ[i][_j] * M[i][_j]);
275 return tanh(res);
276 }
277
278
279 double MR::T(size_t i, size_t _j) {
280 sub_nb j(con[i]);
281 j.set(_j);
282 return T(i,j);
283 }
284
285
286 double MR::Omega(size_t i, size_t _j, size_t _l) {
287 sub_nb jl(con[i]);
288 jl.set(_j);
289 jl.set(_l);
290 double Tijl = T(i,jl);
291 return Tijl / (1.0 + tJ[i][_l] * M[i][_l] * Tijl);
292 }
293
294
295 double MR::Gamma(size_t i, size_t _j, size_t _l1, size_t _l2) {
296 sub_nb jll(con[i]);
297 jll.set(_j);
298 double Tij = T(i,jll);
299 jll.set(_l1);
300 jll.set(_l2);
301 double Tijll = T(i,jll);
302
303 return (Tijll - Tij) / (1.0 + tJ[i][_l1] * tJ[i][_l2] * M[i][_l1] * M[i][_l2] + tJ[i][_l1] * M[i][_l1] * Tijll + tJ[i][_l2] * M[i][_l2] * Tijll);
304 }
305
306
307 double MR::Gamma(size_t i, size_t _l1, size_t _l2) {
308 sub_nb ll(con[i]);
309 double Ti = T(i,ll);
310 ll.set(_l1);
311 ll.set(_l2);
312 double Till = T(i,ll);
313
314 return (Till - Ti) / (1.0 + tJ[i][_l1] * tJ[i][_l2] * M[i][_l1] * M[i][_l2] + tJ[i][_l1] * M[i][_l1] * Till + tJ[i][_l2] * M[i][_l2] * Till);
315 }
316
317
318 double MR::_tJ(size_t i, sub_nb A) {
319 // i is a variable index
320 // A is a subset of nb[i]
321 //
322 // calculate the product of all tJ[i][_j] for _j in A
323
324 sub_nb::size_type _j = A.find_first();
325 if( _j == sub_nb::npos )
326 return 1.0;
327 else
328 return tJ[i][_j] * _tJ(i, A.reset(_j));
329 }
330
331
332 double MR::appM(size_t i, sub_nb A) {
333 // i is a variable index
334 // A is a subset of nb[i]
335 //
336 // calculate the moment of variables in A from M and cors, neglecting higher order cumulants,
337 // defined as the sum over all partitions of A into subsets of cardinality two at most of the
338 // product of the cumulants (either first order, i.e. M, or second order, i.e. cors) of the
339 // entries of the partitions
340
341 sub_nb::size_type _j = A.find_first();
342 if( _j == sub_nb::npos )
343 return 1.0;
344 else {
345 sub_nb A_j(A); A_j.reset(_j);
346
347 double result = M[i][_j] * appM(i, A_j);
348 for( size_t _k = 0; _k < A_j.size(); _k++ )
349 if( A_j.test(_k) ) {
350 sub_nb A_jk(A_j); A_jk.reset(_k);
351 result += cors[i][_j][_k] * appM(i,A_jk);
352 }
353
354 return result;
355 }
356 }
357
358
359 void MR::sum_subs(size_t j, sub_nb A, double *sum_even, double *sum_odd) {
360 // j is a variable index
361 // A is a subset of nb[j]
362
363 // calculate sum over all even/odd subsets B of A of _tJ(j,B) appM(j,B)
364
365 *sum_even = 0.0;
366 *sum_odd = 0.0;
367
368 sub_nb B(A.size());
369 do {
370 if( B.count() % 2 )
371 *sum_odd += _tJ(j,B) * appM(j,B);
372 else
373 *sum_even += _tJ(j,B) * appM(j,B);
374
375 // calc next subset B
376 size_t bit = 0;
377 for( ; bit < A.size(); bit++ )
378 if( A.test(bit) ) {
379 if( B.test(bit) )
380 B.reset(bit);
381 else {
382 B.set(bit);
383 break;
384 }
385 }
386 } while (!B.none());
387 }
388
389
390 void MR::solvemcav() {
391 double sum_even, sum_odd;
392 double maxdev;
393 size_t maxruns = 1000;
394
395 makekindex();
396 for(size_t i=0; i<N; i++)
397 for(size_t _j=0; _j<con[i]; _j++)
398 M[i][_j]=0.1;
399
400 size_t run=0;
401 do {
402 maxdev=0.0;
403 run++;
404 for(size_t i=0; i<N; i++){ // for all i
405 for(size_t _j=0; _j<con[i]; _j++){ // for all j in N_i
406 size_t _i = kindex[i][_j];
407 size_t j = nb[i][_j];
408 assert( nb[j][_i] == i );
409
410 double newM = 0.0;
411 if( props.updates == Properties::UpdateType::FULL ) {
412 // find indices in nb[j] that do not correspond with i
413 sub_nb _nbj_min_i(con[j]);
414 _nbj_min_i.set();
415 _nbj_min_i.reset(kindex[i][_j]);
416
417 // find indices in nb[i] that do not correspond with j
418 sub_nb _nbi_min_j(con[i]);
419 _nbi_min_j.set();
420 _nbi_min_j.reset(_j);
421
422 sum_subs(j, _nbj_min_i, &sum_even, &sum_odd);
423 newM = (tanh(theta[j]) * sum_even + sum_odd) / (sum_even + tanh(theta[j]) * sum_odd);
424
425 sum_subs(i, _nbi_min_j, &sum_even, &sum_odd);
426 double denom = sum_even + tanh(theta[i]) * sum_odd;
427 double numer = 0.0;
428 for(size_t _k=0; _k<con[i]; _k++) if(_k != _j) {
429 sub_nb _nbi_min_jk(_nbi_min_j);
430 _nbi_min_jk.reset(_k);
431 sum_subs(i, _nbi_min_jk, &sum_even, &sum_odd);
432 numer += tJ[i][_k] * cors[i][_j][_k] * (tanh(theta[i]) * sum_even + sum_odd);
433 }
434 newM -= numer / denom;
435 } else if( props.updates == Properties::UpdateType::LINEAR ) {
436 newM = T(j,_i);
437 for(size_t _l=0; _l<con[i]; _l++) if( _l != _j )
438 newM -= Omega(i,_j,_l) * tJ[i][_l] * cors[i][_j][_l];
439 for(size_t _l1=0; _l1<con[j]; _l1++) if( _l1 != _i )
440 for( size_t _l2=_l1+1; _l2<con[j]; _l2++) if( _l2 != _i)
441 newM += Gamma(j,_i,_l1,_l2) * tJ[j][_l1] * tJ[j][_l2] * cors[j][_l1][_l2];
442 }
443
444 double dev = newM - M[i][_j];
445 // dev *= 0.02;
446 if( fabs(dev) >= maxdev )
447 maxdev = fabs(dev);
448
449 newM = M[i][_j] + dev;
450 if( fabs(newM) > 1.0 )
451 newM = sign(newM);
452 M[i][_j] = newM;
453 }
454 }
455 } while((maxdev>props.tol)&&(run<maxruns));
456
457 _iters = run;
458 if( maxdev > _maxdiff )
459 _maxdiff = maxdev;
460
461 if(run==maxruns){
462 if( props.verbose >= 1 )
463 cout << "solve_mcav: Convergence not reached (maxdev=" << maxdev << ")..." << endl;
464 }
465 }
466
467
468 void MR::solveM() {
469 for(size_t i=0; i<N; i++) {
470 if( props.updates == Properties::UpdateType::FULL ) {
471 // find indices in nb[i]
472 sub_nb _nbi(con[i]);
473 _nbi.set();
474
475 // calc numerator1 and denominator1
476 double sum_even, sum_odd;
477 sum_subs(i, _nbi, &sum_even, &sum_odd);
478
479 Mag[i] = (tanh(theta[i]) * sum_even + sum_odd) / (sum_even + tanh(theta[i]) * sum_odd);
480
481 } else if( props.updates == Properties::UpdateType::LINEAR ) {
482 sub_nb empty(con[i]);
483 Mag[i] = T(i,empty);
484
485 for(size_t _l1=0; _l1<con[i]; _l1++)
486 for( size_t _l2=_l1+1; _l2<con[i]; _l2++)
487 Mag[i] += Gamma(i,_l1,_l2) * tJ[i][_l1] * tJ[i][_l2] * cors[i][_l1][_l2];
488 }
489 if(fabs(Mag[i])>1.)
490 Mag[i] = sign(Mag[i]);
491 }
492 }
493
494
495 void MR::init_cor() {
496 for( size_t i = 0; i < nrVars(); i++ ) {
497 vector<Factor> pairq;
498 if( props.inits == Properties::InitType::CLAMPING ) {
499 BP bpcav(*this, PropertySet()("updates", string("SEQMAX"))("tol", string("1e-9"))("maxiter", string("10000UL"))("verbose", string("0UL"))("logdomain", string("0")));
500 bpcav.makeCavity( i );
501 pairq = calcPairBeliefs( bpcav, delta(i), false );
502 } else if( props.inits == Properties::InitType::EXACT ) {
503 JTree jtcav(*this, PropertySet()("updates", string("HUGIN"))("verbose", string("0UL")) );
504 jtcav.makeCavity( i );
505 pairq = calcPairBeliefs( jtcav, delta(i), false );
506 }
507 for( size_t jk = 0; jk < pairq.size(); jk++ ) {
508 VarSet::const_iterator kit = pairq[jk].vars().begin();
509 size_t j = findVar( *(kit) );
510 size_t k = findVar( *(++kit) );
511 pairq[jk].normalize();
512 double cor = (pairq[jk][3] - pairq[jk][2] - pairq[jk][1] + pairq[jk][0]) - (pairq[jk][3] + pairq[jk][2] - pairq[jk][1] - pairq[jk][0]) * (pairq[jk][3] - pairq[jk][2] + pairq[jk][1] - pairq[jk][0]);
513 for( size_t _j = 0; _j < con[i]; _j++ ) if( nb[i][_j] == j )
514 for( size_t _k = 0; _k < con[i]; _k++ ) if( nb[i][_k] == k ) {
515 cors[i][_j][_k] = cor;
516 cors[i][_k][_j] = cor;
517 }
518 }
519 }
520 }
521
522
523 string MR::identify() const {
524 return string(Name) + printProperties();
525 }
526
527
528 double MR::run() {
529 if( supported ) {
530 if( props.verbose >= 1 )
531 cout << "Starting " << identify() << "...";
532
533 double tic = toc();
534 // Diffs diffs(nrVars(), 1.0);
535
536 M.resize(N);
537 for(size_t i=0; i<N; i++)
538 M[i].resize(kmax);
539
540 cors.resize(N);
541 for(size_t i=0; i<N; i++)
542 cors[i].resize(kmax);
543 for(size_t i=0; i<N; i++)
544 for(size_t j=0; j<kmax; j++)
545 cors[i][j].resize(kmax);
546
547 kindex.resize(N);
548 for(size_t i=0; i<N; i++)
549 kindex[i].resize(kmax);
550
551 if( props.inits == Properties::InitType::RESPPROP ) {
552 double md = init_cor_resp();
553 if( md > _maxdiff )
554 _maxdiff = md;
555 } else if( props.inits == Properties::InitType::EXACT )
556 init_cor(); // FIXME no MaxDiff() calculation
557 else if( props.inits == Properties::InitType::CLAMPING )
558 init_cor(); // FIXME no MaxDiff() calculation
559
560 solvemcav();
561
562 Mag.resize(N);
563 solveM();
564
565 if( props.verbose >= 1 )
566 cout << Name << " needed " << toc() - tic << " seconds." << endl;
567
568 return 0.0;
569 } else
570 return 1.0;
571 }
572
573
574 void MR::makekindex() {
575 for(size_t i=0; i<N; i++)
576 for(size_t j=0; j<con[i]; j++) {
577 size_t ij = nb[i][j]; // ij is the j'th neighbour of spin i
578 size_t k=0;
579 while( nb[ij][k] != i )
580 k++;
581 kindex[i][j] = k; // the j'th neighbour of spin i has spin i as its k'th neighbour
582 }
583 }
584
585
586 Factor MR::belief( const Var &n ) const {
587 if( supported ) {
588 size_t i = findVar( n );
589
590 Real x[2];
591 x[0] = 0.5 - Mag[i] / 2.0;
592 x[1] = 0.5 + Mag[i] / 2.0;
593
594 return Factor( n, x );
595 } else
596 return Factor();
597 }
598
599
600 vector<Factor> MR::beliefs() const {
601 vector<Factor> result;
602 for( size_t i = 0; i < nrVars(); i++ )
603 result.push_back( belief( var(i) ) );
604 return result;
605 }
606
607
608
609 MR::MR( const FactorGraph &fg, const PropertySet &opts ) : DAIAlgFG(fg), supported(true), _maxdiff(0.0), _iters(0) {
610 setProperties( opts );
611
612 // check whether all vars in fg are binary
613 // check whether connectivity is <= kmax
614 for( size_t i = 0; i < fg.nrVars(); i++ )
615 if( (fg.var(i).states() > 2) || (fg.delta(i).size() > kmax) ) {
616 supported = false;
617 break;
618 }
619
620 if( !supported )
621 return;
622
623 // check whether all interactions are pairwise or single
624 for( size_t I = 0; I < fg.nrFactors(); I++ )
625 if( fg.factor(I).vars().size() > 2 ) {
626 supported = false;
627 break;
628 }
629
630 if( !supported )
631 return;
632
633 // create w and th
634 size_t Nin = fg.nrVars();
635
636 double *w = new double[Nin*Nin];
637 double *th = new double[Nin];
638
639 for( size_t i = 0; i < Nin; i++ ) {
640 th[i] = 0.0;
641 for( size_t j = 0; j < Nin; j++ )
642 w[i*Nin+j] = 0.0;
643 }
644
645 for( size_t I = 0; I < fg.nrFactors(); I++ ) {
646 const Factor &psi = fg.factor(I);
647 if( psi.vars().size() == 1 ) {
648 size_t i = fg.findVar( *(psi.vars().begin()) );
649 th[i] += 0.5 * log(psi[1] / psi[0]);
650 } else if( psi.vars().size() == 2 ) {
651 size_t i = fg.findVar( *(psi.vars().begin()) );
652 VarSet::const_iterator jit = psi.vars().begin();
653 size_t j = fg.findVar( *(++jit) );
654
655 w[i*Nin+j] += 0.25 * log(psi[3] * psi[0] / (psi[2] * psi[1]));
656 w[j*Nin+i] += 0.25 * log(psi[3] * psi[0] / (psi[2] * psi[1]));
657
658 th[i] += 0.25 * log(psi[3] / psi[2] * psi[1] / psi[0]);
659 th[j] += 0.25 * log(psi[3] / psi[1] * psi[2] / psi[0]);
660 }
661 }
662
663 init(Nin, w, th);
664
665 delete th;
666 delete w;
667 }
668
669
670 } // end of namespace dai