List of usage examples for org.apache.commons.math3.distribution TDistribution cumulativeProbability
public double cumulativeProbability(double x)
From source file:PCC.java
public double pvalue(double corel, double n) { double t = Math.abs(corel * Math.sqrt((n - 2.0) / (1.0 - (corel * corel)))); TDistribution tdist = new TDistribution(n - 2); double pvalue = 2 * (1.0 - tdist.cumulativeProbability(t)); return pvalue; }
From source file:embedded2.ESecure.TTest.java
/** * Computes p-value for 2-sided, 1-sample t-test. * * @param m sample mean/* w w w .j a va 2 s.c o m*/ * @param mu constant to test against * @param v sample variance * @param n sample n * @return p-value * @throws MaxCountExceededException if an error occurs computing the p-value */ protected static double tTest(final double m, final double mu, final double v, final double n) throws MaxCountExceededException { double t = FastMath.abs(t(m, mu, v, n)); TDistribution distribution = new TDistribution(n - 1); return 2.0 * distribution.cumulativeProbability(-t); }
From source file:embedded2.ESecure.TTest.java
/** * Computes p-value for 2-sided, 2-sample t-test. * <p>// www.j av a2 s . c om * Does not assume subpopulation variances are equal. Degrees of freedom * are estimated from the data.</p> * * @param m1 first sample mean * @param m2 second sample mean * @param v1 first sample variance * @param v2 second sample variance * @param n1 first sample n * @param n2 second sample n * @return p-value * @throws MaxCountExceededException if an error occurs computing the p-value */ protected static double tTest(final double m1, final double m2, final double v1, final double v2, final double n1, final double n2) throws MaxCountExceededException { final double t = FastMath.abs(t(m1, m2, v1, v2, n1, n2)); final double degreesOfFreedom = df(v1, v2, n1, n2); TDistribution distribution = new TDistribution(degreesOfFreedom); return 2.0 * distribution.cumulativeProbability(-t); }
From source file:embedded2.ESecure.TTest.java
/** * Computes p-value for 2-sided, 2-sample t-test, under the assumption * of equal subpopulation variances.//from w w w . j a v a 2 s . c om * <p> * The sum of the sample sizes minus 2 is used as degrees of freedom.</p> * * @param m1 first sample mean * @param m2 second sample mean * @param v1 first sample variance * @param v2 second sample variance * @param n1 first sample n * @param n2 second sample n * @return p-value * @throws MaxCountExceededException if an error occurs computing the p-value */ protected static double homoscedasticTTest(double m1, double m2, double v1, double v2, double n1, double n2) throws MaxCountExceededException { final double t = FastMath.abs(homoscedasticT(m1, m2, v1, v2, n1, n2)); final double degreesOfFreedom = n1 + n2 - 2; TDistribution distribution = new TDistribution(degreesOfFreedom); return 2.0 * distribution.cumulativeProbability(-t); }
From source file:edu.washington.gs.skyline.model.quantification.LinearModel.java
public List<LinearFitResult> fit(double[] observations) { if (observations.length != designMatrix.getRowDimension()) { throw new IllegalArgumentException("Wrong number of rows"); }//from w ww . ja va 2 s .c om RealVector coefficients = qrDecomposition.getSolver().solve(new ArrayRealVector(observations)); RealVector fittedValues = new ArrayRealVector(observations.length); RealVector residuals = new ArrayRealVector(observations.length); for (int iRow = 0; iRow < observations.length; iRow++) { RealVector designRow = designMatrix.getRowVector(iRow); fittedValues.setEntry(iRow, designRow.dotProduct(coefficients)); residuals.setEntry(iRow, observations[iRow] - fittedValues.getEntry(iRow)); } double rss = residuals.dotProduct(residuals); int degreesOfFreedom = observations.length - qrDecomposition.getR().getColumnDimension(); double resVar = rss / degreesOfFreedom; double sigma = Math.sqrt(resVar); RealMatrix covarianceUnscaled = matrixCrossproductInverse; RealMatrix scaledCovariance = covarianceUnscaled.scalarMultiply(sigma * sigma); List<LinearFitResult> results = new ArrayList<>(); for (int iContrastRow = 0; iContrastRow < contrastValues.getRowDimension(); iContrastRow++) { RealVector contrastRow = contrastValues.getRowVector(iContrastRow); double standardError = 0; for (int iRow = 0; iRow < independentColumnIndices.length; iRow++) { for (int iCol = 0; iCol < independentColumnIndices.length; iCol++) { standardError = contrastRow.getEntry(independentColumnIndices[iRow]) * scaledCovariance.getEntry(iRow, iCol) * contrastRow.getEntry(independentColumnIndices[iCol]); } } standardError = Math.sqrt(standardError); double foldChange = coefficients.dotProduct(contrastRow); LinearFitResult linearFitResult = new LinearFitResult(foldChange); double tValue = foldChange / standardError; linearFitResult.setTValue(tValue); linearFitResult.setStandardError(standardError); linearFitResult.setDegreesOfFreedom(degreesOfFreedom); if (0 == degreesOfFreedom) { linearFitResult.setPValue(1.0); } else { TDistribution tDistribution = new TDistribution(degreesOfFreedom); double pValue = (1 - tDistribution.cumulativeProbability(Math.abs(tValue))) * 2; linearFitResult.setPValue(pValue); } results.add(linearFitResult); } return results; }
From source file:com.itemanalysis.psychometrics.polycor.Covariance.java
public double correlationPvalue() { double se = correlationStandardError(); if (se == 0.0) return Double.NaN; double r = correlation(true); double tval = r / se; double df = N - 2.0; TDistribution t = new TDistribution(df); double pvalue = 1 - t.cumulativeProbability(tval); double twoSidedPvalue = 2.0 * Math.min(pvalue, 1 - pvalue);//from R function cor.test() return twoSidedPvalue; }
From source file:com.opengamma.strata.math.impl.regression.OrdinaryLeastSquaresRegression.java
private LeastSquaresRegressionResult getResultWithStatistics(double[][] x, double[] y, double[] betas, double[] yModel, DoubleMatrix transpose, DoubleMatrix matrix, boolean useIntercept) { double yMean = 0.; for (double y1 : y) { yMean += y1;/*from w w w .ja va 2 s .co m*/ } yMean /= y.length; double totalSumOfSquares = 0.; double errorSumOfSquares = 0.; int n = x.length; int k = betas.length; double[] residuals = new double[n]; double[] stdErrorBetas = new double[k]; double[] tStats = new double[k]; double[] pValues = new double[k]; for (int i = 0; i < n; i++) { totalSumOfSquares += (y[i] - yMean) * (y[i] - yMean); residuals[i] = y[i] - yModel[i]; errorSumOfSquares += residuals[i] * residuals[i]; } double regressionSumOfSquares = totalSumOfSquares - errorSumOfSquares; double[][] covarianceBetas = convertArray( _algebra.getInverse(_algebra.multiply(transpose, matrix)).toArray()); double rSquared = regressionSumOfSquares / totalSumOfSquares; double adjustedRSquared = 1. - (1 - rSquared) * (n - 1.) / (n - k); double meanSquareError = errorSumOfSquares / (n - k); TDistribution studentT = new TDistribution(n - k); for (int i = 0; i < k; i++) { stdErrorBetas[i] = Math.sqrt(meanSquareError * covarianceBetas[i][i]); tStats[i] = betas[i] / stdErrorBetas[i]; pValues[i] = 1 - studentT.cumulativeProbability(Math.abs(tStats[i])); } return new LeastSquaresRegressionResult(betas, residuals, meanSquareError, stdErrorBetas, rSquared, adjustedRSquared, tStats, pValues, useIntercept); }
From source file:com.thoughtworks.studios.journey.models.ActionCorrelationCalculation.java
public List<CorrelationResult> calculate() { Map<String, List<List<Integer>>> data = rawData(); List<CorrelationResult> results = new ArrayList<>(data.size()); for (String action : data.keySet()) { SpearmansCorrelation correlation = new SpearmansCorrelation(); List<List<Integer>> variables = data.get(action); double[] x = toDoubleArray(variables.get(0)); double[] y = toDoubleArray(variables.get(1)); double r = correlation.correlation(x, y); TDistribution tDistribution = new TDistribution(x.length - 2); double t = FastMath.abs(r * FastMath.sqrt((x.length - 2) / (1 - r * r))); double pValue = 2 * tDistribution.cumulativeProbability(-t); SummaryStatistics successSt = new SummaryStatistics(); SummaryStatistics failSt = new SummaryStatistics(); for (int i = 0; i < x.length; i++) { if (y[i] == 1) { successSt.addValue(x[i]); } else { failSt.addValue(x[i]);/* w w w. jav a 2 s. c o m*/ } } results.add(new CorrelationResult(action, r, pValue, successSt, failSt)); } Collections.sort(results, new Comparator<CorrelationResult>() { @Override public int compare(CorrelationResult r1, CorrelationResult r2) { Double abs1 = Math.abs(r2.getCorrelation()); Double abs2 = Math.abs(r1.getCorrelation()); return abs1.compareTo(abs2); } }); return results; }
From source file:com.opengamma.strata.math.impl.regression.WeightedLeastSquaresRegression.java
private LeastSquaresRegressionResult getResultWithStatistics(double[][] x, double[][] w, double[] y, double[] betas, double[] yModel, DoubleMatrix transpose, DoubleMatrix matrix, boolean useIntercept) { double yMean = 0.; for (double y1 : y) { yMean += y1;/*from w w w . java 2 s. c o m*/ } yMean /= y.length; double totalSumOfSquares = 0.; double errorSumOfSquares = 0.; int n = x.length; int k = betas.length; double[] residuals = new double[n]; double[] standardErrorsOfBeta = new double[k]; double[] tStats = new double[k]; double[] pValues = new double[k]; for (int i = 0; i < n; i++) { totalSumOfSquares += w[i][i] * (y[i] - yMean) * (y[i] - yMean); residuals[i] = y[i] - yModel[i]; errorSumOfSquares += w[i][i] * residuals[i] * residuals[i]; } double regressionSumOfSquares = totalSumOfSquares - errorSumOfSquares; double[][] covarianceBetas = convertArray( s_algebra.getInverse(s_algebra.multiply(transpose, matrix)).toArray()); double rSquared = regressionSumOfSquares / totalSumOfSquares; double adjustedRSquared = 1. - (1 - rSquared) * (n - 1) / (n - k); double meanSquareError = errorSumOfSquares / (n - k); TDistribution studentT = new TDistribution(n - k); for (int i = 0; i < k; i++) { standardErrorsOfBeta[i] = Math.sqrt(meanSquareError * covarianceBetas[i][i]); tStats[i] = betas[i] / standardErrorsOfBeta[i]; pValues[i] = 1 - studentT.cumulativeProbability(Math.abs(tStats[i])); } return new WeightedLeastSquaresRegressionResult(betas, residuals, meanSquareError, standardErrorsOfBeta, rSquared, adjustedRSquared, tStats, pValues, useIntercept); }
From source file:com.mapr.synth.samplers.RandomWalkSamplerTest.java
@Test public void testBasics() throws IOException { // this sampler has four variables // g1 is gamma distributed with alpha = 0.2, beta = 0.2 // v1 is unit normal // v2 is normal with mean = 0, sd = 2 // v3 is gamma-normal with dof=2, mean = 0. SchemaSampler s = new SchemaSampler( Resources.asCharSource(Resources.getResource("schema015.json"), Charsets.UTF_8).read()); TDigest tdG1 = new AVLTreeDigest(500); TDigest tdG2 = new AVLTreeDigest(500); TDigest td1 = new AVLTreeDigest(500); TDigest td2 = new AVLTreeDigest(500); TDigest td3 = new AVLTreeDigest(500); double x1 = 0; double x2 = 0; double x3 = 0; for (int i = 0; i < 1000000; i++) { JsonNode r = s.sample();/*from w w w . jav a 2 s .c o m*/ tdG1.add(r.get("g1").asDouble()); tdG2.add(r.get("g2").asDouble()); double step1 = r.get("v1").get("step").asDouble(); td1.add(step1); x1 += step1; assertEquals(x1, r.get("v1").get("value").asDouble(), 0); assertEquals(x1, r.get("v1-bare").asDouble(), 0); double step2 = r.get("v2").get("step").asDouble(); td2.add(step2); x2 += step2; assertEquals(x2, r.get("v2").get("value").asDouble(), 0); double step3 = r.get("v3").get("step").asDouble(); td3.add(step3); x3 += step3; assertEquals(x3, r.get("v3").get("value").asDouble(), 0); } // now compare against reference distributions to test accuracy of the observed step distributions NormalDistribution normalDistribution = new NormalDistribution(); GammaDistribution gd1 = new GammaDistribution(0.2, 5); GammaDistribution gd2 = new GammaDistribution(1, 1); TDistribution tDistribution = new TDistribution(2); for (double q : new double[] { 0.001, 0.01, 0.1, 0.2, 0.5, 0.8, 0.9, 0.99, 0.99 }) { double uG1 = gd1.cumulativeProbability(tdG1.quantile(q)); assertEquals(q, uG1, (1 - q) * q * 10e-2); double uG2 = gd2.cumulativeProbability(tdG2.quantile(q)); assertEquals(q, uG2, (1 - q) * q * 10e-2); double u1 = normalDistribution.cumulativeProbability(td1.quantile(q)); assertEquals(q, u1, (1 - q) * q * 10e-2); double u2 = normalDistribution.cumulativeProbability(td2.quantile(q) / 2); assertEquals(q, u2, (1 - q) * q * 10e-2); double u3 = tDistribution.cumulativeProbability(td3.quantile(q)); assertEquals(q, u3, (1 - q) * q * 10e-2); } }