/** * Tests whether phase shifts are correctly implemented. */ @Test public void testPhaseShift() { SineIntensity si = (SineIntensity) IntensityFunctions.sineIntensity() .phaseShift(0).build(); nonZeroCheck(Range.closed(0d, 1d), si, Range.open(0d, .5)); si = (SineIntensity) IntensityFunctions.sineIntensity().phaseShift(.5) .build(); nonZeroCheck(Range.closed(0d, 1d), si, Range.open(.25, .75)); // check default assertEquals(IntensityFunctions.sineIntensity().build(), si); si = (SineIntensity) IntensityFunctions.sineIntensity().phaseShift(1) .build(); nonZeroCheck(Range.closed(0d, 1d), si, Range.open(.5, 1d)); si = (SineIntensity) IntensityFunctions.sineIntensity().phaseShift(1.5) .build(); nonZeroCheck(Range.closed(0d, 1d), si, Range.closedOpen(0d, .25), Range.openClosed(.75, 1d)); si = (SineIntensity) IntensityFunctions.sineIntensity().phaseShift(2) .build(); nonZeroCheck(Range.closed(0d, 1d), si, Range.open(0d, .5)); }
/** * Compute the area of a sine intensity by using numerical approximation. The * range for which the area is computed is defined by a lower bound of * <code>0</code> and an upper bound of <code>length</code>. * @param s The intensity function to integrate. * @param lb The lower bound of the range. * @param ub The upper bound of the range. * @return The area. */ public static double areaByIntegration(IntensityFunction s, double lb, double ub) { final UnivariateIntegrator ri = new RombergIntegrator(16, 32); final double val = ri.integrate(10000000, asUnivariateFunction(s), lb, ub); return val; }
SineIntensity sine = (SineIntensity) IntensityFunctions.sineIntensity() .height(.1) .build(); assertEquals( .369903, IntensityFunctions.areaByIntegration(sine, 0, 1d / sine.getFrequency()), 0.000001); sine = (SineIntensity) IntensityFunctions.sineIntensity().build(); assertEquals(.31831, sine.area(), 0.00001); assertEquals( .31831, IntensityFunctions.areaByIntegration(sine, 0, 1d / sine.getFrequency()), 0.00001); sine = (SineIntensity) IntensityFunctions.sineIntensity().height(-.1) .build(); assertEquals(.269903, sine.area(), 0.000001); assertEquals( .269903, IntensityFunctions.areaByIntegration(sine, 0, 1d / sine.getFrequency()), 0.000001); sine = (SineIntensity) IntensityFunctions.sineIntensity() .amplitude(5d) .period(3600d) assertEquals(
/** * Tests whether the Poisson process (crudely) approximates the intensity * function when a large number of runs is done. */ @Test public void intensityApproximationPoissonProcessTest() { final RandomGenerator rng = new MersenneTwister(123); final TimeSeriesGenerator pp = TimeSeries.nonHomogenousPoisson(100d, intensityFunction); final Multiset<Double> ms = TreeMultiset.create(); final int repetitions = 10000; for (int i = 0; i < repetitions; i++) { final List<Double> times = pp.generate(rng.nextLong()); for (final Double d : times) { ms.add(new Double(Math.ceil(d))); } } for (final Multiset.Entry<Double> entry : ms.entrySet()) { final double exp = IntensityFunctions.areaByIntegration( intensityFunction, entry.getElement() - 1d, entry.getElement()); final double observation = entry.getCount() / (double) repetitions; assertEquals(exp, observation, 0.05); } } }
/** * @return The test configs. */ @Parameters public static Collection<Object[]> data() { return Arrays .asList(new Object[][] { {IntensityFunctions.sineIntensity().area(10).period(20).build()}, {IntensityFunctions.sineIntensity().area(10).height(.5).period(20) .build()}, {IntensityFunctions.sineIntensity().area(10).height(-.5).period(20) .build()}, {IntensityFunctions.sineIntensity().area(10).phaseShift(0) .period(20) .build()}, {IntensityFunctions.sineIntensity().area(10).height(1).period(20) .build()} }); }
final IntensityFunction intensity = IntensityFunctions.sineIntensity() .height(d) .period(period)
/** * @return Test configs */ @Parameters public static Collection<Object[]> data() { return Arrays.asList(new Object[][] { {TimeSeries.homogenousPoisson(60d, 10)}, {TimeSeries.homogenousPoisson(60d, 100)}, {TimeSeries.homogenousPoisson(180d, 10)}, {TimeSeries.homogenousPoisson(180d, 100)}, {TimeSeries.nonHomogenousPoisson(60d, IntensityFunctions .sineIntensity() .period(60).area(10).build())}, {TimeSeries.nonHomogenousPoisson(60d, IntensityFunctions .sineIntensity() .period(60).area(10).phaseShift(2).build())}, {TimeSeries.nonHomogenousPoisson(60d, IntensityFunctions .sineIntensity() .period(60).area(10).height(1).build())}, {TimeSeries.nonHomogenousPoisson(300d, IntensityFunctions .sineIntensity() .period(60).area(10).height(1).build())}, {TimeSeries.nonHomogenousPoisson(600d, IntensityFunctions .sineIntensity() .period(600).area(200).height(1).build())} }); }
for (final double h : heights) { final SineIntensity si20 = (SineIntensity) IntensityFunctions .sineIntensity() .period(p) .height(h) .sineIntensity() .period(p) .height(h) .sineIntensity() .period(p) .height(h)
/** * Check whether the * {@link TimeSeries#nonHomogenousPoisson(double, StochasticSupplier)} * function behaves as expected. */ @Test public void test2() { final IntensityFunction func1 = IntensityFunctions.sineIntensity() .period(60) .height(-.5) .build(); final IntensityFunction func2 = IntensityFunctions.sineIntensity() .period(30) .height(1.5) .build(); final StochasticSupplier<IntensityFunction> funcSup = StochasticSuppliers .fromIterable(Iterables.cycle(func1, func2)); final TimeSeriesGenerator tsg = TimeSeries.nonHomogenousPoisson(120, funcSup); // note that these tests will not work for all results of the // TimeSeriesGenerator, it is possible (but less likely) that the produced // dynamism values are different from the expectations as expressed in the // tests below. for (int i = 0; i < 20; i++) { assertTrue(Metrics.measureDynamism(tsg.generate(i), 120) < .25); assertTrue(Metrics.measureDynamism(tsg.generate(i), 120) > .40); } }
.announceTimes( TimeSeries.nonHomogenousPoisson(scenarioLengthMs - 30 * 60 * 1000L, IntensityFunctions.sineIntensity() .period(30 * 60 * 1000L) .area(10)