Factor belief (const Var &n) const;
Factor belief (const VarSet &n) const;
std::vector<Factor> beliefs() const;
- Complex logZ() const;
+ Real logZ() const;
void init( const VarSet &ns );
void undoProbs( const VarSet &ns ) { FactorGraph::undoProbs(ns); init(ns); }
virtual std::vector<Factor> beliefs() const = 0;
/// Get log partition sum
- virtual Complex logZ() const = 0;
+ virtual Real logZ() const = 0;
/// Clear messages and beliefs
virtual void init() = 0;
template<typename T> class TFactor;
typedef TFactor<Real> Factor;
-typedef TFactor<Complex> CFactor;
// predefine friends
template<typename T> Real dist( const TFactor<T> & x, const TFactor<T> & y, Prob::DistType dt );
-template<typename T> Complex KL_dist( const TFactor<T> & p, const TFactor<T> & q );
+template<typename T> Real KL_dist( const TFactor<T> & p, const TFactor<T> & q );
template<typename T> std::ostream& operator<< (std::ostream& os, const TFactor<T>& P);
-// T should be castable from and to double and to complex
+// T should be castable from and to double
template <typename T> class TFactor {
protected:
VarSet _vs;
return l0;
}
- CFactor clog0() const {
- CFactor l0;
- l0._vs = _vs;
- l0._p = _p.clog0();
- return l0;
- }
-
T normalize( typename Prob::NormType norm ) { return _p.normalize( norm ); }
TFactor<T> normalized( typename Prob::NormType norm ) const {
TFactor<T> result;
T totalSum() const { return _p.totalSum(); }
T maxAbs() const { return _p.maxAbs(); }
T maxVal() const { return _p.maxVal(); }
- Complex entropy() const { return _p.entropy(); }
+ Real entropy() const { return _p.entropy(); }
T strength( const Var &i, const Var &j ) const;
friend Real dist( const TFactor<T> & x, const TFactor<T> & y, Prob::DistType dt ) {
return dist( x._p, y._p, dt );
}
}
- friend Complex KL_dist <> (const TFactor<T> & p, const TFactor<T> & q);
+ friend Real KL_dist <> (const TFactor<T> & p, const TFactor<T> & q);
template<class U> friend std::ostream& operator<< (std::ostream& os, const TFactor<U>& P);
};
}
-template<typename T> Complex KL_dist(const TFactor<T> & P, const TFactor<T> & Q) {
+template<typename T> Real KL_dist(const TFactor<T> & P, const TFactor<T> & Q) {
if( P._vs.empty() || Q._vs.empty() )
return -1;
else {
Factor belief( const Var &n ) const;
Factor belief( const VarSet &ns ) const;
std::vector<Factor> beliefs() const;
- Complex logZ () const;
+ Real logZ () const;
void init( const VarSet &ns );
void undoProbs( const VarSet &ns ) { RegionGraph::undoProbs( ns ); init( ns ); }
Factor belief( const Var &n ) const;
Factor belief( const VarSet &ns ) const;
std::vector<Factor> beliefs() const;
- Complex logZ() const;
+ Real logZ() const;
void init( const VarSet &/*ns*/ ) {}
void undoProbs( const VarSet &ns ) { RegionGraph::undoProbs( ns ); init( ns ); }
return Factor();
}
std::vector<Factor> beliefs() const { return _beliefs; }
- Complex logZ() const {
+ Real logZ() const {
DAI_THROW(NOT_IMPLEMENTED);
return 0.0;
}
Factor belief (const Var &n) const;
Factor belief (const VarSet &ns) const;
std::vector<Factor> beliefs() const;
- Complex logZ() const;
+ Real logZ() const;
void init( const VarSet &ns );
void undoProbs( const VarSet &ns ) { FactorGraph::undoProbs(ns); init(ns); }
return Factor();
}
std::vector<Factor> beliefs() const;
- Complex logZ() const {
+ Real logZ() const {
DAI_THROW(NOT_IMPLEMENTED);
return 0.0;
}
#define __defined_libdai_prob_h
-#include <complex>
#include <cmath>
#include <vector>
#include <ostream>
typedef double Real;
-typedef std::complex<double> Complex;
template<typename T> class TProb;
typedef TProb<Real> Prob;
-typedef TProb<Complex> CProb;
/// TProb<T> implements a probability vector of type T.
-/// T should be castable from and to double and to complex.
+/// T should be castable from and to double.
template <typename T> class TProb {
protected:
/// The entries
private:
/// Calculate x times log(x), or 0 if x == 0
- Complex xlogx( Real x ) const { return( x == 0.0 ? 0.0 : Complex(x) * std::log(Complex(x))); }
+ Real xlogx( Real x ) const { return( x == 0.0 ? 0.0 : x * std::log(x)); }
public:
/// NORMPROB means that the sum of all entries should be 1
return l0;
}
- /// Pointwise (complex) log (or 0 if == 0)
-/* CProb clog0() const {
- CProb l0;
- l0._p.reserve( size() );
- for( size_t i = 0; i < size(); i++ )
- l0._p.push_back( (_p[i] == 0.0) ? 0.0 : std::log( Complex( _p[i] ) ) );
- return l0;
- }*/
-
/// Return distance of p and q
friend Real dist( const TProb<T> & p, const TProb<T> & q, DistType dt ) {
#ifdef DAI_DEBUG
return result;
}
- /// Return (complex) Kullback-Leibler distance with q
- friend Complex KL_dist( const TProb<T> & p, const TProb<T> & q ) {
+ /// Return Kullback-Leibler distance with q
+ friend Real KL_dist( const TProb<T> & p, const TProb<T> & q ) {
#ifdef DAI_DEBUG
assert( p.size() == q.size() );
#endif
- Complex result = 0.0;
+ Real result = 0.0;
for( size_t i = 0; i < p.size(); i++ ) {
if( (Real) p[i] != 0.0 ) {
- Complex p_i = p[i];
- Complex q_i = q[i];
+ Real p_i = p[i];
+ Real q_i = q[i];
result += p_i * (std::log(p_i) - std::log(q_i));
}
}
return (std::find_if( _p.begin(), _p.end(), std::bind2nd( std::less_equal<Real>(), 0.0 ) ) != _p.end());
}
- /// Returns (complex) entropy
- Complex entropy() const {
- Complex S = 0.0;
+ /// Returns entropy
+ Real entropy() const {
+ Real S = 0.0;
for( size_t i = 0; i < size(); i++ )
S -= xlogx(_p[i]);
return S;
std::string identify() const;
void init();
double run();
- Complex logZ() const;
+ Real logZ() const;
bool offtree( size_t I ) const { return (fac2OR[I] == -1U); }
// Save logZ
- double logZ = real( obj->logZ() );
+ double logZ = obj->logZ();
// Save maxdiff
double maxdiff = obj->MaxDiff();
}
-Complex BP::logZ() const {
- Complex sum = 0.0;
+Real BP::logZ() const {
+ Real sum = 0.0;
for(size_t i = 0; i < nrVars(); ++i )
- sum += Complex(1.0 - nbV(i).size()) * beliefV(i).entropy();
+ sum += (1.0 - nbV(i).size()) * beliefV(i).entropy();
for( size_t I = 0; I < nrFactors(); ++I )
sum -= KL_dist( beliefF(I), factor(I) );
return sum;
if( !reInit )
clamped->init();
- Complex logZ0;
+ Real logZ0 = 0.0;
for( State s(ns); s.valid(); s++ ) {
// save unclamped factors connected to ns
clamped->saveProbs( ns );
clamped->init();
clamped->run();
- Complex Z;
+ Real Z;
if( s == 0 ) {
logZ0 = clamped->logZ();
Z = 1.0;
} else {
// subtract logZ0 to avoid very large numbers
Z = exp(clamped->logZ() - logZ0);
- if( fabs(imag(Z)) > 1e-5 )
- cout << "Marginal:: WARNING: complex Z (" << Z << ")" << endl;
}
- Pns[s] = real(Z);
+ Pns[s] = Z;
// restore clamped factors
clamped->undoProbs( ns );
if( !reInit )
clamped->init();
- Complex logZ0;
+ Real logZ0 = 0.0;
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++ ) {
logZ0 = clamped->logZ();
} else {
// subtract logZ0 to avoid very large numbers
- Complex Z = exp(clamped->logZ() - logZ0);
- if( fabs(imag(Z)) > 1e-5 )
- cout << "calcPairBelief:: Warning: complex Z: " << Z << endl;
- Z_xj = real(Z);
+ Z_xj = exp(clamped->logZ() - logZ0);
}
for( size_t k = 0; k < N; k++ )
if( !reInit )
clamped->init();
- Complex logZ0;
+ Real logZ0 = 0.0;
VarSet::const_iterator nj = ns.begin();
for( long j = 0; j < (long)ns.size() - 1; j++, nj++ ) {
size_t k = 0;
logZ0 = clamped->logZ();
} else {
// subtract logZ0 to avoid very large numbers
- Complex Z = exp(clamped->logZ() - logZ0);
- if( fabs(imag(Z)) > 1e-5 )
- cout << "calcPairBelief:: Warning: complex Z: " << Z << endl;
- Z_xj = real(Z);
+ Z_xj = exp(clamped->logZ() - logZ0);
}
// we assume that j.label() < k.label()
}
-Complex HAK::logZ() const {
- Complex sum = 0.0;
+Real HAK::logZ() const {
+ Real sum = 0.0;
for( size_t beta = 0; beta < nrIRs(); beta++ )
- sum += Complex(IR(beta).c()) * Qb(beta).entropy();
+ sum += IR(beta).c() * Qb(beta).entropy();
for( size_t alpha = 0; alpha < nrORs(); alpha++ ) {
- sum += Complex(OR(alpha).c()) * Qa(alpha).entropy();
+ sum += OR(alpha).c() * Qa(alpha).entropy();
sum += (OR(alpha).log0() * Qa(alpha)).totalSum();
}
return sum;
}
-Complex JTree::logZ() const {
- Complex sum = 0.0;
+Real JTree::logZ() const {
+ Real sum = 0.0;
for( size_t beta = 0; beta < nrIRs(); beta++ )
- sum += Complex(IR(beta).c()) * _Qb[beta].entropy();
+ sum += IR(beta).c() * _Qb[beta].entropy();
for( size_t alpha = 0; alpha < nrORs(); alpha++ ) {
- sum += Complex(OR(alpha).c()) * _Qa[alpha].entropy();
+ sum += OR(alpha).c() * _Qa[alpha].entropy();
sum += (OR(alpha).log0() * _Qa[alpha]).totalSum();
}
return sum;
}
-Complex MF::logZ() const {
- Complex sum = 0.0;
+Real MF::logZ() const {
+ Real sum = 0.0;
for(size_t i=0; i < nrVars(); i++ )
sum -= beliefV(i).entropy();
Factor piet;
piet = factor(I).log0();
piet *= henk;
- sum -= Complex( piet.totalSum() );
+ sum -= piet.totalSum();
}
return -sum;
if( piet.vars() >> (v_i | *j) ) {
piet = piet.marginal( v_i | *j );
Factor pietf = piet.marginal(v_i) * piet.marginal(*j);
- wg[UEdge(i,findVar(*j))] = real( KL_dist( piet, pietf ) );
+ wg[UEdge(i,findVar(*j))] = KL_dist( piet, pietf );
} else
wg[UEdge(i,findVar(*j))] = 0;
}
}
-Complex TreeEP::logZ() const {
+Real TreeEP::logZ() const {
double sum = 0.0;
// entropy of the tree
for( size_t beta = 0; beta < nrIRs(); beta++ )
- sum -= real(_Qb[beta].entropy());
+ sum -= _Qb[beta].entropy();
for( size_t alpha = 0; alpha < nrORs(); alpha++ )
- sum += real(_Qa[alpha].entropy());
+ sum += _Qa[alpha].entropy();
// energy of the on-tree factors
for( size_t alpha = 0; alpha < nrORs(); alpha++ )
obj->run();
time += toc() - tic;
try {
- logZ = real(obj->logZ());
+ logZ = obj->logZ();
has_logZ = true;
} catch( Exception &e ) {
has_logZ = false;