Created
December 3, 2024 09:09
-
-
Save antoniomaria/51e7a2377f1943c49cc081439744d3ec to your computer and use it in GitHub Desktop.
Java Algorithm for Rolling or Moving Average
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ExponentialMovingAverage { | |
| private double alpha; // Smoothing factor 0-1. 1 calculate the average as latest value. | |
| private Double average; | |
| public ExponentialMovingAverage(double alpha) { | |
| this.alpha = alpha; | |
| } | |
| public void add(double value) { | |
| if (average == null) { | |
| average = value; // Initialize average with the first value | |
| } else { | |
| average = alpha * value + (1 - alpha) * average; | |
| } | |
| } | |
| public Double getAverage() { | |
| return average; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Value: 50.000000 Classic 50.000000 WEL 50.000000 EMA 50.000000 | |
| Value: 51.000000 Classic 50.500000 WEL 50.500000 EMA 50.100000 | |
| Value: 52.000000 Classic 51.000000 WEL 51.000000 EMA 50.290000 | |
| Value: 49.000000 Classic 50.500000 WEL 50.500000 EMA 50.161000 | |
| Value: 48.000000 Classic 50.000000 WEL 50.000000 EMA 49.944900 | |
| Value: 47.000000 Classic 49.500000 WEL 49.500000 EMA 49.650410 | |
| Value: 46.000000 Classic 49.000000 WEL 49.000000 EMA 49.285369 | |
| Value: 50.000000 Classic 49.125000 WEL 49.125000 EMA 49.356832 | |
| Value: 55.000000 Classic 49.777778 WEL 49.777778 EMA 49.921149 | |
| Value: 60.000000 Classic 50.800000 WEL 50.800000 EMA 50.929034 | |
| Value: 70.000000 Classic 52.545455 WEL 52.720000 EMA 52.836131 | |
| Value: 76.000000 Classic 54.500000 WEL 55.048000 EMA 55.152518 | |
| Value: 80.000000 Classic 56.461538 WEL 57.543200 EMA 57.637266 | |
| Value: 82.000000 Classic 58.285714 WEL 59.988880 EMA 60.073539 | |
| Value: 83.000000 Classic 59.933333 WEL 62.289992 EMA 62.366185 | |
| Value: 90.000000 Classic 61.812500 WEL 65.060993 EMA 65.129567 | |
| Value: 200.000000 Classic 69.941176 WEL 78.554894 EMA 78.616610 | |
| Value: 250.000000 Classic 79.944444 WEL 95.699404 EMA 95.754949 | |
| Value: 300.000000 Classic 91.526316 WEL 116.129464 EMA 116.179454 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Seen in https://stackoverflow.com/questions/12636613/how-to-calculate-moving-average-without-keeping-the-count-and-data-total | |
| public class WeldfordAverage { | |
| private double average = 0; // Current average | |
| private double count = 0; // Number of elements | |
| public void add(double value) { | |
| count++; | |
| if (count == 0) { | |
| this.average = value; | |
| } else { | |
| this.average = this.average + (value - average) / count; | |
| } | |
| } | |
| public Double getAverage() { | |
| return average; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class WeldfordMovingAverage { | |
| public static final int WINDOWS_SIZE = 10; | |
| private double average = 0; | |
| private double count = 0; | |
| public void add(double value) { | |
| count++; | |
| double factor = Math.min(count, WINDOWS_SIZE); | |
| if (count == 0) { | |
| this.average = value; | |
| } else { | |
| this.average = this.average + (value - average) / factor; | |
| } | |
| } | |
| public Double getAverage() { | |
| return average; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment