/** * Default constructor. */ public TTestImpl() { this(new TDistributionImpl(1.0)); }
/** * Returns the mean. * * For degrees of freedom parameter df, the mean is * <ul> * <li>if <code>df > 1</code> then <code>0</code></li> * <li>else <code>undefined</code></li> * </ul> * * @return the mean * @since 2.2 */ public double getNumericalMean() { final double df = getDegreesOfFreedom(); if (df > 1) { return 0; } return Double.NaN; }
/** * Create a t distribution using the given degrees of freedom and the * specified inverse cumulative probability absolute accuracy. * * @param degreesOfFreedom the degrees of freedom. * @param inverseCumAccuracy the maximum absolute error in inverse cumulative probability estimates * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}) * @since 2.1 */ public TDistributionImpl(double degreesOfFreedom, double inverseCumAccuracy) { super(); setDegreesOfFreedomInternal(degreesOfFreedom); solverAbsoluteAccuracy = inverseCumAccuracy; }
/** * @return result of T-test between baseline and treatment */ public double pairedTTest() { double sampleSum = 0; double sampleSumSquares = 0; int n = baseline.length; for( int i=0; i<baseline.length; i++ ) { double delta = treatment[i] - baseline[i]; sampleSum += delta; sampleSumSquares += delta*delta; } double sampleVariance = sampleSumSquares / (n - 1); double sampleMean = sampleSum / baseline.length; double sampleDeviation = Math.sqrt(sampleVariance); double meanDeviation = sampleDeviation / Math.sqrt(n); double t = sampleMean / meanDeviation; try { return 1.0 - new TDistributionImpl(n).cumulativeProbability(t); } catch (MathException e) { throw new RuntimeException(e); } }
/** * Create a t distribution using the given degrees of freedom. * @param degreesOfFreedom the degrees of freedom. */ public TDistributionImpl(double degreesOfFreedom) { super(); setDegreesOfFreedom(degreesOfFreedom); }
/** * @return result of T-test between baseline and treatment */ public double pairedTTest() { double sampleSum = 0; double sampleSumSquares = 0; int n = baseline.length; for( int i=0; i<baseline.length; i++ ) { double delta = treatment[i] - baseline[i]; sampleSum += delta; sampleSumSquares += delta*delta; } double sampleVariance = sampleSumSquares / (n - 1); double sampleMean = sampleSum / baseline.length; double sampleDeviation = Math.sqrt(sampleVariance); double meanDeviation = sampleDeviation / Math.sqrt(n); double t = sampleMean / meanDeviation; try { return 1.0 - new TDistributionImpl(n).cumulativeProbability(t); } catch (MathException e) { throw new RuntimeException(e); } }
/** * Create an empty SimpleRegression instance */ public SimpleRegression() { this(new TDistributionImpl(1.0)); }
/** * Create a t distribution using the given degrees of freedom and the * specified inverse cumulative probability absolute accuracy. * * @param degreesOfFreedom the degrees of freedom. * @param inverseCumAccuracy the maximum absolute error in inverse cumulative probability estimates * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}) * @since 2.1 */ public TDistributionImpl(double degreesOfFreedom, double inverseCumAccuracy) { super(); setDegreesOfFreedomInternal(degreesOfFreedom); solverAbsoluteAccuracy = inverseCumAccuracy; }
/** * Returns the variance. * * For degrees of freedom parameter df, the variance is * <ul> * <li>if <code>df > 2</code> then <code>df / (df - 2)</code> </li> * <li>if <code>1 < df <= 2</code> then <code>positive infinity</code></li> * <li>else <code>undefined</code></li> * </ul> * * @return the variance * @since 2.2 */ public double getNumericalVariance() { final double df = getDegreesOfFreedom(); if (df > 2) { return df / (df - 2); } if (df > 1 && df <= 2) { return Double.POSITIVE_INFINITY; } return Double.NaN; }
/** * Default constructor. */ public TTestImpl() { this(new TDistributionImpl(1.0)); }
/** * Modify the degrees of freedom. * @param degreesOfFreedom the new degrees of freedom. * @deprecated as of 2.1 (class will become immutable in 3.0) */ @Deprecated public void setDegreesOfFreedom(double degreesOfFreedom) { setDegreesOfFreedomInternal(degreesOfFreedom); } /**
/** * For this disbution, X, this method returns P(X < <code>x</code>). * @param x the value at which the CDF is evaluated. * @return CDF evaluted at <code>x</code>. * @throws MathException if the cumulative probability can not be * computed due to convergence or other numerical errors. */ public double cumulativeProbability(double x) throws MathException{ double ret; if (x == 0.0) { ret = 0.5; } else { double t = Beta.regularizedBeta( getDegreesOfFreedom() / (getDegreesOfFreedom() + (x * x)), 0.5 * getDegreesOfFreedom(), 0.5); if (x < 0.0) { ret = 0.5 * t; } else { ret = 1.0 - 0.5 * t; } } return ret; }
/** * Default constructor. */ public TTestImpl() { this(new TDistributionImpl(1.0)); }
/** * Modify the degrees of freedom. * @param degreesOfFreedom the new degrees of freedom. * @deprecated as of 2.1 (class will become immutable in 3.0) */ @Deprecated public void setDegreesOfFreedom(double degreesOfFreedom) { setDegreesOfFreedomInternal(degreesOfFreedom); }
/** * Create a new t distribution with the given degrees of freedom. * * @param degreesOfFreedom degrees of freedom * @return a new t distribution. */ public TDistribution createTDistribution(double degreesOfFreedom) { return new TDistributionImpl(degreesOfFreedom); }
/** * Create an empty SimpleRegression instance */ public SimpleRegression() { this(new TDistributionImpl(1.0)); }
/** * Create an empty SimpleRegression instance */ public SimpleRegression() { this(new TDistributionImpl(1.0)); }
/** * Create an empty SimpleRegression. * * @param degrees Number of degrees of freedom of the distribution * used to compute inference statistics. * @since 2.2 */ public SimpleRegression(int degrees) { setDistribution(new TDistributionImpl(degrees)); }
/** * Generates a random value from the {@link TDistributionImpl T Distribution}. * This implementation uses {@link #nextInversionDeviate(ContinuousDistribution) inversion} * to generate random values. * * @param df the degrees of freedom of the T distribution * @return random value from the T(df) distribution * @throws MathException if an error occurs generating the random value * @since 2.2 */ public double nextT(double df) throws MathException { return nextInversionDeviate(new TDistributionImpl(df)); }
/** * 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); }