Click here to Skip to main content
15,880,392 members
Articles / Programming Languages / C

C++ Wrapper Class to Compute Moving Averages

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
28 Apr 2009CPOL3 min read 46.4K   733   14   7
C++ wrapper class to compute moving averages

Introduction

Sometimes you need to calculate the moving average of a quantity. In this article, I describe wrapper classes that help you calculate the moving average. There are various methods to calculate moving average like the simple moving average, the weighted moving average and the exponential moving average. Due to time constraints, I have implemented only the first two.

Background

There is a very good article on Moving Averages on Wikipedia. However, I will go ahead and describe it very briefly.

When you are measuring an average, you are measuring the mean value of the quantity over the whole range of data. Sometimes it is useful to measure only the average of the last N sample values. For example, a trader might be interested to know the average price of a certain stock in the last seven days and he/she wants this value every day. This would be done by calculating the moving average. Every time a new sample is added to the data, we need to throw away the oldest sample, add the new sample to the data and compute the average.

The weighted moving average is a modified version of the simple moving average where each sample can be assigned a weight depending on the application or analysis. Typically, the most recent sample is assigned the highest weight and the least recent sample assigned the lowest weight.

The exponential weighted moving average is another version of the weighted moving average where the weights increase or decrease exponentially.

I have two C++ wrapper classes - CSimpleMovingAverage for simple moving average and CWeightedMovingAverage for weighted moving average. Both classes derive from CMovingAverage so that they present a uniform interface. This way someone can add more complex moving averages to the "library".

Using the Code

In order to use the CSimpleMovingAverage or CWeightedMovingAverage, you must first create the object like this:

C++
CSimpleMovingAverage movingAvgCtr;

Both classes implement only two methods which are quite self explanatory:

C++
virtual void AddSample(const float a_fSample) = 0;
virtual float GetAverage() const = 0;

By default, the classes will measure the average over the most recent 10 samples. Suppose you want to have a bigger sample interval, you can pass the desired sample size to the constructor:

C++
CSimpleMovingAverage movingAvgCtr(20);

Now to start getting the average, you will need to add the samples using the AddSample method:

C++
movingAvgCtr.AddSample(44.5);

You can get the moving average at any point in time by using the GetAverage() method.

Implementation Details

Internally the samples are maintained using an STL queue container. Since after acquiring N samples we need to throw away the least recent sample and add the new sample, I found the queue's push, pop, front and back operations very intuitive while coding.

Points of Interest

The reason I wrote up this code is because when I needed it I could not find any C++ code on the web. I know these classes are lacking in some aspects. In CWeightedMovingAverage, I assign the most recent sample the highest weight, someone may want to do the reverse and assign the least recent sample the highest weight (Why? It depends on the application, unfortunately my understanding of statistics is not good, so I don't know). And of course, the exponential moving average is also missing from the library.

One of my weak points is that I do not write enough automated test cases. In main.cpp, I have written a minimalistic test case which I used to verify the correctness of the code. I got weird values for weighted moving averages when I included negative sample values in the data. I am not sure if it is a bug in the code or if they are expected results?

History

  • 28th April, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionQueue Containers Pin
Member 1057449913-Jan-15 17:00
Member 1057449913-Jan-15 17:00 
Questionfile is not found error Pin
kk2mkk16-Feb-14 20:03
kk2mkk16-Feb-14 20:03 
AnswerRe: file is not found error Pin
kk2mkk16-Feb-14 20:10
kk2mkk16-Feb-14 20:10 
Questionfile not found Pin
Member 249785726-Feb-13 1:40
Member 249785726-Feb-13 1:40 
GeneralBoost.Accumulators Pin
Gast12828-Apr-09 12:14
Gast12828-Apr-09 12:14 
I haven't read the article thorougly, buy doesn't it already exist in the Boost libraries by means of the accumulator framework? In our company we also use some kind of running statistics for years.

http://www.boost.org/doc/libs/1_38_0/doc/html/accumulators.html[^]
GeneralRe: Boost.Accumulators Pin
Member 251663828-Apr-09 15:59
Member 251663828-Apr-09 15:59 
GeneralRe: Boost.Accumulators Pin
Gast12829-Apr-09 7:51
Gast12829-Apr-09 7:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.