calculating Moving averages - review fixes

This commit is contained in:
Jagath Kumar
2024-04-08 17:03:26 +05:30
parent 80f0ac6b10
commit ac32eebb53
11 changed files with 66 additions and 14 deletions
@@ -1,28 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-calculating-moving-averages</artifactId>
<name>core-java-calculating-moving-averages</name>
<packaging>war</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<commons-math3.version>3.6.1</commons-math3.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>${commons-math3.version}</version>
</dependency>
</dependencies>
</project>
@@ -1,24 +0,0 @@
package com.baeldung;
public class ExponentialMovingAverage {
private final 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;
}
}
@@ -1,31 +0,0 @@
package com.baeldung;
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;
}
}
@@ -1,21 +0,0 @@
package com.baeldung;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
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();
}
}
@@ -1,39 +0,0 @@
package com.baeldung;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ExponentialMovingAverageUnitTest {
@Test(expected = IllegalArgumentException.class)
public void whenAlphaIsInvalid_shouldThrowException() {
new ExponentialMovingAverage(0);
}
@Test
public void whenFirstValueIsAdded_shouldHaveExponentialMovingAverageSameAsValue() {
ExponentialMovingAverage ema = new ExponentialMovingAverage(0.5);
assertEquals(10.0, ema.calculateEMA(10.0), 0.001);
}
@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);
}
@Test
public void whenAlphaIsCloserToOne_exponentialMovingAverageShouldRespondFasterToChanges() {
ExponentialMovingAverage ema1 = new ExponentialMovingAverage(0.2);
ExponentialMovingAverage ema2 = new ExponentialMovingAverage(0.8);
assertEquals(10.0, ema1.calculateEMA(10.0), 0.001);
assertEquals(10.0, ema2.calculateEMA(10.0), 0.001);
assertEquals(12.0, ema1.calculateEMA(20.0), 0.001);
assertEquals(18.0, ema2.calculateEMA(20.0), 0.001);
}
}
@@ -1,35 +0,0 @@
package com.baeldung;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MovingAverageByCircularBufferUnitTest {
@Test
public void whenInitialAverageIsCalculated_shouldReturnNAN() {
MovingAverageByCircularBuffer ma = new MovingAverageByCircularBuffer(5);
assertEquals(Double.NaN, ma.getMovingAverage(), 0.001);
}
@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);
}
@Test
public void whenWindowSizeIsFull_shouldOverrideTheOldestValue() {
MovingAverageByCircularBuffer ma = new MovingAverageByCircularBuffer(2);
ma.add(10);
ma.add(20);
assertEquals(15.0, ma.getMovingAverage(), 0.001);
ma.add(30);
assertEquals(25.0, ma.getMovingAverage(), 0.001);
}
}
@@ -1,36 +0,0 @@
package com.baeldung;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MovingAverageWithApacheCommonsMathUnitTest {
@Test
public void whenInitialAverageIsCalculated_shouldReturnNAN() {
MovingAverageWithApacheCommonsMath movingAverageCalculator = new MovingAverageWithApacheCommonsMath(5);
assertEquals(Double.NaN, movingAverageCalculator.getMovingAverage(), 0.001);
}
@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);
}
@Test
public void whenWindowSizeIsFull_shouldDropTheOldestValue() {
MovingAverageWithApacheCommonsMath ma = new MovingAverageWithApacheCommonsMath(2);
ma.add(10);
ma.add(20);
assertEquals(15.0, ma.getMovingAverage(), 0.001);
ma.add(30);
assertEquals(25.0, ma.getMovingAverage(), 0.001);
}
}
-1
View File
@@ -224,7 +224,6 @@
<module>java-rmi</module>
<module>java-spi</module>
<module>java-websocket</module>
<module>core-java-calculating-moving-averages</module>
</modules>
<dependencies>