From 22e68e3efe72371fc8d47797715d78c750c80964 Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Sun, 21 Aug 2011 18:14:19 +0000 Subject: [PATCH] Added storeless covariance implementation contributed by Patrick Meyer. JIRA: MATH-449. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1160026 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 3 + .../StorelessBivariateCovariance.java | 76 +++++ .../stat/correlation/StorelessCovariance.java | 118 +++++++ .../correlation/StorelessCovarianceTest.java | 305 ++++++++++++++++++ 4 files changed, 502 insertions(+) create mode 100644 src/main/java/org/apache/commons/math/stat/correlation/StorelessBivariateCovariance.java create mode 100644 src/main/java/org/apache/commons/math/stat/correlation/StorelessCovariance.java create mode 100644 src/test/java/org/apache/commons/math/stat/correlation/StorelessCovarianceTest.java diff --git a/pom.xml b/pom.xml index 2ed942651..c641daa9f 100644 --- a/pom.xml +++ b/pom.xml @@ -189,6 +189,9 @@ Benjamin McCann + + Patrick Meyer + J. Lewis Muir diff --git a/src/main/java/org/apache/commons/math/stat/correlation/StorelessBivariateCovariance.java b/src/main/java/org/apache/commons/math/stat/correlation/StorelessBivariateCovariance.java new file mode 100644 index 000000000..6fbc5d717 --- /dev/null +++ b/src/main/java/org/apache/commons/math/stat/correlation/StorelessBivariateCovariance.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.stat.correlation; + +import org.apache.commons.math.exception.MathIllegalArgumentException; +import org.apache.commons.math.exception.util.LocalizedFormats; + +/** + * Bivariate Covariance implementation that does not require input data to be + * stored in memory. + * + * @version $Id$ + * @since 3.0 + */ +public class StorelessBivariateCovariance { + + private double deltaX = 0.0; + + private double deltaY = 0.0; + + private double meanX = 0.0; + + private double meanY = 0.0; + + private double n = 0; + + private double covarianceNumerator = 0.0; + + private boolean biasCorrected = true; + + public StorelessBivariateCovariance(){ + } + + public StorelessBivariateCovariance(boolean biasCorrected){ + this.biasCorrected = biasCorrected; + } + + public void increment(double x, double y){ + n++; + deltaX = x - meanX; + deltaY = y - meanY; + meanX += deltaX / n; + meanY += deltaY / n; + covarianceNumerator += ((n-1.0) / n) * deltaX * deltaY; + } + + public double getN(){ + return n; + } + + public double getResult()throws IllegalArgumentException{ + if (n < 2) throw new MathIllegalArgumentException( + LocalizedFormats.INSUFFICIENT_DIMENSION, n, 2); + if(biasCorrected){ + return covarianceNumerator / (n - 1d); + }else{ + return covarianceNumerator / n; + } + } + +} + diff --git a/src/main/java/org/apache/commons/math/stat/correlation/StorelessCovariance.java b/src/main/java/org/apache/commons/math/stat/correlation/StorelessCovariance.java new file mode 100644 index 000000000..46ee368ee --- /dev/null +++ b/src/main/java/org/apache/commons/math/stat/correlation/StorelessCovariance.java @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.stat.correlation; + +import org.apache.commons.math.exception.MathIllegalArgumentException; +import org.apache.commons.math.exception.MathUnsupportedOperationException; +import org.apache.commons.math.exception.util.LocalizedFormats; +import org.apache.commons.math.linear.Array2DRowRealMatrix; +import org.apache.commons.math.linear.RealMatrix; + +/** + * Covariance implementation that does not require input data to be + * stored in memory. + * + * @version $Id$ + * @since 3.0 + */ +public class StorelessCovariance extends Covariance { + + private StorelessBivariateCovariance[][] covMatrix = null; + + private int rowDimension = 1; + + private int colDimension = 1; + + private boolean biasCorrected = true; + + public StorelessCovariance(int rowDimension, int colDimension){ + this(rowDimension, colDimension, true); + } + + public StorelessCovariance(int rowDimension, int colDimension, boolean biasCorrected){ + this.rowDimension = rowDimension; + this.colDimension = colDimension; + this.biasCorrected = biasCorrected; + covMatrix = new StorelessBivariateCovariance[rowDimension][colDimension]; + initializeMatrix(); + } + + private void initializeMatrix(){ + for(int i=0;i