本文整理汇总了Java中org.apache.commons.math.distribution.TDistributionImpl类的典型用法代码示例。如果您正苦于以下问题:Java TDistributionImpl类的具体用法?Java TDistributionImpl怎么用?Java TDistributionImpl使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TDistributionImpl类属于org.apache.commons.math.distribution包,在下文中一共展示了TDistributionImpl类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: calculateLogPosteriorPredictiveCDF
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
public double calculateLogPosteriorPredictiveCDF(double value, boolean upperTail){
double mean = currentParameters[0];
double sd = Math.sqrt(currentParameters[3]*(currentParameters[1]+1)
/(currentParameters[2]*currentParameters[1]));
double scaledValue = (value - mean)/sd;
double out;
if(2*currentParameters[2]<=normalApproximationThreshold) {
TDistributionImpl tDist = new TDistributionImpl(2 * currentParameters[2]);
try {
out = upperTail ? Math.log(tDist.cumulativeProbability(-scaledValue))
: Math.log(tDist.cumulativeProbability(scaledValue));
} catch (MathException e){
throw new RuntimeException(e.toString());
}
} else {
out = upperTail ? NormalDistribution.standardCDF(-scaledValue, true) :
NormalDistribution.standardCDF(scaledValue, true);
}
return out;
}
示例2: pairedTTest
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
/**
* @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);
}
}
示例3: getCorrelationPValues
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
/**
* 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);
}
示例4: testStdErrorConsistency
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
/**
* 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);
}
}
}
示例5: getCorrelationPValues
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
/**
* 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);
}
示例6: addData
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
/**
* 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);
}
}
示例7: removeData
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
/**
* 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);
}
}
}
示例8: testStdErrorConsistency
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
/**
* 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);
}
}
}
示例9: testStdErrorConsistency
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
/**
* 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);
}
}
}
示例10: getCorrelationPValues
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
/**
* 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);
}
示例11: testStdErrorConsistency
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
/**
* 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);
}
}
}
示例12: getResultWithStatistics
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
private LeastSquaresRegressionResult getResultWithStatistics(final double[][] x, final double[][] w, final double[] y, final double[] betas,
final double[] yModel, final DoubleMatrix2D transpose, final DoubleMatrix2D matrix, final boolean useIntercept) {
double yMean = 0.;
for (final double y1 : y) {
yMean += y1;
}
yMean /= y.length;
double totalSumOfSquares = 0.;
double errorSumOfSquares = 0.;
final int n = x.length;
final int k = betas.length;
final double[] residuals = new double[n];
final double[] standardErrorsOfBeta = new double[k];
final double[] tStats = new double[k];
final 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];
}
final double regressionSumOfSquares = totalSumOfSquares - errorSumOfSquares;
final double[][] covarianceBetas = convertArray(_algebra.inverse(_algebra.mult(transpose, matrix)).toArray());
final double rSquared = regressionSumOfSquares / totalSumOfSquares;
final double adjustedRSquared = 1. - (1 - rSquared) * (n - 1) / (n - k);
final double meanSquareError = errorSumOfSquares / (n - k);
final ContinuousDistribution studentT = new TDistributionImpl(n - k);
for (int i = 0; i < k; i++) {
standardErrorsOfBeta[i] = Math.sqrt(meanSquareError * covarianceBetas[i][i]);
tStats[i] = betas[i] / standardErrorsOfBeta[i];
try {
pValues[i] = 1 - studentT.cumulativeProbability(Math.abs(tStats[i]));
} catch (final org.apache.commons.math.MathException e) {
throw new com.opengamma.analytics.math.MathException(e);
}
}
return new WeightedLeastSquaresRegressionResult(betas, residuals, meanSquareError, standardErrorsOfBeta, rSquared, adjustedRSquared, tStats, pValues,
useIntercept);
}
示例13: getResultWithStatistics
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
private LeastSquaresRegressionResult getResultWithStatistics(final double[][] x, final double[] y, final double[] betas, final double[] yModel, final DoubleMatrix2D transpose,
final DoubleMatrix2D matrix, final boolean useIntercept) {
double yMean = 0.;
for (final double y1 : y) {
yMean += y1;
}
yMean /= y.length;
double totalSumOfSquares = 0.;
double errorSumOfSquares = 0.;
final int n = x.length;
final int k = betas.length;
final double[] residuals = new double[n];
final double[] stdErrorBetas = new double[k];
final double[] tStats = new double[k];
final 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];
}
final double regressionSumOfSquares = totalSumOfSquares - errorSumOfSquares;
final double[][] covarianceBetas = convertArray(_algebra.inverse(_algebra.mult(transpose, matrix)).toArray());
final double rSquared = regressionSumOfSquares / totalSumOfSquares;
final double adjustedRSquared = 1. - (1 - rSquared) * (n - 1.) / (n - k);
final double meanSquareError = errorSumOfSquares / (n - k);
final ContinuousDistribution studentT = new TDistributionImpl(n - k);
// final ProbabilityDistribution<Double> studentT = new
// StudentTDistribution(n - k);
for (int i = 0; i < k; i++) {
stdErrorBetas[i] = Math.sqrt(meanSquareError * covarianceBetas[i][i]);
tStats[i] = betas[i] / stdErrorBetas[i];
try {
pValues[i] = 1 - studentT.cumulativeProbability(Math.abs(tStats[i]));
} catch (final org.apache.commons.math.MathException e) {
throw new com.opengamma.analytics.math.MathException(e);
}
}
return new LeastSquaresRegressionResult(betas, residuals, meanSquareError, stdErrorBetas, rSquared, adjustedRSquared, tStats, pValues, useIntercept);
}
示例14: testNextT
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
public void testNextT() throws Exception {
double[] quartiles = TestUtils.getDistributionQuartiles(new TDistributionImpl(10));
long[] counts = new long[4];
randomData.reSeed(1000);
for (int i = 0; i < 1000; i++) {
double value = randomData.nextT(10);
TestUtils.updateCounts(value, counts, quartiles);
}
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
}
示例15: testNextT
import org.apache.commons.math.distribution.TDistributionImpl; //导入依赖的package包/类
@Test
public void testNextT() throws Exception {
double[] quartiles = TestUtils.getDistributionQuartiles(new TDistributionImpl(10));
long[] counts = new long[4];
randomData.reSeed(1000);
for (int i = 0; i < 1000; i++) {
double value = randomData.nextT(10);
TestUtils.updateCounts(value, counts, quartiles);
}
TestUtils.assertChiSquareAccept(expected, counts, 0.001);
}