18 #ifndef MATHS_RATE_OF_CHANGE
19 #define MATHS_RATE_OF_CHANGE
48 template<
typename Observation =
double,
typename RoC =
double>
52 std::vector<Observation> history;
54 std::size_t index = 0;
56 RoC rate_of_change = 0;
62 static constexpr std::size_t
LENGTH_MAX = std::numeric_limits<uint16_t>::max() - 1;
70 throw "length must be >= " + std::to_string(
LENGTH_MIN);
72 throw "length must be <= " + std::to_string(
LENGTH_MAX);
73 history.resize(length, 0);
80 std::size_t
get_length()
const {
return history.size(); }
90 std::fill(history.begin(), history.end(), 0);
99 inline void process(Observation observation) {
101 history[index] = observation;
102 auto next_index = (index + 1) % history.size();
104 auto value = history[next_index];
107 if (value == 0)
return;
109 rate_of_change = (observation - value) / value;
115 #endif // MATHS_RATE_OF_CHANGE
static constexpr std::size_t LENGTH_MAX
the inclusive maximal size for the length of the history
Definition: rate_of_change.hpp:62
void reset()
Reset the history to its initial state.
Definition: rate_of_change.hpp:89
A structure for calculating the Rate of Change (ROC) over a rolling window.
Definition: rate_of_change.hpp:49
static constexpr std::size_t LENGTH_MIN
the inclusive minimal size for the length of the history
Definition: rate_of_change.hpp:60
std::size_t get_length() const
Return the length of the rate of change history.
Definition: rate_of_change.hpp:80
Functions for doing maths.
Definition: exponential_moving_average.hpp:25
void process(Observation observation)
Calculate the next RoC based on observation .
Definition: rate_of_change.hpp:99
RateOfChange(std::size_t length)
Initialize a new RoC with given history length .
Definition: rate_of_change.hpp:68
RoC get_rate_of_change() const
Return the current rate of change (RoC).
Definition: rate_of_change.hpp:86