~ubuntu-branches/ubuntu/quantal/commons-math/quantal

« back to all changes in this revision

Viewing changes to src/main/java/org/apache/commons/math/stat/correlation/PearsonsCorrelation.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan, Torsten Werner, Damien Raude-Morvan
  • Date: 2011-03-07 21:14:46 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110307211446-4zea7og4eeyzhpai
Tags: 2.2-1
[ Torsten Werner ]
* Change maintainers into Maintainers.

[ Damien Raude-Morvan ]
* New upstream release (Closes: #617209).
* d/control: Bump Standards-Version to 3.9.1 (no changes needed).
* d/copyright: Refresh years, upgrade to DEP5 r166 and relicence my work
  under Apache-2.0.
* d/ant.properties: Set junit.jar to /usr/share/java/junit4.jar
  to ensure unit tests are launched.
* d/docs: Install upstream RELEASE-NOTES.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import org.apache.commons.math.MathRuntimeException;
21
21
import org.apache.commons.math.distribution.TDistribution;
22
22
import org.apache.commons.math.distribution.TDistributionImpl;
 
23
import org.apache.commons.math.exception.util.LocalizedFormats;
 
24
import org.apache.commons.math.exception.NullArgumentException;
 
25
import org.apache.commons.math.exception.DimensionMismatchException;
23
26
import org.apache.commons.math.linear.RealMatrix;
24
27
import org.apache.commons.math.linear.BlockRealMatrix;
25
28
import org.apache.commons.math.stat.regression.SimpleRegression;
 
29
import org.apache.commons.math.util.FastMath;
26
30
 
27
31
/**
28
32
 * Computes Pearson's product-moment correlation coefficients for pairs of arrays
36
40
 * where <code>E(X)</code> is the mean of <code>X</code>, <code>E(Y)</code>
37
41
 * is the mean of the <code>Y</code> values and s(X), s(Y) are standard deviations.
38
42
 *
39
 
 * @version $Revision: 811685 $ $Date: 2009-09-05 13:36:48 -0400 (Sat, 05 Sep 2009) $
 
43
 * @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 août 2010) $
40
44
 * @since 2.0
41
45
 */
42
46
public class PearsonsCorrelation {
91
95
    public PearsonsCorrelation(Covariance covariance) {
92
96
        RealMatrix covarianceMatrix = covariance.getCovarianceMatrix();
93
97
        if (covarianceMatrix == null) {
94
 
            throw MathRuntimeException.createIllegalArgumentException("covariance matrix is null");
 
98
            throw new NullArgumentException(LocalizedFormats.COVARIANCE_MATRIX);
95
99
        }
96
100
        nObs = covariance.getN();
97
101
        correlationMatrix = covarianceToCorrelation(covarianceMatrix);
138
142
        for (int i = 0; i < nVars; i++) {
139
143
            for (int j = 0; j < nVars; j++) {
140
144
                double r = correlationMatrix.getEntry(i, j);
141
 
                out[i][j] = Math.sqrt((1 - r * r) /(nObs - 2));
 
145
                out[i][j] = FastMath.sqrt((1 - r * r) /(nObs - 2));
142
146
            }
143
147
        }
144
148
        return new BlockRealMatrix(out);
167
171
                    out[i][j] = 0d;
168
172
                } else {
169
173
                    double r = correlationMatrix.getEntry(i, j);
170
 
                    double t = Math.abs(r * Math.sqrt((nObs - 2)/(1 - r * r)));
171
 
                    out[i][j] = 2 * (1 - tDistribution.cumulativeProbability(t));
 
174
                    double t = FastMath.abs(r * FastMath.sqrt((nObs - 2)/(1 - r * r)));
 
175
                    out[i][j] = 2 * tDistribution.cumulativeProbability(-t);
172
176
                }
173
177
            }
174
178
        }
223
227
     */
224
228
    public double correlation(final double[] xArray, final double[] yArray) throws IllegalArgumentException {
225
229
        SimpleRegression regression = new SimpleRegression();
226
 
        if(xArray.length == yArray.length && xArray.length > 1) {
 
230
        if (xArray.length != yArray.length) {
 
231
            throw new DimensionMismatchException(xArray.length, yArray.length);
 
232
        } else if (xArray.length < 2) {
 
233
            throw MathRuntimeException.createIllegalArgumentException(
 
234
                  LocalizedFormats.INSUFFICIENT_DIMENSION, xArray.length, 2);
 
235
        } else {
227
236
            for(int i=0; i<xArray.length; i++) {
228
237
                regression.addData(xArray[i], yArray[i]);
229
238
            }
230
239
            return regression.getR();
231
240
        }
232
 
        else {
233
 
            throw MathRuntimeException.createIllegalArgumentException(
234
 
                    "invalid array dimensions. xArray has size {0}; yArray has {1} elements",
235
 
                    xArray.length, yArray.length);
236
 
        }
237
241
    }
238
242
 
239
243
    /**
251
255
        int nVars = covarianceMatrix.getColumnDimension();
252
256
        RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars);
253
257
        for (int i = 0; i < nVars; i++) {
254
 
            double sigma = Math.sqrt(covarianceMatrix.getEntry(i, i));
 
258
            double sigma = FastMath.sqrt(covarianceMatrix.getEntry(i, i));
255
259
            outMatrix.setEntry(i, i, 1d);
256
260
            for (int j = 0; j < i; j++) {
257
261
                double entry = covarianceMatrix.getEntry(i, j) /
258
 
                       (sigma * Math.sqrt(covarianceMatrix.getEntry(j, j)));
 
262
                       (sigma * FastMath.sqrt(covarianceMatrix.getEntry(j, j)));
259
263
                outMatrix.setEntry(i, j, entry);
260
264
                outMatrix.setEntry(j, i, entry);
261
265
            }
274
278
        int nCols = matrix.getColumnDimension();
275
279
        if (nRows < 2 || nCols < 2) {
276
280
            throw MathRuntimeException.createIllegalArgumentException(
277
 
                    "insufficient data: only {0} rows and {1} columns.",
 
281
                    LocalizedFormats.INSUFFICIENT_ROWS_AND_COLUMNS,
278
282
                    nRows, nCols);
279
283
        }
280
284
    }