1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.stat.descriptive.moment;
18
19 import java.util.Arrays;
20
21 import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
22 import org.apache.commons.math4.legacy.linear.MatrixUtils;
23 import org.apache.commons.math4.legacy.linear.RealMatrix;
24
25
26
27
28
29 public class VectorialCovariance {
30
31 private final double[] sums;
32
33
34 private final double[] productsSums;
35
36
37 private final boolean isBiasCorrected;
38
39
40 private long n;
41
42
43
44
45
46
47 public VectorialCovariance(int dimension, boolean isBiasCorrected) {
48 sums = new double[dimension];
49 productsSums = new double[dimension * (dimension + 1) / 2];
50 n = 0;
51 this.isBiasCorrected = isBiasCorrected;
52 }
53
54
55
56
57
58
59 public void increment(double[] v) throws DimensionMismatchException {
60 if (v.length != sums.length) {
61 throw new DimensionMismatchException(v.length, sums.length);
62 }
63 int k = 0;
64 for (int i = 0; i < v.length; ++i) {
65 sums[i] += v[i];
66 for (int j = 0; j <= i; ++j) {
67 productsSums[k++] += v[i] * v[j];
68 }
69 }
70 n++;
71 }
72
73
74
75
76
77 public RealMatrix getResult() {
78
79 int dimension = sums.length;
80 RealMatrix result = MatrixUtils.createRealMatrix(dimension, dimension);
81
82 if (n > 1) {
83 double c = 1.0 / (n * (isBiasCorrected ? (n - 1) : n));
84 int k = 0;
85 for (int i = 0; i < dimension; ++i) {
86 for (int j = 0; j <= i; ++j) {
87 double e = c * (n * productsSums[k++] - sums[i] * sums[j]);
88 result.setEntry(i, j, e);
89 result.setEntry(j, i, e);
90 }
91 }
92 }
93
94 return result;
95 }
96
97
98
99
100
101 public long getN() {
102 return n;
103 }
104
105
106
107
108 public void clear() {
109 n = 0;
110 Arrays.fill(sums, 0.0);
111 Arrays.fill(productsSums, 0.0);
112 }
113
114
115 @Override
116 public int hashCode() {
117 final int prime = 31;
118 int result = 1;
119 result = prime * result + (isBiasCorrected ? 1231 : 1237);
120 result = prime * result + (int) (n ^ (n >>> 32));
121 result = prime * result + Arrays.hashCode(productsSums);
122 result = prime * result + Arrays.hashCode(sums);
123 return result;
124 }
125
126
127 @Override
128 public boolean equals(Object obj) {
129 if (this == obj) {
130 return true;
131 }
132 if (!(obj instanceof VectorialCovariance)) {
133 return false;
134 }
135 VectorialCovariance other = (VectorialCovariance) obj;
136 if (isBiasCorrected != other.isBiasCorrected) {
137 return false;
138 }
139 if (n != other.n) {
140 return false;
141 }
142 if (!Arrays.equals(productsSums, other.productsSums)) {
143 return false;
144 }
145 return Arrays.equals(sums, other.sums);
146 }
147 }