1 38 39 package org.jfree.data; 40 41 46 public abstract class Regression { 47 48 58 public static double[] getOLSRegression(double[][] data) { 59 60 int n = data.length; 61 if (n < 2) { 62 throw new IllegalArgumentException ("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 103 public static double[] getOLSRegression(XYDataset data, int series) { 104 105 int n = data.getItemCount(series); 106 if (n < 2) { 107 throw new IllegalArgumentException ("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 147 public static double[] getPowerRegression(double[][] data) { 148 149 int n = data.length; 150 if (n < 2) { 151 throw new IllegalArgumentException ("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 192 public static double[] getPowerRegression(XYDataset data, int series) { 193 194 int n = data.getItemCount(series); 195 if (n < 2) { 196 throw new IllegalArgumentException ("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 |