KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Regression.java
24  * ---------------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: Regression.java,v 1.3 2003/08/20 12:16:13 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 30-Sep-2002 : Version 1 (DG);
35  * 18-Aug-2003 : Added 'abstract' (DG);
36  *
37  */

38
39 package org.jfree.data;
40
41 /**
42  * A utility class for fitting regression curves to data.
43  *
44  * @author David Gilbert.
45  */

46 public abstract class Regression {
47
48     /**
49      * Returns the parameters 'a' and 'b' for an equation y = a + bx, fitted to the data using
50      * ordinary least squares regression.
51      * <p>
52      * The result is returned as a double[], where result[0] --> a, and result[1] --> b.
53      *
54      * @param data the data.
55      *
56      * @return the parameters.
57      */

58     public static double[] getOLSRegression(double[][] data) {
59
60         int n = data.length;
61         if (n < 2) {
62             throw new IllegalArgumentException JavaDoc("Not enough data.");
63         }
64
65         double sumX = 0;
66         double sumY = 0;
67         double sumXX = 0;
68         double sumXY = 0;
69         for (int i = 0; i < n; i++) {
70             double x = data[i][0];
71             double y = data[i][1];
72             sumX += x;
73             sumY += y;
74             double xx = x * x;
75             sumXX += xx;
76             double xy = x * y;
77             sumXY += xy;
78         }
79         double sxx = sumXX - (sumX * sumX) / n;
80         double sxy = sumXY - (sumX * sumY) / n;
81         double xbar = sumX / n;
82         double ybar = sumY / n;
83
84         double[] result = new double[2];
85         result[1] = sxy / sxx;
86         result[0] = ybar - result[1] * xbar;
87
88         return result;
89
90     }
91
92     /**
93      * Returns the parameters 'a' and 'b' for an equation y = a + bx, fitted to the data using
94      * ordinary least squares regression.
95      * <p>
96      * The result is returned as a double[], where result[0] --> a, and result[1] --> b.
97      *
98      * @param data the data.
99      * @param series the series (zero-based index).
100      *
101      * @return the parameters.
102      */

103     public static double[] getOLSRegression(XYDataset data, int series) {
104
105         int n = data.getItemCount(series);
106         if (n < 2) {
107             throw new IllegalArgumentException JavaDoc("Not enough data.");
108         }
109
110         double sumX = 0;
111         double sumY = 0;
112         double sumXX = 0;
113         double sumXY = 0;
114         for (int i = 0; i < n; i++) {
115             double x = data.getXValue(series, i).doubleValue();
116             double y = data.getYValue(series, i).doubleValue();
117             sumX += x;
118             sumY += y;
119             double xx = x * x;
120             sumXX += xx;
121             double xy = x * y;
122             sumXY += xy;
123         }
124         double sxx = sumXX - (sumX * sumX) / n;
125         double sxy = sumXY - (sumX * sumY) / n;
126         double xbar = sumX / n;
127         double ybar = sumY / n;
128
129         double[] result = new double[2];
130         result[1] = sxy / sxx;
131         result[0] = ybar - result[1] * xbar;
132
133         return result;
134
135     }
136
137     /**
138      * Returns the parameters 'a' and 'b' for an equation y = ax^b, fitted to the data using
139      * a power regression equation.
140      * <p>
141      * The result is returned as an array, where double[0] --> a, and double[1] --> b.
142      *
143      * @param data the data.
144      *
145      * @return the parameters.
146      */

147     public static double[] getPowerRegression(double[][] data) {
148
149         int n = data.length;
150         if (n < 2) {
151             throw new IllegalArgumentException JavaDoc("Not enough data.");
152         }
153
154         double sumX = 0;
155         double sumY = 0;
156         double sumXX = 0;
157         double sumXY = 0;
158         for (int i = 0; i < n; i++) {
159             double x = Math.log(data[i][0]);
160             double y = Math.log(data[i][1]);
161             sumX += x;
162             sumY += y;
163             double xx = x * x;
164             sumXX += xx;
165             double xy = x * y;
166             sumXY += xy;
167         }
168         double sxx = sumXX - (sumX * sumX) / n;
169         double sxy = sumXY - (sumX * sumY) / n;
170         double xbar = sumX / n;
171         double ybar = sumY / n;
172
173         double[] result = new double[2];
174         result[1] = sxy / sxx;
175         result[0] = Math.pow(Math.exp(1.0), ybar - result[1] * xbar);
176
177         return result;
178
179     }
180
181     /**
182      * Returns the parameters 'a' and 'b' for an equation y = ax^b, fitted to the data using
183      * a power regression equation.
184      * <p>
185      * The result is returned as an array, where double[0] --> a, and double[1] --> b.
186      *
187      * @param data the data.
188      * @param series the series to fit the regression line against.
189      *
190      * @return the parameters.
191      */

192     public static double[] getPowerRegression(XYDataset data, int series) {
193
194         int n = data.getItemCount(series);
195         if (n < 2) {
196             throw new IllegalArgumentException JavaDoc("Not enough data.");
197         }
198
199         double sumX = 0;
200         double sumY = 0;
201         double sumXX = 0;
202         double sumXY = 0;
203         for (int i = 0; i < n; i++) {
204             double x = Math.log(data.getXValue(series, i).doubleValue());
205             double y = Math.log(data.getYValue(series, i).doubleValue());
206             sumX += x;
207             sumY += y;
208             double xx = x * x;
209             sumXX += xx;
210             double xy = x * y;
211             sumXY += xy;
212         }
213         double sxx = sumXX - (sumX * sumX) / n;
214         double sxy = sumXY - (sumX * sumY) / n;
215         double xbar = sumX / n;
216         double ybar = sumY / n;
217
218         double[] result = new double[2];
219         result[1] = sxy / sxx;
220         result[0] = Math.pow(Math.exp(1.0), ybar - result[1] * xbar);
221
222         return result;
223
224     }
225
226 }
227
Popular Tags