1 /* This file is part of libDAI - http://www.libdai.org/
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
7 * Copyright (C) 2007 Bastian Wemmenhove
8 * Copyright (C) 2007-2009 Joris Mooij [joris dot mooij at libdai dot org]
9 * Copyright (C) 2007 Radboud University Nijmegen, The Netherlands
19 #include <dai/jtree.h>
29 const char *MR::Name
= "MR";
32 void MR::setProperties( const PropertySet
&opts
) {
33 assert( opts
.hasKey("tol") );
34 assert( opts
.hasKey("verbose") );
35 assert( opts
.hasKey("updates") );
36 assert( opts
.hasKey("inits") );
38 props
.tol
= opts
.getStringAs
<double>("tol");
39 props
.verbose
= opts
.getStringAs
<size_t>("verbose");
40 props
.updates
= opts
.getStringAs
<Properties::UpdateType
>("updates");
41 props
.inits
= opts
.getStringAs
<Properties::InitType
>("inits");
45 PropertySet
MR::getProperties() const {
47 opts
.Set( "tol", props
.tol
);
48 opts
.Set( "verbose", props
.verbose
);
49 opts
.Set( "updates", props
.updates
);
50 opts
.Set( "inits", props
.inits
);
55 string
MR::printProperties() const {
56 stringstream
s( stringstream::out
);
58 s
<< "tol=" << props
.tol
<< ",";
59 s
<< "verbose=" << props
.verbose
<< ",";
60 s
<< "updates=" << props
.updates
<< ",";
61 s
<< "inits=" << props
.inits
<< "]";
66 // init N, con, nb, tJ, theta
67 void MR::init(size_t Nin
, double *_w
, double *_th
) {
80 if( _w
[i
*N
+j
] != 0.0 ) {
82 tJ
[i
][con
[i
]] = tanh(_w
[i
*N
+j
]);
94 double MR::init_cor_resp() {
95 size_t j
,k
,l
, runx
,i2
;
96 double variab1
, variab2
;
98 double thbJsite
[kmax
];
109 vector
<vector
<double> > tJ_org
;
110 vector
<vector
<size_t> > nb_org
;
111 vector
<size_t> con_org
;
112 vector
<double> theta_org
;
114 vector
<double> xfield(N
*kmax
,0.0);
115 vector
<double> rfield(N
*kmax
,0.0);
116 vector
<double> Hfield(N
,0.0);
117 vector
<double> devs(N
*kmax
,0.0);
118 vector
<double> devs2(N
*kmax
,0.0);
119 vector
<double> dev(N
,0.0);
120 vector
<double> avmag(N
,0.0);
122 // save original tJ, nb
129 for(cavity
=0; cavity
<N
; cavity
++){ // for each spin to be removed
136 // Adapt the graph variables nb[], tJ[] and con[]
137 for(size_t i
=0; i
<con
[cavity
]; i
++) {
138 size_t ij
= nb
[cavity
][i
];
142 if(nb
[ij
][j
]==cavity
){
143 while(j
<(con
[ij
]-1)){
144 nb
[ij
][j
]=nb
[ij
][j
+1];
145 tJ
[ij
][j
] = tJ
[ij
][j
+1];
153 for(size_t i
=0; i
<con
[cavity
]; i
++)
154 con
[nb
[cavity
][i
]]--;
158 // Do everything starting from the new graph********
163 for(size_t i
=0; i
<kmax
*N
; i
++)
164 xfield
[i
] = 3.0*(2*rnd_uniform()-1.);
166 for(i2
=0; i2
<concav
; i2
++){ // Subsequently apply a field to each cavity spin ****************
168 s2
= nb
[cavity
][i2
]; // identify the index of the cavity spin
169 for(size_t i
=0; i
<con
[s2
]; i
++)
170 rfield
[kmax
*s2
+i
] = 1.;
173 do { // From here start the response and belief propagation
178 for(size_t i
=0; i
<con
[k
]; i
++)
179 thbJsite
[i
] = tJ
[k
][i
];
180 for(l
=0; l
<con
[k
]; l
++){
183 if(k
==s2
) rinter
+= 1.;
184 for(j
=0; j
<con
[k
]; j
++)
186 variab2
= tanh(xfield
[kmax
*nb
[k
][j
]+kindex
[k
][j
]]);
187 variab1
= thbJsite
[j
]*variab2
;
188 xinter
*= (1.+variab1
)/(1.-variab1
);
190 rinter
+= thbJsite
[j
]*rfield
[kmax
*nb
[k
][j
]+kindex
[k
][j
]]*(1-variab2
*variab2
)/(1-variab1
*variab1
);
193 variab1
= 0.5*log(xinter
);
194 xinter
= variab1
+ theta
[k
];
195 devs
[kmax
*k
+l
] = xinter
-xfield
[kmax
*k
+l
];
196 xfield
[kmax
*k
+l
] = xfield
[kmax
*k
+l
]+devs
[kmax
*k
+l
]*eps
;
197 if( fabs(devs
[kmax
*k
+l
]) > md
)
198 md
= fabs(devs
[kmax
*k
+l
]);
200 devs2
[kmax
*k
+l
] = rinter
-rfield
[kmax
*k
+l
];
201 rfield
[kmax
*k
+l
] = rfield
[kmax
*k
+l
]+devs2
[kmax
*k
+l
]*eps
;
202 if( fabs(devs2
[kmax
*k
+l
]) > md
)
203 md
= fabs(devs2
[kmax
*k
+l
]);
207 } while((md
> props
.tol
)&&(runx
<runs
)); // Precision condition reached -> BP and RP finished
209 if( props
.verbose
>= 2 )
210 cerr
<< "init_cor_resp: Convergence not reached (md=" << md
<< ")..." << endl
;
214 // compute the observables (i.e. magnetizations and responses)******
216 for(size_t i
=0; i
<concav
; i
++){
220 for(j
=0; j
<con
[nb
[cavity
][i
]]; j
++){
221 variab2
= tanh(xfield
[kmax
*nb
[nb
[cavity
][i
]][j
]+kindex
[nb
[cavity
][i
]][j
]]);
222 variab1
= tJ
[nb
[cavity
][i
]][j
]*variab2
;
223 rinter
+= tJ
[nb
[cavity
][i
]][j
]*rfield
[kmax
*nb
[nb
[cavity
][i
]][j
]+kindex
[nb
[cavity
][i
]][j
]]*(1-variab2
*variab2
)/(1-variab1
*variab1
);
224 xinter
*= (1.+variab1
)/(1.-variab1
);
226 xinter
= tanh(0.5*log(xinter
)+theta
[nb
[cavity
][i
]]);
227 res
[i
] = rinter
*(1-xinter
*xinter
);
230 // *******************
232 for(size_t i
=0; i
<concav
; i
++)
233 if(nb
[cavity
][i
]!=s2
)
235 cors
[cavity
][i2
][i
] = res
[i
];
237 cors
[cavity
][i2
][i
] = 0;
238 } // close for i2 = 0...concav
241 // restore nb, tJ, con
251 double MR::T(size_t i
, sub_nb A
) {
252 // i is a variable index
253 // A is a subset of nb[i]
255 // calculate T{(i)}_A as defined in Rizzo&Montanari e-print (2.17)
257 sub_nb
_nbi_min_A(con
[i
]);
261 double res
= theta
[i
];
262 for( size_t _j
= 0; _j
< _nbi_min_A
.size(); _j
++ )
263 if( _nbi_min_A
.test(_j
) )
264 res
+= atanh(tJ
[i
][_j
] * M
[i
][_j
]);
269 double MR::T(size_t i
, size_t _j
) {
276 double MR::Omega(size_t i
, size_t _j
, size_t _l
) {
280 double Tijl
= T(i
,jl
);
281 return Tijl
/ (1.0 + tJ
[i
][_l
] * M
[i
][_l
] * Tijl
);
285 double MR::Gamma(size_t i
, size_t _j
, size_t _l1
, size_t _l2
) {
288 double Tij
= T(i
,jll
);
291 double Tijll
= T(i
,jll
);
293 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
);
297 double MR::Gamma(size_t i
, size_t _l1
, size_t _l2
) {
302 double Till
= T(i
,ll
);
304 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
);
308 double MR::_tJ(size_t i
, sub_nb A
) {
309 // i is a variable index
310 // A is a subset of nb[i]
312 // calculate the product of all tJ[i][_j] for _j in A
314 sub_nb::size_type _j
= A
.find_first();
315 if( _j
== sub_nb::npos
)
318 return tJ
[i
][_j
] * _tJ(i
, A
.reset(_j
));
322 double MR::appM(size_t i
, sub_nb A
) {
323 // i is a variable index
324 // A is a subset of nb[i]
326 // calculate the moment of variables in A from M and cors, neglecting higher order cumulants,
327 // defined as the sum over all partitions of A into subsets of cardinality two at most of the
328 // product of the cumulants (either first order, i.e. M, or second order, i.e. cors) of the
329 // entries of the partitions
331 sub_nb::size_type _j
= A
.find_first();
332 if( _j
== sub_nb::npos
)
335 sub_nb
A_j(A
); A_j
.reset(_j
);
337 double result
= M
[i
][_j
] * appM(i
, A_j
);
338 for( size_t _k
= 0; _k
< A_j
.size(); _k
++ )
340 sub_nb
A_jk(A_j
); A_jk
.reset(_k
);
341 result
+= cors
[i
][_j
][_k
] * appM(i
,A_jk
);
349 void MR::sum_subs(size_t j
, sub_nb A
, double *sum_even
, double *sum_odd
) {
350 // j is a variable index
351 // A is a subset of nb[j]
353 // calculate sum over all even/odd subsets B of A of _tJ(j,B) appM(j,B)
361 *sum_odd
+= _tJ(j
,B
) * appM(j
,B
);
363 *sum_even
+= _tJ(j
,B
) * appM(j
,B
);
365 // calc next subset B
367 for( ; bit
< A
.size(); bit
++ )
380 void MR::solvemcav() {
381 double sum_even
, sum_odd
;
383 size_t maxruns
= 1000;
386 for(size_t i
=0; i
<N
; i
++)
387 for(size_t _j
=0; _j
<con
[i
]; _j
++)
394 for(size_t i
=0; i
<N
; i
++){ // for all i
395 for(size_t _j
=0; _j
<con
[i
]; _j
++){ // for all j in N_i
396 size_t _i
= kindex
[i
][_j
];
397 size_t j
= nb
[i
][_j
];
398 assert( nb
[j
][_i
] == i
);
401 if( props
.updates
== Properties::UpdateType::FULL
) {
402 // find indices in nb[j] that do not correspond with i
403 sub_nb
_nbj_min_i(con
[j
]);
405 _nbj_min_i
.reset(kindex
[i
][_j
]);
407 // find indices in nb[i] that do not correspond with j
408 sub_nb
_nbi_min_j(con
[i
]);
410 _nbi_min_j
.reset(_j
);
412 sum_subs(j
, _nbj_min_i
, &sum_even
, &sum_odd
);
413 newM
= (tanh(theta
[j
]) * sum_even
+ sum_odd
) / (sum_even
+ tanh(theta
[j
]) * sum_odd
);
415 sum_subs(i
, _nbi_min_j
, &sum_even
, &sum_odd
);
416 double denom
= sum_even
+ tanh(theta
[i
]) * sum_odd
;
418 for(size_t _k
=0; _k
<con
[i
]; _k
++) if(_k
!= _j
) {
419 sub_nb
_nbi_min_jk(_nbi_min_j
);
420 _nbi_min_jk
.reset(_k
);
421 sum_subs(i
, _nbi_min_jk
, &sum_even
, &sum_odd
);
422 numer
+= tJ
[i
][_k
] * cors
[i
][_j
][_k
] * (tanh(theta
[i
]) * sum_even
+ sum_odd
);
424 newM
-= numer
/ denom
;
425 } else if( props
.updates
== Properties::UpdateType::LINEAR
) {
427 for(size_t _l
=0; _l
<con
[i
]; _l
++) if( _l
!= _j
)
428 newM
-= Omega(i
,_j
,_l
) * tJ
[i
][_l
] * cors
[i
][_j
][_l
];
429 for(size_t _l1
=0; _l1
<con
[j
]; _l1
++) if( _l1
!= _i
)
430 for( size_t _l2
=_l1
+1; _l2
<con
[j
]; _l2
++) if( _l2
!= _i
)
431 newM
+= Gamma(j
,_i
,_l1
,_l2
) * tJ
[j
][_l1
] * tJ
[j
][_l2
] * cors
[j
][_l1
][_l2
];
434 double dev
= newM
- M
[i
][_j
];
436 if( fabs(dev
) >= maxdev
)
439 newM
= M
[i
][_j
] + dev
;
440 if( fabs(newM
) > 1.0 )
445 } while((maxdev
>props
.tol
)&&(run
<maxruns
));
448 if( maxdev
> _maxdiff
)
452 if( props
.verbose
>= 1 )
453 cerr
<< "solve_mcav: Convergence not reached (maxdev=" << maxdev
<< ")..." << endl
;
459 for(size_t i
=0; i
<N
; i
++) {
460 if( props
.updates
== Properties::UpdateType::FULL
) {
461 // find indices in nb[i]
465 // calc numerator1 and denominator1
466 double sum_even
, sum_odd
;
467 sum_subs(i
, _nbi
, &sum_even
, &sum_odd
);
469 Mag
[i
] = (tanh(theta
[i
]) * sum_even
+ sum_odd
) / (sum_even
+ tanh(theta
[i
]) * sum_odd
);
471 } else if( props
.updates
== Properties::UpdateType::LINEAR
) {
472 sub_nb
empty(con
[i
]);
475 for(size_t _l1
=0; _l1
<con
[i
]; _l1
++)
476 for( size_t _l2
=_l1
+1; _l2
<con
[i
]; _l2
++)
477 Mag
[i
] += Gamma(i
,_l1
,_l2
) * tJ
[i
][_l1
] * tJ
[i
][_l2
] * cors
[i
][_l1
][_l2
];
480 Mag
[i
] = sign(Mag
[i
]);
485 void MR::init_cor() {
486 for( size_t i
= 0; i
< nrVars(); i
++ ) {
487 vector
<Factor
> pairq
;
488 if( props
.inits
== Properties::InitType::CLAMPING
) {
489 BP
bpcav(*this, PropertySet()("updates", string("SEQMAX"))("tol", 1.0e-9)("maxiter", (size_t)10000)("verbose", (size_t)0)("logdomain", false));
490 bpcav
.makeCavity( i
);
491 pairq
= calcPairBeliefs( bpcav
, delta(i
), false );
492 } else if( props
.inits
== Properties::InitType::EXACT
) {
493 JTree
jtcav(*this, PropertySet()("updates", string("HUGIN"))("verbose", (size_t)0) );
494 jtcav
.makeCavity( i
);
495 pairq
= calcPairBeliefs( jtcav
, delta(i
), false );
497 for( size_t jk
= 0; jk
< pairq
.size(); jk
++ ) {
498 VarSet::const_iterator kit
= pairq
[jk
].vars().begin();
499 size_t j
= findVar( *(kit
) );
500 size_t k
= findVar( *(++kit
) );
501 pairq
[jk
].normalize();
502 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]);
503 for( size_t _j
= 0; _j
< con
[i
]; _j
++ ) if( nb
[i
][_j
] == j
)
504 for( size_t _k
= 0; _k
< con
[i
]; _k
++ ) if( nb
[i
][_k
] == k
) {
505 cors
[i
][_j
][_k
] = cor
;
506 cors
[i
][_k
][_j
] = cor
;
513 string
MR::identify() const {
514 return string(Name
) + printProperties();
520 if( props
.verbose
>= 1 )
521 cerr
<< "Starting " << identify() << "...";
524 // Diffs diffs(nrVars(), 1.0);
527 for(size_t i
=0; i
<N
; i
++)
531 for(size_t i
=0; i
<N
; i
++)
532 cors
[i
].resize(kmax
);
533 for(size_t i
=0; i
<N
; i
++)
534 for(size_t j
=0; j
<kmax
; j
++)
535 cors
[i
][j
].resize(kmax
);
538 for(size_t i
=0; i
<N
; i
++)
539 kindex
[i
].resize(kmax
);
541 if( props
.inits
== Properties::InitType::RESPPROP
) {
542 double md
= init_cor_resp();
545 } else if( props
.inits
== Properties::InitType::EXACT
)
546 init_cor(); // FIXME no MaxDiff() calculation
547 else if( props
.inits
== Properties::InitType::CLAMPING
)
548 init_cor(); // FIXME no MaxDiff() calculation
555 if( props
.verbose
>= 1 )
556 cerr
<< Name
<< " needed " << toc() - tic
<< " seconds." << endl
;
564 void MR::makekindex() {
565 for(size_t i
=0; i
<N
; i
++)
566 for(size_t j
=0; j
<con
[i
]; j
++) {
567 size_t ij
= nb
[i
][j
]; // ij is the j'th neighbour of spin i
569 while( nb
[ij
][k
] != i
)
571 kindex
[i
][j
] = k
; // the j'th neighbour of spin i has spin i as its k'th neighbour
576 Factor
MR::belief( const Var
&n
) const {
578 size_t i
= findVar( n
);
581 x
[0] = 0.5 - Mag
[i
] / 2.0;
582 x
[1] = 0.5 + Mag
[i
] / 2.0;
584 return Factor( n
, x
);
590 vector
<Factor
> MR::beliefs() const {
591 vector
<Factor
> result
;
592 for( size_t i
= 0; i
< nrVars(); i
++ )
593 result
.push_back( belief( var(i
) ) );
599 MR::MR( const FactorGraph
&fg
, const PropertySet
&opts
) : DAIAlgFG(fg
), supported(true), _maxdiff(0.0), _iters(0) {
600 setProperties( opts
);
602 // check whether all vars in fg are binary
603 // check whether connectivity is <= kmax
604 for( size_t i
= 0; i
< fg
.nrVars(); i
++ )
605 if( (fg
.var(i
).states() > 2) || (fg
.delta(i
).size() > kmax
) ) {
613 // check whether all interactions are pairwise or single
614 for( size_t I
= 0; I
< fg
.nrFactors(); I
++ )
615 if( fg
.factor(I
).vars().size() > 2 ) {
624 size_t Nin
= fg
.nrVars();
626 double *w
= new double[Nin
*Nin
];
627 double *th
= new double[Nin
];
629 for( size_t i
= 0; i
< Nin
; i
++ ) {
631 for( size_t j
= 0; j
< Nin
; j
++ )
635 for( size_t I
= 0; I
< fg
.nrFactors(); I
++ ) {
636 const Factor
&psi
= fg
.factor(I
);
637 if( psi
.vars().size() == 1 ) {
638 size_t i
= fg
.findVar( *(psi
.vars().begin()) );
639 th
[i
] += 0.5 * log(psi
[1] / psi
[0]);
640 } else if( psi
.vars().size() == 2 ) {
641 size_t i
= fg
.findVar( *(psi
.vars().begin()) );
642 VarSet::const_iterator jit
= psi
.vars().begin();
643 size_t j
= fg
.findVar( *(++jit
) );
645 w
[i
*Nin
+j
] += 0.25 * log(psi
[3] * psi
[0] / (psi
[2] * psi
[1]));
646 w
[j
*Nin
+i
] += 0.25 * log(psi
[3] * psi
[0] / (psi
[2] * psi
[1]));
648 th
[i
] += 0.25 * log(psi
[3] / psi
[2] * psi
[1] / psi
[0]);
649 th
[j
] += 0.25 * log(psi
[3] / psi
[1] * psi
[2] / psi
[0]);
660 } // end of namespace dai