org.apache.commons.math.distribution.TDistributionImpl Java Examples

The following examples show how to use org.apache.commons.math.distribution.TDistributionImpl. 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: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that direct t-tests using standard error estimates are consistent
 * with reported p-values
 */
public void testStdErrorConsistency() throws Exception {
    TDistribution tDistribution = new TDistributionImpl(45);
    RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    RealMatrix rValues = corrInstance.getCorrelationMatrix();
    RealMatrix pValues = corrInstance.getCorrelationPValues();
    RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
            double t = Math.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j);
            double p = 2 * (1 - tDistribution.cumulativeProbability(t));
            assertEquals(p, pValues.getEntry(i, j), 10E-15);
        }
    }
}
 
Example #2
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that direct t-tests using standard error estimates are consistent
 * with reported p-values
 */
public void testStdErrorConsistency() throws Exception {
    TDistribution tDistribution = new TDistributionImpl(45);
    RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    RealMatrix rValues = corrInstance.getCorrelationMatrix();
    RealMatrix pValues = corrInstance.getCorrelationPValues();
    RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
            double t = Math.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j);
            double p = 2 * (1 - tDistribution.cumulativeProbability(t));
            assertEquals(p, pValues.getEntry(i, j), 10E-15);
        }
    }
}
 
Example #3
Source File: Math_69_PearsonsCorrelation_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Returns a matrix of p-values associated with the (two-sided) null
 * hypothesis that the corresponding correlation coefficient is zero.
 * <p><code>getCorrelationPValues().getEntry(i,j)</code> is the probability
 * that a random variable distributed as <code>t<sub>n-2</sub></code> takes
 * a value with absolute value greater than or equal to <br>
 * <code>|r|((n - 2) / (1 - r<sup>2</sup>))<sup>1/2</sup></code></p>
 * <p>The values in the matrix are sometimes referred to as the
 * <i>significance</i> of the corresponding correlation coefficients.</p>
 *
 * @return matrix of p-values
 * @throws MathException if an error occurs estimating probabilities
 */
public RealMatrix getCorrelationPValues() throws MathException {
    TDistribution tDistribution = new TDistributionImpl(nObs - 2);
    int nVars = correlationMatrix.getColumnDimension();
    double[][] out = new double[nVars][nVars];
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < nVars; j++) {
            if (i == j) {
                out[i][j] = 0d;
            } else {
                double r = correlationMatrix.getEntry(i, j);
                double t = Math.abs(r * Math.sqrt((nObs - 2)/(1 - r * r)));
                out[i][j] = 2 * tDistribution.cumulativeProbability(-t);
            }
        }
    }
    return new BlockRealMatrix(out);
}
 
Example #4
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Removes the observation (x,y) from the regression data set.
 * <p>
 * Mirrors the addData method.  This method permits the use of
 * SimpleRegression instances in streaming mode where the regression
 * is applied to a sliding "window" of observations, however the caller is
 * responsible for maintaining the set of observations in the window.</p>
 *
 * The method has no effect if there are no points of data (i.e. n=0)
 *
 * @param x independent variable value
 * @param y dependent variable value
 */
public void removeData(double x, double y) {
    if (n > 0) {
        double dx = x - xbar;
        double dy = y - ybar;
        sumXX -= dx * dx * (double) n / (n - 1d);
        sumYY -= dy * dy * (double) n / (n - 1d);
        sumXY -= dx * dy * (double) n / (n - 1d);
        xbar -= dx / (n - 1.0);
        ybar -= dy / (n - 1.0);
        sumX -= x;
        sumY -= y;
        n--;

        if (n > 2) {
            distribution = new TDistributionImpl(n - 2);
        }
    }
}
 
Example #5
Source File: Math_69_PearsonsCorrelation_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Returns a matrix of p-values associated with the (two-sided) null
 * hypothesis that the corresponding correlation coefficient is zero.
 * <p><code>getCorrelationPValues().getEntry(i,j)</code> is the probability
 * that a random variable distributed as <code>t<sub>n-2</sub></code> takes
 * a value with absolute value greater than or equal to <br>
 * <code>|r|((n - 2) / (1 - r<sup>2</sup>))<sup>1/2</sup></code></p>
 * <p>The values in the matrix are sometimes referred to as the
 * <i>significance</i> of the corresponding correlation coefficients.</p>
 *
 * @return matrix of p-values
 * @throws MathException if an error occurs estimating probabilities
 */
public RealMatrix getCorrelationPValues() throws MathException {
    TDistribution tDistribution = new TDistributionImpl(nObs - 2);
    int nVars = correlationMatrix.getColumnDimension();
    double[][] out = new double[nVars][nVars];
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < nVars; j++) {
            if (i == j) {
                out[i][j] = 0d;
            } else {
                double r = correlationMatrix.getEntry(i, j);
                double t = Math.abs(r * Math.sqrt((nObs - 2)/(1 - r * r)));
                out[i][j] = 2 * (1 - tDistribution.cumulativeProbability(t));
            }
        }
    }
    return new BlockRealMatrix(out);
}
 
Example #6
Source File: Nopol2017_0076_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Returns a matrix of p-values associated with the (two-sided) null
 * hypothesis that the corresponding correlation coefficient is zero.
 * <p><code>getCorrelationPValues().getEntry(i,j)</code> is the probability
 * that a random variable distributed as <code>t<sub>n-2</sub></code> takes
 * a value with absolute value greater than or equal to <br>
 * <code>|r|((n - 2) / (1 - r<sup>2</sup>))<sup>1/2</sup></code></p>
 * <p>The values in the matrix are sometimes referred to as the
 * <i>significance</i> of the corresponding correlation coefficients.</p>
 *
 * @return matrix of p-values
 * @throws MathException if an error occurs estimating probabilities
 */
public RealMatrix getCorrelationPValues() throws MathException {
    TDistribution tDistribution = new TDistributionImpl(nObs - 2);
    int nVars = correlationMatrix.getColumnDimension();
    double[][] out = new double[nVars][nVars];
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < nVars; j++) {
            if (i == j) {
                out[i][j] = 0d;
            } else {
                double r = correlationMatrix.getEntry(i, j);
                double t = Math.abs(r * Math.sqrt((nObs - 2)/(1 - r * r)));
                out[i][j] = 2 * (1 - tDistribution.cumulativeProbability(t));
            }
        }
    }
    return new BlockRealMatrix(out);
}
 
Example #7
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that direct t-tests using standard error estimates are consistent
 * with reported p-values
 */
public void testStdErrorConsistency() throws Exception {
    TDistribution tDistribution = new TDistributionImpl(45);
    RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    RealMatrix rValues = corrInstance.getCorrelationMatrix();
    RealMatrix pValues = corrInstance.getCorrelationPValues();
    RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
            double t = FastMath.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j);
            double p = 2 * (1 - tDistribution.cumulativeProbability(t));
            assertEquals(p, pValues.getEntry(i, j), 10E-15);
        }
    }
}
 
Example #8
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a matrix of p-values associated with the (two-sided) null
 * hypothesis that the corresponding correlation coefficient is zero.
 * <p><code>getCorrelationPValues().getEntry(i,j)</code> is the probability
 * that a random variable distributed as <code>t<sub>n-2</sub></code> takes
 * a value with absolute value greater than or equal to <br>
 * <code>|r|((n - 2) / (1 - r<sup>2</sup>))<sup>1/2</sup></code></p>
 * <p>The values in the matrix are sometimes referred to as the
 * <i>significance</i> of the corresponding correlation coefficients.</p>
 *
 * @return matrix of p-values
 * @throws MathException if an error occurs estimating probabilities
 */
public RealMatrix getCorrelationPValues() throws MathException {
    TDistribution tDistribution = new TDistributionImpl(nObs - 2);
    int nVars = correlationMatrix.getColumnDimension();
    double[][] out = new double[nVars][nVars];
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < nVars; j++) {
            if (i == j) {
                out[i][j] = 0d;
            } else {
                double r = correlationMatrix.getEntry(i, j);
                double t = FastMath.abs(r * FastMath.sqrt((nObs - 2)/(1 - r * r)));
                out[i][j] = 2 * tDistribution.cumulativeProbability(-t);
            }
        }
    }
    return new BlockRealMatrix(out);
}
 
Example #9
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds the observation (x,y) to the regression data set.
 * <p>
 * Uses updating formulas for means and sums of squares defined in
 * "Algorithms for Computing the Sample Variance: Analysis and
 * Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J.
 * 1983, American Statistician, vol. 37, pp. 242-247, referenced in
 * Weisberg, S. "Applied Linear Regression". 2nd Ed. 1985.</p>
 *
 *
 * @param x independent variable value
 * @param y dependent variable value
 */
public void addData(double x, double y) {
    if (n == 0) {
        xbar = x;
        ybar = y;
    } else {
        double dx = x - xbar;
        double dy = y - ybar;
        sumXX += dx * dx * (double) n / (n + 1d);
        sumYY += dy * dy * (double) n / (n + 1d);
        sumXY += dx * dy * (double) n / (n + 1d);
        xbar += dx / (n + 1.0);
        ybar += dy / (n + 1.0);
    }
    sumX += x;
    sumY += y;
    n++;

    if (n > 2) {
        distribution = new TDistributionImpl(n - 2);
    }
}
 
Example #10
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Removes the observation (x,y) from the regression data set.
 * <p>
 * Mirrors the addData method.  This method permits the use of
 * SimpleRegression instances in streaming mode where the regression
 * is applied to a sliding "window" of observations, however the caller is
 * responsible for maintaining the set of observations in the window.</p>
 *
 * The method has no effect if there are no points of data (i.e. n=0)
 *
 * @param x independent variable value
 * @param y dependent variable value
 */
public void removeData(double x, double y) {
    if (n > 0) {
        double dx = x - xbar;
        double dy = y - ybar;
        sumXX -= dx * dx * (double) n / (n - 1d);
        sumYY -= dy * dy * (double) n / (n - 1d);
        sumXY -= dx * dy * (double) n / (n - 1d);
        xbar -= dx / (n - 1.0);
        ybar -= dy / (n - 1.0);
        sumX -= x;
        sumY -= y;
        n--;

        if (n > 2) {
            distribution = new TDistributionImpl(n - 2);
        }
    }
}
 
Example #11
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that direct t-tests using standard error estimates are consistent
 * with reported p-values
 */
@Test
public void testStdErrorConsistency() throws Exception {
    TDistribution tDistribution = new TDistributionImpl(45);
    RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    RealMatrix rValues = corrInstance.getCorrelationMatrix();
    RealMatrix pValues = corrInstance.getCorrelationPValues();
    RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
            double t = FastMath.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j);
            double p = 2 * (1 - tDistribution.cumulativeProbability(t));
            Assert.assertEquals(p, pValues.getEntry(i, j), 10E-15);
        }
    }
}
 
Example #12
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that direct t-tests using standard error estimates are consistent
 * with reported p-values
 */
public void testStdErrorConsistency() throws Exception {
    TDistribution tDistribution = new TDistributionImpl(45);
    RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    RealMatrix rValues = corrInstance.getCorrelationMatrix();
    RealMatrix pValues = corrInstance.getCorrelationPValues();
    RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
            double t = Math.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j);
            double p = 2 * (1 - tDistribution.cumulativeProbability(t));
            assertEquals(p, pValues.getEntry(i, j), 10E-15);
        }
    }
}
 
Example #13
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a matrix of p-values associated with the (two-sided) null
 * hypothesis that the corresponding correlation coefficient is zero.
 * <p><code>getCorrelationPValues().getEntry(i,j)</code> is the probability
 * that a random variable distributed as <code>t<sub>n-2</sub></code> takes
 * a value with absolute value greater than or equal to <br>
 * <code>|r|((n - 2) / (1 - r<sup>2</sup>))<sup>1/2</sup></code></p>
 * <p>The values in the matrix are sometimes referred to as the 
 * <i>significance</i> of the corresponding correlation coefficients.</p>
 * 
 * @return matrix of p-values
 * @throws MathException if an error occurs estimating probabilities
 */
public RealMatrix getCorrelationPValues() throws MathException {
    TDistribution tDistribution = new TDistributionImpl(nObs - 2);
    int nVars = correlationMatrix.getColumnDimension();
    double[][] out = new double[nVars][nVars];
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < nVars; j++) {
            if (i == j) {
                out[i][j] = 0d;
            } else {
                double r = correlationMatrix.getEntry(i, j);
                double t = Math.abs(r * Math.sqrt((nObs - 2)/(1 - r * r)));
                out[i][j] = 2 * (1 - tDistribution.cumulativeProbability(t));
            }
        }
    }
    return new BlockRealMatrix(out);
}
 
Example #14
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that direct t-tests using standard error estimates are consistent
 * with reported p-values
 */
public void testStdErrorConsistency() throws Exception {
    TDistribution tDistribution = new TDistributionImpl(45);
    RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    RealMatrix rValues = corrInstance.getCorrelationMatrix();
    RealMatrix pValues = corrInstance.getCorrelationPValues();
    RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
            double t = Math.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j);
            double p = 2 * (1 - tDistribution.cumulativeProbability(t));
            assertEquals(p, pValues.getEntry(i, j), 10E-15);
        }
    }
}
 
Example #15
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a matrix of p-values associated with the (two-sided) null
 * hypothesis that the corresponding correlation coefficient is zero.
 * <p><code>getCorrelationPValues().getEntry(i,j)</code> is the probability
 * that a random variable distributed as <code>t<sub>n-2</sub></code> takes
 * a value with absolute value greater than or equal to <br>
 * <code>|r|((n - 2) / (1 - r<sup>2</sup>))<sup>1/2</sup></code></p>
 * <p>The values in the matrix are sometimes referred to as the
 * <i>significance</i> of the corresponding correlation coefficients.</p>
 *
 * @return matrix of p-values
 * @throws MathException if an error occurs estimating probabilities
 */
public RealMatrix getCorrelationPValues() throws MathException {
    TDistribution tDistribution = new TDistributionImpl(nObs - 2);
    int nVars = correlationMatrix.getColumnDimension();
    double[][] out = new double[nVars][nVars];
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < nVars; j++) {
            if (i == j) {
                out[i][j] = 0d;
            } else {
                double r = correlationMatrix.getEntry(i, j);
                double t = Math.abs(r * Math.sqrt((nObs - 2)/(1 - r * r)));
                out[i][j] = 2 * (1 - tDistribution.cumulativeProbability(t));
            }
        }
    }
    return new BlockRealMatrix(out);
}
 
Example #16
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that direct t-tests using standard error estimates are consistent
 * with reported p-values
 */
public void testStdErrorConsistency() throws Exception {
    TDistribution tDistribution = new TDistributionImpl(45);
    RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    RealMatrix rValues = corrInstance.getCorrelationMatrix();
    RealMatrix pValues = corrInstance.getCorrelationPValues();
    RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
            double t = Math.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j);
            double p = 2 * (1 - tDistribution.cumulativeProbability(t));
            assertEquals(p, pValues.getEntry(i, j), 10E-15);
        }
    }
}
 
Example #17
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a matrix of p-values associated with the (two-sided) null
 * hypothesis that the corresponding correlation coefficient is zero.
 * <p><code>getCorrelationPValues().getEntry(i,j)</code> is the probability
 * that a random variable distributed as <code>t<sub>n-2</sub></code> takes
 * a value with absolute value greater than or equal to <br>
 * <code>|r|((n - 2) / (1 - r<sup>2</sup>))<sup>1/2</sup></code></p>
 * <p>The values in the matrix are sometimes referred to as the
 * <i>significance</i> of the corresponding correlation coefficients.</p>
 *
 * @return matrix of p-values
 * @throws MathException if an error occurs estimating probabilities
 */
public RealMatrix getCorrelationPValues() throws MathException {
    TDistribution tDistribution = new TDistributionImpl(nObs - 2);
    int nVars = correlationMatrix.getColumnDimension();
    double[][] out = new double[nVars][nVars];
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < nVars; j++) {
            if (i == j) {
                out[i][j] = 0d;
            } else {
                double r = correlationMatrix.getEntry(i, j);
                double t = Math.abs(r * Math.sqrt((nObs - 2)/(1 - r * r)));
                out[i][j] = 2 * (1 - tDistribution.cumulativeProbability(t));
            }
        }
    }
    return new BlockRealMatrix(out);
}
 
Example #18
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a matrix of p-values associated with the (two-sided) null
 * hypothesis that the corresponding correlation coefficient is zero.
 * <p><code>getCorrelationPValues().getEntry(i,j)</code> is the probability
 * that a random variable distributed as <code>t<sub>n-2</sub></code> takes
 * a value with absolute value greater than or equal to <br>
 * <code>|r|((n - 2) / (1 - r<sup>2</sup>))<sup>1/2</sup></code></p>
 * <p>The values in the matrix are sometimes referred to as the
 * <i>significance</i> of the corresponding correlation coefficients.</p>
 *
 * @return matrix of p-values
 * @throws MathException if an error occurs estimating probabilities
 */
public RealMatrix getCorrelationPValues() throws MathException {
    TDistribution tDistribution = new TDistributionImpl(nObs - 2);
    int nVars = correlationMatrix.getColumnDimension();
    double[][] out = new double[nVars][nVars];
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < nVars; j++) {
            if (i == j) {
                out[i][j] = 0d;
            } else {
                double r = correlationMatrix.getEntry(i, j);
                double t = Math.abs(r * Math.sqrt((nObs - 2)/(1 - r * r)));
                out[i][j] = 2 * (1 - tDistribution.cumulativeProbability(t));
            }
        }
    }
    return new BlockRealMatrix(out);
}
 
Example #19
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that direct t-tests using standard error estimates are consistent
 * with reported p-values
 */
public void testStdErrorConsistency() throws Exception {
    TDistribution tDistribution = new TDistributionImpl(45);
    RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    RealMatrix rValues = corrInstance.getCorrelationMatrix();
    RealMatrix pValues = corrInstance.getCorrelationPValues();
    RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
            double t = Math.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j);
            double p = 2 * (1 - tDistribution.cumulativeProbability(t));
            assertEquals(p, pValues.getEntry(i, j), 10E-15);
        }
    }
}
 
Example #20
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that direct t-tests using standard error estimates are consistent
 * with reported p-values
 */
public void testStdErrorConsistency() throws Exception {
    TDistribution tDistribution = new TDistributionImpl(45);
    RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix); 
    RealMatrix rValues = corrInstance.getCorrelationMatrix();
    RealMatrix pValues = corrInstance.getCorrelationPValues();
    RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
            double t = Math.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j);
            double p = 2 * (1 - tDistribution.cumulativeProbability(t));
            assertEquals(p, pValues.getEntry(i, j), 10E-15);
        }
    }
}
 
Example #21
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a matrix of p-values associated with the (two-sided) null
 * hypothesis that the corresponding correlation coefficient is zero.
 * <p><code>getCorrelationPValues().getEntry(i,j)</code> is the probability
 * that a random variable distributed as <code>t<sub>n-2</sub></code> takes
 * a value with absolute value greater than or equal to <br>
 * <code>|r|((n - 2) / (1 - r<sup>2</sup>))<sup>1/2</sup></code></p>
 * <p>The values in the matrix are sometimes referred to as the
 * <i>significance</i> of the corresponding correlation coefficients.</p>
 *
 * @return matrix of p-values
 * @throws MathException if an error occurs estimating probabilities
 */
public RealMatrix getCorrelationPValues() throws MathException {
    TDistribution tDistribution = new TDistributionImpl(nObs - 2);
    int nVars = correlationMatrix.getColumnDimension();
    double[][] out = new double[nVars][nVars];
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < nVars; j++) {
            if (i == j) {
                out[i][j] = 0d;
            } else {
                double r = correlationMatrix.getEntry(i, j);
                double t = Math.abs(r * Math.sqrt((nObs - 2)/(1 - r * r)));
                out[i][j] = 2 * (1 - tDistribution.cumulativeProbability(t));
            }
        }
    }
    return new BlockRealMatrix(out);
}
 
Example #22
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that direct t-tests using standard error estimates are consistent
 * with reported p-values
 */
public void testStdErrorConsistency() throws Exception {
    TDistribution tDistribution = new TDistributionImpl(45);
    RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    RealMatrix rValues = corrInstance.getCorrelationMatrix();
    RealMatrix pValues = corrInstance.getCorrelationPValues();
    RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
            double t = Math.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j);
            double p = 2 * (1 - tDistribution.cumulativeProbability(t));
            assertEquals(p, pValues.getEntry(i, j), 10E-15);
        }
    }
}
 
Example #23
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a matrix of p-values associated with the (two-sided) null
 * hypothesis that the corresponding correlation coefficient is zero.
 * <p><code>getCorrelationPValues().getEntry(i,j)</code> is the probability
 * that a random variable distributed as <code>t<sub>n-2</sub></code> takes
 * a value with absolute value greater than or equal to <br>
 * <code>|r|((n - 2) / (1 - r<sup>2</sup>))<sup>1/2</sup></code></p>
 * <p>The values in the matrix are sometimes referred to as the
 * <i>significance</i> of the corresponding correlation coefficients.</p>
 *
 * @return matrix of p-values
 * @throws MathException if an error occurs estimating probabilities
 */
public RealMatrix getCorrelationPValues() throws MathException {
    TDistribution tDistribution = new TDistributionImpl(nObs - 2);
    int nVars = correlationMatrix.getColumnDimension();
    double[][] out = new double[nVars][nVars];
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < nVars; j++) {
            if (i == j) {
                out[i][j] = 0d;
            } else {
                double r = correlationMatrix.getEntry(i, j);
                double t = FastMath.abs(r * FastMath.sqrt((nObs - 2)/(1 - r * r)));
                out[i][j] = 2 * tDistribution.cumulativeProbability(-t);
            }
        }
    }
    return new BlockRealMatrix(out);
}
 
Example #24
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that direct t-tests using standard error estimates are consistent
 * with reported p-values
 */
public void testStdErrorConsistency() throws Exception {
    TDistribution tDistribution = new TDistributionImpl(45);
    RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    RealMatrix rValues = corrInstance.getCorrelationMatrix();
    RealMatrix pValues = corrInstance.getCorrelationPValues();
    RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
            double t = Math.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j);
            double p = 2 * (1 - tDistribution.cumulativeProbability(t));
            assertEquals(p, pValues.getEntry(i, j), 10E-15);
        }
    }
}
 
Example #25
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Adds the observation (x,y) to the regression data set.
 * <p>
 * Uses updating formulas for means and sums of squares defined in
 * "Algorithms for Computing the Sample Variance: Analysis and
 * Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J.
 * 1983, American Statistician, vol. 37, pp. 242-247, referenced in
 * Weisberg, S. "Applied Linear Regression". 2nd Ed. 1985.</p>
 *
 *
 * @param x independent variable value
 * @param y dependent variable value
 */
public void addData(double x, double y) {
    if (n == 0) {
        xbar = x;
        ybar = y;
    } else {
        double dx = x - xbar;
        double dy = y - ybar;
        sumXX += dx * dx * (double) n / (n + 1d);
        sumYY += dy * dy * (double) n / (n + 1d);
        sumXY += dx * dy * (double) n / (n + 1d);
        xbar += dx / (n + 1.0);
        ybar += dy / (n + 1.0);
    }
    sumX += x;
    sumY += y;
    n++;

    if (n > 2) {
        distribution = new TDistributionImpl(n - 2);
    }
}
 
Example #26
Source File: SimpleRegression.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Removes the observation (x,y) from the regression data set.
 * <p>
 * Mirrors the addData method.  This method permits the use of
 * SimpleRegression instances in streaming mode where the regression
 * is applied to a sliding "window" of observations, however the caller is
 * responsible for maintaining the set of observations in the window.</p>
 *
 * The method has no effect if there are no points of data (i.e. n=0)
 *
 * @param x independent variable value
 * @param y dependent variable value
 */
public void removeData(double x, double y) {
    if (n > 0) {
        double dx = x - xbar;
        double dy = y - ybar;
        sumXX -= dx * dx * (double) n / (n - 1d);
        sumYY -= dy * dy * (double) n / (n - 1d);
        sumXY -= dx * dy * (double) n / (n - 1d);
        xbar -= dx / (n - 1.0);
        ybar -= dy / (n - 1.0);
        sumX -= x;
        sumY -= y;
        n--;

        if (n > 2) {
            distribution = new TDistributionImpl(n - 2);
        }
    }
}
 
Example #27
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that direct t-tests using standard error estimates are consistent
 * with reported p-values
 */
@Test
public void testStdErrorConsistency() throws Exception {
    TDistribution tDistribution = new TDistributionImpl(45);
    RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    RealMatrix rValues = corrInstance.getCorrelationMatrix();
    RealMatrix pValues = corrInstance.getCorrelationPValues();
    RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
            double t = FastMath.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j);
            double p = 2 * (1 - tDistribution.cumulativeProbability(t));
            Assert.assertEquals(p, pValues.getEntry(i, j), 10E-15);
        }
    }
}
 
Example #28
Source File: PearsonsCorrelation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a matrix of p-values associated with the (two-sided) null
 * hypothesis that the corresponding correlation coefficient is zero.
 * <p><code>getCorrelationPValues().getEntry(i,j)</code> is the probability
 * that a random variable distributed as <code>t<sub>n-2</sub></code> takes
 * a value with absolute value greater than or equal to <br>
 * <code>|r|((n - 2) / (1 - r<sup>2</sup>))<sup>1/2</sup></code></p>
 * <p>The values in the matrix are sometimes referred to as the 
 * <i>significance</i> of the corresponding correlation coefficients.</p>
 * 
 * @return matrix of p-values
 * @throws MathException if an error occurs estimating probabilities
 */
public RealMatrix getCorrelationPValues() throws MathException {
    TDistribution tDistribution = new TDistributionImpl(nObs - 2);
    int nVars = correlationMatrix.getColumnDimension();
    double[][] out = new double[nVars][nVars];
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < nVars; j++) {
            if (i == j) {
                out[i][j] = 0d;
            } else {
                double r = correlationMatrix.getEntry(i, j);
                double t = Math.abs(r * Math.sqrt((nObs - 2)/(1 - r * r)));
                out[i][j] = 2 * (1 - tDistribution.cumulativeProbability(t));
            }
        }
    }
    return new BlockRealMatrix(out);
}
 
Example #29
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that direct t-tests using standard error estimates are consistent
 * with reported p-values
 */
public void testStdErrorConsistency() throws Exception {
    TDistribution tDistribution = new TDistributionImpl(45);
    RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix); 
    RealMatrix rValues = corrInstance.getCorrelationMatrix();
    RealMatrix pValues = corrInstance.getCorrelationPValues();
    RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
            double t = Math.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j);
            double p = 2 * (1 - tDistribution.cumulativeProbability(t));
            assertEquals(p, pValues.getEntry(i, j), 10E-15);
        }
    }
}
 
Example #30
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Verify that direct t-tests using standard error estimates are consistent
 * with reported p-values
 */
public void testStdErrorConsistency() throws Exception {
    TDistribution tDistribution = new TDistributionImpl(45);
    RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix); 
    RealMatrix rValues = corrInstance.getCorrelationMatrix();
    RealMatrix pValues = corrInstance.getCorrelationPValues();
    RealMatrix stdErrors = corrInstance.getCorrelationStandardErrors();
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < i; j++) {
            double t = Math.abs(rValues.getEntry(i, j)) / stdErrors.getEntry(i, j);
            double p = 2 * (1 - tDistribution.cumulativeProbability(t));
            assertEquals(p, pValues.getEntry(i, j), 10E-15);
        }
    }
}