-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matthew Goldsmith
committed
Apr 25, 2018
1 parent
fa7ba41
commit 1ede3f9
Showing
8 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
60 changes: 60 additions & 0 deletions
60
osc-project-6/Exchange/src/osdi/loyola/index/MathUtils.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package osdi.loyola.index; | ||
|
||
import java.util.LinkedList; | ||
|
||
/** | ||
* Math Utilities. | ||
*/ | ||
public class MathUtils | ||
{ | ||
/** | ||
* Simple Moving Average | ||
*/ | ||
public static class SMA | ||
{ | ||
private LinkedList values = new LinkedList(); | ||
|
||
private int length; | ||
|
||
private double sum = 0; | ||
|
||
private double average = 0; | ||
|
||
/** | ||
* | ||
* @param length the maximum length | ||
*/ | ||
public SMA(int length) | ||
{ | ||
if (length <= 0) | ||
{ | ||
throw new IllegalArgumentException("length must be greater than zero"); | ||
} | ||
this.length = length; | ||
} | ||
|
||
public double currentAverage() | ||
{ | ||
return average; | ||
} | ||
|
||
/** | ||
* Compute the moving average. | ||
* Synchronised so that no changes in the underlying data is made during calculation. | ||
* @param value The value | ||
* @return The average | ||
*/ | ||
public synchronized double compute(double value) | ||
{ | ||
if (values.size() == length && length > 0) | ||
{ | ||
sum -= ((Double) values.getFirst()).doubleValue(); | ||
values.removeFirst(); | ||
} | ||
sum += value; | ||
values.addLast(new Double(value)); | ||
average = sum / values.size(); | ||
return average; | ||
} | ||
} | ||
} |