/** * 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); }
/** * 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)); }
/** * @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; }
/** * @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; }
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); }