예제 #1
0
파일: TTestImpl.java 프로젝트: mfwzan/astor
 /**
  * Computes p-value for 2-sided, 2-sample t-test, under the assumption of equal subpopulation
  * variances.
  *
  * <p>The sum of the sample sizes minus 2 is used as degrees of freedom.
  *
  * @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 MathException if an error occurs computing the p-value
  */
 protected double homoscedasticTTest(
     double m1, double m2, double v1, double v2, double n1, double n2) throws MathException {
   double t = Math.abs(homoscedasticT(m1, m2, v1, v2, n1, n2));
   double degreesOfFreedom = n1 + n2 - 2;
   distribution.setDegreesOfFreedom(degreesOfFreedom);
   return 2.0 * distribution.cumulativeProbability(-t);
 }
예제 #2
0
파일: TTestImpl.java 프로젝트: mfwzan/astor
 /**
  * Computes p-value for 2-sided, 2-sample t-test.
  *
  * <p>Does not assume subpopulation variances are equal. Degrees of freedom are estimated from the
  * data.
  *
  * @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 MathException if an error occurs computing the p-value
  */
 protected double tTest(double m1, double m2, double v1, double v2, double n1, double n2)
     throws MathException {
   double t = Math.abs(t(m1, m2, v1, v2, n1, n2));
   double degreesOfFreedom = 0;
   degreesOfFreedom = df(v1, v2, n1, n2);
   distribution.setDegreesOfFreedom(degreesOfFreedom);
   return 2.0 * distribution.cumulativeProbability(-t);
 }
  @Override
  public final void compute() {

    if (input[0].isDefined() && input[1].isDefined()) {
      double param = a.getDouble();
      double val = b.getDouble();
      try {
        TDistribution t = getTDistribution(param);
        num.setValue(t.inverseCumulativeProbability(val));

      } catch (Exception e) {
        num.setUndefined();
      }
    } else num.setUndefined();
  }
  /**
   * Modify the distribution used to compute inference statistics.
   *
   * @param value the new distribution
   * @since 1.2
   */
  public void setDistribution(TDistribution value) {
    distribution = value;

    // modify degrees of freedom
    if (n > 2) {
      distribution.setDegreesOfFreedom(n - 2);
    }
  }
 /**
  * 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>The values in the matrix are sometimes referred to as the <i>significance</i> of the
  * corresponding correlation coefficients.
  *
  * @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);
 }
  /**
   * 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.
   *
   * @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 / (double) (n + 1.0);
      sumYY += dy * dy * (double) n / (double) (n + 1.0);
      sumXY += dx * dy * (double) n / (double) (n + 1.0);
      xbar += dx / (double) (n + 1.0);
      ybar += dy / (double) (n + 1.0);
    }
    sumX += x;
    sumY += y;
    n++;

    if (n > 2) {
      distribution.setDegreesOfFreedom(n - 2);
    }
  }
 /**
  * Returns the significance level of the slope (equiv) correlation.
  *
  * <p>Specifically, the returned value is the smallest <code>alpha</code> such that the slope
  * confidence interval with significance level equal to <code>alpha</code> does not include <code>
  * 0</code>. On regression output, this is often denoted <code>Prob(|t| > 0)</code>
  *
  * <p><strong>Usage Note</strong>:<br>
  * The validity of this statistic depends on the assumption that the observations included in the
  * model are drawn from a <a href="http://mathworld.wolfram.com/BivariateNormalDistribution.html">
  * Bivariate Normal Distribution</a>.
  *
  * <p>If there are fewer that <strong>three</strong> observations in the model, or if there is no
  * variation in x, this returns <code>Double.NaN</code>.
  *
  * @return significance level for slope/correlation
  * @throws MathException if the significance level can not be computed.
  */
 public double getSignificance() throws MathException {
   return 2d * (1.0 - distribution.cumulativeProbability(Math.abs(getSlope()) / getSlopeStdErr()));
 }
 /**
  * Returns the half-width of a (100-100*alpha)% confidence interval for the slope estimate.
  *
  * <p>The (100-100*alpha)% confidence interval is
  *
  * <p><code>(getSlope() - getSlopeConfidenceInterval(),
  * getSlope() + getSlopeConfidenceInterval())</code>
  *
  * <p>To request, for example, a 99% confidence interval, use <code>alpha = .01</code>
  *
  * <p><strong>Usage Note</strong>:<br>
  * The validity of this statistic depends on the assumption that the observations included in the
  * model are drawn from a <a href="http://mathworld.wolfram.com/BivariateNormalDistribution.html">
  * Bivariate Normal Distribution</a>.
  *
  * <p><strong> Preconditions:</strong>
  *
  * <ul>
  *   <li>If there are fewer that <strong>three</strong> observations in the model, or if there is
  *       no variation in x, this returns <code>Double.NaN</code>.
  *   <li><code>(0 < alpha < 1)</code>; otherwise an <code>IllegalArgumentException</code> is
  *       thrown.
  * </ul>
  *
  * @param alpha the desired significance level
  * @return half-width of 95% confidence interval for the slope estimate
  * @throws MathException if the confidence interval can not be computed.
  */
 public double getSlopeConfidenceInterval(double alpha) throws MathException {
   if (alpha >= 1 || alpha <= 0) {
     throw new IllegalArgumentException();
   }
   return getSlopeStdErr() * distribution.inverseCumulativeProbability(1d - alpha / 2d);
 }
예제 #9
0
파일: TTestImpl.java 프로젝트: mfwzan/astor
 /**
  * Computes p-value for 2-sided, 1-sample t-test.
  *
  * @param m sample mean
  * @param mu constant to test against
  * @param v sample variance
  * @param n sample n
  * @return p-value
  * @throws MathException if an error occurs computing the p-value
  */
 protected double tTest(double m, double mu, double v, double n) throws MathException {
   double t = Math.abs(t(m, mu, v, n));
   distribution.setDegreesOfFreedom(n - 1);
   return 2.0 * distribution.cumulativeProbability(-t);
 }
예제 #10
0
 /**
  * @param param degrees of freedom
  * @return T-distribution
  */
 protected TDistribution getTDistribution(double param) {
   if (t == null || t.getDegreesOfFreedom() != param) t = new TDistributionImpl(param);
   return t;
 }