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

change BoxedPDF Variance calculation now unsupported, fix invalid ReplayFile...

change BoxedPDF Variance calculation now unsupported, fix invalid ReplayFile path now throws exception
parent 4bceccf4
No related branches found
No related tags found
1 merge request!1Randomvarhelper
......@@ -86,7 +86,7 @@ public class RandomVariableHelper {
variance = samples.parallelStream().mapToDouble(sample -> Math.pow(sample, 2)).average().getAsDouble()
- Math.pow(mean, 2);
} catch (IOException e) {
e.printStackTrace();
throw new IllegalArgumentException("Specified ReplayFile does not exist!");
}
return variance;
}
......@@ -138,15 +138,7 @@ public class RandomVariableHelper {
}
private static Double _getNumericalVariance(BoxedPDF bpdf) {
double lastValue = 0;
double meanSquaredSamples = 0.0;
double numericalMean = _getNumericalMean(bpdf);
for (ContinuousSample cs : bpdf.getSample()) {
meanSquaredSamples += cs.getProbability().doubleValue()
* Math.pow((lastValue + (cs.getValue().doubleValue() - lastValue) / 2), 2);
lastValue = cs.getValue().doubleValue();
}
return meanSquaredSamples - Math.pow(numericalMean, 2);
throw new UnsupportedOperationException("BoxedPDF variance is unsupported!");
}
private static Double _getNumericalVariance(NormalDistribution nd) {
......@@ -182,8 +174,7 @@ public class RandomVariableHelper {
try {
mean = _getNumericalMean(collectSamples(replay));
} catch (IOException e) {
// TODO consult for proper error handling
e.printStackTrace();
throw new IllegalArgumentException("Specified ReplayFile does not exist!");
}
return mean;
}
......
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 BoxedPDFVarianceTest extends AbstractRandomVariableHelperTest {
@Test
public void boxedPdfUnaryTest() {
@Test(expected = UnsupportedOperationException.class)
public void boxedPdfUnsupportedTest() {
RandomVariable randomVariable = loadModel("testfiles/boxed-pdf-unary.functions", RandomVariable.class);
Assert.assertEquals(2.08, RandomVariableHelper.getNumericalVariance(randomVariable.getProbFunction()), 0.00001);
}
@Test
public void boxedPdfSimpleTest() {
RandomVariable randomVariable = loadModel("testfiles/boxed-pdf-four-samples.functions", RandomVariable.class);
Assert.assertEquals(2.83, RandomVariableHelper.getNumericalVariance(randomVariable.getProbFunction()), 0.00001);
}
@Test
public void boxedPdfZeroProbIntervalsTest() {
RandomVariable randomVariable = loadModel("testfiles/boxed-pdf-zero-intervals.functions", RandomVariable.class);
Assert.assertEquals(2.33, RandomVariableHelper.getNumericalVariance(randomVariable.getProbFunction()), 0.00001);
RandomVariableHelper.getNumericalVariance(randomVariable.getProbFunction());
}
}
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