Skip to content
Snippets Groups Projects
Commit 4bceccf4 authored by Long Bui's avatar Long Bui
Browse files

implement ReplayFile mean and variance calculation + some tests

parent 22818aa6
No related branches found
No related tags found
1 merge request!1Randomvarhelper
package tools.descartes.dml.mm.applicationlevel.functions.util;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.math3.distribution.EnumeratedIntegerDistribution;
import org.apache.commons.math3.distribution.EnumeratedRealDistribution;
......@@ -71,6 +78,19 @@ public class RandomVariableHelper {
return _getNumericalVariance(probfn);
}
private static Double _getNumericalVariance(ReplayFile replay) {
double variance = 0;
try {
Collection<Double> samples = collectSamples(replay);
double mean = _getNumericalMean(samples);
variance = samples.parallelStream().mapToDouble(sample -> Math.pow(sample, 2)).average().getAsDouble()
- Math.pow(mean, 2);
} catch (IOException e) {
e.printStackTrace();
}
return variance;
}
private static Double _getNumericalVariance(EnumSampleList enumSl) {
throw new UnsupportedOperationException("Enums do not have a numerical variance.");
}
......@@ -158,8 +178,14 @@ public class RandomVariableHelper {
}
private static Double _getNumericalMean(ReplayFile replay) {
double sum = 0;
throw new UnsupportedOperationException("File Path: " + replay.getFilepath());
double mean = 0;
try {
mean = _getNumericalMean(collectSamples(replay));
} catch (IOException e) {
// TODO consult for proper error handling
e.printStackTrace();
}
return mean;
}
private static Double _getNumericalMean(ProbabilityMassFunction pmf) {
......@@ -246,4 +272,17 @@ public class RandomVariableHelper {
throw new IllegalStateException("Unknown type of ProbabilityFunction: " + probfn.getClass().getName());
}
private static Collection<Double> collectSamples(ReplayFile replay) throws IOException {
List<Double> samples = new ArrayList<Double>();
BufferedReader br = new BufferedReader(new FileReader(replay.getFilepath()));
while (br.ready()) {
samples.add(Double.parseDouble(br.readLine()));
}
br.close();
return samples;
}
private static Double _getNumericalMean(Collection<Double> samples) {
return samples.parallelStream().mapToDouble(sample -> sample).average().getAsDouble();
}
}
package tools.descartes.dml.mm.applicationlevel.functions.util.tests;
import org.junit.Assert;
import org.junit.Test;
import tools.descartes.dml.mm.applicationlevel.functions.RandomVariable;
import tools.descartes.dml.mm.applicationlevel.functions.util.RandomVariableHelper;
public class ReplayMeanTest extends AbstractRandomVariableHelperTest {
@Test
public void replayFileUnaryTest() {
RandomVariable randomVariable = loadModel("testfiles/replay-unary.functions", RandomVariable.class);
Assert.assertEquals(10, RandomVariableHelper.getNumericalMean(randomVariable.getProbFunction()), 0.0001);
}
@Test
public void replayFileSequenceTest() {
RandomVariable randomVariable = loadModel("testfiles/replay-sequence.functions", RandomVariable.class);
Assert.assertEquals(-4.89, RandomVariableHelper.getNumericalMean(randomVariable.getProbFunction()), 0.01);
}
}
package tools.descartes.dml.mm.applicationlevel.functions.util.tests;
import org.junit.Assert;
import org.junit.Test;
import tools.descartes.dml.mm.applicationlevel.functions.RandomVariable;
import tools.descartes.dml.mm.applicationlevel.functions.util.RandomVariableHelper;
public class ReplayVarianceTest extends AbstractRandomVariableHelperTest {
@Test
public void replayFileUnaryTest() {
RandomVariable randomVariable = loadModel("testfiles/replay-unary.functions", RandomVariable.class);
Assert.assertEquals(0, RandomVariableHelper.getNumericalVariance(randomVariable.getProbFunction()), 0.0001);
}
@Test
public void replayFileSequenceTest() {
RandomVariable randomVariable = loadModel("testfiles/replay-sequence.functions", RandomVariable.class);
Assert.assertEquals(3250.2379, RandomVariableHelper.getNumericalVariance(randomVariable.getProbFunction()),
0.0001);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<functions:RandomVariable xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:functions="http://www.descartes.tools/metamodel/functions/1.0">
<probFunction xsi:type="functions:ReplayFile" Filepath="/Users/longbui/Downloads/sequence.csv"/>
</functions:RandomVariable>
<?xml version="1.0" encoding="UTF-8"?>
<functions:RandomVariable xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:functions="http://www.descartes.tools/metamodel/functions/1.0">
<probFunction xsi:type="functions:ReplayFile" Filepath="/Users/longbui/Downloads/test.csv"/>
</functions:RandomVariable>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment