1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.stat.descriptive;
23
24 import java.io.Serializable;
25 import java.util.Arrays;
26
27 import org.hipparchus.exception.LocalizedCoreFormats;
28 import org.hipparchus.exception.MathIllegalArgumentException;
29 import org.hipparchus.linear.RealMatrix;
30 import org.hipparchus.stat.descriptive.moment.GeometricMean;
31 import org.hipparchus.stat.descriptive.moment.Mean;
32 import org.hipparchus.stat.descriptive.rank.Max;
33 import org.hipparchus.stat.descriptive.rank.Min;
34 import org.hipparchus.stat.descriptive.summary.Sum;
35 import org.hipparchus.stat.descriptive.summary.SumOfLogs;
36 import org.hipparchus.stat.descriptive.summary.SumOfSquares;
37 import org.hipparchus.stat.descriptive.vector.VectorialCovariance;
38 import org.hipparchus.stat.descriptive.vector.VectorialStorelessStatistic;
39 import org.hipparchus.util.FastMath;
40 import org.hipparchus.util.MathArrays;
41 import org.hipparchus.util.MathUtils;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public class MultivariateSummaryStatistics
62 implements StatisticalMultivariateSummary, Serializable {
63
64
65 private static final long serialVersionUID = 20160424L;
66
67
68 private final int k;
69
70
71 private final StorelessMultivariateStatistic sumImpl;
72
73 private final StorelessMultivariateStatistic sumSqImpl;
74
75 private final StorelessMultivariateStatistic minImpl;
76
77 private final StorelessMultivariateStatistic maxImpl;
78
79 private final StorelessMultivariateStatistic sumLogImpl;
80
81 private final StorelessMultivariateStatistic geoMeanImpl;
82
83 private final StorelessMultivariateStatistic meanImpl;
84
85 private final VectorialCovariance covarianceImpl;
86
87
88 private long n;
89
90
91
92
93
94
95
96
97
98
99 public MultivariateSummaryStatistics(int dimension) {
100 this(dimension, true);
101 }
102
103
104
105
106
107
108
109
110
111
112
113 public MultivariateSummaryStatistics(int dimension, boolean covarianceBiasCorrection) {
114 this.k = dimension;
115
116 sumImpl = new VectorialStorelessStatistic(k, new Sum());
117 sumSqImpl = new VectorialStorelessStatistic(k, new SumOfSquares());
118 minImpl = new VectorialStorelessStatistic(k, new Min());
119 maxImpl = new VectorialStorelessStatistic(k, new Max());
120 sumLogImpl = new VectorialStorelessStatistic(k, new SumOfLogs());
121 geoMeanImpl = new VectorialStorelessStatistic(k, new GeometricMean());
122 meanImpl = new VectorialStorelessStatistic(k, new Mean());
123
124 covarianceImpl = new VectorialCovariance(k, covarianceBiasCorrection);
125 }
126
127
128
129
130
131
132
133
134 public void addValue(double[] value) throws MathIllegalArgumentException {
135 MathUtils.checkNotNull(value, LocalizedCoreFormats.INPUT_ARRAY);
136 MathUtils.checkDimension(value.length, k);
137 sumImpl.increment(value);
138 sumSqImpl.increment(value);
139 minImpl.increment(value);
140 maxImpl.increment(value);
141 sumLogImpl.increment(value);
142 geoMeanImpl.increment(value);
143 meanImpl.increment(value);
144 covarianceImpl.increment(value);
145 n++;
146 }
147
148
149
150
151 public void clear() {
152 this.n = 0;
153 minImpl.clear();
154 maxImpl.clear();
155 sumImpl.clear();
156 sumLogImpl.clear();
157 sumSqImpl.clear();
158 geoMeanImpl.clear();
159 meanImpl.clear();
160 covarianceImpl.clear();
161 }
162
163
164 @Override
165 public int getDimension() {
166 return k;
167 }
168
169
170 @Override
171 public long getN() {
172 return n;
173 }
174
175
176 @Override
177 public double[] getSum() {
178 return sumImpl.getResult();
179 }
180
181
182 @Override
183 public double[] getSumSq() {
184 return sumSqImpl.getResult();
185 }
186
187
188 @Override
189 public double[] getSumLog() {
190 return sumLogImpl.getResult();
191 }
192
193
194 @Override
195 public double[] getMean() {
196 return meanImpl.getResult();
197 }
198
199
200 @Override
201 public RealMatrix getCovariance() {
202 return covarianceImpl.getResult();
203 }
204
205
206 @Override
207 public double[] getMax() {
208 return maxImpl.getResult();
209 }
210
211
212 @Override
213 public double[] getMin() {
214 return minImpl.getResult();
215 }
216
217
218 @Override
219 public double[] getGeometricMean() {
220 return geoMeanImpl.getResult();
221 }
222
223
224
225
226
227
228
229
230 @Override
231 public double[] getStandardDeviation() {
232 double[] stdDev = new double[k];
233 if (getN() < 1) {
234 Arrays.fill(stdDev, Double.NaN);
235 } else if (getN() < 2) {
236 Arrays.fill(stdDev, 0.0);
237 } else {
238 RealMatrix matrix = getCovariance();
239 for (int i = 0; i < k; ++i) {
240 stdDev[i] = FastMath.sqrt(matrix.getEntry(i, i));
241 }
242 }
243 return stdDev;
244 }
245
246
247
248
249
250
251
252 @Override
253 public String toString() {
254 final String separator = ", ";
255 final String suffix = System.getProperty("line.separator");
256 StringBuilder outBuffer = new StringBuilder(200);
257 outBuffer.append("MultivariateSummaryStatistics:").append(suffix).
258 append("n: ").append(getN()).append(suffix);
259 append(outBuffer, getMin(), "min: ", separator, suffix);
260 append(outBuffer, getMax(), "max: ", separator, suffix);
261 append(outBuffer, getMean(), "mean: ", separator, suffix);
262 append(outBuffer, getGeometricMean(), "geometric mean: ", separator, suffix);
263 append(outBuffer, getSumSq(), "sum of squares: ", separator, suffix);
264 append(outBuffer, getSumLog(), "sum of logarithms: ", separator, suffix);
265 append(outBuffer, getStandardDeviation(), "standard deviation: ", separator, suffix);
266 outBuffer.append("covariance: ").append(getCovariance().toString()).append(suffix);
267 return outBuffer.toString();
268 }
269
270
271
272
273
274
275
276
277
278 private void append(StringBuilder buffer, double[] data,
279 String prefix, String separator, String suffix) {
280 buffer.append(prefix);
281 for (int i = 0; i < data.length; ++i) {
282 if (i > 0) {
283 buffer.append(separator);
284 }
285 buffer.append(data[i]);
286 }
287 buffer.append(suffix);
288 }
289
290
291
292
293
294
295
296 @Override
297 public boolean equals(Object object) {
298 if (object == this) {
299 return true;
300 }
301 if (!(object instanceof MultivariateSummaryStatistics)) {
302 return false;
303 }
304 MultivariateSummaryStatistics other = (MultivariateSummaryStatistics) object;
305 return other.getN() == getN() &&
306 MathArrays.equalsIncludingNaN(other.getGeometricMean(), getGeometricMean()) &&
307 MathArrays.equalsIncludingNaN(other.getMax(), getMax()) &&
308 MathArrays.equalsIncludingNaN(other.getMean(), getMean()) &&
309 MathArrays.equalsIncludingNaN(other.getMin(), getMin()) &&
310 MathArrays.equalsIncludingNaN(other.getSum(), getSum()) &&
311 MathArrays.equalsIncludingNaN(other.getSumSq(), getSumSq()) &&
312 MathArrays.equalsIncludingNaN(other.getSumLog(), getSumLog()) &&
313 other.getCovariance().equals(getCovariance());
314 }
315
316
317
318
319
320
321 @Override
322 public int hashCode() {
323 int result = 31 + MathUtils.hash(getN());
324 result = result * 31 + MathUtils.hash(getGeometricMean());
325 result = result * 31 + MathUtils.hash(getMax());
326 result = result * 31 + MathUtils.hash(getMean());
327 result = result * 31 + MathUtils.hash(getMin());
328 result = result * 31 + MathUtils.hash(getSum());
329 result = result * 31 + MathUtils.hash(getSumSq());
330 result = result * 31 + MathUtils.hash(getSumLog());
331 result = result * 31 + getCovariance().hashCode();
332 return result;
333 }
334
335 }