KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > Statistics


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ---------------
23  * Statistics.java
24  * ---------------
25  * (C) Copyright 2000-2003, by Matthew Wright and Contributors.
26  *
27  * Original Author: Matthew Wright;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: Statistics.java,v 1.3 2003/11/11 15:13:09 mungady Exp $
31  *
32  * Changes (from 08-Nov-2001)
33  * --------------------------
34  * 08-Nov-2001 : Added standard header and tidied Javadoc comments (DG);
35  * Moved from JFreeChart to package com.jrefinery.data.* in JCommon class
36  * library (DG);
37  * 24-Jun-2002 : Removed unnecessary local variable (DG);
38  * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
39  * 11-Nov-2003 : Marked as deprecated (DG);
40  *
41  */

42
43 package org.jfree.data;
44
45 /**
46  * A utility class that provides some simple statistical functions.
47  *
48  * @author Matthew Wright
49  *
50  * @deprecated Use org.jfree.data.statistics.Statistics instead.
51  */

52 public class Statistics {
53
54     /**
55      * Returns the average of a set of numbers.
56      *
57      * @param data the data.
58      *
59      * @return the average of a set of numbers.
60      */

61     public static double getAverage(Number JavaDoc[] data) {
62         double sum = 0.0;
63         int counter = 0;
64         for (; counter < data.length; counter++) {
65             sum = sum + data[counter].doubleValue();
66         }
67         return (sum / counter);
68     }
69
70     /**
71      * Returns the standard deviation of a set of numbers.
72      *
73      * @param data the data.
74      *
75      * @return the standard deviation of a set of numbers.
76      */

77     public static double getStdDev(Number JavaDoc[] data) {
78         double avg = getAverage(data);
79         double sum = 0.0;
80         int counter = 0;
81         double diff = 0.0;
82
83         for (; counter < data.length; counter++) {
84             diff = data[counter].doubleValue() - avg;
85             sum = sum + diff * diff;
86         }
87         return Math.sqrt(sum / (counter - 1));
88     }
89
90     /**
91      * Fits a straight line to a set of (x, y) data, returning the slope and
92      * intercept.
93      *
94      * @param xData the x-data.
95      * @param yData the y-data.
96      *
97      * @return a double array with the intercept in [0] and the slope in [1].
98      */

99     public static double[] getLinearFit(Number JavaDoc[] xData, Number JavaDoc[] yData) {
100
101         // check arguments...
102
if (xData.length != yData.length) {
103             throw new IllegalArgumentException JavaDoc(
104                 "Statistics.getLinearFit(...): array lengths must be equal.");
105         }
106
107         double[] result = new double[2];
108         // slope
109
result[1] = getSlope(xData, yData);
110         // intercept
111
result[0] = getAverage(yData) - result[1] * getAverage(xData);
112
113         return result;
114
115     }
116
117     /**
118      * Finds the slope of a regression line using least squares.
119      *
120      * @param xData an array of Numbers (the x values).
121      * @param yData an array of Numbers (the y values).
122      *
123      * @return the slope.
124      */

125     public static double getSlope(Number JavaDoc[] xData, Number JavaDoc[] yData) {
126
127         // check arguments...
128
if (xData.length != yData.length) {
129             throw new IllegalArgumentException JavaDoc(
130                 "Statistics.getSlope(...): array lengths must be equal.");
131         }
132
133         // ********* stat function for linear slope ********
134
// y = a + bx
135
// a = ybar - b * xbar
136
// sum(x * y) - (sum (x) * sum(y)) / n
137
// b = ------------------------------------
138
// sum (x^2) - (sum(x)^2 / n
139
// *************************************************
140

141         // sum of x, x^2, x * y, y
142
double sx = 0.0, sxx = 0.0, sxy = 0.0, sy = 0.0;
143         int counter;
144         for (counter = 0; counter < xData.length; counter++) {
145             sx = sx + xData[counter].doubleValue();
146             sxx = sxx + Math.pow(xData[counter].doubleValue(), 2);
147             sxy = sxy + yData[counter].doubleValue() * xData[counter].doubleValue();
148             sy = sy + yData[counter].doubleValue();
149         }
150         return (sxy - (sx * sy) / counter) / (sxx - (sx * sx) / counter);
151
152     }
153
154     /**
155      * Calculates the correlation between two datasets.
156      *
157      * @param data1 the first dataset.
158      * @param data2 the second dataset.
159      *
160      * @return the correlation between two datasets.
161      */

162     public static double getCorrelation(Number JavaDoc[] data1, Number JavaDoc[] data2) {
163
164         // check arguments...
165
if (data1.length != data2.length) {
166             throw new IllegalArgumentException JavaDoc(
167                 "Statistics.getCorrelation(...): array lengths must be equal.");
168         }
169
170         double xavg = 0, yavg = 0;
171         double xstd = 0, ystd = 0;
172         int counter = 0;
173
174         // copy to a local variable
175
Number JavaDoc[] xData = new Double JavaDoc[data1.length];
176         Number JavaDoc[] yData = new Double JavaDoc[data2.length];
177         for (int i = 0; i < data1.length; i++) {
178             xData[i] = new Double JavaDoc(data1[i].doubleValue());
179         }
180         for (int i = 0; i < data2.length; i++) {
181             yData[i] = new Double JavaDoc(data2[i].doubleValue());
182         }
183
184         // get averages and standard deviations for calculations
185
xavg = getAverage(xData);
186         yavg = getAverage(yData);
187         xstd = getStdDev(xData);
188         ystd = getStdDev(yData);
189
190         // convert to standard units
191
for (; counter < xData.length; counter++) {
192             xData[counter] = new Double JavaDoc((xData[counter].doubleValue() - xavg) / xstd);
193             yData[counter] = new Double JavaDoc((yData[counter].doubleValue() - yavg) / ystd);
194         }
195
196         // get the product of the standard units
197
for (counter = 0; counter < xData.length; counter++) {
198             xData[counter] = new Double JavaDoc(xData[counter].doubleValue()
199                                         * yData[counter].doubleValue());
200         }
201
202         return getAverage(xData);
203
204     }
205
206     /**
207      * Returns a data set for a moving average on the data set passed in.
208      *
209      * @param xData an array of the x data.
210      * @param yData an array of the y data.
211      * @param period the number of data points to average
212      *
213      * @return a double[][] the length of the data set in the first dimension,
214      * with two doubles for x and y in the second dimension
215      */

216     public static double[][] getMovingAverage(Number JavaDoc[] xData, Number JavaDoc[] yData, int period) {
217
218         // check arguments...
219
if (xData.length != yData.length) {
220             throw new IllegalArgumentException JavaDoc(
221                 "Statistics.getMovingAverage(...): array lengths must be equal.");
222         }
223
224         if (period > xData.length) {
225             throw new IllegalArgumentException JavaDoc(
226                 "Statistics.getMovingAverage(...): period can't be longer than dataset.");
227         }
228
229         double[][] result = new double[xData.length - period][2];
230         for (int i = 0; i < result.length; i++) {
231             result[i][0] = xData[i + period].doubleValue();
232             // holds the moving average sum
233
double sum = 0.0;
234             for (int j = 0; j < period; j++) {
235                 sum += yData[i + j].doubleValue();
236             }
237             sum = sum / period;
238             result[i][1] = sum;
239         }
240         return result;
241
242     }
243
244 }
245
Popular Tags