loadDefaultRegistry();
std::map<std::string, ParamEstFactory>::iterator i = _registry->find(method);
if( i == _registry->end() )
- DAI_THROW(UNKNOWN_PARAMETER_ESTIMATION_METHOD);
+ DAI_THROWE(UNKNOWN_PARAMETER_ESTIMATION_METHOD, "Unknown parameter estimation method: " + method);
ParamEstFactory factory = i->second;
return factory(p);
}
}
-CondProbEstimation::CondProbEstimation( size_t target_dimension, const Prob &pseudocounts )
- : _target_dim(target_dimension), _stats(pseudocounts), _initial_stats(pseudocounts)
+CondProbEstimation::CondProbEstimation( size_t target_dimension, const Prob &pseudocounts )
+ : _target_dim(target_dimension), _stats(pseudocounts), _initial_stats(pseudocounts)
{
- if( _stats.size() % _target_dim )
- DAI_THROW(MALFORMED_PROPERTY);
+ assert( !(_stats.size() % _target_dim) );
}
labels.push_back( varorder[i].label() );
outVS |= varorder[i];
}
-
+
// Construct the sigma array for the permutation object
std::vector<size_t> sigma;
sigma.reserve( dims.size() );
for( VarSet::iterator set_iterator = outVS.begin(); sigma.size() < dims.size(); ++set_iterator )
sigma.push_back( find(labels.begin(), labels.end(), set_iterator->label()) - labels.begin() );
-
+
return Permute( dims, sigma );
}
void SharedParameters::setPermsAndVarSetsFromVarOrders() {
if( _varorders.size() == 0 )
return;
- if( _estimation == NULL )
- DAI_THROW(INVALID_SHARED_PARAMETERS_ORDER);
+ assert( _estimation != NULL );
// Construct the permutation objects and the varsets
for( FactorOrientations::const_iterator foi = _varorders.begin(); foi != _varorders.end(); ++foi ) {
VarSet vs;
_perms[foi->first] = calculatePermutation( foi->second, vs );
_varsets[foi->first] = vs;
- if( _estimation->probSize() != vs.nrStates() )
- DAI_THROW(INVALID_SHARED_PARAMETERS_ORDER);
+ assert( _estimation->probSize() == vs.nrStates() );
}
}
SharedParameters::SharedParameters( std::istream &is, const FactorGraph &fg_varlookup )
- : _varsets(), _perms(), _varorders(), _estimation(NULL), _deleteEstimation(true)
+ : _varsets(), _perms(), _varorders(), _estimation(NULL), _deleteEstimation(true)
{
// Read the desired parameter estimation method from the stream
std::string est_method;
// Lookup the factor in the factorgraph
if( fields.size() < 1 )
- DAI_THROW(INVALID_SHARED_PARAMETERS_INPUT_LINE);
+ DAI_THROW(INVALID_EMALG_FILE);
std::istringstream iss;
iss.str( fields[0] );
size_t factor;
iss >> factor;
const VarSet &vs = fg_varlookup.factor(factor).vars();
if( fields.size() != vs.size() + 1 )
- DAI_THROW(INVALID_SHARED_PARAMETERS_INPUT_LINE);
+ DAI_THROW(INVALID_EMALG_FILE);
// Construct the vector of Vars
std::vector<Var> var_order;
labelparse >> label;
VarSet::const_iterator vsi = vs.begin();
for( ; vsi != vs.end(); ++vsi )
- if( vsi->label() == label )
+ if( vsi->label() == label )
break;
if( vsi == vs.end() )
- DAI_THROW(INVALID_SHARED_PARAMETERS_INPUT_LINE);
+ DAI_THROW(INVALID_EMALG_FILE);
var_order.push_back( *vsi );
}
_varorders[factor] = var_order;
}
-SharedParameters::SharedParameters( const SharedParameters &sp )
+SharedParameters::SharedParameters( const SharedParameters &sp )
: _varsets(sp._varsets), _perms(sp._perms), _varorders(sp._varorders), _estimation(sp._estimation), _deleteEstimation(sp._deleteEstimation)
{
// If sp owns its _estimation object, we should clone it instead
}
-SharedParameters::SharedParameters( const FactorOrientations &varorders, ParameterEstimation *estimation )
- : _varsets(), _perms(), _varorders(varorders), _estimation(estimation), _deleteEstimation(false)
+SharedParameters::SharedParameters( const FactorOrientations &varorders, ParameterEstimation *estimation, bool deletePE )
+ : _varsets(), _perms(), _varorders(varorders), _estimation(estimation), _deleteEstimation(deletePE)
{
// Calculate the necessary permutations
setPermsAndVarSetsFromVarOrders();
for( std::map< FactorIndex, Permute >::iterator i = _perms.begin(); i != _perms.end(); ++i ) {
Permute &perm = i->second;
VarSet &vs = _varsets[i->first];
-
+
Factor b = alg.belief(vs);
Prob p( b.states(), 0.0 );
for( size_t entry = 0; entry < b.states(); ++entry )
for( std::map<FactorIndex, Permute>::iterator i = _perms.begin(); i != _perms.end(); ++i ) {
Permute &perm = i->second;
VarSet &vs = _varsets[i->first];
-
+
Factor f( vs, 0.0 );
for( size_t entry = 0; entry < f.states(); ++entry )
f[perm.convert_linear_index(entry)] = p[entry];
}
+void SharedParameters::collectParameters( const FactorGraph &fg, std::vector<Real> &outVals, std::vector<Var> &outVarOrder ) {
+ FactorOrientations::iterator it = _varorders.begin();
+ if( it == _varorders.end() )
+ return;
+ FactorIndex I = it->first;
+ for( std::vector<Var>::const_iterator var_it = _varorders[I].begin(); var_it != _varorders[I].end(); ++var_it )
+ outVarOrder.push_back( *var_it );
+
+ const Factor &f = fg.factor(I);
+ assert( f.vars() == _varsets[I] );
+ const Permute &perm = _perms[I];
+ for( size_t val_index = 0; val_index < f.states(); ++val_index )
+ outVals.push_back( f[perm.convert_linear_index(val_index)] );
+}
+
+
MaximizationStep::MaximizationStep( std::istream &is, const FactorGraph &fg_varlookup ) : _params() {
size_t num_params = -1;
is >> num_params;
_msteps.reserve(num_msteps);
for( size_t i = 0; i < num_msteps; ++i )
_msteps.push_back( MaximizationStep( msteps_file, estep.fg() ) );
-}
+}
void EMAlg::setTermConditions( const PropertySet &p ) {
std::cerr << "Error: in EM log-likehood decreased from " << previous << " to " << current << std::endl;
return true;
}
- return diff / abs(previous) <= _log_z_tol;
+ return (diff / fabs(previous)) <= _log_z_tol;
}
}
Real EMAlg::iterate( MaximizationStep &mstep ) {
Real logZ = 0;
+ Real likelihood = 0;
+
+ _estep.run();
+ logZ = _estep.logZ();
// Expectation calculation
for( Evidence::const_iterator e = _evidence.begin(); e != _evidence.end(); ++e ) {
e->applyEvidence( *clamped );
clamped->init();
clamped->run();
-
- logZ += clamped->logZ();
+
+ likelihood += clamped->logZ() - logZ;
mstep.addExpectations( *clamped );
delete clamped;
}
-
+
// Maximization of parameters
mstep.maximize( _estep.fg() );
- return logZ;
+ return likelihood;
}