Java Code Examples for org.apache.commons.math3.distribution.NormalDistribution#cumulativeProbability()

The following examples show how to use org.apache.commons.math3.distribution.NormalDistribution#cumulativeProbability() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MannWhitneyUTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param Umin smallest Mann-Whitney U value
 * @param n1 number of subjects in first sample
 * @param n2 number of subjects in second sample
 * @return two-sided asymptotic p-value
 * @throws ConvergenceException if the p-value can not be computed
 * due to a convergence error
 * @throws MaxCountExceededException if the maximum number of
 * iterations is exceeded
 */
private double calculateAsymptoticPValue(final double Umin,
                                         final int n1,
                                         final int n2)
    throws ConvergenceException, MaxCountExceededException {

    /* long multiplication to avoid overflow (double not used due to efficiency
     * and to avoid precision loss)
     */
    final long n1n2prod = (long) n1 * n2;

    // http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U#Normal_approximation
    final double EU = n1n2prod / 2.0;
    final double VarU = n1n2prod * (n1 + n2 + 1) / 12.0;

    final double z = (Umin - EU) / FastMath.sqrt(VarU);

    // No try-catch or advertised exception because args are valid
    // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
    final NormalDistribution standardNormal = new NormalDistribution(null, 0, 1);

    return 2 * standardNormal.cumulativeProbability(z);
}
 
Example 2
Source File: WilcoxonSignedRankTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param Wmin smallest Wilcoxon signed rank value
 * @param N number of subjects (corresponding to x.length)
 * @return two-sided asymptotic p-value
 */
private double calculateAsymptoticPValue(final double Wmin, final int N) {

    final double ES = (double) (N * (N + 1)) / 4.0;

    /* Same as (but saves computations):
     * final double VarW = ((double) (N * (N + 1) * (2*N + 1))) / 24;
     */
    final double VarS = ES * ((double) (2 * N + 1) / 6.0);

    // - 0.5 is a continuity correction
    final double z = (Wmin - ES - 0.5) / FastMath.sqrt(VarS);

    final NormalDistribution standardNormal = new NormalDistribution(0, 1);

    return 2*standardNormal.cumulativeProbability(z);
}
 
Example 3
Source File: MannWhitneyUTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param Umin smallest Mann-Whitney U value
 * @param n1 number of subjects in first sample
 * @param n2 number of subjects in second sample
 * @return two-sided asymptotic p-value
 * @throws ConvergenceException if the p-value can not be computed
 * due to a convergence error
 * @throws MaxCountExceededException if the maximum number of
 * iterations is exceeded
 */
private double calculateAsymptoticPValue(final double Umin,
                                         final int n1,
                                         final int n2)
    throws ConvergenceException, MaxCountExceededException {

    /* long multiplication to avoid overflow (double not used due to efficiency
     * and to avoid precision loss)
     */
    final long n1n2prod = (long) n1 * n2;

    // http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U#Normal_approximation
    final double EU = n1n2prod / 2.0;
    final double VarU = n1n2prod * (n1 + n2 + 1) / 12.0;

    final double z = (Umin - EU) / FastMath.sqrt(VarU);

    final NormalDistribution standardNormal = new NormalDistribution(0, 1);

    return 2 * standardNormal.cumulativeProbability(z);
}
 
Example 4
Source File: WilcoxonSignedRankTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param Wmin smallest Wilcoxon signed rank value
 * @param N number of subjects (corresponding to x.length)
 * @return two-sided asymptotic p-value
 */
private double calculateAsymptoticPValue(final double Wmin, final int N) {

    final double ES = (double) (N * (N + 1)) / 4.0;

    /* Same as (but saves computations):
     * final double VarW = ((double) (N * (N + 1) * (2*N + 1))) / 24;
     */
    final double VarS = ES * ((double) (2 * N + 1) / 6.0);

    // - 0.5 is a continuity correction
    final double z = (Wmin - ES - 0.5) / FastMath.sqrt(VarS);

    // No try-catch or advertised exception because args are valid
    final NormalDistribution standardNormal = new NormalDistribution(0, 1);

    return 2*standardNormal.cumulativeProbability(z);
}
 
Example 5
Source File: MannWhitneyUTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param Umin smallest Mann-Whitney U value
 * @param n1 number of subjects in first sample
 * @param n2 number of subjects in second sample
 * @return two-sided asymptotic p-value
 * @throws ConvergenceException if the p-value can not be computed
 * due to a convergence error
 * @throws MaxCountExceededException if the maximum number of
 * iterations is exceeded
 */
private double calculateAsymptoticPValue(final double Umin,
                                         final int n1,
                                         final int n2)
    throws ConvergenceException, MaxCountExceededException {

    /* long multiplication to avoid overflow (double not used due to efficiency
     * and to avoid precision loss)
     */
    final long n1n2prod = (long) n1 * n2;

    // http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U#Normal_approximation
    final double EU = n1n2prod / 2.0;
    final double VarU = n1n2prod * (n1 + n2 + 1) / 12.0;

    final double z = (Umin - EU) / FastMath.sqrt(VarU);

    // No try-catch or advertised exception because args are valid
    // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
    final NormalDistribution standardNormal = new NormalDistribution(null, 0, 1);

    return 2 * standardNormal.cumulativeProbability(z);
}
 
Example 6
Source File: WilcoxonSignedRankTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param Wmin smallest Wilcoxon signed rank value
 * @param N number of subjects (corresponding to x.length)
 * @return two-sided asymptotic p-value
 */
private double calculateAsymptoticPValue(final double Wmin, final int N) {

    final double ES = (double) (N * (N + 1)) / 4.0;

    /* Same as (but saves computations):
     * final double VarW = ((double) (N * (N + 1) * (2*N + 1))) / 24;
     */
    final double VarS = ES * ((double) (2 * N + 1) / 6.0);

    // - 0.5 is a continuity correction
    final double z = (Wmin - ES - 0.5) / FastMath.sqrt(VarS);

    // No try-catch or advertised exception because args are valid
    // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
    final NormalDistribution standardNormal = new NormalDistribution(null, 0, 1);

    return 2*standardNormal.cumulativeProbability(z);
}
 
Example 7
Source File: Context.java    From macrobase with Apache License 2.0 6 votes vote down vote up
/**
 * Estimating the size of a context
 * The Null Hypothesis is that density(c) >= minDensity
 *
 * @param c
 * @param minDensity
 * @return true if the estimation > minSize
 */
private boolean densityPruning(Context c, double minDensity) {
    if (densityPruning == false)
        return false;
    int sampleSize = globalSample.size();
    int sampleHit = c.getSample().size();
    double estimatedDensity = (double) sampleHit / sampleSize;
    double sampleSD = Math.sqrt(minDensity * (1 - minDensity) / sampleSize);
    double zScore = (estimatedDensity - minDensity) / sampleSD;
    NormalDistribution unitNormal = new NormalDistribution(0d, 1d);
    double pValue = unitNormal.cumulativeProbability(zScore);
    if (pValue <= alpha) {
        return true;
    } else {
        //fail to reject
        return false;
    }
}
 
Example 8
Source File: WilcoxonSignedRankTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param Wmin smallest Wilcoxon signed rank value
 * @param N number of subjects (corresponding to x.length)
 * @return two-sided asymptotic p-value
 */
private double calculateAsymptoticPValue(final double Wmin, final int N) {

    final double ES = (double) (N * (N + 1)) / 4.0;

    /* Same as (but saves computations):
     * final double VarW = ((double) (N * (N + 1) * (2*N + 1))) / 24;
     */
    final double VarS = ES * ((double) (2 * N + 1) / 6.0);

    // - 0.5 is a continuity correction
    final double z = (Wmin - ES - 0.5) / FastMath.sqrt(VarS);

    final NormalDistribution standardNormal = new NormalDistribution(0, 1);

    return 2*standardNormal.cumulativeProbability(z);
}
 
Example 9
Source File: patchedMannWhitneyUTest.java    From coming with MIT License 6 votes vote down vote up
/**
 * @param Umin smallest Mann-Whitney U value
 * @param n1 number of subjects in first sample
 * @param n2 number of subjects in second sample
 * @return two-sided asymptotic p-value
 * @throws ConvergenceException if the p-value can not be computed
 * due to a convergence error
 * @throws MaxCountExceededException if the maximum number of
 * iterations is exceeded
 */
private double calculateAsymptoticPValue(final double Umin,
                                         final int n1,
                                         final int n2)
    throws ConvergenceException, MaxCountExceededException {

    final int n1n2prod = n1 * n2;

    // http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U#Normal_approximation
    final double EU = n1n2prod / 2.0;
    final double VarU = (double) ((double) n1n2prod * (n1 + n2 + 1)) / 12.0;

    final double z = (Umin - EU) / FastMath.sqrt(VarU);

    final NormalDistribution standardNormal = new NormalDistribution(0, 1);

    return 2 * standardNormal.cumulativeProbability(z);
}
 
Example 10
Source File: Math_30_MannWhitneyUTest_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * @param Umin smallest Mann-Whitney U value
 * @param n1 number of subjects in first sample
 * @param n2 number of subjects in second sample
 * @return two-sided asymptotic p-value
 * @throws ConvergenceException if the p-value can not be computed
 * due to a convergence error
 * @throws MaxCountExceededException if the maximum number of
 * iterations is exceeded
 */
private double calculateAsymptoticPValue(final double Umin,
                                         final int n1,
                                         final int n2)
    throws ConvergenceException, MaxCountExceededException {

    final int n1n2prod = n1 * n2;

    // http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U#Normal_approximation
    final double EU = n1n2prod / 2.0;
    final double VarU = n1n2prod * (n1 + n2 + 1) / 12.0;

    final double z = (Umin - EU) / FastMath.sqrt(VarU);

    final NormalDistribution standardNormal = new NormalDistribution(0, 1);

    return 2 * standardNormal.cumulativeProbability(z);
}
 
Example 11
Source File: Math_30_MannWhitneyUTest_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @param Umin smallest Mann-Whitney U value
 * @param n1 number of subjects in first sample
 * @param n2 number of subjects in second sample
 * @return two-sided asymptotic p-value
 * @throws ConvergenceException if the p-value can not be computed
 * due to a convergence error
 * @throws MaxCountExceededException if the maximum number of
 * iterations is exceeded
 */
private double calculateAsymptoticPValue(final double Umin,
                                         final int n1,
                                         final int n2)
    throws ConvergenceException, MaxCountExceededException {

    final double n1n2prod = n1 * n2;

    // http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U#Normal_approximation
    final double EU = n1n2prod / 2.0;
    final double VarU = n1n2prod * (n1 + n2 + 1) / 12.0;

    final double z = (Umin - EU) / FastMath.sqrt(VarU);

    final NormalDistribution standardNormal = new NormalDistribution(0, 1);

    return 2 * standardNormal.cumulativeProbability(z);
}
 
Example 12
Source File: WilcoxonSignedRankTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param Wmin smallest Wilcoxon signed rank value
 * @param N number of subjects (corresponding to x.length)
 * @return two-sided asymptotic p-value
 */
private double calculateAsymptoticPValue(final double Wmin, final int N) {

    final double ES = (double) (N * (N + 1)) / 4.0;

    /* Same as (but saves computations):
     * final double VarW = ((double) (N * (N + 1) * (2*N + 1))) / 24;
     */
    final double VarS = ES * ((double) (2 * N + 1) / 6.0);

    // - 0.5 is a continuity correction
    final double z = (Wmin - ES - 0.5) / FastMath.sqrt(VarS);

    // No try-catch or advertised exception because args are valid
    // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
    final NormalDistribution standardNormal = new NormalDistribution(null, 0, 1);

    return 2*standardNormal.cumulativeProbability(z);
}
 
Example 13
Source File: Elixir_0022_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @param Umin smallest Mann-Whitney U value
 * @param n1 number of subjects in first sample
 * @param n2 number of subjects in second sample
 * @return two-sided asymptotic p-value
 * @throws ConvergenceException if the p-value can not be computed
 * due to a convergence error
 * @throws MaxCountExceededException if the maximum number of
 * iterations is exceeded
 */
private double calculateAsymptoticPValue(final double Umin,
                                         final int n1,
                                         final int n2)
    throws ConvergenceException, MaxCountExceededException {

    final double n1n2prod = n1 * n2;

    // http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U#Normal_approximation
    final double EU = n1n2prod / 2.0;
    final double VarU = n1n2prod * (n1 + n2 + 1) / 12.0;

    final double z = (Umin - EU) / FastMath.sqrt(VarU);

    final NormalDistribution standardNormal = new NormalDistribution(0, 1);

    return 2 * standardNormal.cumulativeProbability(z);
}
 
Example 14
Source File: WilcoxonSignedRankTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param Wmin smallest Wilcoxon signed rank value
 * @param N number of subjects (corresponding to x.length)
 * @return two-sided asymptotic p-value
 */
private double calculateAsymptoticPValue(final double Wmin, final int N) {

    final double ES = (double) (N * (N + 1)) / 4.0;

    /* Same as (but saves computations):
     * final double VarW = ((double) (N * (N + 1) * (2*N + 1))) / 24;
     */
    final double VarS = ES * ((double) (2 * N + 1) / 6.0);

    // - 0.5 is a continuity correction
    final double z = (Wmin - ES - 0.5) / FastMath.sqrt(VarS);

    // No try-catch or advertised exception because args are valid
    final NormalDistribution standardNormal = new NormalDistribution(0, 1);

    return 2*standardNormal.cumulativeProbability(z);
}
 
Example 15
Source File: MannWhitneyUTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param Umin smallest Mann-Whitney U value
 * @param n1 number of subjects in first sample
 * @param n2 number of subjects in second sample
 * @return two-sided asymptotic p-value
 * @throws ConvergenceException if the p-value can not be computed
 * due to a convergence error
 * @throws MaxCountExceededException if the maximum number of
 * iterations is exceeded
 */
private double calculateAsymptoticPValue(final double Umin,
                                         final int n1,
                                         final int n2)
    throws ConvergenceException, MaxCountExceededException {

    final int n1n2prod = n1 * n2;

    // http://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U#Normal_approximation
    final double EU = (double) n1n2prod / 2.0;
    final double VarU = (double) (n1n2prod * (n1 + n2 + 1)) / 12.0;

    final double z = (Umin - EU) / FastMath.sqrt(VarU);

    final NormalDistribution standardNormal = new NormalDistribution(0, 1);

    return 2 * standardNormal.cumulativeProbability(z);
}
 
Example 16
Source File: WilcoxonSignedRankTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param Wmin smallest Wilcoxon signed rank value
 * @param N number of subjects (corresponding to x.length)
 * @return two-sided asymptotic p-value
 */
private double calculateAsymptoticPValue(final double Wmin, final int N) {

    final double ES = (double) (N * (N + 1)) / 4.0;

    /* Same as (but saves computations):
     * final double VarW = ((double) (N * (N + 1) * (2*N + 1))) / 24;
     */
    final double VarS = ES * ((double) (2 * N + 1) / 6.0);

    // - 0.5 is a continuity correction
    final double z = (Wmin - ES - 0.5) / FastMath.sqrt(VarS);

    // No try-catch or advertised exception because args are valid
    final NormalDistribution standardNormal = new NormalDistribution(0, 1);

    return 2*standardNormal.cumulativeProbability(z);
}
 
Example 17
Source File: Statistics.java    From systemsgenetics with GNU General Public License v3.0 5 votes vote down vote up
/**
* based on https://www.researchgate.net/post/How_do_you_calculate_a_p_value_for_spearmans_rank_correlation
* and https://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient#Determining_significance
* 
* this is an approximation, so won't give exactly same results as R cor.test(), but with many samples and
* when there is a correlation they approximate eachother
* 
* @param spearmanCorrelation Spearman correlation from SpearmansCorrelation()
* 
* @param sampleSize Number of samples used to calculate correlation
* 
* @return Spearman two-tailed p-value from correlation
*/
  public static double calculateSpearmanTwoTailedPvalue(double spearmanCorrelation, int sampleSize){
  	double z = Math.sqrt((sampleSize-3)/1.06) * atanh(spearmanCorrelation);
  	NormalDistribution normalDistribution = new NormalDistribution();
  	double p = 2*normalDistribution.cumulativeProbability(-Math.abs(z));

  	if (Double.isNaN(z)){
  		p = 0;
  	}
  	if (Double.isNaN(spearmanCorrelation)){
  		p = 1;
  	}
  	return p;
  }
 
Example 18
Source File: TruncatedNormal.java    From pacaya with Apache License 2.0 5 votes vote down vote up
public TruncatedNormal(RandomGenerator rng, double mu, double sigma, double lowerBound, double upperBound) {
    super(rng);
    this.mu = mu;
    this.sigma = sigma;
    this.lowerBound = lowerBound;
    this.upperBound = upperBound;
    unnormalized = new NormalDistribution(mu, sigma);
    if (upperBound < lowerBound) {
        throw new IllegalArgumentException("upper bound must be no lower than lower bound");
    }
    lowerZ = unnormalized.cumulativeProbability(lowerBound);
    upperZ = 1 - unnormalized.cumulativeProbability(upperBound);
    reZ = 1 - (lowerZ + upperZ);
    NormalDistribution standardNormal = new NormalDistribution();
    double alpha = (lowerBound - mu) / sigma;
    double beta = (upperBound - mu) / sigma;
    double phiAlpha = standardNormal.density(alpha);
    double phiBeta = standardNormal.density(beta);
    double zPhiAlpha = standardNormal.cumulativeProbability(alpha);
    double zPhiBeta = standardNormal.cumulativeProbability(beta);
    double denom = (zPhiBeta - zPhiAlpha);
    double c = (phiBeta - phiAlpha) / denom;
    mean = mu - sigma * c;
    double d = 1 - c * c;
    if (phiBeta > 0.0) d -= beta * phiBeta / denom;
    if (phiAlpha > 0.0) d += alpha * phiAlpha / denom;
    variance = (sigma * sigma) * d;
    
}
 
Example 19
Source File: NormalDistributionTester.java    From Java-Data-Analysis with MIT License 5 votes vote down vote up
public static void main(String[] args) {
    NormalDistribution nd = new NormalDistribution(mu, sigma);
    
    double a = 17.5, b = 21.5;
    double Fa = nd.cumulativeProbability(a);
    System.out.printf("F(a) = %6.4f%n", Fa);
    double Fb = nd.cumulativeProbability(b);
    System.out.printf("F(b) = %6.4f%n", Fb);
    System.out.printf("F(b) - F(a) = %6.4f%n", Fb - Fa);
}
 
Example 20
Source File: Probit.java    From Alink with Apache License 2.0 4 votes vote down vote up
/**
 *
 * @param eta: eta
 * @return getmu
 */
@Override
public double unlink(double eta) {
    NormalDistribution distribution = new NormalDistribution();
    return distribution.cumulativeProbability(eta);
}