Removed dependency on DistributionFactory. Added settable t distribution field.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@545161 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brent Worden 2007-06-07 13:08:57 +00:00
parent ad8f706395
commit 8d14277eb6
1 changed files with 32 additions and 10 deletions

View File

@ -19,8 +19,8 @@ package org.apache.commons.math.stat.regression;
import java.io.Serializable; import java.io.Serializable;
import org.apache.commons.math.MathException; import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.DistributionFactory;
import org.apache.commons.math.distribution.TDistribution; import org.apache.commons.math.distribution.TDistribution;
import org.apache.commons.math.distribution.TDistributionImpl;
/** /**
* Estimates an ordinary least squares regression model * Estimates an ordinary least squares regression model
@ -57,6 +57,9 @@ public class SimpleRegression implements Serializable {
/** Serializable version identifier */ /** Serializable version identifier */
private static final long serialVersionUID = -3004689053607543335L; private static final long serialVersionUID = -3004689053607543335L;
/** the distribution used to compute inference statistics. */
private TDistribution distribution;
/** sum of x values */ /** sum of x values */
private double sumX = 0d; private double sumX = 0d;
@ -87,7 +90,18 @@ public class SimpleRegression implements Serializable {
* Create an empty SimpleRegression instance * Create an empty SimpleRegression instance
*/ */
public SimpleRegression() { public SimpleRegression() {
this(new TDistributionImpl(1.0));
}
/**
* Create an empty SimpleRegression using the given distribution object to
* compute inference statistics.
* @param t the distribution used to compute inference statistics.
* @since 1.2
*/
public SimpleRegression(TDistribution t) {
super(); super();
setDistribution(t);
} }
/** /**
@ -119,6 +133,10 @@ public class SimpleRegression implements Serializable {
sumX += x; sumX += x;
sumY += y; sumY += y;
n++; n++;
if (n > 2) {
distribution.setDegreesOfFreedom(n - 2);
}
} }
/** /**
@ -455,7 +473,7 @@ public class SimpleRegression implements Serializable {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
return getSlopeStdErr() * return getSlopeStdErr() *
getTDistribution().inverseCumulativeProbability(1d - alpha / 2d); distribution.inverseCumulativeProbability(1d - alpha / 2d);
} }
/** /**
@ -480,7 +498,7 @@ public class SimpleRegression implements Serializable {
* @throws MathException if the significance level can not be computed. * @throws MathException if the significance level can not be computed.
*/ */
public double getSignificance() throws MathException { public double getSignificance() throws MathException {
return 2d* (1.0 - getTDistribution().cumulativeProbability( return 2d * (1.0 - distribution.cumulativeProbability(
Math.abs(getSlope()) / getSlopeStdErr())); Math.abs(getSlope()) / getSlopeStdErr()));
} }
@ -507,14 +525,18 @@ public class SimpleRegression implements Serializable {
private double getRegressionSumSquares(double slope) { private double getRegressionSumSquares(double slope) {
return slope * slope * sumXX; return slope * slope * sumXX;
} }
/** /**
* Uses distribution framework to get a t distribution instance * Modify the distribution used to compute inference statistics.
* with df = n - 2 * @param value the new distribution
* * @since 1.2
* @return t distribution with df = n - 2
*/ */
private TDistribution getTDistribution() { public void setDistribution(TDistribution value) {
return DistributionFactory.newInstance().createTDistribution(n - 2); distribution = value;
// modify degrees of freedom
if (n > 2) {
distribution.setDegreesOfFreedom(n - 2);
}
} }
} }