Calculate rolling / moving average in C++

前端 未结 10 2056
攒了一身酷
攒了一身酷 2020-12-02 08:09

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?

But I really would

相关标签:
10条回答
  • 2020-12-02 08:47

    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.

    0 讨论(0)
  • 2020-12-02 08:50

    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();
    }
    
    0 讨论(0)
  • 2020-12-02 08:51

    You simply need a circular array (circular buffer) 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.

    0 讨论(0)
  • 2020-12-02 08:51

    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);
        }
    
    };
    
    0 讨论(0)
  • 2020-12-02 08:53

    Incrementing on @Nilesh's answer (credit goes to him), you can:

    • keep track of the sum, no need to divide and then multiply every time, generating error
    • avoid if conditions using % operator

    This is UNTESTED sample code to show the idea, it could also be wrapped into a class:

    const unsigned int size=10; // ten elements buffer
    
    unsigned int counterPosition=0;
    unsigned int counterNum=0;
    
    int buffer[size];
    long sum=0;
    
    void reset() {
        for(int i=0;i<size;i++) {
            buffer[i]=0;
        }
    }
    
    float addValue(int value) {
        unsigned  int oldPos = ((counterPosition + 1) % size);
    
        buffer[counterPosition] = value;
        sum = (sum - buffer[oldPos] + value); 
    
        counterPosition=(counterPosition+1) % size;
        if(counterNum<size) counterNum++;
    
        return ((float)sum)/(float)counterNum;
    }
    
    float removeValue() {
        unsigned  int oldPos =((counterPosition + 1) % size);
    
        buffer[counterPosition] = 0;
        sum = (sum - buffer[oldPos]); 
    
        if(counterNum>1) { // leave one last item at the end, forever
            counterPosition=(counterPosition+1) % size;
            counterNum--; // here the two counters are different
        }
        return ((float)sum)/(float)counterNum;
    }
    

    It should be noted that, if the buffer is reset to all zeroes, this method works fine while receiving the first values in as - buffer[oldPos] is zero and counter grows. First output is the first number received. Second output is the average of only the first two, and so on, fading in the values while they arrive until size items are reached.

    It is also worth considering that this method, like any other for rolling average, is asymmetrical, if you stop at the end of the input array, because the same fading does not happen at the end (it can happen after the end of data, with the right calculations).

    That is correct. The rolling average of 100 elements with a buffer of 10 gives different results: 10 fading in, 90 perfectly rolling 10 elements, and finally 10 fading out, giving a total of 110 results for 100 numbers fed in! It's your choice to decide which ones to show (and if it's better going the straight way, old to recent, or backwards, recent to old).

    To fade out correctly after the end, you can go on adding zeroes one by one and reducing the count of items by one every time until you have reached size elements (still keeping track of correct position of old values).

    Usage is like this:

    int avg=0;
    reset();
    
    avg=addValue(2); // Rpeat for 100 times
    avg=addValue(3); // Use avg value
    
    ...
    
    avg=addValue(-4);
    avg=addValue(12); // last numer, 100th input 
    
    // If you want to fade out repeat 10 times after the end of data:
    
    avg=removeValue(); // Rpeat for last 10 times after data has finished
    avg=removeValue(); // Use avg value
    ...
    avg=removeValue();
    avg=removeValue();
    
    0 讨论(0)
  • 2020-12-02 08:55

    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, size_t N>
    class Moving_Average
    {
      public:
        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;
            }
        }
    
        operator double() const { return total_ / std::min(num_samples_, N); }
    
      private:
        T samples_[N];
        size_t num_samples_{0};
        Total total_{0};
    };
    

    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.

    Issues

    This is a bit flawed in that num_samples_ could conceptually wrap back to 0, but it's hard to imagine anyone having 2^64 samples: if concerned, 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").

    Another issue is inherent with floating point precision, and can be illustrated with a simple scenario for T=double, N=2: we start with total_ = 0, then inject samples...

    • 1E17, we execute total_ += 1E17, so total_ == 1E17, then inject

    • 1, we execute total += 1, but total_ == 1E17 still, as the "1" is too insignificant to change the 64-bit double representation of a number as large as 1E17, then we inject

    • 2, we execute total += 2 - 1E17, in which 2 - 1E17 is evaluated first and yields -1E17 as the 2 is lost to imprecision/insignificance, so to our total of 1E17 we add -1E17 and total_ becomes 0, despite current samples of 1 and 2 for which we'd want total_ to be 3. Our moving average will calculate 0 instead of 1.5. As we add another sample, we'll subtract the "oldest" 1 from total_ despite it never having been properly incorporated therein; our total_ and moving averages are likely to remain wrong.

    You could add code that stores the highest recent total_ and if the current total_ is too small a fraction of that (a template parameter could provide a multiplicative threshold), you recalculate the total_ from all the samples in the samples_ array (and set highest_recent_total_ to the new total_), but I'll leave that to the reader who cares sufficiently.

    0 讨论(0)
提交回复
热议问题