# Calculate rolling / moving average in c or c++

I know this is achievable with boost as per:

Using boost::accumulators, how can I reset a rolling window size, does it keep extra history?

Basically I want to track the moving average of an ongoing stream of a stream of floating point numbers using the most recent 1000 numbers as a data sample.

What is the easiest way to achieve this?

I experimented with using a circular array, exponential moving average and a more simple moving average and found that the results from the circular array suited my needs best.

How to calculate moving/rolling average in T-SQL

I have yearly data (day wise) in my SQL table. I want to calculate 3 months rolling/moving average.How this can be done?

How to calculate simple moving average faster in C#?

What is the fastest library/algorithm for calculating simple moving average? I wrote my own, but it takes too long on 330 000 items decimal dataset. period / time(ms) 20 / 300; 60 / 1500; 120 / 3

Calculate Exponential Moving Average on a Queue in C#

I have a simple class for calculating the moving average of values I add to it. I use it like this: MovingAverage ma = new MovingAverage(); ma.push(value1); ma.push(value2); … Console.Writeline(aver

SQLite – calculate moving average

i have an table in sqlite using pysqlite: create table t ( id integer primary key not null, time datetime not null, price decimal(5,2) ) how can i from this data calculate moving average with window

Calculating moving average in C++

I am trying to calculate the moving average of a signal. The signal value ( a double ) is updated at random times. I am looking for an efficient way to calculate it’s time weighted average over a time

Moving/Rolling Average in SQL

I am using SQL Server 2005. Consider the following table with three columns: issueid, date and rate: sqlfiddle.com/#!2/611682 The result I am looking for is: For issueid 1, the average on 3/31/2014 i

Trying to calculate EMA (exponential moving average) using BigQuery

I am trying to calculate the exponential moving average (EMA) of a stock price. I am using a formula to calculate EMA from this site: http://www.iexplain.org/ema-how-to-calculate/ I am totally stuck c

Calculate Moving Average in Excel

I want to calculate a moving average of the last, say 20, numbers of a column. A problem is that some of the cells of the column may be empty, they should be ignored. Example: A 175 154 188 145 155 1

How to calculate moving average?

I have a field in my database named Value and I need to calculate the moving average of this value. The table has a primary key index which is used as the sort order for the table. There appears

how to calculate moving average in RxJava

In finance domain, we usually need to calculate the moving-window aggregate value from a stream of time series data, use moving average as an example, say we have the following data stream(T is time s

If your needs are simple, you might just try using an exponential moving average.

http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average

Put simply, you make an accumulator variable, and as your code looks at each sample, the code updates the accumulator with the new value. You pick a constant “alpha” that is between 0 and 1, and compute this:

```accumulator = (alpha * new_value) + (1.0 - alpha) * accumulator
```

You just need to find a value of “alpha” where the effect of a given sample only lasts for about 1000 samples.

Hmm, I’m not actually sure this is suitable for you, now that I’ve put it here. The problem is that 1000 is a pretty long window for an exponential moving average; I’m not sure there is an alpha that would spread the average over the last 1000 numbers, without underflow in the floating point calculation. But if you wanted a smaller average, like 30 numbers or so, this is a very easy and fast way to do it.

You simply need a circular array of 1000 elements, where you add the element to the previous element and store it… It becomes an increasing sum, where you can always get the sum between any two pairs of elements, and divide by the number of elements between them, to yield the average.

You could implement a ring buffer. Make an array of 1000 elements, and some fields to store the start and end indexes and total size. Then just store the last 1000 elements in the ring buffer, and recalculate the average as needed.

Basically I want to track the moving average of an ongoing stream of a stream of floating point numbers using the most recent 1000 numbers as a data sample.

Note that the below updates the total_ as elements as added/replaced, avoiding costly O(N) traversal to calculate the sum – needed for the average – on demand.

```template <typename T, typename Total, int N>
class Moving_Average
{
public:
Moving_Average()
: num_samples_(0), total_(0)
{ }

void operator()(T sample)
{
if (num_samples_ < N)
{
samples_[num_samples_++] = sample;
total_ += sample;
}
else
{
T& oldest = samples_[num_samples_++ % N];
total_ += sample - oldest;
oldest = sample;
}
}

private:
T samples_[N];
int num_samples_;
Total total_;
};
```

Total is made a different parameter from T to support e.g. using a long long when totalling 1000 longs, an int for chars, or a double to total floats.

This is a bit flawed in that num_samples_ could go past INT_MAX – if you care you could use a unsigned long long, or use an extra bool data member to record when the container is first filled while cycling num_samples_ around the array (best then renamed something innocuous like “pos”).

You can approximate a rolling average by applying a weighted average on your input stream.

```template <unsigned N>
double approxRollingAverage (double avg, double input) {
avg -= avg/N;
avg += input/N;
return avg;
}
```

This way, you don’t need to maintain 1000 buckets. However, it is an approximation, so it’s value will not match exactly with a true rolling average.

Edit: Just noticed @steveha’s post. This is equivalent to the exponential moving average, with the alpha being 1/N (I was taking N to be 1000 in this case to simulate 1000 buckets).

Simple class to calculate rolling average and also rolling standard deviation:

```#define _stdev(cnt, sum, ssq) sqrt((((double)(cnt))*ssq-pow((double)(sum),2)) / ((double)(cnt)*((double)(cnt)-1)))

class moving_average {
private:
boost::circular_buffer<int> *q;
double sum;
double ssq;
public:
moving_average(int n)  {
sum=0;
ssq=0;
q = new boost::circular_buffer<int>(n);
}
~moving_average() {
delete q;
}
void push(double v) {
if (q->size() == q->capacity()) {
double t=q->front();
sum-=t;
ssq-=t*t;
q->pop_front();
}
q->push_back(v);
sum+=v;
ssq+=v*v;
}
double size() {
return q->size();
}
double mean() {
return sum/size();
}
double stdev() {
return _stdev(size(), sum, ssq);
}

};
```

a simple moving average for 10 items, using a list:

```#include <list>

std::list<float> listDeltaMA;

float getDeltaMovingAverage(float delta)
{
listDeltaMA.push_back(delta);
if (listDeltaMA.size() > 10) listDeltaMA.pop_front();
float sum = 0;
for (std::list<float>::iterator p = listDeltaMA.begin(); p != listDeltaMA.end(); ++p)
sum += (float)*p;
return sum / listDeltaMA.size();
}
```

One way can be to circularly store the values in the buffer array. and calculate average this way.

```int j = (int) (counter % size);
buffer[j] = mostrecentvalue;
avg = (avg * size - buffer[j - 1 == -1 ? size - 1 : j - 1] + buffer[j]) / size;

counter++;

// buffer[j - 1 == -1 ? size - 1 : j - 1] is the oldest value stored
```

The whole thing runs in a loop where most recent value is dynamic.