📄 estimator.h
字号:
//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, Yusuke Miyao/// You may distribute under the terms of the Artistic License.////// <id>$Id: Estimator.h,v 1.8 2003/05/16 12:10:44 yusuke Exp $</id>/// <collection>Maximum Entropy Estimator</collection>/// <name>Estimator.h</name>/// <overview>Base class for Maximum Entropy Estimators</overview>/////////////////////////////////////////////////////////////////////////#ifndef Amis_Estimator_h_#define Amis_Estimator_h_#include <amis/configure.h>#include <amis/Real.h>#include <amis/ModelBase.h>#include <amis/EventSpaceBase.h>#include <amis/Profile.h>#include <amis/LogStream.h>#include <amis/EmpiricalExpect.h>#include <iomanip>#include <iostream>#include <memory>AMIS_NAMESPACE_BEGIN///////////////////////////////////////////////////////////////////////// <classdef>/// <name>EstimatorInitError</name>/// <overview>Error for failing initialization of an estimator</overview>/// <desc>/// The error is thrown when an initialization of an estimator failed./// </desc>/// <body>class EstimatorInitError : public ErrorBase { public: EstimatorInitError( const std::string& s ) : ErrorBase( s ) {} /// Initialize with an error message EstimatorInitError( char* s ) : ErrorBase( s ) {} /// Initialize with an error message};/// </body>/// </classdef>///////////////////////////////////////////////////////////////////////// <classdef>/// <name>Estimator</name>/// <overview>Base class for Maximum Entropy Estimators</overview>/// <desc>/// This class is a base class for maximum entropy estimators/// such as GIS and IIS./// </desc>/// <body>class Estimator {protected: int iteration_count; /// Iteration counter int report_interval; /// Number of report interval Real machine_epsilon; /// Minimum significant number int estimation_precision; Real log_likelihood; /// Log-likelihood of the event space with the current model Real log_likelihood_per_event; /// Log-likelihood of the event space with the current model (per event average) Real objective_function_value; /// The current value of the objective function of the estimation algorithm (as maximization) Real prev_objective_function_value; /// The value of the objective function at the previous iteration Real min_update; /// Minimum value of model update Real max_update; /// Maximum value of model update bool till_convergence; /// Whether to iterate until convergence bool suppress_message; /// Whether to show messages ProfTimer profile_timer; /// Timer to measure the estimation time public: static const int DEFAULT_NUM_ITERATIONS = 100; /// Default number of iterations static const int DEFAULT_PRECISION = 6; /// Default precision static const int DEFAULT_REPORT_INTERVAL = 1; /// Default number of report interval static const int MAX_NUM_ITERATIONS = 5000; static const int CONVERGED_ATOL = 0; static const int CONVERGED_RTOL = 1; static const int BAD_INCREASE = 2; Real ATOL; Real RTOL; public: Estimator(); virtual ~Estimator() {}public: virtual void setModel( ModelBase& model ) = 0; virtual void setEventSpace( EventSpaceBase& event_space ) = 0; virtual const ModelBase& getModel() const = 0; virtual const EventSpaceBase& getEventSpace() const = 0;protected: void initMinMaxUpdate() { min_update = REAL_MAX; max_update = -REAL_MAX; } void setMinMaxUpdate( Real m ) { if ( min_update > m ) min_update = m; if ( max_update < m ) max_update = m; }public: int iterationCount() const { return iteration_count; } /// Get the iteration counter int reportInterval() const { return report_interval; } /// Get the report interval bool isReportIteration() const { return ( iteration_count % report_interval == 0 ); } /// Whether this iteration is a report iteration Real machineEpsilon() const { return machine_epsilon; } /// Get the machine-epsilon value int estimationPrecision() const { return estimation_precision; } Real logLikelihood() const { return log_likelihood; } /// Get the log-likelihood of the event space with the current model Real logLikelihoodPerEvent() const { return log_likelihood_per_event; } /// Get the log-likelihood of the event space with the current model (per event average) Real objectiveFunctionValue() const { return objective_function_value; } /// Get the value of the objective function Real previousObjectiveFunctionValue() const { return prev_objective_function_value; } /// Get the previous value of the objective function Real minUpdate() const { return min_update; } /// Get the minimum value of the model Real maxUpdate() const { return max_update; } /// Get the maximum value of the model bool tillConvergence() { return till_convergence; } void setTillConvergence( bool t = true ) { till_convergence = t; } bool suppressMessage() { return suppress_message; } void setSuppressMessage( bool t = true ) { suppress_message = t; } const ProfTimer& getEstimationTimer() { return profile_timer; }protected: void setLogLikelihood( Real ll_pe, Real sum_event_count ) { log_likelihood_per_event = ll_pe; log_likelihood = ll_pe * sum_event_count; } void setObjectiveFunctionValue( Real fv ) { objective_function_value = fv; } void setPreviousObjectiveFunctionValue( Real fv ) { prev_objective_function_value = fv; }protected: virtual void initialize() = 0; /// Initialization of the internal data before estimation virtual void finalize() = 0; /// Finalization of the internal data after estimation virtual void iteration() = 0; /// Each iteration for estimation virtual void postIteration() {} /// After one iteration virtual void startupMessage(); /// Print start-up message virtual void reportMessage(); /// Print a message for each report iteration virtual void endingMessage(); /// Print ending messagepublic: virtual bool isConverged() = 0; /// Check whether the model is converged virtual const std::string estimatorName() const = 0; /// Name of the estimator virtual const EmpiricalExpectBase& getEmpiricalExpectation() = 0; /// Get the empirical expectation //virtual Real setLogLikelihood() = 0; virtual void estimate( int num_iterations = DEFAULT_NUM_ITERATIONS, int precision = DEFAULT_PRECISION, int r = DEFAULT_REPORT_INTERVAL ); /// Estimation of parameters of a maximum entropy model /* virtual bool checkConvergence( double pf, double f, int* reason ) { /// Use Amis style (I don't know which is better. return checkConvergenceAmisStyle( pf, f, reason ); } virtual bool checkConvergenceTaoStyle( double pf, double f, int* reason ) { AMIS_DEBUG_MESSAGE( 3, "checkConvergenceTaoStyle\n" ); AMIS_DEBUG_MESSAGE( 3, "pf = " << pf << " f=" << f << " atol=" << atol << " rtol=" << rtol << '\n' ); if( pf - f <= ATOL ) { AMIS_DEBUG_MESSAGE( 3, "ABS_CONV=true\n" ); *reason = CONVERGED_ATOL; return true; } if( fabs( pf - f ) / ( fabs( pf ) + 1 ) <= RTOL ) { AMIS_DEBUG_MESSAGE( 3, "REL_CONV=true\n" ); *reason = CONVERGED_RTOL; return true; } return false; } virtual bool checkConvergenceAmisStyle( double pf, double f, int* reason ) { /// From the implementation of BFGSSolver by Y. Miyao AMIS_DEBUG_MESSAGE( 3, "checkConvergenceAmisStyle\n" ); AMIS_DEBUG_MESSAGE( 3, "pf = " << pf << " f=" << f << " atol=" << ATOL << " rtol=" << RTOL << '\n' ); if( pf - f <= ATOL ) { if( pf - f <= 0.0 ) { *reason = BAD_INCREASE; } else{ *reason = CONVERGED_ATOL; } return true; } if( fabs( 1.0 - fabs( f/ pf ) ) <= RTOL ) { *reason = CONVERGED_RTOL; return true; } //std::cerr << "check failed" << std::endl; return false; } */};typedef std::auto_ptr< Estimator > EstimatorPtr;AMIS_NAMESPACE_END/// </body>/// </classdef>#endif // Estimator_h_// end of Estimator.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -