1. Overview

Moving averages are a fundamental tool in analyzing trends and patterns in data and are widely used in finance, economics, and engineering.

They help smooth out short-term fluctuations and reveal underlying trends, making data easier to interpret.

In this tutorial, we’ll explore various methods and techniques for calculating moving averages, ranging from traditional approaches to libraries and Stream API’s.

2. Common Methods for Calculating Moving Averages

In this section, we’ll explore three common methods for calculating moving averages.

2.1. Using Apache Commons Math Library

Apache Commons Math is a powerful Java library that offers a wide range of mathematical and statistical functions, including tools for calculating moving averages.

By utilizing the DescriptiveStatistics class from the Apache Commons Math library, we can streamline the process of moving average computation and leverage optimized algorithms for efficient data processing. It adds data points to the statistics object and retrieves the mean, which represents the moving average.

Let’s use the DescriptiveStatistics class to calculate a moving average with a windowSize:

public class MovingAverageWithApacheCommonsMath {

    private final DescriptiveStatistics stats;

    public MovingAverageWithApacheCommonsMath(int windowSize) {
        this.stats = new DescriptiveStatistics(windowSize);
    }

    public void add(double value) {
        stats.addValue(value);
    }

    public double getMovingAverage() {
        return stats.getMean();
    }
}

Let’s test our implementation:

@Test
public void whenValuesAreAdded_shouldUpdateAverageCorrectly() {
    MovingAverageWithApacheCommonsMath movingAverageCalculator = new MovingAverageWithApacheCommonsMath(3);
    movingAverageCalculator.add(10);
    assertEquals(10.0, movingAverageCalculator.getMovingAverage(), 0.001);
    movingAverageCalculator.add(20);
    assertEquals(15.0, movingAverageCalculator.getMovingAverage(), 0.001);
    movingAverageCalculator.add(30);
    assertEquals(20.0, movingAverageCalculator.getMovingAverage(), 0.001);
}

First, we create an instance of the MovingAverageWithApacheCommonsMath class with a window size of 3. Then, three values (10, 20, and 30) are added to the calculator individually, and its mean is verified.

2.2. Using Circular Buffer Approach

The circular buffer approach is a classic method for calculating moving averages and is known for its efficient memory usage. This approach is straightforward and may offer better performance in some cases, especially if we’re concerned about the overhead of external dependencies.

In this approach, new data points overwrite the oldest ones, and the average is calculated based on the current elements in the buffer.

By cycling through the buffer circularly, we can achieve a constant time complexity for each update, making it suitable for real-time data processing applications.

Let’s calculate the moving average using a circular buffer:

public class MovingAverageByCircularBuffer {

    private final double[] buffer;
    private int head;
    private int count;

    public MovingAverageByCircularBuffer(int windowSize) {
        this.buffer = new double[windowSize];
    }

    public void add(double value) {
        buffer[head] = value;
        head = (head + 1) % buffer.length;
        if (count < buffer.length) {
            count++;
        }
    }

    public double getMovingAverage() {
        if (count == 0) {
            return Double.NaN;
        }
        double sum = 0;
        for (int i = 0; i < count; i++) {
            sum += buffer[i];
        }
        return sum / count;
    }
}

Let’s write a test case to verify the method:

@Test
public void whenValuesAreAdded_shouldUpdateAverageCorrectly() {
    MovingAverageByCircularBuffer ma = new MovingAverageByCircularBuffer(3);
    ma.add(10);
    assertEquals(10.0, ma.getMovingAverage(), 0.001);
    ma.add(20);
    assertEquals(15.0, ma.getMovingAverage(), 0.001);
    ma.add(30);
    assertEquals(20.0, ma.getMovingAverage(), 0.001);
}

We create an instance of the MovingAverageByCircularBuffer class with a window size of 3. After adding each value, the test asserts that the calculated moving average matches the expected value with a tolerance of 0.001.

2.3. Using Exponential Moving Average

Another approach is to use exponential smoothing to calculate the moving average.

Exponential smoothing assigns exponentially decreasing weights to older observations, which can be useful for capturing trends and reacting quickly to changes in the data:

public class ExponentialMovingAverage {

    private double alpha;
    private Double previousEMA;

    public ExponentialMovingAverage(double alpha) {
        if (alpha <= 0 || alpha > 1) {
            throw new IllegalArgumentException("Alpha must be in the range (0, 1]");
        }
        this.alpha = alpha;
        this.previousEMA = null;
    }

    public double calculateEMA(double newValue) {
        if (previousEMA == null) {
            previousEMA = newValue;
        } else {
            previousEMA = alpha * newValue + (1 - alpha) * previousEMA;
        }
        return previousEMA;
    }
}

Here, the alpha parameter controls the rate of decay, with smaller values giving more weight to recent observations.

Exponential moving averages are particularly useful when we want to react quickly to changes in the data while still capturing longer-term trends.

Let’s verify it with a test case:

@Test
public void whenValuesAreAdded_shouldUpdateExponentialMovingAverageCorrectly() {
    ExponentialMovingAverage ema = new ExponentialMovingAverage(0.4);
    assertEquals(10.0, ema.calculateEMA(10.0), 0.001);
    assertEquals(14.0, ema.calculateEMA(20.0), 0.001);
    assertEquals(20.4, ema.calculateEMA(30.0), 0.001);
}

We first create an instance of ExponentialMovingAverage (EMA) with a smoothing factor (alpha) of 0.4.

Then, as each value is added, the test asserts that the calculated EMA matches the expected value within a small tolerance of 0.001.

2.4. Stream-Based Approach

We can leverage the Stream API to calculate moving averages in a more functional and declarative manner. This approach is particularly useful if we want to deal with data streams or collections.

Here’s a simplified example of how we can use a stream-based approach to calculate a moving average:

public class MovingAverageWithStreamBasedApproach {
    private int windowSize;

    public MovingAverageWithStreamBasedApproach(int windowSize) {
        this.windowSize = windowSize;
    }
    public double calculateAverage(double[] data) {
        return DoubleStream.of(data)
                .skip(Math.max(0, data.length - windowSize))
                .limit(Math.min(data.length, windowSize))
                .summaryStatistics()
                .getAverage();
    }
}

Here , we created a stream from the input data array, skipping elements outside the specified window size, limiting the stream to the windowSize, and then using summaryStatistics() to calculate the average.

This approach leverages the functional programming capabilities of Java Streams API to perform the calculation in a concise and efficient manner.

Now, let’s write some JUnit tests to make sure our code works as expected:

@Test
public void whenValidDataIsPassed_shouldReturnCorrectAverage() {
    double[] data = {10, 20, 30, 40, 50};
    int windowSize = 3;
    double expectedAverage = 40;
    MovingAverageWithStreamBasedApproach calculator = new MovingAverageWithStreamBasedApproach(windowSize);
    double actualAverage = calculator.calculateAverage(data);
    assertEquals(expectedAverage, actualAverage);
}

In these tests, we check if our calculateAverage() method returns the correct average for given scenarios such as valid data and windowSize.

3. Additional Methods

While the above methods are some of the more convenient and efficient ways to calculate moving averages in Java, there are alternative approaches we can consider depending on our specific requirements and constraints. Here, we’ll introduce two such approaches.

3.1. Parallel Processing

If performance is our top priority and we have access to multiple CPU cores, we can utilize parallel processing techniques to compute moving averages more efficiently.

Java provides support for parallel streams, which can automatically distribute computation across multiple threads.

3.2. Weighted Moving Average

The Weighted Moving Average (WMA) is a method for calculating moving averages that assigns different weights to each data point within the window.

The weights are typically determined based on predefined criteria such as significance, relevance, or proximity to the center of the window.

3.3. Cumulative Moving Average

The Cumulative Moving Average (CMA) calculates the average of all data points up to a certain point in time. Unlike other moving average methods, CMA does not use a fixed-size window but includes all available data.

4. Conclusion

Calculating moving averages is a fundamental aspect of time-series analysis, with applications spanning various domains such as finance, economics, and engineering.

Using Apache Commons Math, circular buffer, and exponential moving average techniques, analysts can gain valuable insights into their data’s underlying trends and patterns.

Moreover, exploring weighted and cumulative moving averages expands the analyst’s toolkit, enabling more sophisticated analysis and interpretation of time-series data.

Again, the choice depends entirely on specific project requirements and preferences.

As always, the source code is available over on GitHub.