| 1 | package de.uka.ipd.sdq.probfunction.math.test; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertTrue; |
| 5 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.Collections; |
| 8 | import java.util.List; |
| 9 | |
| 10 | import junit.framework.JUnit4TestAdapter; |
| 11 | |
| 12 | import org.eclipse.emf.common.util.EList; |
| 13 | import org.junit.Before; |
| 14 | import org.junit.Test; |
| 15 | |
| 16 | import de.uka.ipd.sdq.probfunction.BoxedPDF; |
| 17 | import de.uka.ipd.sdq.probfunction.ContinuousSample; |
| 18 | import de.uka.ipd.sdq.probfunction.ProbabilityMassFunction; |
| 19 | import de.uka.ipd.sdq.probfunction.ProbfunctionFactory; |
| 20 | import de.uka.ipd.sdq.probfunction.Sample; |
| 21 | import de.uka.ipd.sdq.probfunction.math.IBoxedPDF; |
| 22 | import de.uka.ipd.sdq.probfunction.math.IContinuousSample; |
| 23 | import de.uka.ipd.sdq.probfunction.math.IExponentialDistribution; |
| 24 | import de.uka.ipd.sdq.probfunction.math.IGammaDistribution; |
| 25 | import de.uka.ipd.sdq.probfunction.math.ILognormalDistribution; |
| 26 | import de.uka.ipd.sdq.probfunction.math.IPDFFactory; |
| 27 | import de.uka.ipd.sdq.probfunction.math.IProbabilityDensityFunction; |
| 28 | import de.uka.ipd.sdq.probfunction.math.IProbabilityFunctionFactory; |
| 29 | import de.uka.ipd.sdq.probfunction.math.IProbabilityMassFunction; |
| 30 | import de.uka.ipd.sdq.probfunction.math.ISample; |
| 31 | import de.uka.ipd.sdq.probfunction.math.ISamplePDF; |
| 32 | import de.uka.ipd.sdq.probfunction.math.IUnit; |
| 33 | import de.uka.ipd.sdq.probfunction.math.exception.DoubleSampleException; |
| 34 | import de.uka.ipd.sdq.probfunction.math.exception.FunctionNotInTimeDomainException; |
| 35 | import de.uka.ipd.sdq.probfunction.math.exception.InvalidSampleValueException; |
| 36 | import de.uka.ipd.sdq.probfunction.math.exception.NegativeDistanceException; |
| 37 | import de.uka.ipd.sdq.probfunction.math.exception.ProbabilitySumNotOneException; |
| 38 | import de.uka.ipd.sdq.probfunction.math.exception.UnitNameNotSetException; |
| 39 | import de.uka.ipd.sdq.probfunction.math.exception.UnitNotSetException; |
| 40 | import de.uka.ipd.sdq.probfunction.math.exception.UnknownPDFTypeException; |
| 41 | |
| 42 | import de.uka.ipd.sdq.probfunction.math.impl.ProbabilityFunctionFactoryImpl; |
| 43 | |
| 44 | /** |
| 45 | * @author Ihssane |
| 46 | * |
| 47 | */ |
| 48 | public class ProbabilityFunctionFactoryTest { |
| 49 | |
| 50 | private IBoxedPDF boxed; |
| 51 | |
| 52 | private double err = 1e-10; |
| 53 | |
| 54 | private IProbabilityFunctionFactory pfFactory = IProbabilityFunctionFactory.eINSTANCE; |
| 55 | private ProbfunctionFactory epfFactory = ProbfunctionFactory.eINSTANCE; |
| 56 | |
| 57 | @Before |
| 58 | public void setUp() { |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void boxedToSamplePDF() { |
| 63 | List<IContinuousSample> samples = new ArrayList<IContinuousSample>(); |
| 64 | Collections.addAll(samples, pfFactory.createContinuousSample(0.9, 0.3), |
| 65 | pfFactory.createContinuousSample(1.5, 0.4), pfFactory |
| 66 | .createContinuousSample(1.8, 0.2), pfFactory |
| 67 | .createContinuousSample(2.4, 0.1)); |
| 68 | try { |
| 69 | boxed = pfFactory.createBoxedPDF(samples, null); |
| 70 | } catch (DoubleSampleException e1) { |
| 71 | e1.printStackTrace(); |
| 72 | } |
| 73 | |
| 74 | ISamplePDF samplePDF; |
| 75 | try { |
| 76 | samplePDF = pfFactory.transformToSamplePDF(boxed); |
| 77 | |
| 78 | assertTrue(Math.abs(samplePDF.getDistance() - 0.3) < err); |
| 79 | assertEquals(samplePDF.getValuesAsDouble().size(), 9); |
| 80 | |
| 81 | assertTrue(Math |
| 82 | .abs(samplePDF.getValuesAsDouble().get(0) - 0.3 * 0.15 / 0.9) < err); |
| 83 | assertTrue(Math |
| 84 | .abs(samplePDF.getValuesAsDouble().get(1) - 0.3 * 0.3 / 0.9) < err); |
| 85 | assertTrue(Math |
| 86 | .abs(samplePDF.getValuesAsDouble().get(2) - 0.3 * 0.3 / 0.9) < err); |
| 87 | assertTrue(Math.abs(samplePDF.getValuesAsDouble().get(3) |
| 88 | - ((0.3 * 0.15 / 0.9) + (0.4 * 0.15 / 0.6))) < err); |
| 89 | assertTrue(Math |
| 90 | .abs(samplePDF.getValuesAsDouble().get(4) - 0.4 * 0.3 / 0.6) < err); |
| 91 | assertTrue(Math.abs(samplePDF.getValuesAsDouble().get(5) |
| 92 | - ((0.4 * 0.15 / 0.6) + (0.2 * 0.15 / 0.3))) < err); |
| 93 | assertTrue(Math.abs(samplePDF.getValuesAsDouble().get(6) |
| 94 | - ((0.2 * 0.15 / 0.3) + (0.1 * 0.15 / 0.6))) < err); |
| 95 | assertTrue(Math.abs(samplePDF.getValuesAsDouble().get(7) |
| 96 | - (0.1 * 0.3 / 0.6)) < err); |
| 97 | assertTrue(Math.abs(samplePDF.getValuesAsDouble().get(7) |
| 98 | - (0.1 * 0.3 / 0.6)) < err); |
| 99 | assertTrue(Math.abs(samplePDF.getValuesAsDouble().get(8) |
| 100 | - (0.1 * 0.15 / 0.6)) < err); |
| 101 | } catch (UnknownPDFTypeException e) { |
| 102 | // TODO Auto-generated catch block |
| 103 | e.printStackTrace(); |
| 104 | } |
| 105 | |
| 106 | samplePDF = ((ProbabilityFunctionFactoryImpl) pfFactory) |
| 107 | .transformBoxedToSamplePDF(boxed, 0.5); |
| 108 | assertTrue(Math.abs(samplePDF.getDistance() - 0.5) < err); |
| 109 | assertEquals(samplePDF.getValuesAsDouble().size(), 6); |
| 110 | |
| 111 | assertTrue(Math |
| 112 | .abs(samplePDF.getValuesAsDouble().get(0) - 0.3 * 0.25 / 0.9) < err); |
| 113 | assertTrue(Math |
| 114 | .abs(samplePDF.getValuesAsDouble().get(1) - 0.3 * 0.5 / 0.9) < err); |
| 115 | assertTrue(Math.abs(samplePDF.getValuesAsDouble().get(2) |
| 116 | - ((0.3 * 0.15 / 0.9) + (0.4 * 0.35 / 0.6))) < err); |
| 117 | assertTrue(Math.abs(samplePDF.getValuesAsDouble().get(3) |
| 118 | - ((0.4 * 0.25 / 0.6) + (0.2 * 0.25 / 0.3))) < err); |
| 119 | assertTrue(Math.abs(samplePDF.getValuesAsDouble().get(4) |
| 120 | - ((0.2 * 0.05 / 0.3) + (0.1 * 0.45 / 0.6))) < err); |
| 121 | assertTrue(Math.abs(samplePDF.getValuesAsDouble().get(5) |
| 122 | - (0.1 * 0.15 / 0.6)) < err); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Transforming IProbabilityMassFunction to ProbabilityMassFunction should |
| 127 | * leave samples, unit, and ordered domain intact. |
| 128 | */ |
| 129 | @Test |
| 130 | public void iPMFToPMF() { |
| 131 | IProbabilityMassFunction iProbFunc = pfFactory |
| 132 | .createProbabilityMassFunction(getTestSamples(), getTestUnit(), |
| 133 | true); |
| 134 | |
| 135 | // this is the method to be tested: |
| 136 | ProbabilityMassFunction probFunc = pfFactory |
| 137 | .transformToModelPMF(iProbFunc); |
| 138 | |
| 139 | Sample sample0 = (Sample) probFunc.getSamples().get(0); |
| 140 | assertTrue((Integer) sample0.getValue() == 1); |
| 141 | assertTrue(sample0.getProbability() == 0.1); |
| 142 | Sample sample1 = (Sample) probFunc.getSamples().get(1); |
| 143 | assertTrue((Integer) sample1.getValue() == 2); |
| 144 | assertTrue(sample1.getProbability() == 0.3); |
| 145 | Sample sample2 = (Sample) probFunc.getSamples().get(2); |
| 146 | assertTrue((Integer) sample2.getValue() == 3); |
| 147 | assertTrue(sample2.getProbability() == 0.5); |
| 148 | Sample sample3 = (Sample) probFunc.getSamples().get(3); |
| 149 | assertTrue((Integer) sample3.getValue() == 4); |
| 150 | assertTrue(sample3.getProbability() == 0.1); |
| 151 | |
| 152 | // assertTrue(probFunc.getUnitSpecification().equals("sec")); |
| 153 | assertTrue(probFunc.isOrderedDomain()); |
| 154 | |
| 155 | // |
| 156 | iProbFunc = pfFactory.transformToPMF(probFunc); |
| 157 | |
| 158 | ISample s0 = iProbFunc.getSamples().get(0); |
| 159 | assertTrue((Integer) s0.getValue() == 1); |
| 160 | assertTrue(s0.getProbability() == 0.1); |
| 161 | ISample s1 = iProbFunc.getSamples().get(1); |
| 162 | assertTrue((Integer) s1.getValue() == 2); |
| 163 | assertTrue(s1.getProbability() == 0.3); |
| 164 | ISample s2 = iProbFunc.getSamples().get(2); |
| 165 | assertTrue((Integer) s2.getValue() == 3); |
| 166 | assertTrue(s2.getProbability() == 0.5); |
| 167 | ISample s3 = iProbFunc.getSamples().get(3); |
| 168 | assertTrue((Integer) s3.getValue() == 4); |
| 169 | assertTrue(s3.getProbability() == 0.1); |
| 170 | |
| 171 | //assertTrue(iProbFunc.getUnit().getUnitName().equals("sec")); |
| 172 | assertTrue(iProbFunc.hasOrderedDomain()); |
| 173 | } |
| 174 | |
| 175 | @Test |
| 176 | public void ePMFToIPMF() { |
| 177 | ProbabilityMassFunction epmf = epfFactory |
| 178 | .createProbabilityMassFunction(); |
| 179 | epmf.setOrderedDomain(true); |
| 180 | //epmf.setUnit(pfFactory.transformToModelUnit(getTestUnit())); |
| 181 | initTestESamples(epmf.getSamples()); |
| 182 | |
| 183 | IProbabilityMassFunction pmf = pfFactory.transformToPMF(epmf); |
| 184 | |
| 185 | ISample sample0 = pmf.getSamples().get(0); |
| 186 | assertTrue((Integer) sample0.getValue() == 2); |
| 187 | assertTrue(sample0.getProbability() == 0.1); |
| 188 | ISample sample1 = pmf.getSamples().get(1); |
| 189 | assertTrue((Integer) sample1.getValue() == 4); |
| 190 | assertTrue(sample1.getProbability() == 0.3); |
| 191 | ISample sample2 = pmf.getSamples().get(2); |
| 192 | assertTrue((Integer) sample2.getValue() == 6); |
| 193 | assertTrue(sample2.getProbability() == 0.5); |
| 194 | ISample sample3 = pmf.getSamples().get(3); |
| 195 | assertTrue((Integer) sample3.getValue() == 8); |
| 196 | assertTrue(sample3.getProbability() == 0.1); |
| 197 | |
| 198 | //assertTrue(pmf.getUnit().getUnitName().equals("sec")); |
| 199 | assertTrue(pmf.hasOrderedDomain()); |
| 200 | } |
| 201 | |
| 202 | // @Test |
| 203 | // public void iSamplePDFToSamplePDF() throws UnknownPDFTypeException { |
| 204 | // List<Double> samples = new ArrayList<Double>(); |
| 205 | // Collections.addAll(samples, 0.1, 0.3, 0.2, 0.2, 0.2); |
| 206 | // ISamplePDF spdf = pfFactory.createSamplePDFFromDouble(0.1, samples, |
| 207 | // getTestUnit()); |
| 208 | // |
| 209 | // SamplePDF espdf = pfFactory.transformToModelSamplePDF(spdf); |
| 210 | // |
| 211 | // assertTrue(MathTools.equalsDouble(espdf.getDistance(), 0.1)); |
| 212 | // assertTrue((Double) espdf.getValues().get(0) == 0.1); |
| 213 | // assertTrue((Double) espdf.getValues().get(1) == 0.3); |
| 214 | // assertTrue((Double) espdf.getValues().get(2) == 0.2); |
| 215 | // assertTrue((Double) espdf.getValues().get(3) == 0.2); |
| 216 | // assertTrue((Double) espdf.getValues().get(4) == 0.2); |
| 217 | // assertEquals(espdf.getUnit().getUnitName(), getTestUnit().getUnitName()); |
| 218 | // } |
| 219 | |
| 220 | // @SuppressWarnings("unchecked") |
| 221 | // @Test |
| 222 | // public void eSamplePDFToSamplePDF() throws UnknownPDFTypeException, |
| 223 | // ProbabilitySumNotOneException, DoubleSampleException { |
| 224 | // SamplePDF espdf = epfFactory.createSamplePDF(); |
| 225 | // espdf.setDistance(0.2); |
| 226 | // Collections.addAll(espdf.getValues(), 0.1, 0.3, 0.2, 0.2, 0.2); |
| 227 | // espdf.setUnit(pfFactory.transformToModelUnit(getTestUnit())); |
| 228 | // |
| 229 | // ISamplePDF spdf = pfFactory.transformToSamplePDF(espdf); |
| 230 | // assertTrue(spdf.getDistance() == 0.2); |
| 231 | // //assertEquals(espdf.getUnit().getUnitName(), getTestUnit().getUnitName()); |
| 232 | // assertTrue(spdf.getValues().get(0).getReal() == 0.1); |
| 233 | // assertTrue(spdf.getValues().get(1).getReal() == 0.3); |
| 234 | // assertTrue(spdf.getValues().get(2).getReal() == 0.2); |
| 235 | // assertTrue(spdf.getValues().get(3).getReal() == 0.2); |
| 236 | // assertTrue(spdf.getValues().get(4).getReal() == 0.2); |
| 237 | // } |
| 238 | |
| 239 | // @Test |
| 240 | // public void iBoxedPDFToBoxedPDF() throws DoubleSampleException, |
| 241 | // UnknownPDFTypeException, ProbabilitySumNotOneException, |
| 242 | // FunctionNotInTimeDomainException { |
| 243 | // IBoxedPDF bpdf = pfFactory.createBoxedPDF(getTestContinuousSamples(), |
| 244 | // getTestUnit()); |
| 245 | // |
| 246 | // // this is the method to be tested: |
| 247 | // BoxedPDF ebpdf = pfFactory.transformToModelBoxedPDF(bpdf); |
| 248 | // |
| 249 | // ContinuousSample sample0 = (ContinuousSample) ebpdf.getSamples().get(0); |
| 250 | // assertTrue(sample0.getValue() == 1.0); |
| 251 | // assertTrue(sample0.getProbability() == 0.1); |
| 252 | // ContinuousSample sample1 = (ContinuousSample) ebpdf.getSamples().get(1); |
| 253 | // assertTrue(sample1.getValue() == 2.0); |
| 254 | // assertTrue(sample1.getProbability() == 0.3); |
| 255 | // ContinuousSample sample2 = (ContinuousSample) ebpdf.getSamples().get(2); |
| 256 | // assertTrue(sample2.getValue() == 3.0); |
| 257 | // assertTrue(sample2.getProbability() == 0.5); |
| 258 | // ContinuousSample sample3 = (ContinuousSample) ebpdf.getSamples().get(3); |
| 259 | // assertTrue(sample3.getValue() == 4.0); |
| 260 | // assertTrue(sample3.getProbability() == 0.1); |
| 261 | // |
| 262 | // assertTrue(ebpdf.getUnit()..getUnitName().equals("sec")); |
| 263 | // } |
| 264 | |
| 265 | @Test |
| 266 | public void eBoxedPDFToIBoxedPDF() throws ProbabilitySumNotOneException, |
| 267 | DoubleSampleException { |
| 268 | BoxedPDF ebpdf = epfFactory.createBoxedPDF(); |
| 269 | //ebpdf.setUnit(pfFactory.transformToModelUnit(getTestUnit())); |
| 270 | initTestEContinuousSamples(ebpdf.getSamples()); |
| 271 | |
| 272 | IBoxedPDF bpdf = pfFactory.transformToBoxedPDF(ebpdf); |
| 273 | |
| 274 | IContinuousSample sample0 = (IContinuousSample) bpdf.getSamples() |
| 275 | .get(0); |
| 276 | assertTrue(sample0.getValue() == 2.1); |
| 277 | assertTrue(sample0.getProbability() == 0.1); |
| 278 | IContinuousSample sample1 = (IContinuousSample) bpdf.getSamples() |
| 279 | .get(1); |
| 280 | assertTrue(sample1.getValue() == 3.5); |
| 281 | assertTrue(sample1.getProbability() == 0.3); |
| 282 | IContinuousSample sample2 = (IContinuousSample) bpdf.getSamples() |
| 283 | .get(2); |
| 284 | assertTrue(sample2.getValue() == 6.2); |
| 285 | assertTrue(sample2.getProbability() == 0.5); |
| 286 | IContinuousSample sample3 = (IContinuousSample) bpdf.getSamples() |
| 287 | .get(3); |
| 288 | assertTrue(sample3.getValue() == 6.7); |
| 289 | assertTrue(sample3.getProbability() == 0.1); |
| 290 | |
| 291 | //assertTrue(bpdf.getUnit().getUnitName().equals("sec")); |
| 292 | |
| 293 | try { |
| 294 | ebpdf = pfFactory.transformToModelBoxedPDF(bpdf); |
| 295 | } catch (UnknownPDFTypeException e) { |
| 296 | e.printStackTrace(); |
| 297 | } catch (FunctionNotInTimeDomainException e) { |
| 298 | e.printStackTrace(); |
| 299 | } |
| 300 | |
| 301 | ContinuousSample s0 = (ContinuousSample) ebpdf.getSamples().get(0); |
| 302 | assertTrue(s0.getValue() == 2.1); |
| 303 | assertTrue(s0.getProbability() == 0.1); |
| 304 | ContinuousSample s1 = (ContinuousSample) ebpdf.getSamples().get(1); |
| 305 | assertTrue(s1.getValue() == 3.5); |
| 306 | assertTrue(s1.getProbability() == 0.3); |
| 307 | ContinuousSample s2 = (ContinuousSample) ebpdf.getSamples().get(2); |
| 308 | assertTrue(s2.getValue() == 6.2); |
| 309 | assertTrue(s2.getProbability() == 0.5); |
| 310 | ContinuousSample s3 = (ContinuousSample) ebpdf.getSamples().get(3); |
| 311 | assertTrue(s3.getValue() == 6.7); |
| 312 | assertTrue(s3.getProbability() == 0.1); |
| 313 | |
| 314 | //assertTrue(ebpdf.getUnit().getUnitName().equals("sec")); |
| 315 | } |
| 316 | |
| 317 | @Test |
| 318 | public void createISamplePDF() { |
| 319 | List<Double> samples = new ArrayList<Double>(); |
| 320 | Collections.addAll(samples, 0.1, 0.3, 0.2, 0.2, 0.2); |
| 321 | ISamplePDF spdf = pfFactory.createSamplePDFFromDouble(0.1, samples, |
| 322 | false, getTestUnit()); |
| 323 | assertTrue(!spdf.isInFrequencyDomain()); |
| 324 | assertTrue(spdf.getUnit().getUnitName().equals("sec")); |
| 325 | |
| 326 | } |
| 327 | // @Test |
| 328 | // public void createSamplePDFFromMeasurements() { |
| 329 | // List<Double> measurements = new ArrayList<Double>(); |
| 330 | // Collections.addAll(measurements, 0.12, 0.34, 0.54, 1.4, 4.0, 3.0, 1.7, |
| 331 | // 1.8, 1.9, 2.1); |
| 332 | // ISamplePDF spdf = pfFactory.createSamplePDFFromMeasurements(0.5, |
| 333 | // measurements, getTestUnit()); |
| 334 | // |
| 335 | // assertTrue(MathTools.equalsDouble(0.5, spdf.getDistance())); |
| 336 | // assertEquals(spdf.getValues().size(), 9); |
| 337 | // assertTrue(MathTools.equalsDouble(spdf.getValues().get(0).getReal(), |
| 338 | // 0.1)); |
| 339 | // assertTrue(MathTools.equalsDouble(spdf.getValues().get(1).getReal(), |
| 340 | // 0.2)); |
| 341 | // assertTrue(MathTools.equalsDouble(spdf.getValues().get(2).getReal(), |
| 342 | // 0.0)); |
| 343 | // assertTrue(MathTools.equalsDouble(spdf.getValues().get(3).getReal(), |
| 344 | // 0.2)); |
| 345 | // assertTrue(MathTools.equalsDouble(spdf.getValues().get(4).getReal(), |
| 346 | // 0.3)); |
| 347 | // assertTrue(MathTools.equalsDouble(spdf.getValues().get(5).getReal(), |
| 348 | // 0.0)); |
| 349 | // assertTrue(MathTools.equalsDouble(spdf.getValues().get(6).getReal(), |
| 350 | // 0.1)); |
| 351 | // assertTrue(MathTools.equalsDouble(spdf.getValues().get(7).getReal(), |
| 352 | // 0.0)); |
| 353 | // assertTrue(MathTools.equalsDouble(spdf.getValues().get(8).getReal(), |
| 354 | // 0.1)); |
| 355 | // } |
| 356 | |
| 357 | @Test |
| 358 | public void createPMFFromMeasurements() { |
| 359 | Double[] measurements = {0.123, 0.340, 0.124, 0.343, 1.934, 0.345, |
| 360 | 1.935, 1.940, 1.945, 2.134}; |
| 361 | IProbabilityMassFunction pmf = pfFactory.createPMFFromMeasurements( |
| 362 | measurements, 0.01, getTestUnit(), true); |
| 363 | |
| 364 | assertEquals(5, pmf.getSamples().size()); |
| 365 | assertTrue(0.2 == pmf.getSamples().get(0).getProbability()); |
| 366 | assertTrue(0.3 == pmf.getSamples().get(1).getProbability()); |
| 367 | assertTrue(0.3 == pmf.getSamples().get(2).getProbability()); |
| 368 | assertTrue(0.1 == pmf.getSamples().get(3).getProbability()); |
| 369 | assertTrue(0.1 == pmf.getSamples().get(4).getProbability()); |
| 370 | |
| 371 | Integer[] m2 = {2, 7, 7, 3, 2, 3, 3, 1, 3, 0}; |
| 372 | pmf = pfFactory.createPMFFromMeasurements(m2, getTestUnit(), true); |
| 373 | assertEquals(5, pmf.getSamples().size()); |
| 374 | assertTrue(0.1 == pmf.getSamples().get(0).getProbability()); |
| 375 | assertTrue(0.1 == pmf.getSamples().get(1).getProbability()); |
| 376 | assertTrue(0.2 == pmf.getSamples().get(2).getProbability()); |
| 377 | assertTrue(0.4 == pmf.getSamples().get(3).getProbability()); |
| 378 | assertTrue(0.2 == pmf.getSamples().get(4).getProbability()); |
| 379 | |
| 380 | String[] m3 = {"test1", "test2", "1test", "test1", "1test", "test3", |
| 381 | "test3", "test1", "test2", "test2"}; |
| 382 | pmf = pfFactory.createPMFFromMeasurements(m3, getTestUnit(), true); |
| 383 | assertEquals(4, pmf.getSamples().size()); |
| 384 | assertTrue(0.2 == pmf.getSamples().get(0).getProbability()); |
| 385 | assertTrue(0.3 == pmf.getSamples().get(1).getProbability()); |
| 386 | assertTrue(0.3 == pmf.getSamples().get(2).getProbability()); |
| 387 | assertTrue(0.2 == pmf.getSamples().get(3).getProbability()); |
| 388 | |
| 389 | Boolean[] m4 = {true, true, false, true, false, true, false, false, |
| 390 | false, false}; |
| 391 | pmf = pfFactory.createPMFFromMeasurements(m4, getTestUnit(), true); |
| 392 | assertEquals(2, pmf.getSamples().size()); |
| 393 | assertTrue(0.6 == pmf.getSamples().get(0).getProbability()); |
| 394 | assertTrue(0.4 == pmf.getSamples().get(1).getProbability()); |
| 395 | } |
| 396 | |
| 397 | @Test |
| 398 | public void transformContinuousToBoxedPDF() throws UnknownPDFTypeException, ProbabilitySumNotOneException, DoubleSampleException, FunctionNotInTimeDomainException, NegativeDistanceException, UnitNotSetException, UnitNameNotSetException, InvalidSampleValueException{ |
| 399 | |
| 400 | IPDFFactory factory = ProbabilityFunctionFactoryImpl.getInstance().getPDFFactory(); |
| 401 | |
| 402 | ILognormalDistribution lognorm = factory.createLognormalDistribution(1, 0.5); |
| 403 | IExponentialDistribution exp = factory.createExponentialDistribution(0.5); |
| 404 | IGammaDistribution gamma = factory.createGammaDistribution(3, 2); |
| 405 | |
| 406 | //Should not throw any exceptions |
| 407 | transformAndCheck(lognorm); |
| 408 | transformAndCheck(exp); |
| 409 | transformAndCheck(gamma); |
| 410 | } |
| 411 | |
| 412 | private void transformAndCheck(IProbabilityDensityFunction func) throws UnknownPDFTypeException, |
| 413 | ProbabilitySumNotOneException, DoubleSampleException, |
| 414 | FunctionNotInTimeDomainException, NegativeDistanceException, |
| 415 | UnitNotSetException, UnitNameNotSetException, |
| 416 | InvalidSampleValueException { |
| 417 | IProbabilityFunctionFactory factory = ProbabilityFunctionFactoryImpl.getInstance(); |
| 418 | IBoxedPDF boxedPDF = factory.transformToBoxedPDF(func); |
| 419 | boxedPDF.checkConstrains(); |
| 420 | } |
| 421 | |
| 422 | public static junit.framework.Test suite() { |
| 423 | return new JUnit4TestAdapter(ProbabilityFunctionFactoryTest.class); |
| 424 | } |
| 425 | |
| 426 | |
| 427 | private List<ISample> getTestSamples() { |
| 428 | Object[] testSamples = {1, 0.1, 2, 0.3, 3, 0.5, 4, 0.1}; |
| 429 | List<ISample> sList = new ArrayList<ISample>(); |
| 430 | for (int i = 0; i < testSamples.length; i += 2) { |
| 431 | ISample s = pfFactory.createSample(testSamples[i], |
| 432 | (Double) testSamples[i + 1]); |
| 433 | sList.add(s); |
| 434 | } |
| 435 | return sList; |
| 436 | } |
| 437 | |
| 438 | private List<IContinuousSample> getTestContinuousSamples() { |
| 439 | Double[] testSamples = {1.0, 0.1, 2.0, 0.3, 3.0, 0.5, 4.0, 0.1}; |
| 440 | List<IContinuousSample> sList = new ArrayList<IContinuousSample>(); |
| 441 | for (int i = 0; i < testSamples.length; i += 2) { |
| 442 | IContinuousSample s = pfFactory.createContinuousSample( |
| 443 | testSamples[i], (Double) testSamples[i + 1]); |
| 444 | sList.add(s); |
| 445 | } |
| 446 | return sList; |
| 447 | } |
| 448 | |
| 449 | @SuppressWarnings("unchecked") |
| 450 | private void initTestESamples(EList esList) { |
| 451 | Object[] testSamples = {2, 0.1, 4, 0.3, 6, 0.5, 8, 0.1}; |
| 452 | for (int i = 0; i < testSamples.length; i += 2) { |
| 453 | Sample s = epfFactory.createSample(); |
| 454 | s.setValue(testSamples[i]); |
| 455 | s.setProbability((Double) testSamples[i + 1]); |
| 456 | esList.add(s); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | @SuppressWarnings("unchecked") |
| 461 | private void initTestEContinuousSamples(EList esList) { |
| 462 | Double[] testSamples = {2.1, 0.1, 3.5, 0.3, 6.2, 0.5, 6.7, 0.1}; |
| 463 | for (int i = 0; i < testSamples.length; i += 2) { |
| 464 | ContinuousSample s = epfFactory.createContinuousSample(); |
| 465 | s.setValue(testSamples[i]); |
| 466 | s.setProbability((Double) testSamples[i + 1]); |
| 467 | esList.add(s); |
| 468 | } |
| 469 | } |
| 470 | private IUnit getTestUnit() { |
| 471 | return pfFactory.createUnit("sec"); |
| 472 | } |
| 473 | } |