/** * Create a binomial distribution with the given number of trials and * probability of success. * * @param numberOfTrials the number of trials * @param probabilityOfSuccess the probability of success * @return a new binomial distribution */ public BinomialDistribution createBinomialDistribution( int numberOfTrials, double probabilityOfSuccess) { return new BinomialDistributionImpl(numberOfTrials, probabilityOfSuccess); }
/** * Returns the mean. * * For <code>n</code> number of trials and * probability parameter <code>p</code>, the mean is * <code>n * p</code> * * @return the mean * @since 2.2 */ public double getNumericalMean() { return (double)getNumberOfTrials() * getProbabilityOfSuccess(); }
/** * Create a binomial distribution with the given number of trials and * probability of success. * * @param trials the number of trials. * @param p the probability of success. */ public BinomialDistributionImpl(int trials, double p) { super(); setNumberOfTrialsInternal(trials); setProbabilityOfSuccessInternal(p); }
/** * Create a binomial distribution with the given number of trials and * probability of success. * @param trials the number of trials. * @param p the probability of success. */ public BinomialDistributionImpl(int trials, double p) { super(); setNumberOfTrials(trials); setProbabilityOfSuccess(p); }
/** * @return result of sign test between baseline and treatment */ public double signTest() { int treatmentIsBetter = 0; int different = 0; for( int i=0; i<treatment.length; i++ ) { if( treatment[i] > baseline[i] ) treatmentIsBetter++; if( treatment[i] != baseline[i] ) different++; } double pvalue; try { pvalue = 1 - new BinomialDistributionImpl(different, 0.5).cumulativeProbability(treatmentIsBetter - 1); } catch (MathException e) { throw new RuntimeException(e); } return pvalue; }
/** * For this distribution, X, this method returns P(X ≤ x). * * @param x the value at which the PDF is evaluated. * @return PDF for this distribution. * @throws MathException if the cumulative probability can not be computed * due to convergence or other numerical errors. */ @Override public double cumulativeProbability(int x) throws MathException { double ret; if (x < 0) { ret = 0.0; } else if (x >= numberOfTrials) { ret = 1.0; } else { ret = 1.0 - Beta.regularizedBeta(getProbabilityOfSuccess(), x + 1.0, numberOfTrials - x); } return ret; }
/** * Returns the upper bound of the support for the distribution. * * The upper bound of the support is the number of trials. * * @return upper bound of the support (equal to number of trials) * @since 2.2 */ public int getSupportUpperBound() { return getNumberOfTrials(); }
/** * Change the probability of success for this distribution. * * @param p the new probability of success. * @throws IllegalArgumentException if <code>p</code> is not a valid * probability. * @deprecated as of 2.1 (class will become immutable in 3.0) */ @Deprecated public void setProbabilityOfSuccess(double p) { setProbabilityOfSuccessInternal(p); }
/** * Change the number of trials for this distribution. * * @param trials the new number of trials. * @throws IllegalArgumentException if <code>trials</code> is not a valid * number of trials. * @deprecated as of 2.1 (class will become immutable in 3.0) */ @Deprecated public void setNumberOfTrials(int trials) { setNumberOfTrialsInternal(trials); }
/** * @return result of sign test between baseline and treatment */ public double signTest() { int treatmentIsBetter = 0; int different = 0; for( int i=0; i<treatment.length; i++ ) { if( treatment[i] > baseline[i] ) treatmentIsBetter++; if( treatment[i] != baseline[i] ) different++; } double pvalue; try { pvalue = 1 - new BinomialDistributionImpl(different, 0.5).cumulativeProbability(treatmentIsBetter - 1); } catch (MathException e) { throw new RuntimeException(e); } return pvalue; }
/** * For this distribution, X, this method returns P(X ≤ x). * * @param x the value at which the PDF is evaluated. * @return PDF for this distribution. * @throws MathException if the cumulative probability can not be computed * due to convergence or other numerical errors. */ @Override public double cumulativeProbability(int x) throws MathException { double ret; if (x < 0) { ret = 0.0; } else if (x >= numberOfTrials) { ret = 1.0; } else { ret = 1.0 - Beta.regularizedBeta(getProbabilityOfSuccess(), x + 1.0, numberOfTrials - x); } return ret; }
/** * Access the domain value upper bound, based on <code>p</code>, used to * bracket a PDF root. * * @param p the desired probability for the critical value * @return domain value upper bound, i.e. * P(X < <i>upper bound</i>) > <code>p</code> */ protected int getDomainUpperBound(double p) { return getNumberOfTrials(); }
/** * Change the probability of success for this distribution. * * @param p the new probability of success. * @throws IllegalArgumentException if <code>p</code> is not a valid * probability. * @deprecated as of 2.1 (class will become immutable in 3.0) */ @Deprecated public void setProbabilityOfSuccess(double p) { setProbabilityOfSuccessInternal(p); } /**
/** * Change the number of trials for this distribution. * * @param trials the new number of trials. * @throws IllegalArgumentException if <code>trials</code> is not a valid * number of trials. * @deprecated as of 2.1 (class will become immutable in 3.0) */ @Deprecated public void setNumberOfTrials(int trials) { setNumberOfTrialsInternal(trials); } /**
/** * Returns the variance. * * For <code>n</code> number of trials and * probability parameter <code>p</code>, the variance is * <code>n * p * (1 - p)</code> * * @return the variance * @since 2.2 */ public double getNumericalVariance() { final double p = getProbabilityOfSuccess(); return (double)getNumberOfTrials() * p * (1 - p); } }
/** * Generates a random value from the {@link BinomialDistributionImpl Binomial Distribution}. * This implementation uses {@link #nextInversionDeviate(ContinuousDistribution) inversion} * to generate random values. * * @param numberOfTrials number of trials of the Binomial distribution * @param probabilityOfSuccess probability of success of the Binomial distribution * @return random value sampled from the Binomial(numberOfTrials, probabilityOfSuccess) distribution * @throws MathException if an error occurs generating the random value * @since 2.2 */ public int nextBinomial(int numberOfTrials, double probabilityOfSuccess) throws MathException { return nextInversionDeviate(new BinomialDistributionImpl(numberOfTrials, probabilityOfSuccess)); }
/** * Create a binomial distribution with the given number of trials and * probability of success. * * @param trials the number of trials. * @param p the probability of success. */ public BinomialDistributionImpl(int trials, double p) { super(); setNumberOfTrialsInternal(trials); setProbabilityOfSuccessInternal(p); }
/** * For this disbution, X, this method returns P(X = x). * * @param x the value at which the PMF is evaluated. * @return PMF for this distribution. */ public double probability(int x) { double ret; if (x < 0 || x > getNumberOfTrials()) { ret = 0.0; } else { ret = MathUtils.binomialCoefficientDouble( getNumberOfTrials(), x) * Math.pow(getProbabilityOfSuccess(), x) * Math.pow(1.0 - getProbabilityOfSuccess(), getNumberOfTrials() - x); } return ret; }
private double calculatePower(final int numReads, final double alleleFraction) throws MathException { if (numReads==0) return 0; // TODO: add the factor of 1/3 final double probAltRead = alleleFraction*(1 - errorProbability) + (1/3)*(1 - alleleFraction) * errorProbability; final BinomialDistribution binom = new BinomialDistributionImpl(numReads, probAltRead); final double[] binomialProbabilities = IntStream.range(0, numReads + 1).mapToDouble(binom::probability).toArray(); // find the smallest number of ALT reads k such that tumorLOD(k) > tumorLODThreshold final OptionalInt smallestKAboveLogThreshold = IntStream.range(0, numReads + 1) .filter(k -> calculateTumorLod(numReads, k) > tumorLODThreshold) .findFirst(); if (! smallestKAboveLogThreshold.isPresent()){ return 0; } if (smallestKAboveLogThreshold.getAsInt() <= 0){ throw new IllegalStateException("smallest k that meets the tumor LOD threshold is less than or equal to 0"); } double power = Arrays.stream(binomialProbabilities, smallestKAboveLogThreshold.getAsInt(), binomialProbabilities.length).sum(); // here we correct for the fact that the exact lod threshold is likely somewhere between // the k and k-1 bin, so we prorate the power from that bin if ( enableSmoothing ){ final double tumorLODAtK = calculateTumorLod(numReads, smallestKAboveLogThreshold.getAsInt()); final double tumorLODAtKMinusOne = calculateTumorLod(numReads, smallestKAboveLogThreshold.getAsInt()-1); final double weight = 1 - (tumorLODThreshold - tumorLODAtKMinusOne ) / (tumorLODAtK - tumorLODAtKMinusOne); power += weight * binomialProbabilities[smallestKAboveLogThreshold.getAsInt() - 1]; } return(power); }
/** * For this distribution, X, this method returns P(X ≤ x). * @param x the value at which the PDF is evaluated. * @return PDF for this distribution. * @throws MathException if the cumulative probability can not be * computed due to convergence or other numerical errors. */ public double cumulativeProbability(int x) throws MathException { double ret; if (x < 0) { ret = 0.0; } else if (x >= getNumberOfTrials()) { ret = 1.0; } else { ret = 1.0 - Beta.regularizedBeta( getProbabilityOfSuccess(), x + 1.0, getNumberOfTrials() - x); } return ret; }