Example usage for org.apache.commons.math3.distribution NormalDistribution sample

List of usage examples for org.apache.commons.math3.distribution NormalDistribution sample

Introduction

In this page you can find the example usage for org.apache.commons.math3.distribution NormalDistribution sample.

Prototype

public double[] sample(int sampleSize) 

Source Link

Document

The default implementation generates the sample by calling #sample() in a loop.

Usage

From source file:hivemall.anomaly.ChangeFinder2DTest.java

public void testSota5D() throws HiveException {
    final int DIM = 5;
    final int EXAMPLES = 20001;

    final Double[] x = new Double[DIM];
    final List<Double> xList = Arrays.asList(x);

    Parameters params = new Parameters();
    params.set(LossFunction.logloss);//from w  ww  .j  a va  2  s  . co m
    params.r1 = 0.01d;
    params.k = 10;
    params.T1 = 10;
    params.T2 = 10;
    PrimitiveObjectInspector oi = PrimitiveObjectInspectorFactory.javaDoubleObjectInspector;
    ListObjectInspector listOI = ObjectInspectorFactory.getStandardListObjectInspector(oi);
    final ChangeFinder2D cf = new ChangeFinder2D(params, listOI);
    final double[] outScores = new double[2];

    RandomGenerator rng1 = new Well19937c(31L);
    final UniformIntegerDistribution uniform = new UniformIntegerDistribution(rng1, 0, 10);
    RandomGenerator rng2 = new Well19937c(41L);
    final PoissonDistribution poissonEvent = new PoissonDistribution(rng2, 1000.d,
            PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS);
    final StringBuilder buf = new StringBuilder(256);

    println("# time x0 x1 x2 x3 x4 mean0 mean1 mean2 mean3 mean4 outlier change");
    FIN: for (int i = 0; i < EXAMPLES;) {
        int len = poissonEvent.sample();
        double data[][] = new double[DIM][len];
        double mean[] = new double[DIM];
        double sd[] = new double[DIM];
        for (int j = 0; j < DIM; j++) {
            mean[j] = uniform.sample() * 5.d;
            sd[j] = uniform.sample() / 10.d * 5.d + 1.d;
            if (i % 5 == 0) {
                mean[j] += 50.d;
            }
            NormalDistribution normDist = new NormalDistribution(new Well19937c(i + j), mean[j], sd[j]);
            data[j] = normDist.sample(len);
            data[j][len / (j + 2) + DIM % (j + 1)] = mean[j] + (j + 4) * sd[j];
        }
        for (int j = 0; j < len; j++) {
            if (i >= EXAMPLES) {
                break FIN;
            }
            x[0] = data[0][j];
            x[1] = data[1][j];
            x[2] = data[2][j];
            x[3] = data[3][j];
            x[4] = data[4][j];
            cf.update(xList, outScores);
            buf.append(i).append(' ').append(x[0].doubleValue()).append(' ').append(x[1].doubleValue())
                    .append(' ').append(x[2].doubleValue()).append(' ').append(x[3].doubleValue()).append(' ')
                    .append(x[4].doubleValue()).append(' ').append(mean[0]).append(' ').append(mean[1])
                    .append(' ').append(mean[2]).append(' ').append(mean[3]).append(' ').append(mean[4])
                    .append(' ').append(outScores[0]).append(' ').append(outScores[1]);
            println(buf.toString());
            StringUtils.clear(buf);
            i++;
        }
    }
}

From source file:org.apache.solr.client.solrj.io.stream.StreamExpressionTest.java

@Test
public void fakeTest() {
    NormalDistribution a = new NormalDistribution(10, 2);
    NormalDistribution c = new NormalDistribution(100, 6);
    double[] d = c.sample(250);

    KolmogorovSmirnovTest ks = new KolmogorovSmirnovTest();
    double pv = ks.kolmogorovSmirnovStatistic(a, d);

    String s = "";
}

From source file:org.powertac.genco.CpGencoTest.java

/**
 * Test method for {@link org.powertac.genco.CpGenco#generateOrders(org.joda.time.Instant, java.util.List)}.
 *//*from   w  ww  . j a  v  a2  s . c  o m*/
@Test
public void generateFixedOrders() {
    init();
    genco.withMinQuantity(100.0);
    genco.withPriceInterval(10.0);
    // mock the normal distribution
    NormalDistribution mockNorm = mock(NormalDistribution.class);
    ReflectionTestUtils.setField(genco, "normal01", mockNorm);
    double[] samples = new double[2];
    samples[0] = 0.0;
    samples[1] = 0.0;
    when(mockNorm.sample(2)).thenReturn(samples);
    // capture orders
    final ArrayList<Order> orderList = new ArrayList<Order>();
    doAnswer(new Answer() {
        public Object answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            orderList.add((Order) args[0]);
            return null;
        }
    }).when(mockProxy).routeMessage(isA(Order.class));
    // set up some timeslots
    Timeslot ts1 = timeslotRepo.makeTimeslot(start);
    Timeslot ts2 = timeslotRepo.makeTimeslot(start.plus(TimeService.HOUR));
    Timeslot ts3 = timeslotRepo.makeTimeslot(start.plus(TimeService.HOUR * 2));
    assertEquals("4 enabled timeslots", 4, timeslotRepo.enabledTimeslots().size());
    // 50 mwh already sold in ts2
    MarketPosition posn2 = new MarketPosition(genco, ts2, -50.0);
    genco.addMarketPosition(posn2, ts2.getSerialNumber());
    // generate orders and check
    genco.generateOrders(start, timeslotRepo.enabledTimeslots());
    assertEquals("30 orders", 30, orderList.size());
}