-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
estimate the start time by variance (#257)
- Loading branch information
Showing
7 changed files
with
139 additions
and
13 deletions.
There are no files selected for viewing
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
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
11 changes: 11 additions & 0 deletions
11
api/src/main/java/com/microsoft/gctoolkit/online/statistics/NotEnoughSampleException.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,11 @@ | ||
package com.microsoft.gctoolkit.online.statistics; | ||
|
||
public class NotEnoughSampleException extends ArithmeticException { | ||
public NotEnoughSampleException() { | ||
super(); | ||
} | ||
|
||
public NotEnoughSampleException(String s) { | ||
super(s); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
api/src/main/java/com/microsoft/gctoolkit/online/statistics/OnlineMeanCalculator.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,16 @@ | ||
package com.microsoft.gctoolkit.online.statistics; | ||
|
||
public class OnlineMeanCalculator implements OnlineStatisticsCalculator { | ||
private int numSamples = 0; | ||
private double mean = 0.0; | ||
|
||
public void update(double sampleValue) { | ||
numSamples++; | ||
mean += (sampleValue - mean) / numSamples; | ||
} | ||
|
||
@Override | ||
public double getValue() { | ||
return mean; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
api/src/main/java/com/microsoft/gctoolkit/online/statistics/OnlineStatisticsCalculator.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,19 @@ | ||
package com.microsoft.gctoolkit.online.statistics; | ||
|
||
public interface OnlineStatisticsCalculator { | ||
|
||
|
||
/** | ||
* Updates the statistics calculation with the given value. | ||
* <p> | ||
* For example, if the statistics calculation is a mean, this method would update the mean with the given value. | ||
* | ||
* @param sampleValue the value to be added to the statistics calculation | ||
*/ | ||
void update(double sampleValue); | ||
|
||
/** | ||
* @return the value of the statistics calculation | ||
*/ | ||
double getValue(); | ||
} |
27 changes: 27 additions & 0 deletions
27
api/src/main/java/com/microsoft/gctoolkit/online/statistics/WelfordVarianceCalculator.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,27 @@ | ||
package com.microsoft.gctoolkit.online.statistics; | ||
|
||
public class WelfordVarianceCalculator implements OnlineStatisticsCalculator { | ||
private int numSamples = 0; | ||
private double m2 = 0.0; | ||
private final OnlineMeanCalculator onlineMeanCalculator = new OnlineMeanCalculator(); | ||
|
||
@Override | ||
public void update(double sampleValue) { | ||
double oldMean = onlineMeanCalculator.getValue(); | ||
|
||
onlineMeanCalculator.update(sampleValue); | ||
numSamples++; | ||
|
||
double newMean = onlineMeanCalculator.getValue(); | ||
|
||
m2 += (sampleValue - oldMean) * (sampleValue - newMean); | ||
} | ||
|
||
@Override | ||
public double getValue() throws NotEnoughSampleException { | ||
if (numSamples < 2) { | ||
throw new NotEnoughSampleException("Variance requires at least 2 samples."); | ||
} | ||
return m2 / (numSamples - 1); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...rc/test/java/com/microsoft/gctoolkit/online/statistics/WelfordVarianceCalculatorTest.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,34 @@ | ||
package com.microsoft.gctoolkit.online.statistics; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class WelfordVarianceCalculatorTest { | ||
|
||
@Test | ||
void insufficientSamples() { | ||
WelfordVarianceCalculator calculator = new WelfordVarianceCalculator(); | ||
calculator.update(1.23d); | ||
assertThrows(NotEnoughSampleException.class, calculator::getValue); | ||
} | ||
|
||
@Test | ||
void getVariance() { | ||
WelfordVarianceCalculator calculator = new WelfordVarianceCalculator(); | ||
calculator.update(1421.23); | ||
calculator.update(2897.34); | ||
calculator.update(3907.45); | ||
assertEquals(1563418.8054333332, calculator.getValue(), 0.0001d); | ||
} | ||
|
||
@Test | ||
void getVarianceWithSmallDifference() { | ||
WelfordVarianceCalculator calculator = new WelfordVarianceCalculator(); | ||
calculator.update(71899123.1273789); | ||
calculator.update(71899123.1378323); | ||
calculator.update(71899123.1478654); | ||
assertEquals(0.00010493893, calculator.getValue(), 0.0001d); | ||
} | ||
|
||
} |