java拟合_Java拟合算法

java拟合_Java拟合算法
1 s.math3.fitting.PolynomialCurveFitter;2
s.math3.fitting.WeightedObservedPoints;3
s.math3.linear.ArrayRealVector;4
s.math3.linear.DecompositionSolver;5
s.math3.linear.LUDecomposition;6 s.math3.linear.MatrixUtils;7 s.math3.linear.RealMatrix;8 s.math3.linear.RealVector;9
10 public classMathUtil {11 /**
12 * ⼀元线性拟合 y = a + b*x13 *14 *@paramx15 *@paramy16 * reuslt[0] = a result[1] = b result[2] 相关系数 result[3] 决定系数17 * result[4] 点数量(长度) result[5] ⾃由度18 */
19 public static double[] lineFitting(double x[], doubley[]) {20 int size =x.length;21 double xmean = 0.0;22 double ymean = 0.0;23 double xNum = 0.0;24 double yNum = 0.0;25 double xyNum = 0;26 double xNum2 = 0;27 double yNum2 = 0;28 double rss = 0;29 double tss = 0;30 double result[] = new double[6];31
32 for (int i = 0; i < size; i++) {33 xmean +=x[i];34 ymean +=y[i];35 xNum2 += x[i] *x[i];36 yNum2 += y[i] *y[i];37 xyNum += x[i] *y[i];38 }39 xNum =xmean;40 yNum =ymean;41 xmean /=size;42 ymean /=size;43
44 double sumx2 = 0.0f;45 double sumxy = 0.0f;46 for (int i = 0; i < size; i++) {47 sumx2 += (x[i] - xmean) * (x[i] -xmean);48 sumxy += (y[i] - ymean) * (x[i] -xmean);49 }50
公共财物
51 double b = sumxy /sumx2;52 double a = ymean - b *xmean;53
54 result[0] =a;55 result[1] =b;56 System.out.println("a = " + a + ", b=" +b);57
58 double correlation = (xyNum - xNum * yNum /size)59 / Math.sqrt((xNum2 - xNum * xNum / size) * (yNum2 - yNum * yNum /size));60
61 System.out.println("相关系数:" +correlation);62 result[2] =correlation;63
64 for (int i = 0; i < size; i++) {65 rss += (y[i] - (a + b * x[i])) * (y[i] - (a + b *x[i]));66 tss += (y[i] - ymean) * (y[i] -ymean);67
}68
69 double r2 = 1 - (rss / (size - 1 - 1)) / (tss / (size - 1));70
71 result[3] =r2;72 System.out.println("决定系数" +r2);73
74 result[4] =x.length;75 result[5] = x.length - 1 - 1;76
77 returnresult;78 }79
80 /**
81 * 多元线性拟合 y = a + b*x1 + c*x282 *83 *@paramx84 *@paramy85 * result[0] = a result[b] = b . . . result[len - 4] 点数result[len -86 * 3] ⾃由度 result[len - 2] 残差平⽅和 result[len - 1] 确定系数87 */
88 public static double[] lineFitting2(double x[][], doubley[]) {89 double[] a = new double[x.length + 1];90 double[] v = new double[2]; //这⾥的2为m
91 double[] dt = new double[4];92
93 line2sqt(x, y, 2, 11, a, dt, v);94
95 inti;96
97 System.out.println("残差平⽅和:" + dt[0]);98
99 double temp = a[a.length - 1];100 //更换输出位置,把常数放到第⼀位
101 for (i = a.length - 1; i > 0; i--) {102 a[i] = a[i - 1];103 }104
105 a[0] =temp;106
107 double[] result = new double[x.length + 5];108
109 for (i = 0; i <= x.length; i++) {110 result[i] =a[i];111 }112
113 result[x.length + 1] =y.length;114 result[x.length + 2] = y.length -x.length;115 result[x.length + 3] = dt[0];116
result[x.length + 4] =getLine2R(x, y, a, x.length);117
118 System.out.println("校正确定系数:" + result[x.length + 4]);119
120 returnresult;121 }122
123 /**
124 * 多项式拟合 y = a + b*x1 + c*x1^2…… result[0] = a result[1] = b . . . result[n + 1]125 * 点数 result[n + 2] ⾃由度 result[n + 3] 确定系数126 *127 *@paramn128 * ⼏级129 *@return
130 */
131 public static double[] dxsFitting(double x[], double y[], intn) {132 double result[] = new double[n + 4];133
134 WeightedObservedPoints obs = newWeightedObservedPoints();135
136 for (int i = 0; i < x.length; i++) {137 obs.add(x[i], y[i]);138 }139
140 //Instantiate a third-degree polynomial fitterm.
141 final PolynomialCurveFitter fitter =ate(n);142
143 //Retrieve fitted parameters (coefficients of the polynomial function).
144 final double[] coeff =fitter.List());145
146 for (int i = 0; i < coeff.length; i++) {147 result[i] =coeff[i];148 }149
150 double s = getDxsR(x, y, coeff, 5);151
152 System.out.println("确定系数:" +s);153
154 result[n + 1] =x.length;155 result[n + 2] = x.length - n - 1;156 result[n + 3] =s;157
158 returnresult;159 }160
161 /**
162 * 指数拟合 y = b*exp(ax) result[0] = a result[1] = b result[2] 数据点数 result[3] ⾃由度163 * result[4] 确定系数164 *165
*@paramx166 *@paramy167 *@return
168 */
169 public static double[] expFitting(double x[], doubley[]) {170 int size =x.length;171 double xmean = 0.0;172 double ymean = 0.0;173 double rss = 0;174 double tss = 0;175 double result[] = new double[5];176
177 for (int i = 0; i < size; i++) {178 xmean +=x[i];179 y[i] =Math.log(y[i]);180 ymean +=y[i];181 }182 xmean /=size;183 ymean /=size;184
185 double sumx2 = 0.0f;186 double sumxy = 0.0f;187 for (int i = 0; i < size; i++) {188 sumx2 += (x[i] - xmean) * (x[i] -xmean);189 sumxy += (y[i] - ymean) * (x[i] -xmean);190 }191
192 double b = sumxy /sumx2;193 double a = ymean - b *xmean;194
195 for (int i = 0; i < size; i++) {196 rss += (y[i] - (a + b * x[i])) * (y[i] - (a + b *x[i]));197 tss += (y[i] - ymean) * (y[i] -ymean);198 }199
200 double r2 = 1 - (rss / (size - 1 - 1)) / (tss / (size - 1));201
202 System.out.println("决定系数" +r2);203
204 a =p(a);205
206 System.out.println("a = " + a + ";b= " +b);207
208 result[0] =a;209 result[1] =b;210 result[2] =x.length;211 result[3] = x.length - 2;212 result[4] =r2;213
214 returnresult;215 }216
217 /**
218 * 对数拟合 y = ln(a * x + b) result[0] = a result[1] = b result[2] 数据点数 result[3]219 * ⾃由度 result[4] 确定系数220 *221 *@paramx222 *@paramy223 */
224 public static double[] logFitting(double x[], doubley[]) {225 int size =x.length;226 double xmean = 0.0;227 double ymean = 0.0;228 double rss = 0;229 double tss = 0;230 double result[] = new double[5];231
232 for (int i = 0; i < size; i++) {233 xmean +=x[i];234 y[i] =p(y[i]);235 ymean +=y[i];236 }237 xmean /=size;238 ymean /=size;239
240 double sumx2 = 0.0f;241 double sumxy = 0.0f;242 for (int i = 0; i < size; i++) {243 sumx2 += (x[i] - xmean) * (x[i] -xmean);244 sumxy += (y[i] - ymean) * (x[i] -xmean);245 }246
247 double b = sumxy /sumx2;248 double a = ymean - b *xmean;249
250 for (int i = 0; i < size; i++) {251 rss += (y[i] - (b + a * x[i])) * (y[i] - (b + a *x[i]));252 tss += (y[i] - ymean) * (y[i] -ymean);253 }254
255 double r2 = 1 - (rss / (size - 1 - 1)) / (tss / (size - 1));256
257 System.out.println("决定系数" +r2);258
259 System.out.println("a = " + a + ";b= " +b);260
261 result[0] =a;262 result[1] =b;263 result[2] =x.length;264 result[3] = x.length - 2;265 result[4] =r2;266
267 returnresult;268
269 }270
271 /**
280 public static double[] peakFitting(double x[], doubley[]) {281 int size =x.length;282 double maxX
= 0;283 double maxY = 0;284 double minY =Integer.MAX_VALUE;285 double result[] = new double[6];286 double[][] left = new double[x.length] [3];287 double[][] right = new double[x.length][1];288
vip客户管理289 for (int i = 0; i < size; i++) {290 if (y[i] >maxY) {291 maxX =x[i];292 maxY =y[i];293 }294
295 if (y[i]
299 for (int j = 0; j < 3; j++) {300 left[i][j] =getPeakValue(j, x[i]);301 }302
303 right[i][0] =Math.log(y[i]);304 }305
306 RealMatrix leftMatrix =ateRealMatrix(left);307 RealMatrix rightMatrix
=ateRealMatrix(right);308 RealMatrix leftMatrix1 =anspose();309 RealMatrix m
=leftMatrix1.multiply(leftMatrix);310
311 RealMatrix tMatrix = newLUDecomposition(m).getSolver().getInverse().multiply(leftMatrix1)312
.
multiply(rightMatrix);313
314 result[0] =maxX;315 result[1] =maxY;316 result[2] = -1.0 / Entry(2, 0);317
318 System.out.println(result[0] + " " + result[1] + " " + result[2]);319 double aaaa = getPearR(x, y, result, 2);320 System.out.println("确定系数:" +aaaa);321
322 result[3] =x.length;323 result[4] = x.length - 1 - 2;324 result[5] =aaaa;325
326 returnresult;327 }328
329 /**
330 * ⽤户⾃定义拟合331 *332 *@paramx333 *@paramy334 *@paramsf335 * 算法 常数336 * x337 * x^2338 * x^3339 * exp(x)340 * ln(x)341 * sin(x)342 * cos(x)343 *344 * result[0] = 第⼀项系数 result[1] = 第⼆项系数 . . . result[n] = 第N项系数345 * result[sf.length] 点数 result[sf.length+1] ⾃由度 result[sf.length+2]346 * 确定系数347 *@return
348 */
349 public static double[] userDefineFitting(double x[], double y[], intsf[]) {350 double[][] left = new double[sf.length] [sf.length];351 double[] right = new double[sf.length];352 double[] result = new double[sf.length + 3];353
354 result[sf.length] =x.length;355 boolean containZero = false;356
357 for (int i = 0; i < sf.length; i++) {358 double yValue = 0;359
360 //数值中包括0
361 if (sf[i] == 0) {362 containZero = true;363 }364
365 for (int j = 0; j < sf.length; j++) {366 double xValue = 0;367
368 //计算X的的司格马
369 for (int k = 0; k < x.length; k++) {370 xValue += getUserDefineValue(sf[i], x[k]) *getUserDefineValue(sf[j], x[k]);371 }372
373 left[i][j] =xValue;374 }375
376 //计算Y的的司格马
377 for (int k = 0; k < x.length; k++) {378 yValue += y[k] *getUserDefineValue(sf[i], x[k]);379 }380
381 right[i] =yValue;382 }383
384 //计算⾃由度
385 result[sf.length + 1] = x.length - sf.length - 1;386
387 //计算⾃由度
388 if(containZero) {389 result[sf.length + 1] = x.length -sf.length;390 }391
392 //矩阵求解
393 RealMatrix leftMatrix =ateRealMatrix(left);394 DecompositionSolver solver =
newLUDecomposition(leftMatrix).getSolver();395 RealVector constants = new ArrayRealVector(right, false);396
397 RealVector solution =solver.solve(constants);398
学习型组织理论
399 System.out.println(solution);400
401 //获得系数值
402 for (int i = 0; i < Dimension(); i++) {403 result[i] =Entry(i);404 }405
406 double rss = 0, tss = 0, ymean = 0;407
408 for (int i = 0; i < x.length; i++) {409 ymean +=y[i];410 }411
412 ymean /=x.length;413
414 for (int i = 0; i < x.length; i++) {415 rss += (y[i] -getUserDefineValueByX(sf, x[i], solution))416 * (y[i] -getUserDefineValueByX(sf, x[i], solution));417 tss += (y[i] - ymean) * (y[i] -ymean);418 }419
420 double r2 = 1 - (rss / result[sf.length + 1]) / (tss / (x.length - 1));421
dcns
422 System.out.println("决定系数" +r2);423
424 result[sf.length + 2] =r2;425
under age10 years old426 returnresult;427 }428
429 /**
430 * ⽤户⾃定义函数,传⼊X值获得Y值431 *432 *@paramn433 *@paramx434 *@paramsolution435 *@return
436 */
437 private static double getUserDefineValueByX(int[] n, doublex, RealVector solution) {438 double value = 0;439
440 for (int i = 0; i < n.length; i++) {441 value += getUserDefineValue(n[i], x) *Entry(i);442 }443
444 returnvalue;445 }446
447 /**
448 * 获得⽤户⾃定义的值, 常数449 * x x^2450 * x^3451 * exp(x)452 * ln(x)453 * sin(x)454 * cos(x)455 *456 *@parami457 *@paramx458 *@return
459 */
460 private static double getUserDefineValue(int i, doublex) {461 //常数
462 if (i == 0) {463 return 1;464 } else if (i == 1) {465 //x
466 returnx;467 } else if (i == 2) {468 //x^2
469 return x *x;470 } else if (i == 3) {471 //x^3
472 return x * x *x;473 } else if (i == 4) {474 //exp(x)文史天地
475 returnMath.pow(Math.E, x);476 } else if (i == 5) {477 //ln(x)
478 returnMath.log(x);479 } else if (i == 6) {480 //sin(x)
481 returnMath.sin(x);482 } else if (i == 7) {483 //cos(x)
s(x);485 }486
487 return 0;488 }489
490 /**
491 * 获得决定系数492 *493 *@paramcoeff494 *@return
495 */
496 private static double getPearR(double x[], double y[], double coeff[], intn) {497 int size =x.length;498 double ymean = 0.0;499 double rss = 0;500 double tss = 0;501
502 for (int i = 0; i < size; i++) {503 ymean +=y[i];504 }505
506 ymean /=size;507

本文发布于:2024-09-25 18:27:27,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/10549.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:拟合   获得   系数   确定   位置   常数   定义   数值
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议