Search in sources :

Example 1 with InverseTransformDiscreteSampler

use of org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler in project GDSC-SMLM by aherbert.

the class BinomialFitterTest method createData.

private static int[] createData(UniformRandomProvider rg, int n, double p, boolean zeroTruncated) {
    final BinomialDistribution bd = new BinomialDistribution(null, n, p);
    final DiscreteSampler sampler = new InverseTransformDiscreteSampler(rg, pvalue -> bd.inverseCumulativeProbability(pvalue));
    final int[] data = new int[2000];
    if (zeroTruncated) {
        if (p <= 0) {
            throw new RuntimeException("p must be positive");
        }
        for (int i = 0; i < data.length; i++) {
            int count;
            do {
                count = sampler.sample();
            } while (count == 0);
            data[i] = count;
        }
    } else {
        for (int i = 0; i < data.length; i++) {
            data[i] = sampler.sample();
        }
    }
    return data;
}
Also used : InverseTransformDiscreteSampler(org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler) DiscreteSampler(org.apache.commons.rng.sampling.distribution.DiscreteSampler) InverseTransformDiscreteSampler(org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler) BinomialDistribution(org.apache.commons.math3.distribution.BinomialDistribution)

Aggregations

BinomialDistribution (org.apache.commons.math3.distribution.BinomialDistribution)1 DiscreteSampler (org.apache.commons.rng.sampling.distribution.DiscreteSampler)1 InverseTransformDiscreteSampler (org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler)1