mcpele  1.0.0
The Monte Carlo Python Energy Landscape Explorer
mcpele/moving_average.h
00001 #ifndef _MCPELE_MOVING_AVERAGE_H
00002 #define _MCPELE_MOVING_AVERAGE_H
00003 
00004 #include <algorithm>
00005 #include <vector>
00006 
00007 #include "histogram.h"
00008 
00009 namespace mcpele {
00010 
00026 class MovingAverageAcc {
00027 private:
00028     const std::vector<double>& m_time_series;
00029     const size_t m_nr_steps_total;
00030     const size_t m_window_size;
00031     const size_t m_nr_steps_ma;
00032     std::vector<double>::const_iterator m_begin;
00033     std::vector<double>::const_iterator m_end;
00034     mcpele::Moments m_moments;
00035 public:
00036     MovingAverageAcc(const std::vector<double>& time_series, const size_t nr_steps_total, const size_t nr_steps_ma)
00037         : m_time_series(time_series),
00038           m_nr_steps_total(nr_steps_total),
00039           m_window_size(nr_steps_ma), //window size
00040           m_nr_steps_ma(nr_steps_total - m_window_size + 1), //number of steps to move window from left to right end
00041           m_begin(m_time_series.end() - nr_steps_total),
00042           m_end(m_begin + m_window_size),
00043           m_moments()
00044     {
00045         if (nr_steps_ma % 2 != 0) {
00046             throw std::runtime_error("MovingAverageAcc: illegal input: nr_steps_ma");
00047         }
00048         if (time_series.size() < nr_steps_total) {
00049             throw std::runtime_error("MovingAverageAcc: illegal input: time series too short");
00050         }
00051         //initialise moments
00052         for(auto it = m_begin; it != m_end; ++it) {
00053             m_moments(*it);
00054         }
00055     }
00056     double get_mean() const { return m_moments.mean(); }
00057     double get_variance() const { return m_moments.variance(); }
00058     size_t get_nr_steps_ma() const { return m_nr_steps_ma; }
00059     void shift_right()
00060     {
00061         ++m_begin;
00062         ++m_end;
00063         if (m_end == m_time_series.end()) {
00064             reset();
00065         }
00066         else {
00067             m_moments.replace(*(m_begin - 1), *(m_end - 1));
00068         }
00069     }
00070     void reset()
00071     {
00072         m_begin = m_time_series.end() - m_nr_steps_total;
00073         m_end = m_begin + m_window_size;
00074         m_moments = mcpele::Moments();
00075         //initialise moments
00076         for(auto it = m_begin; it != m_end; ++it) {
00077             m_moments(*it);
00078         }
00079     }
00080 };
00081 
00082 } // namespace mcpele
00083 
00084 #endif // #ifndef _MCPELE_MOVING_AVERAGE_H
 All Classes Namespaces Functions Variables Typedefs