2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.math.stat.descriptive.moment;
19
import java.io.Serializable;
20
import java.util.Arrays;
22
import org.apache.commons.math.DimensionMismatchException;
23
import org.apache.commons.math.linear.RealMatrix;
24
import org.apache.commons.math.linear.RealMatrixImpl;
27
* Returns the covariance matrix of the available vectors.
29
* @version $Revision: 619928 $ $Date: 2008-02-08 09:19:17 -0700 (Fri, 08 Feb 2008) $
31
public class VectorialCovariance implements Serializable {
33
/** Serializable version identifier */
34
private static final long serialVersionUID = 4118372414238930270L;
36
/** Sums for each component. */
37
private double[] sums;
39
/** Sums of products for each component. */
40
private double[] productsSums;
42
/** Indicator for bias correction. */
43
private boolean isBiasCorrected;
45
/** Number of vectors in the sample. */
48
/** Constructs a VectorialMean.
49
* @param dimension vectors dimension
50
* @param isBiasCorrected if true, computed the unbiased sample covariance,
51
* otherwise computes the biased population covariance
53
public VectorialCovariance(int dimension, boolean isBiasCorrected) {
54
sums = new double[dimension];
55
productsSums = new double[dimension * (dimension + 1) / 2];
57
this.isBiasCorrected = isBiasCorrected;
61
* Add a new vector to the sample.
62
* @param v vector to add
63
* @exception DimensionMismatchException if the vector does not have the right dimension
65
public void increment(double[] v) throws DimensionMismatchException {
66
if (v.length != sums.length) {
67
throw new DimensionMismatchException(v.length, sums.length);
70
for (int i = 0; i < v.length; ++i) {
72
for (int j = 0; j <= i; ++j) {
73
productsSums[k++] += v[i] * v[j];
80
* Get the covariance matrix.
81
* @return covariance matrix
83
public RealMatrix getResult() {
85
int dimension = sums.length;
86
RealMatrixImpl result = new RealMatrixImpl(dimension, dimension);
89
double[][] resultData = result.getDataRef();
90
double c = 1.0 / (n * (isBiasCorrected ? (n - 1) : n));
92
for (int i = 0; i < dimension; ++i) {
93
for (int j = 0; j <= i; ++j) {
94
double e = c * (n * productsSums[k++] - sums[i] * sums[j]);
106
* Get the number of vectors in the sample.
107
* @return number of vectors in the sample
114
* Clears the internal state of the Statistic
116
public void clear() {
118
Arrays.fill(sums, 0.0);
119
Arrays.fill(productsSums, 0.0);
b'\\ No newline at end of file'