mcpele
1.0.0
The Monte Carlo Python Energy Landscape Explorer
|
00001 #include "mcpele/record_scalar_timeseries.h" 00002 #include "mcpele/moving_average.h" 00003 00004 using pele::Array; 00005 00006 namespace mcpele { 00007 00008 RecordScalarTimeseries::RecordScalarTimeseries(const size_t niter, const size_t record_every) 00009 : m_record_every(record_every) 00010 { 00011 if (record_every == 0) { 00012 throw std::runtime_error("RecordScalarTimeseries: record_every expected to be at least 1"); 00013 } 00014 m_time_series.reserve(niter / record_every); 00015 } 00016 00017 void RecordScalarTimeseries::action(Array<double> &coords, double energy, bool accepted, MC* mc) 00018 { 00019 const size_t counter = mc->get_iterations_count(); 00020 if (counter % m_record_every == 0) { 00021 m_record_scalar_value(this->get_recorded_scalar(coords, energy, accepted, mc)); 00022 } 00023 } 00024 00040 bool RecordScalarTimeseries::moving_average_is_stable(const size_t nr_steps_to_check, const double rel_std_threshold) 00041 { 00042 std::pair<double,double> mean_ma = this->get_moving_average_mean(nr_steps_to_check); 00043 const double mu_mean_ma = mean_ma.first; 00044 const double std_mean_ma = mean_ma.second; 00045 //std::cout<<"std_mean_ma/mu_mean_ma "<<100*std_mean_ma/mu_mean_ma<<std::endl; 00046 00047 return ( (std_mean_ma / mu_mean_ma) < rel_std_threshold); 00048 /*std::pair<double,double> var_ma = this->get_moving_average_variance(nr_steps_to_check); 00049 const double mu_var_ma = var_ma.first; 00050 const double std_var_ma = var_ma.second; 00051 std::cout<<"std_var_ma/mu_var_ma "<<100*std_var_ma/mu_var_ma<<std::endl; 00052 && (std_var_ma / mu_var_ma) < rel_std_threshold*/ 00053 } 00054 00055 std::pair<double,double> RecordScalarTimeseries::get_moving_average_mean(const size_t nr_steps_to_check) 00056 { 00057 const size_t scale = 10; 00058 const size_t tmp = nr_steps_to_check / scale; 00059 const size_t window_size = tmp + (tmp % 2); 00060 MovingAverageAcc moving_average(m_time_series, nr_steps_to_check, window_size); 00061 Moments moving_average_mean; 00062 const size_t nr_steps_ma = moving_average.get_nr_steps_ma(); 00063 for (size_t i = 0; i < nr_steps_ma; ++i, moving_average.shift_right()) { 00064 moving_average_mean.update(moving_average.get_mean()); 00065 } 00066 return std::pair<double,double>(moving_average_mean.mean(),moving_average_mean.std()); 00067 } 00068 00069 std::pair<double,double> RecordScalarTimeseries::get_moving_average_variance(const size_t nr_steps_to_check) 00070 { 00071 const size_t scale = 10; 00072 const size_t tmp = nr_steps_to_check / scale; 00073 const size_t window_size = tmp + (tmp % 2); 00074 MovingAverageAcc moving_average(m_time_series, nr_steps_to_check, window_size); 00075 Moments moving_average_var; 00076 const size_t nr_steps_ma = moving_average.get_nr_steps_ma(); 00077 for (size_t i = 0; i < nr_steps_ma; ++i, moving_average.shift_right()) { 00078 moving_average_var.update(moving_average.get_variance()); 00079 } 00080 return std::pair<double,double>(moving_average_var.mean(),moving_average_var.std()); 00081 } 00082 00083 } // namespace mcpele