/** * Sets the {@link StochasticSupplier} that will be used to generate the * amplitude of the {@link IntensityFunction} that will be created by this * builder. Default value: 1. * @param a Must be positive. * @return This, as per the builder pattern. */ public SineIntensityBuilder amplitude(StochasticSupplier<Double> a) { amplitudeSup = checked(a, POSITIVE); return this; }
/** * Creates a {@link StochasticSupplier} that produces uniformly distributed * {@link Long}s. * @param lower The (inclusive) lower bound of the uniform distribution. * @param upper The (inclusive) upper bound of the uniform distribution. * @return The supplier. */ public static StochasticSupplier<Long> uniformLong(int lower, int upper) { return intToLong(uniformInt(lower, upper)); }
@Parameters public static List<Object[]> configs() { return asList(new Object[][] { {uniformDouble(0.5, 5.5)}, {uniformInt(0, 10)}, {uniformLong(-100, 100)}, {constant(new Object())}, {constant(100L)}, {normal().buildDouble()}, {normal().bounds(-1, 1).buildInteger()}, {normal().mean(100).std(50).buildLong()} }); }
new Object[] { builder() .pickupUrgency(constant(100L)) .pickupTimeWindowLength(constant(0L)) .build(), 100d, new Object[] { builder() .pickupUrgency(uniformLong(0, 1000)) .build(), 500d, new Object[] { builder() .pickupUrgency(uniformLong(0, 1000)) .pickupTimeWindowLength( normal().bounds(0, 1000).mean(500).std(300).buildLong()) .build(), 500d, new Object[] { builder() .pickupUrgency(constant(0L)) .pickupTimeWindowLength(constant(0L)) .build(), 0d, builder()
/** * Tests for * {@link StochasticSuppliers#checked(StochasticSupplier, Predicate)}. */ @Test public void testCheckedSupplier() { final Predicate<Double> positive = Range.closedOpen(0d, Double.POSITIVE_INFINITY); checked(constant(0d), positive).get(0); checked(constant(453453453.34), positive).get(0); checked(constant(Double.MAX_VALUE), positive).get(0); boolean fail = false; try { checked(constant(Double.POSITIVE_INFINITY), positive).get(0); } catch (final IllegalArgumentException e) { fail = true; } assertTrue(fail); fail = false; try { checked(constant(-0.0000000001), positive).get(0); } catch (final IllegalArgumentException e) { fail = true; } assertTrue(fail); }
private static StochasticSupplier<Double> normalVar(Optional<Double> min, Optional<Double> max, Optional<Double> mean, Optional<Double> std, Optional<Boolean> redraw) { checkArgument(min.isPresent(), "Min should be set."); checkArgument(max.isPresent(), "Max should be set."); checkArgument(mean.isPresent(), "Mean should be set."); checkArgument(std.isPresent(), "Std should be set."); checkArgument(redraw.isPresent(), "Redraw should be set."); final StochasticSuppliers.Builder builder = StochasticSuppliers.normal() .mean(mean.get()) .std(std.get()) .lowerBound(min.get()) .upperBound(max.get()); if (redraw.get()) { builder.redrawWhenOutOfBounds(); } else { builder.roundWhenOutOfBounds(); } return builder.buildDouble(); } }
/** * Create a uniform distributed {@link LocationGenerator}. The min and max * values set to this builder take precedence over the mean and standard * deviations. In case the boundaries were set any specified means and * standard deviations are ignored when generating the locations. However, * the mean as supplied to this builder will be returned in the * {@link LocationGenerator#getCenter()} method. * @return A uniform distributed generator. */ public LocationGenerator buildUniform() { final Point xMinMax = getUniformMinMax(xMin, xMax, xMean, xSd); final Point yMinMax = getUniformMinMax(yMin, yMax, yMean, ySd); final Point min = new Point(xMinMax.x, yMinMax.x); final Point max = new Point(xMinMax.y, yMinMax.y); final double xCenter = getUniformCenter(xMean, min.x, max.x); final double yCenter = getUniformCenter(yMean, min.y, max.y); return new SupplierLocGen( min, max, new Point(xCenter, yCenter), uniformDouble(min.x, max.x), uniformDouble(min.y, max.y)); }
@Test public void testUniform() { final RandomGenerator rng = new MersenneTwister(123L); final StochasticSupplier<Integer> sup = uniformInt(2, 10); final IntegerDistribution id = new UniformIntegerDistribution(2, 10); final Multiset<Integer> ms = TreeMultiset.create(); for (int i = 0; i < 1000; i++) { ms.add(sup.get(rng.nextLong())); } final List<Integer> observations = newArrayList(); final List<Double> expectations = newArrayList(); for (final Multiset.Entry<Integer> entry : ms.entrySet()) { observations.add(entry.getCount()); expectations.add(id.probability(entry.getElement())); } assertTrue(chiSquare(expectations, observations, .01)); }
/** * Creates a {@link TimeSeriesGenerator} that uses a truncated normal * distribution for the inter arrival times of events. The normal distribution * is truncated at a lower bound of <code>0</code> since it is not allowed (it * makes no sense) to have negative inter arrival times. For more information * about the normal distribution see {@link StochasticSuppliers#normal()}. * @param length The length of the time series, all generated times will be in * the interval [0,length). * @param numEvents The total number of events in the time series (on * average). * @param sd The standard deviation of the normal distribution. * @return A {@link TimeSeriesGenerator} based on a normal distribution. */ public static TimeSeriesGenerator normal(double length, int numEvents, double sd) { checkArgument(length > 0d); checkArgument(numEvents > 0); final double average = length / numEvents; return toTimeSeries(length, StochasticSuppliers.normal() .mean(average) .std(sd) .lowerBound(0d) .redrawWhenOutOfBounds() .scaleMean() .buildDouble()); }
/** * Tests whether the rescaling of the mean of a truncated normal distribution * is implemented correctly. */ @Test public void testNormalScaleMean() { final double[] means = new double[] {1d, 2d, 3d, 10d, 100d}; final double[] sds = new double[] {1d, 1d, 3d, 5d, 100d}; for (int i = 0; i < means.length; i++) { final StochasticSupplier<Double> ss = StochasticSuppliers.normal() .mean(means[i]) .std(sds[i]) .lowerBound(0) .scaleMean() .redrawWhenOutOfBounds() .buildDouble(); final RandomGenerator rng = new MersenneTwister(123); final SummaryStatistics stats = new SummaryStatistics(); for (int j = 0; j < 10000; j++) { stats.addValue(ss.get(rng.nextLong())); } // 1 % deviation from mean is acceptable final double allowedDeviation = 0.01 * means[i]; assertEquals(means[i], stats.getMean(), allowedDeviation); } } }
/** * Sets the {@link StochasticSupplier} that will be used to generate the * phaseShift of the {@link IntensityFunction} that will be created by this * builder. Default value: 1/2. * @param s The phase shift. * @return This, as per the builder pattern. */ public SineIntensityBuilder phaseShift(StochasticSupplier<Double> s) { phaseShiftSup = checked(s, FINITE); return this; }
/** * Sets the period of the {@link IntensityFunction} that will be created by * this builder. Default value: 1. * @param p Must be positive. * @return This, as per the builder pattern. */ public SineIntensityBuilder period(double p) { checkArgument(p > 0d); checkArgument(Doubles.isFinite(p)); frequencySup = constant(1d / p); return this; }
/** * Sets the {@link StochasticSupplier} that will be used to generate the * frequency of the {@link IntensityFunction} that will be created by this * builder. Default value: 1. * @param f Must be positive. * @return This, as per the builder pattern. */ public SineIntensityBuilder frequency(StochasticSupplier<Double> f) { frequencySup = checked(f, POSITIVE); return this; }
/** * Sets the phaseShift of the {@link IntensityFunction} that will be created * by this builder. Default value: 1/2. * @param s The phase shift. * @return This, as per the builder pattern. */ public SineIntensityBuilder phaseShift(double s) { checkArgument(Doubles.isFinite(s)); phaseShiftSup = constant(s); return this; }
/** * Sets the {@link StochasticSupplier} that will be used to generate the * height of the {@link IntensityFunction} that will be created by this * builder. Default value: 0. Typical values range between <code>-1</code> * and <code>1</code>. If the height is close to <code>-1</code> almost the * entire function will be negative. If the height is <code>1</code> or * higher the entire function will be positive. * @param h Must be <code> > -1</code>. * @return This, as per the builder pattern. */ public SineIntensityBuilder height(StochasticSupplier<Double> h) { heightSup = checked(h, GREATER_THAN_MINUS_ONE); return this; }
/** * Sets the amplitude of the {@link IntensityFunction} that will be created * by this builder. Default value: 1. * @param a Must be positive. * @return This, as per the builder pattern. */ public SineIntensityBuilder amplitude(double a) { checkArgument(a > 0d); checkArgument(Doubles.isFinite(a)); amplitudeSup = constant(a); return this; }
/** * Creates a {@link TimeSeriesGenerator} using a uniform distribution for the * inter arrival times of events. The spread of the uniform distribution is * defined by the <code>maxDeviation</code> {@link StochasticSupplier}, this * means that each time a time series is generated a different max deviation * settings is used. * @param length The length of the time series, all generated times will be in * the interval [0,length). * @param numEvents The total number of events in the time series (on * average). * @param maxDeviation A supplier that is used for max deviation values. * @return A {@link TimeSeriesGenerator} based on a uniform distribution, each * time series that is generated has a different max deviation drawn * from the supplier. */ public static TimeSeriesGenerator uniform(double length, int numEvents, StochasticSupplier<Double> maxDeviation) { checkArgument(length > 0d); checkArgument(numEvents > 0); final double average = length / numEvents; return new UniformTimeSeries(length, average, StochasticSuppliers.checked( maxDeviation, Range.atLeast(0d))); }
/** * Sets the frequency of the {@link IntensityFunction} that will be created * by this builder. Default value: 1. * @param f Must be positive. * @return This, as per the builder pattern. */ public SineIntensityBuilder frequency(double f) { checkArgument(f > 0d); checkArgument(Doubles.isFinite(f)); frequencySup = constant(f); return this; }