Example usage for org.apache.commons.math3.distribution PoissonDistribution DEFAULT_EPSILON

List of usage examples for org.apache.commons.math3.distribution PoissonDistribution DEFAULT_EPSILON

Introduction

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

Prototype

double DEFAULT_EPSILON

To view the source code for org.apache.commons.math3.distribution PoissonDistribution DEFAULT_EPSILON.

Click Source Link

Document

Default convergence criterion.

Usage

From source file:com.cloudera.oryx.ml.serving.als.model.LoadTestALSModelFactory.java

public static ALSServingModel buildTestModel() {

    log.info("Building load test model...");

    System.gc();//from   w ww . j av  a2 s .  co  m
    long startMemory = JVMUtils.getUsedMemory();

    RandomGenerator random = RandomManager.getRandom();
    PoissonDistribution itemPerUserDist = new PoissonDistribution(random, AVG_ITEMS_PER_USER,
            PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS);
    ALSServingModel model = new ALSServingModel(FEATURES, true);

    long totalEntries = 0;
    for (int user = 0; user < USERS; user++) {
        String userID = "U" + user;
        model.setUserVector(userID, randomVector(random));
        int itemsPerUser = itemPerUserDist.sample();
        totalEntries += itemsPerUser;
        Collection<String> knownIDs = new ArrayList<>(itemsPerUser);
        for (int i = 0; i < itemsPerUser; i++) {
            knownIDs.add("I" + random.nextInt(ITEMS));
        }
        model.addKnownItems(userID, knownIDs);
    }

    for (int item = 0; item < ITEMS; item++) {
        model.setItemVector("I" + item, randomVector(random));
    }

    System.gc();
    long endMemory = JVMUtils.getUsedMemory();

    log.info("Built model over {} users, {} items, {} features, {} entries, using {}MB", USERS, ITEMS, FEATURES,
            totalEntries, (endMemory - startMemory) / 1_000_000);
    return model;
}

From source file:com.ibm.bi.dml.runtime.util.PoissonPRNGenerator.java

public void setup(double mean, long sd) {
    seed = sd;/*from  w  w w .j a  v a2 s .  c o m*/

    SynchronizedRandomGenerator srg = new SynchronizedRandomGenerator(new Well1024a());
    srg.setSeed(seed);
    _pdist = new PoissonDistribution(srg, _mean, PoissonDistribution.DEFAULT_EPSILON,
            PoissonDistribution.DEFAULT_MAX_ITERATIONS);
}

From source file:com.cloudera.oryx.app.serving.als.model.LoadTestALSModelFactory.java

public static ALSServingModel buildTestModel() {

    log.info("Building load test model...");

    System.gc();/*  w w w .  java  2s.c  o  m*/
    long startMemory = JVMUtils.getUsedMemory();

    ALSServingModel model = new ALSServingModel(FEATURES, true, LSH_SAMPLE_RATE, new TestALSRescorerProvider());
    AtomicLong totalEntries = new AtomicLong();

    int numCores = Runtime.getRuntime().availableProcessors();
    log.info("Adding {} users", USERS);
    AtomicInteger userCount = new AtomicInteger();
    ExecUtils.doInParallel(numCores, i -> {
        RandomGenerator random = RandomManager.getRandom(((long) i << 32) ^ System.nanoTime());
        PoissonDistribution itemPerUserDist = new PoissonDistribution(random, AVG_ITEMS_PER_USER,
                PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS);
        for (int user = userCount.getAndIncrement(); user < USERS; user = userCount.getAndIncrement()) {
            String userID = "U" + user;
            model.setUserVector(userID, VectorMath.randomVectorF(FEATURES, random));
            int itemsPerUser = itemPerUserDist.sample();
            totalEntries.addAndGet(itemsPerUser);
            Collection<String> knownIDs = new ArrayList<>(itemsPerUser);
            for (int item = 0; item < itemsPerUser; item++) {
                knownIDs.add("I" + random.nextInt(ITEMS));
            }
            model.addKnownItems(userID, knownIDs);
        }
    });

    log.info("Adding {} items", ITEMS);
    AtomicInteger itemCount = new AtomicInteger();
    ExecUtils.doInParallel(numCores, i -> {
        RandomGenerator random = RandomManager.getRandom(((long) i << 32) ^ System.nanoTime());
        for (int item = itemCount.getAndIncrement(); item < ITEMS; item = itemCount.getAndIncrement()) {
            model.setItemVector("I" + item, VectorMath.randomVectorF(FEATURES, random));
        }
    });

    System.gc();
    long endMemory = JVMUtils.getUsedMemory();

    log.info("Built model over {} users, {} items, {} features, {} entries, using {}MB", USERS, ITEMS, FEATURES,
            totalEntries, (endMemory - startMemory) / 1_000_000);
    log.info("Model: {}", model);
    return model;
}

From source file:com.cloudera.oryx.app.serving.als.model.ALSServingModelTest.java

@Test
public void testLSHEffect() {
    RandomGenerator random = RandomManager.getRandom();
    PoissonDistribution itemPerUserDist = new PoissonDistribution(random, 20,
            PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS);
    int features = 20;
    ALSServingModel mainModel = new ALSServingModel(features, true, 1.0, null);
    ALSServingModel lshModel = new ALSServingModel(features, true, 0.5, null);

    int userItemCount = 20000;
    for (int user = 0; user < userItemCount; user++) {
        String userID = "U" + user;
        float[] vec = VectorMath.randomVectorF(features, random);
        mainModel.setUserVector(userID, vec);
        lshModel.setUserVector(userID, vec);
        int itemsPerUser = itemPerUserDist.sample();
        Collection<String> knownIDs = new ArrayList<>(itemsPerUser);
        for (int i = 0; i < itemsPerUser; i++) {
            knownIDs.add("I" + random.nextInt(userItemCount));
        }//  w ww  .j  a va 2s  .c om
        mainModel.addKnownItems(userID, knownIDs);
        lshModel.addKnownItems(userID, knownIDs);
    }

    for (int item = 0; item < userItemCount; item++) {
        String itemID = "I" + item;
        float[] vec = VectorMath.randomVectorF(features, random);
        mainModel.setItemVector(itemID, vec);
        lshModel.setItemVector(itemID, vec);
    }

    int numRecs = 10;
    Mean meanMatchLength = new Mean();
    for (int user = 0; user < userItemCount; user++) {
        String userID = "U" + user;
        List<Pair<String, Double>> mainRecs = mainModel
                .topN(new DotsFunction(mainModel.getUserVector(userID)), null, numRecs, null)
                .collect(Collectors.toList());
        List<Pair<String, Double>> lshRecs = lshModel
                .topN(new DotsFunction(lshModel.getUserVector(userID)), null, numRecs, null)
                .collect(Collectors.toList());
        int i = 0;
        while (i < lshRecs.size() && i < mainRecs.size() && lshRecs.get(i).equals(mainRecs.get(i))) {
            i++;
        }
        meanMatchLength.increment(i);
    }
    log.info("Mean matching prefix: {}", meanMatchLength.getResult());
    assertGreaterOrEqual(meanMatchLength.getResult(), 4.0);

    meanMatchLength.clear();
    for (int item = 0; item < userItemCount; item++) {
        String itemID = "I" + item;
        List<Pair<String, Double>> mainRecs = mainModel
                .topN(new CosineAverageFunction(mainModel.getItemVector(itemID)), null, numRecs, null)
                .collect(Collectors.toList());
        List<Pair<String, Double>> lshRecs = lshModel
                .topN(new CosineAverageFunction(lshModel.getItemVector(itemID)), null, numRecs, null)
                .collect(Collectors.toList());
        int i = 0;
        while (i < lshRecs.size() && i < mainRecs.size() && lshRecs.get(i).equals(mainRecs.get(i))) {
            i++;
        }
        meanMatchLength.increment(i);
    }
    log.info("Mean matching prefix: {}", meanMatchLength.getResult());
    assertGreaterOrEqual(meanMatchLength.getResult(), 5.0);
}

From source file:gdsc.smlm.ij.plugins.EMGainAnalysis.java

/**
 * Randomly generate a histogram from poisson-gamma-gaussian samples
 * //  w  w w. j  a  va2 s  .c  o m
 * @return The histogram
 */
private int[] simulateFromPoissonGammaGaussian() {
    // Randomly sample
    RandomGenerator random = new Well44497b(System.currentTimeMillis() + System.identityHashCode(this));

    PoissonDistribution poisson = new PoissonDistribution(random, _photons, PoissonDistribution.DEFAULT_EPSILON,
            PoissonDistribution.DEFAULT_MAX_ITERATIONS);

    CustomGammaDistribution gamma = new CustomGammaDistribution(random, _photons, _gain,
            GammaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);

    final int steps = simulationSize;
    int[] sample = new int[steps];
    for (int n = 0; n < steps; n++) {
        if (n % 64 == 0)
            IJ.showProgress(n, steps);

        // Poisson
        double d = poisson.sample();

        // Gamma
        if (d > 0) {
            gamma.setShapeUnsafe(d);
            d = gamma.sample();
        }

        // Gaussian
        d += _noise * random.nextGaussian();

        // Convert the sample to a count 
        sample[n] = (int) Math.round(d + _bias);
    }

    int max = Maths.max(sample);
    int[] h = new int[max + 1];
    for (int s : sample)
        h[s]++;
    return h;
}

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 w  w . j  a  va2s .  c o  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:gdsc.smlm.ij.plugins.CreateData.java

private float[] createBackground(RandomDataGenerator random) {
    float[] pixels2 = null;

    if (settings.background > 0) {
        if (random == null)
            random = new RandomDataGenerator();
        createBackgroundPixels();//from w  ww.j  a  va  2s.  c  o  m
        pixels2 = Arrays.copyOf(backgroundPixels, backgroundPixels.length);

        // Add Poisson noise.
        if (uniformBackground) {
            // We can do N random samples thus ensuring the background average is constant.
            // Note: The number of samples must be Poisson distributed.
            final int samples = (int) random.nextPoisson(pixels2[0] * pixels2.length);

            // Only do sampling if the number of samples is valid
            if (samples >= 1) {
                pixels2 = new float[pixels2.length];
                final int upper = pixels2.length - 1;
                for (int i = 0; i < samples; i++) {
                    pixels2[random.nextInt(0, upper)] += 1;
                }
            } else {
                // If using a uniform illumination then we can use a fixed Poisson distribution
                PoissonDistribution dist = new PoissonDistribution(random.getRandomGenerator(), pixels2[0],
                        PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS);
                for (int i = 0; i < pixels2.length; i++) {
                    pixels2[i] = dist.sample();
                }
            }
        } else {
            for (int i = 0; i < pixels2.length; i++) {
                pixels2[i] = random.nextPoisson(pixels2[i]);
            }
        }
    } else {
        pixels2 = new float[settings.size * settings.size];
    }

    return pixels2;
}

From source file:org.apache.mahout.math.random.PoissonSampler.java

public PoissonSampler(double lambda) {
    limit = 1;/*from  w  w  w.  j  av  a 2s.c  o  m*/
    gen = RandomUtils.getRandom();
    pd = new PoissonDistribution(gen.getRandomGenerator(), lambda, PoissonDistribution.DEFAULT_EPSILON,
            PoissonDistribution.DEFAULT_MAX_ITERATIONS);
}

From source file:org.apache.mahout.math.random.PoissonSamplerTest.java

private static void checkDistribution(Sampler<Double> pd, double alpha) {
    int[] count = new int[(int) Math.max(10, 5 * alpha)];
    for (int i = 0; i < 10000; i++) {
        count[pd.sample().intValue()]++;
    }//from   w  ww  .j  a  va 2s  . c  om

    IntegerDistribution ref = new PoissonDistribution(RandomUtils.getRandom().getRandomGenerator(), alpha,
            PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS);
    for (int i = 0; i < count.length; i++) {
        assertEquals(ref.probability(i), count[i] / 10000.0, 2.0e-2);
    }
}