-/* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
- Radboud University Nijmegen, The Netherlands
-
+/* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
+ Radboud University Nijmegen, The Netherlands /
+ Max Planck Institute for Biological Cybernetics, Germany
+
This file is part of libDAI.
libDAI is free software; you can redistribute it and/or modify
using namespace std;
-/// Calculate the marginal of obj on ns by clamping
-/// all variables in ns and calculating logZ for each joined state
+/// Calculates the marginal of obj on ns by clamping all variables in ns and calculating logZ for each joined state.
Factor calcMarginal( const InfAlg & obj, const VarSet & ns, bool reInit ) {
Factor Pns (ns);
Real logZ0 = 0.0;
for( State s(ns); s.valid(); s++ ) {
// save unclamped factors connected to ns
- clamped->saveProbs( ns );
+ clamped->backupFactors( ns );
// set clamping Factors to delta functions
for( VarSet::const_iterator n = ns.begin(); n != ns.end(); n++ )
// run DAIAlg, calc logZ, store in Pns
if( reInit )
clamped->init();
+ else
+ clamped->init(ns);
clamped->run();
Real Z;
Pns[s] = Z;
// restore clamped factors
- clamped->undoProbs( ns );
+ clamped->restoreFactors( ns );
}
delete clamped;
- return( Pns.normalized(Prob::NORMPROB) );
+ return( Pns.normalized() );
}
+/// Calculates beliefs of all pairs in ns (by clamping nodes in ns and calculating logZ and the beliefs for each state).
vector<Factor> calcPairBeliefs( const InfAlg & obj, const VarSet& ns, bool reInit ) {
// convert ns to vector<VarSet>
size_t N = ns.size();
for( size_t j = 0; j < N; j++ )
for( size_t k = 0; k < N; k++ )
if( j == k )
- pairbeliefs.push_back(Factor());
+ pairbeliefs.push_back( Factor() );
else
- pairbeliefs.push_back(Factor(vns[j] | vns[k]));
+ pairbeliefs.push_back( Factor( VarSet(vns[j], vns[k]) ) );
InfAlg *clamped = obj.clone();
if( !reInit )
for( size_t j = 0; j < N; j++ ) {
// clamp Var j to its possible values
for( size_t j_val = 0; j_val < vns[j].states(); j_val++ ) {
- // save unclamped factors connected to ns
- clamped->saveProbs( ns );
-
- clamped->clamp( vns[j], j_val );
+ clamped->clamp( vns[j], j_val, true );
if( reInit )
clamped->init();
+ else
+ clamped->init(ns);
clamped->run();
//if( j == 0 )
}
// restore clamped factors
- clamped->undoProbs( ns );
+ clamped->restoreFactors( ns );
}
}
}
+/// Calculates beliefs of all pairs in ns (by clamping pairs in ns and calculating logZ for each joined state).
Factor calcMarginal2ndO( const InfAlg & obj, const VarSet& ns, bool reInit ) {
// returns a a probability distribution whose 1st order interactions
// are unspecified, whose 2nd order interactions approximate those of
for( size_t ij = 0; ij < pairbeliefs.size(); ij++ )
Pns *= pairbeliefs[ij];
- return( Pns.normalized(Prob::NORMPROB) );
+ return( Pns.normalized() );
}
+/// Calculates 2nd order interactions of the marginal of obj on ns.
vector<Factor> calcPairBeliefsNew( const InfAlg & obj, const VarSet& ns, bool reInit ) {
vector<Factor> result;
result.reserve( ns.size() * (ns.size() - 1) / 2 );
for( long j = 0; j < (long)ns.size() - 1; j++, nj++ ) {
size_t k = 0;
for( VarSet::const_iterator nk = nj; (++nk) != ns.end(); k++ ) {
- Factor pairbelief( *nj | *nk );
+ Factor pairbelief( VarSet(*nj, *nk) );
// clamp Vars j and k to their possible values
for( size_t j_val = 0; j_val < nj->states(); j_val++ )
for( size_t k_val = 0; k_val < nk->states(); k_val++ ) {
// save unclamped factors connected to ns
- clamped->saveProbs( ns );
+ clamped->backupFactors( ns );
clamped->clamp( *nj, j_val );
clamped->clamp( *nk, k_val );
if( reInit )
clamped->init();
+ else
+ clamped->init(ns);
clamped->run();
double Z_xj = 1.0;
pairbelief[j_val + (k_val * nj->states())] = Z_xj;
// restore clamped factors
- clamped->undoProbs( ns );
+ clamped->restoreFactors( ns );
}
result.push_back( pairbelief );