use of org.apache.commons.math3.distribution.BinomialDistribution in project blog by BayesianLogic.
the class Multinomial method sample_value_use_binomial.
/** Sample value from Multinomial Distribution using Binomial Distribution */
/*
* Authored by yiwu on Oct.8.2014
* reference:
* chapter 2.2 of
* http://www.sciencedirect.com/science/article/pii/016794739390115A
*/
private ArrayList<Integer> sample_value_use_binomial() {
BinomialDistribution binom = null;
int cur = 0;
double cdf = 0;
ArrayList<Integer> result = new ArrayList<Integer>(k);
for (int i = 0; i < k - 1; i++) {
if (n == cur) {
result.add(0);
continue;
}
binom = new BinomialDistribution(n - cur, p[i] / (1.0 - cdf));
int x = binom.sample();
cur += x;
cdf += p[i];
result.add(x);
}
result.add(n - cur);
return result;
}
use of org.apache.commons.math3.distribution.BinomialDistribution in project GDSC-SMLM by aherbert.
the class PulseActivationAnalysis method simulateActivations.
private int simulateActivations(RandomDataGenerator rdg, BinomialDistribution bd, float[][] molecules, MemoryPeakResults results, int t, double precision, int id) {
if (bd == null)
return 0;
int n = molecules.length;
int k = bd.sample();
// Sample
RandomGenerator rand = rdg.getRandomGenerator();
int[] sample = Random.sample(k, n, rand);
while (k-- > 0) {
float[] xy = molecules[sample[k]];
float x, y;
do {
x = (float) (xy[0] + rand.nextGaussian() * precision);
} while (outOfBounds(x));
do {
y = (float) (xy[1] + rand.nextGaussian() * precision);
} while (outOfBounds(y));
results.add(createResult(t, x, y));
}
return sample.length;
}
use of org.apache.commons.math3.distribution.BinomialDistribution in project GDSC-SMLM by aherbert.
the class PCPALMClusters method addToPlot.
private void addToPlot(int n, double p, String title, Plot2 plot, Color color) {
double[] x = new double[n + 1];
double[] y = new double[n + 1];
BinomialDistribution dist = new BinomialDistribution(n, p);
int startIndex = 1;
// Normalise optionally excluding the x=0 point
double total = 1;
if (startIndex > 0)
total -= dist.probability(0);
double cumul = 0;
for (int i = startIndex; i <= n; i++) {
cumul += dist.probability(i) / total;
x[i] = i;
y[i] = cumul;
}
plot.setColor(color);
plot.addPoints(x, y, Plot2.LINE);
//plot.addPoints(x, y, Plot2.CIRCLE);
Utils.display(title, plot);
}
use of org.apache.commons.math3.distribution.BinomialDistribution in project gatk-protected by broadinstitute.
the class AlleleFractionSegmenterUnitTest method generateAllelicCount.
protected static AllelicCount generateAllelicCount(final double minorFraction, final SimpleInterval position, final RandomGenerator rng, final GammaDistribution biasGenerator, final double outlierProbability) {
final int numReads = 100;
final double bias = biasGenerator.sample();
//flip a coin to decide alt minor (alt fraction = minor fraction) or ref minor (alt fraction = 1 - minor fraction)
final double altFraction = rng.nextDouble() < 0.5 ? minorFraction : 1 - minorFraction;
//the probability of an alt read is the alt fraction modified by the bias or, in the case of an outlier, random
final double pAlt = rng.nextDouble() < outlierProbability ? rng.nextDouble() : altFraction / (altFraction + (1 - altFraction) * bias);
final int numAltReads = new BinomialDistribution(rng, numReads, pAlt).sample();
final int numRefReads = numReads - numAltReads;
return new AllelicCount(position, numAltReads, numRefReads);
}
Aggregations