root/src/java/com/omniti/reconnoiter/esper/ExactBaseStatisticsBean.java

Revision 5640498f39ed92d4917c04691ad51ab3d17d0674, 9.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 8 years ago)

Regressions that use arbitrary precision math libraries.... step 1, refs #210

  • Property mode set to 100644
Line 
1 /**************************************************************************************
2  * Copyright (C) 2008 EsperTech, Inc. All rights reserved.                            *
3  * Copyright (C) 2008 OmniTI, Inc. All rights reserved.                               *
4  * http://esper.codehaus.org                                                          *
5  * http://www.espertech.com                                                           *
6  * ---------------------------------------------------------------------------------- *
7  * The software in this package is published under the terms of the GPL license       *
8  * a copy of which has been included with this distribution in the license.txt file.  *
9  **************************************************************************************/
10 package com.omniti.reconnoiter.esper;
11
12 import com.espertech.esper.view.ViewProcessingException;
13 import com.espertech.esper.client.EPException;
14 import com.espertech.esper.util.MetaDefItem;
15
16 import java.io.Serializable;
17 import java.math.BigDecimal;
18 import java.math.MathContext;
19
20 /**
21  * Bean for performing statistical calculations. The bean keeps sums of X and Y datapoints and sums on squares
22  * that can be reused by subclasses. The bean calculates standard deviation (sample and population), variance,
23  * average and sum.
24   */
25 public class ExactBaseStatisticsBean implements Cloneable, Serializable
26 {
27     private BigDecimal sumX;
28     private BigDecimal sumXSq;
29     private BigDecimal sumY;
30     private BigDecimal sumYSq;
31     private BigDecimal sumXY;
32     private long dataPoints;
33     private static final long serialVersionUID = 7985123761256377184L;
34
35     private void initialize()
36     {
37         sumX =
38         sumXSq =
39         sumY =
40         sumYSq =
41         sumXY = BigDecimal.ZERO;
42         dataPoints = 0;
43     }
44
45     public ExactBaseStatisticsBean()
46     {
47         super();
48         initialize();
49     }
50
51     /**
52      * Add a data point for the X data set only.
53      * @param x is the X data point to add.
54      */
55     public final void addPoint(BigDecimal x)
56     {
57         dataPoints++;
58         sumX = sumX.add(x);
59         sumXSq = sumXSq.add(x.multiply(x));
60     }
61
62     /**
63      * Add a data point.
64      * @param x is the X data point to add.
65      * @param y is the Y data point to add.
66      */
67     public final void addPoint(BigDecimal x, BigDecimal y)
68     {
69         dataPoints++;
70         sumX = sumX.add(x);
71         sumXSq = sumXSq.add(x.multiply(x));
72         sumY = sumY.add(y);
73         sumYSq = sumYSq.add(y.multiply(y));
74         sumXY = sumXY.add(x.multiply(y));
75     }
76
77     /**
78      * Remove a X data point only.
79      * @param x is the X data point to remove.
80      */
81     public final void removePoint(BigDecimal x)
82     {
83         dataPoints--;
84         if (dataPoints <= 0)
85         {
86             initialize();
87         }
88         else
89         {
90             sumX = sumX.subtract(x);
91             sumXSq = sumXSq.subtract(x.multiply(x));
92         }
93     }
94
95     /**
96      * Remove a data point.
97      * @param x is the X data point to remove.
98      * @param y is the Y data point to remove.
99      */
100     public final void removePoint(BigDecimal x, BigDecimal y)
101     {
102         dataPoints--;
103         if (dataPoints <= 0)
104         {
105             initialize();
106         }
107         else
108         {
109             sumX = sumX.subtract(x);
110             sumXSq = sumXSq.subtract(x.multiply(x));
111             sumY = sumY.subtract(y);
112             sumYSq = sumYSq.subtract(y.multiply(y));
113             sumXY = sumXY.subtract(x.multiply(y));
114         }
115     }
116
117     public final BigDecimal getXStandardDeviationPop()
118     {
119         if (dataPoints == 0) return null;
120         BigDecimal temp = sumXSq.subtract(sumX.multiply(sumX).divide(new BigDecimal(dataPoints), MathContext.DECIMAL128)).divide(new BigDecimal(dataPoints), MathContext.DECIMAL128);
121         return new BigDecimal(Math.sqrt(temp.doubleValue()));
122     }
123
124     public final BigDecimal getYStandardDeviationPop()
125     {
126         if (dataPoints == 0) return null;
127         BigDecimal temp = sumYSq.subtract(sumY.multiply(sumY).divide(new BigDecimal(dataPoints), MathContext.DECIMAL128)).divide(new BigDecimal(dataPoints), MathContext.DECIMAL128);
128         return new BigDecimal(Math.sqrt(temp.doubleValue()));
129     }
130
131     public final BigDecimal getXStandardDeviationSample()
132     {
133         if (dataPoints < 2) return null;
134
135         BigDecimal variance = getXVariance();
136         return new BigDecimal(Math.sqrt(variance.doubleValue()));
137     }
138
139     /**
140      * Calculates standard deviation for Y based on the sample data points supplied.
141      * Equivalent to Microsoft Excel formula STDEV.
142      * @return standard deviation assuming sample for Y
143      */
144     public final BigDecimal getYStandardDeviationSample()
145     {
146         if (dataPoints < 2) return BigDecimal.ZERO;
147
148         BigDecimal variance = getYVariance();
149         return new BigDecimal(Math.sqrt(variance.doubleValue()));
150     }
151
152     /**
153      * Calculates standard deviation for X based on the sample data points supplied.
154      * Equivalent to Microsoft Excel formula STDEV.
155      * @return variance as the square of the sample standard deviation for X
156      */
157     public final BigDecimal getXVariance()
158     {
159         if (dataPoints < 2) return null;
160         return sumXSq.subtract(sumX.multiply(sumX).divide(new BigDecimal(dataPoints), MathContext.DECIMAL128)).divide(new BigDecimal(dataPoints - 1), MathContext.DECIMAL128);
161     }
162
163     /**
164      * Calculates standard deviation for Y based on the sample data points supplied.
165      * Equivalent to Microsoft Excel formula STDEV.
166      * @return variance as the square of the sample standard deviation for Y
167      */
168     public final BigDecimal getYVariance()
169     {
170         if (dataPoints < 2) return null;
171         return sumYSq.subtract(sumY.multiply(sumY).divide(new BigDecimal(dataPoints), MathContext.DECIMAL128)).divide(new BigDecimal(dataPoints - 1), MathContext.DECIMAL128);
172     }
173
174     /**
175      * Returns the number of data points.
176      * @return number of data points
177      */
178     public final long getN()
179     {
180         return dataPoints;
181     }
182
183     /**
184      * Returns the sum of all X data points.
185      * @return sum of X data points
186      */
187     public final BigDecimal getXSum()
188     {
189         return sumX;
190     }
191
192     /**
193      * Returns the sum of all Y data points.
194      * @return sum of Y data points
195      */
196     public final BigDecimal getYSum()
197     {
198         return sumY;
199     }
200
201     /**
202      * Returns the average of all X data points.
203      * @return average of X data points
204      */
205     public final BigDecimal getXAverage()
206     {
207         if (dataPoints == 0) return null;
208         return sumX.divide(new BigDecimal(dataPoints), MathContext.DECIMAL128);
209     }
210
211     /**
212      * Returns the average of all Y data points.
213      * @return average of Y data points
214      */
215     public final BigDecimal getYAverage()
216     {
217         if (dataPoints == 0) return null;
218         return sumY.divide(new BigDecimal(dataPoints), MathContext.DECIMAL128);
219     }
220
221     /**
222      * For use by subclasses, returns sum (X * X).
223      * @return sum of X squared
224      */
225     public final BigDecimal getSumXSq()
226     {
227         return sumXSq;
228     }
229
230     /**
231      * For use by subclasses, returns sum (Y * Y).
232      * @return sum of Y squared
233      */
234     public final BigDecimal getSumYSq()
235     {
236         return sumYSq;
237     }
238
239     /**
240      * For use by subclasses, returns sum (X * Y).
241      * @return sum of X times Y
242      */
243     public final BigDecimal getSumXY()
244     {
245         return sumXY;
246     }
247
248     public final Object clone()
249     {
250         try
251         {
252             return super.clone();
253         }
254         catch (CloneNotSupportedException e)
255         {
256             throw new EPException(e);
257         }
258     }
259
260     public final String toString()
261     {
262         return "datapoints=" + this.dataPoints +
263                "  sumX=" + this.sumX +
264                "  sumXSq=" + this.sumXSq +
265                "  sumY=" + this.sumY +
266                "  sumYSq=" + this.sumYSq +
267                "  sumXY=" + this.sumXY;
268     }
269
270     /**
271      * Sets the sum X.
272      * @param sumX to set
273      */
274     public void setSumX(BigDecimal sumX)
275     {
276         this.sumX = sumX;
277     }
278
279     /**
280      * Sets the sum X square.
281      * @param sumXSq to set
282      */
283     public void setSumXSq(BigDecimal sumXSq)
284     {
285         this.sumXSq = sumXSq;
286     }
287
288     /**
289      * Sets the sum Y.
290      * @param sumY to set
291      */
292     public void setSumY(BigDecimal sumY)
293     {
294         this.sumY = sumY;
295     }
296
297     /**
298      * Sets the sum Y square.
299      * @param sumYSq to set
300      */
301     public void setSumYSq(BigDecimal sumYSq)
302     {
303         this.sumYSq = sumYSq;
304     }
305
306     /**
307      * Sets the sum of x times y.
308      * @param sumXY sum of x times y.
309      */
310     public void setSumXY(BigDecimal sumXY)
311     {
312         this.sumXY = sumXY;
313     }
314
315     /**
316      * Sets the number of datapoints
317      * @param dataPoints to set
318      */
319     public void setDataPoints(long dataPoints)
320     {
321         this.dataPoints = dataPoints;
322     }
323
324     /**
325      * Returns sum of x.
326      * @return sum of x
327      */
328     public BigDecimal getSumX()
329     {
330         return sumX;
331     }
332
333     /**
334      * Returns sum of y.
335      * @return sum of y
336      */
337     public BigDecimal getSumY()
338     {
339         return sumY;
340     }
341
342     /**
343      * Returns the number of datapoints.
344      * @return datapoints
345      */
346     public long getDataPoints()
347     {
348         return dataPoints;
349     }
350 }
Note: See TracBrowser for help on using the browser.