Example usage for org.apache.commons.math.distribution BinomialDistributionImpl BinomialDistributionImpl

List of usage examples for org.apache.commons.math.distribution BinomialDistributionImpl BinomialDistributionImpl

Introduction

In this page you can find the example usage for org.apache.commons.math.distribution BinomialDistributionImpl BinomialDistributionImpl.

Prototype

public BinomialDistributionImpl(int trials, double p) 

Source Link

Document

Create a binomial distribution with the given number of trials and probability of success.

Usage

From source file:es.cnio.bioinfo.bicycle.BinomialAnnotator.java

public static void main(String[] args) throws IOException, MathException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    double p = Double.parseDouble(args[0]);
    String line = null;//w  w  w  .ja  va2  s. c om

    while ((line = in.readLine()) != null) {

        String[] tokens = line.split("\t");
        int reads = Integer.parseInt(tokens[5]);
        int cCount = Integer.parseInt(tokens[4]);
        BinomialDistribution binomial = new BinomialDistributionImpl(reads, p);
        double pval = (reads == 0) ? 1.0d : (1.0d - binomial.cumulativeProbability(cCount - 1));
        if (System.out.checkError()) {
            System.exit(1);
        }
        System.out.println(line + "\t" + pval);

    }
}

From source file:de.unihannover.se.processSimulation.dataGenerator.StatisticsUtil.java

static int qBinom(double q, int trials, double p) {
    try {//from   ww  w  .  j  a  v a 2s. c  om
        final BinomialDistribution d = new BinomialDistributionImpl(trials, p);
        final int result = d.inverseCumulativeProbability(q);
        if (result == -1) {
            return 0;
        } else if (result == Integer.MAX_VALUE) {
            return trials;
        } else {
            return result + 1;
        }
    } catch (final MathException e) {
        throw new RuntimeException(e);
    }
}

From source file:cal.cumulativeBinomialProbability.score.CumulativeBinomialProbabilityBasedScoring.java

/**
 * To calculate CumulativeBinominalProbability with given n,N and p values.
 * by calling BinomialDistribution class on Apache
 *
 * @return/*w  w  w .  jav  a2 s. co  m*/
 * @throws Exception
 */
protected double calculateCumulativeBinominalProbability() throws Exception {
    BinomialDistribution b = new BinomialDistributionImpl(N, p);
    int tmp = n - 1;
    double probability = 1 - b.cumulativeProbability(tmp);
    return probability;
}

From source file:desmoj.core.dist.DiscreteDistBinomial.java

/**
 * Creates a stream of pseudo random numbers following a binomial
 * distribution. The specific parameters p (probability) and n (amount) have
 * to be given here at creation time./* w w  w. ja  v a2s  .  c  o  m*/
 * 
 * @param owner
 *            Model : The distribution's owner
 * @param name
 *            java.lang.String : The distribution's name
 * @param probability
 *            double : The probability of success in each separate Bernoulli
 *            experiment.
 * @param amount
 *            int : The amount of separate Bernoulli experiments that lead to
 *            the result.
 * @param showInReport
 *            boolean : Flag for producing reports
 * @param showInTrace
 *            boolean : Flag for producing trace output
 */
public DiscreteDistBinomial(Model owner, String name, double probability, int amount, boolean showInReport,
        boolean showInTrace) {
    super(owner, name, showInReport, showInTrace);
    this.probability = probability;
    this.amount = amount;
    valueList = new ArrayList<Entry>();
    Entry e;
    BinomialDistribution bdist = new BinomialDistributionImpl(this.amount, this.probability);
    for (int i = 0; i < this.amount; i++) {
        try {
            e = new Entry(i, bdist.cumulativeProbability(i));
            valueList.add(e);

        } catch (MathException e1) {
            sendWarning(
                    "Failed to compute cumulative Probability of value " + Long.toString(i) + ", entry ignored",
                    "DiscreteDistBinomial : " + getName() + " at construction time",
                    "Impossible to compute cumulative Probability",
                    "Make sure the probabilty is set between 0 and 1 and the amount of trials is positive");
        }
    }
}

From source file:edu.utexas.cs.tactex.servercustomers.factoredcustomer.ProbabilityDistribution.java

ProbabilityDistribution(FactoredCustomerService service, Element xml) {
    if (null == randomSeedRepo)
        randomSeedRepo = (RandomSeedRepo) SpringApplicationContext.getBean("randomSeedRepo");

    type = Enum.valueOf(DistType.class, xml.getAttribute("distribution"));
    switch (type) {
    case POINTMASS:
    case DEGENERATE:
        param1 = Double.parseDouble(xml.getAttribute("value"));
        sampler = new DegenerateSampler(param1);
        break;//from ww w .j a  v  a 2  s  .co  m
    case UNIFORM:
        param1 = Double.parseDouble(xml.getAttribute("low"));
        param2 = Double.parseDouble(xml.getAttribute("high"));
        sampler = new UniformSampler(param1, param2);
        break;
    case INTERVAL:
        param1 = Double.parseDouble(xml.getAttribute("mean"));
        param2 = Double.parseDouble(xml.getAttribute("stdDev"));
        param3 = Double.parseDouble(xml.getAttribute("low"));
        param4 = Double.parseDouble(xml.getAttribute("high"));
        sampler = new IntervalSampler(param1, param2, param3, param4);
        break;
    case NORMAL:
    case GAUSSIAN:
        param1 = Double.parseDouble(xml.getAttribute("mean"));
        param2 = Double.parseDouble(xml.getAttribute("stdDev"));
        sampler = new ContinuousSampler(new NormalDistributionImpl(param1, param2));
        break;
    case STDNORMAL:
        param1 = 0;
        param2 = 1;
        sampler = new ContinuousSampler(new NormalDistributionImpl(param1, param2));
        break;
    case LOGNORMAL:
        param1 = Double.parseDouble(xml.getAttribute("expMean"));
        param2 = Double.parseDouble(xml.getAttribute("expStdDev"));
        sampler = new LogNormalSampler(param1, param2);
        break;
    case CAUCHY:
        param1 = Double.parseDouble(xml.getAttribute("median"));
        param2 = Double.parseDouble(xml.getAttribute("scale"));
        sampler = new ContinuousSampler(new CauchyDistributionImpl(param1, param2));
        break;
    case BETA:
        param1 = Double.parseDouble(xml.getAttribute("alpha"));
        param2 = Double.parseDouble(xml.getAttribute("beta"));
        sampler = new ContinuousSampler(new BetaDistributionImpl(param1, param2));
        break;
    case BINOMIAL:
        param1 = Double.parseDouble(xml.getAttribute("trials"));
        param2 = Double.parseDouble(xml.getAttribute("success"));
        sampler = new DiscreteSampler(new BinomialDistributionImpl((int) param1, param2));
        break;
    case POISSON:
        param1 = Double.parseDouble(xml.getAttribute("lambda"));
        sampler = new DiscreteSampler(new PoissonDistributionImpl(param1));
        break;
    case CHISQUARED:
        param1 = Double.parseDouble(xml.getAttribute("dof"));
        sampler = new ContinuousSampler(new ChiSquaredDistributionImpl(param1));
        break;
    case EXPONENTIAL:
        param1 = Double.parseDouble(xml.getAttribute("mean"));
        sampler = new ContinuousSampler(new ExponentialDistributionImpl(param1));
        break;
    case GAMMA:
        param1 = Double.parseDouble(xml.getAttribute("alpha"));
        param2 = Double.parseDouble(xml.getAttribute("beta"));
        sampler = new ContinuousSampler(new GammaDistributionImpl(param1, param2));
        break;
    case WEIBULL:
        param1 = Double.parseDouble(xml.getAttribute("alpha"));
        param2 = Double.parseDouble(xml.getAttribute("beta"));
        sampler = new ContinuousSampler(new WeibullDistributionImpl(param1, param2));
        break;
    case STUDENT:
        param1 = Double.parseDouble(xml.getAttribute("dof"));
        sampler = new ContinuousSampler(new TDistributionImpl(param1));
        break;
    case SNEDECOR:
        param1 = Double.parseDouble(xml.getAttribute("d1"));
        param2 = Double.parseDouble(xml.getAttribute("d2"));
        sampler = new ContinuousSampler(new FDistributionImpl(param1, param2));
        break;
    default:
        throw new Error("Invalid probability distribution type!");
    }
    sampler.reseedRandomGenerator(service.getRandomSeedRepo()
            .getRandomSeed("factoredcustomer.ProbabilityDistribution", SeedIdGenerator.getId(), "Sampler")
            .getValue());
}

From source file:geogebra.kernel.statistics.AlgoDistribution.java

BinomialDistribution getBinomialDistribution(int param, double param2) {
    if (binomial == null)
        binomial = new BinomialDistributionImpl(param, param2);
    else {//ww w.  ja v  a 2s .  c o m
        binomial.setNumberOfTrials(param);
        binomial.setProbabilityOfSuccess(param2);
    }
    return binomial;
}

From source file:geogebra.common.kernel.statistics.AlgoDistribution.java

/**
 * @param param//from   w w  w .  j  ava 2 s  .com
 *            number of trials
 * @param param2
 *            prob. of success
 * @return binomial distribution
 */
protected BinomialDistribution getBinomialDistribution(int param, double param2) {
    if (binomial == null || binomial.getNumberOfTrials() != param
            || binomial.getProbabilityOfSuccess() != param2)
        binomial = new BinomialDistributionImpl(param, param2);

    return binomial;
}

From source file:geogebra.common.kernel.algos.AlgoBarChart.java

/**
 * Prepares list1 and list2 for use with probability distribution bar charts
 *//*  w  w  w. j  a va2 s .c  o  m*/
private boolean prepareDistributionLists() {
    IntegerDistribution dist = null;
    int first = 0, last = 0;
    try {
        // get the distribution and the first, last list values for given
        // distribution type
        switch (type) {
        case TYPE_BARCHART_BINOMIAL:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            int n = (int) Math.round(p1.getDouble());
            double p = p2.getDouble();
            dist = new BinomialDistributionImpl(n, p);
            first = 0;
            last = n;
            break;

        case TYPE_BARCHART_PASCAL:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            n = (int) Math.round(p1.getDouble());
            p = p2.getDouble();
            dist = new PascalDistributionImpl(n, p);

            first = 0;
            last = (int) Math.max(1, (kernel).getXmax() + 1);
            break;
        case TYPE_BARCHART_ZIPF:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            n = (int) Math.round(p1.getDouble());
            p = p2.getDouble();
            dist = new ZipfDistributionImpl(n, p);

            first = 0;
            last = n;
            break;
        case TYPE_BARCHART_POISSON:
            if (!p1geo.isDefined())
                return false;
            double lambda = p1.getDouble();
            dist = new PoissonDistributionImpl(lambda);
            first = 0;
            last = (int) Math.max(1, kernel.getXmax() + 1);
            break;

        case TYPE_BARCHART_HYPERGEOMETRIC:
            if (!(p1geo.isDefined() && p2geo.isDefined() && p3geo.isDefined()))
                return false;
            int pop = (int) p1.getDouble();
            int successes = (int) p2.getDouble();
            int sample = (int) p3.getDouble();
            dist = new HypergeometricDistributionImpl(pop, successes, sample);
            first = Math.max(0, successes + sample - pop);
            last = Math.min(successes, sample);
            break;
        }

        // load class list and probability list
        loadDistributionLists(first, last, dist);
    }

    catch (Exception e) {
        App.debug(e.getMessage());
        return false;
    }

    return true;
}

From source file:es.cnio.bioinfo.bicycle.gatk.MethylationFilePair.java

private double computePval(Strand strand, Context context, ContigBisulfiteError error, int mCCount, int depth) {
    BinomialDistribution binomial = new BinomialDistributionImpl(depth,
            error.getError(strand, context).getError());

    double pval = 1.0;

    try {//from w w  w  .ja v a2 s.c o m
        pval = (mCCount == 0) ? 1.0d : (1.0d - binomial.cumulativeProbability(mCCount - 1));
    } catch (MathException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    return pval;
}

From source file:geogebra.kernel.AlgoFunctionAreaSums.java

/**
 * Prepares list1 and list2 for use with probability distribution bar charts
 *//*  w ww.  j ava  2  s.  c o m*/
private boolean prepareDistributionLists() {
    IntegerDistribution dist = null;
    int first = 0, last = 0;
    try {
        // get the distribution and the first, last list values for given distribution type
        switch (type) {
        case TYPE_BARCHART_BINOMIAL:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            int n = (int) Math.round(p1.getDouble());
            double p = p2.getDouble();
            dist = new BinomialDistributionImpl(n, p);
            first = 0;
            last = n;
            break;

        case TYPE_BARCHART_PASCAL:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            n = (int) Math.round(p1.getDouble());
            p = p2.getDouble();
            dist = new PascalDistributionImpl(n, p);

            first = 0;
            last = (int) Math.max(1, kernel.getXmax() + 1);
            break;
        case TYPE_BARCHART_ZIPF:
            if (!(p1geo.isDefined() && p2geo.isDefined()))
                return false;
            n = (int) Math.round(p1.getDouble());
            p = p2.getDouble();
            dist = new ZipfDistributionImpl(n, p);

            first = 0;
            last = n;
            break;
        case TYPE_BARCHART_POISSON:
            if (!p1geo.isDefined())
                return false;
            double lambda = p1.getDouble();
            dist = new PoissonDistributionImpl(lambda);
            first = 0;
            last = (int) Math.max(1, kernel.getXmax() + 1);
            break;

        case TYPE_BARCHART_HYPERGEOMETRIC:
            if (!(p1geo.isDefined() && p2geo.isDefined() && p3geo.isDefined()))
                return false;
            int pop = (int) p1.getDouble();
            int successes = (int) p2.getDouble();
            int sample = (int) p3.getDouble();
            dist = new HypergeometricDistributionImpl(pop, successes, sample);
            first = Math.max(0, successes + sample - pop);
            last = Math.min(successes, sample);
            break;
        }

        // load class list and probability list
        loadDistributionLists(first, last, dist);
    }

    catch (Exception e) {
        Application.debug(e.getMessage());
        return false;
    }

    return true;
}