[Solved] How to calculate a decaying average? [closed]

Introduction

A decaying average is a type of moving average that gives more weight to recent data points and less weight to older data points. This type of average is useful for tracking trends in data that changes over time. In this article, we will discuss how to calculate a decaying average and provide an example of how to use it.

Solution

A decaying average can be calculated using the following formula:

New Average = (Previous Average * (n-1) + New Value) / n

Where n is the number of values in the average.

For example, if you have an average of 10 and you want to add a new value of 15, the new average would be calculated as follows:

New Average = (10 * (2-1) + 15) / 2
New Average = (10 + 15) / 2
New Average = 25 / 2
New Average = 12.5


Here’s a simplistic implementation of what you asking.

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

int normalize_weights(double *weights, size_t num_weights)
{
  double sum = 0;

  for (size_t i = 0; i < num_weights; ++i)
  {
    if (weights[i] < 0)
      return -1;

    sum += weights[i];
  }

  if (sum == 0)
    return -1;

  for (size_t i = 0; i < num_weights; ++i)
    weights[i] /= sum;

  return 0;
}

int weighted_avg(double *avg_ptr, const double *samples, size_t num_samples, const double *weights, size_t num_weights)
{
  if (num_samples != num_weights)
    return -1;

  double avg = 0;

  for (size_t i = 0; i < num_weights; ++i)
    avg += samples[i] * weights[i];

  *avg_ptr = avg;

  return 0;
}

int main(int argc, char **argv)
{
  double weights[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; 
  double samples[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  double avg;

  if (normalize_weights(weights, sizeof(weights) / sizeof(weights[0])))
    abort();

  for (size_t i = 0; i < sizeof(weights) / sizeof(weights[0]); ++i)
    printf("Normalized weight[%u] = %lf\n", (unsigned) i, weights[i]);

  if (weighted_avg(&avg, samples, sizeof(samples) / sizeof(samples[0]), weights, sizeof(weights) / sizeof(weights[0])))
    abort();

  printf("\nWeighted average is %lf\n", avg);

  return 0;
}

solved How to calculate a decaying average? [closed]


Solved: How to Calculate a Decaying Average

Calculating a decaying average is a useful tool for tracking changes in data over time. It is especially useful when the data is volatile and changes rapidly. A decaying average is calculated by taking the average of the most recent data points and giving them more weight than older data points. This allows the average to more accurately reflect the current state of the data.

Steps to Calculate a Decaying Average

  1. Gather the data points you want to use to calculate the average.
  2. Calculate the average of the data points.
  3. Assign a weight to each data point. The most recent data points should have a higher weight than older data points.
  4. Multiply each data point by its weight.
  5. Add up the weighted data points.
  6. Divide the sum of the weighted data points by the sum of the weights.
  7. The result is the decaying average.

Example

Let’s say you want to calculate the decaying average of the last five days of stock prices for a company. The data points are as follows:

  • Day 1: $10
  • Day 2: $12
  • Day 3: $14
  • Day 4: $16
  • Day 5: $18

The average of the data points is $14. To calculate the decaying average, we assign a weight to each data point. We’ll assign a weight of 5 to the most recent data point (Day 5) and a weight of 1 to the oldest data point (Day 1). The weights for the other data points can be calculated using a linear scale.

  • Day 1: $10 x 1 = $10
  • Day 2: $12 x 2 = $24
  • Day 3: $14 x 3 = $42
  • Day 4: $16 x 4 = $64
  • Day 5: $18 x 5 = $90

The sum of the weighted data points is $220. The sum of the weights is 15. The decaying average is calculated by dividing the sum of the weighted data points by the sum of the weights:

$220 / 15 = $14.67

The decaying average of the last five days of stock prices is $14.67.

Conclusion

Calculating a decaying average is a useful tool for tracking changes in data over time. It is especially useful when the data is volatile and changes rapidly. By assigning a weight to each data point, the most recent data points are given more weight than older data points, allowing the average to more accurately reflect the current state of the data.