package com.util;
/**
* File : DataPoint.java
* Description : Java实现一元线性回归的算法,回归线实现类,(可实现统计指标的预测)
*/
import java.math.BigDecimal;
import java.util.ArrayList;
public class RegressionLine // implements Evaluatable
{
/** sum of x */
private double sumX;
/** sum of y */
private double sumY;
/** sum of x*x */
private double sumXX;
/** sum of x*y */
private double sumXY;
/** sum of y*y */
private double sumYY;
/** sum of yi-y */
private double sumDeltaY;
/** sum of sumDeltaY^2 */
private double sumDeltaY2;
/** 误差 */
private double sse;
private double sst;
private double E;
private String[] xy;
private ArrayList listX;
private ArrayList listY;
private int XMin, XMax, YMin, YMax;
/** line coefficient a0 */
private float a0;
/** line coefficient a1 */
private float a1;
/** number of data points */
private int pn;
/** true if coefficients valid */
private boolean coefsValid;
/**
* Constructor.
*/
public RegressionLine() {
XMax = 0;
YMax = 0;
pn = 0;
xy = new String[2];
listX = new ArrayList();
listY = new ArrayList();
}
/**
* Constructor.
*
* @param data
* the array of data points
*/
public RegressionLine(DataPoint data[]) {
pn = 0;
xy = new String[2];
listX = new ArrayList();
listY = new ArrayList();
for (int i = 0; i < data.length; ++i) {
addDataPoint(data[i]);
}
}
/**
* Return the current number of data points.
*
* @return the count
*/
public int getDataPointCount() {
return pn;
}
/**
* Return the coefficient a0.
*
* @return the value of a0
*/
public float getA0() {
validateCoefficients();
return a0;
}
/**
* Return the coefficient a1.
*
* @return the value of a1
*/
public float getA1() {
validateCoefficients();
return a1;
}
/**
* Return the sum of the x values.
*
* @return the sum
*/
public double getSumX() {
return sumX;
}
/**
* Return the sum of the y values.
*
* @return the sum
*/
public double getSumY() {
return sumY;
}
/**
* Return the sum of the x*x values.
*
* @return the sum
*/
public double getSumXX() {
return sumXX;
}
/**
* Return the sum of the x*y values.
*
* @return the sum
*/
public double getSumXY() {
return sumXY;
}
public double getSumYY() {
return sumYY;
}
public int getXMin() {
return XMin;
}
public int getXMax() {
return XMax;
}
public int getYMin() {
return YMin;
}
public int getYMax() {
return YMax;
}
/**
* Add a new data point: Update the sums.
*
* @param dataPoint
* the new data point
*/
public void addDataPoint(DataPoint dataPoint) {
sumX += dataPoint.x;
sumY += dataPoint.y;
sumXX += dataPoint.x * dataPoint.x;
sumXY += dataPoint.x * dataPoint.y;
sumYY += dataPoint.y * dataPoint.y;
if (dataPoint.x > XMax) {
XMax = (int) dataPoint.x;
}
if (dataPoint.y > YMax) {
YMax = (int) dataPoint.y;
}
// 把每个点的具体坐标存入ArrayList中,备用
xy[0] = (int) dataPoint.x + "";
xy[1] = (int) dataPoint.y + "";
if (dataPoint.x != 0 && dataPoint.y != 0) {
System.out.print(xy[0] + ",");
System.out.println(xy[1]);
try {
// System.out.println("n:"+n);
listX.add(pn, xy[0]);
listY.add(pn, xy[1]);
} catch (Exception e) {
e.printStackTrace();
}
/*
* System.out.println("N:" + n); System.out.println("ArrayList
* listX:"+ listX.get(n)); System.out.println("ArrayList listY:"+
* listY.get(n));
*/
}
++pn;
coefsValid = false;
}
/**
* Return the value of the regression line function at x. (Implementation of
* Evaluatable.)
*
* @param x
* the value of x
* @return the value of the function at x
*/
public float at(int x) {
if (pn < 2)
return Float.NaN;
validateCoefficients();
return a0 + a1 * x;
}
/**
* Reset.
*/
public void reset() {
pn = 0;
sumX = sumY = sumXX = sumXY = 0;
coefsValid = false;
}
/**
* Validate the coefficients. 计算方程系数 y=ax+b 中的a
*/
private void validateCoefficients() {
if (coefsValid)
return;
if (pn >= 2) {
float xBar = (float) sumX / pn;
float yBar = (float) sumY / pn;
a1 = (float) ((pn * sumXY - sumX * sumY) / (pn * sumXX - sumX
* sumX));
a0 = (float) (yBar - a1 * xBar);
} else {
a0 = a1 = Float.NaN;
}
coefsValid = true;
}
/**
* 返回误差
*/
public double getR() {
// 遍历这个list并计算分母
for (int i = 0; i < pn - 1; i++) {
float Yi = (float) Integer.parseInt(listY.get(i).toString());
float Y = at(Integer.parseInt(listX.get(i).toString()));
float deltaY = Yi - Y;
float deltaY2 = deltaY * deltaY;
/*
* System.out.println("Yi:" + Yi); System.out.println("Y:" + Y);
* System.out.println("deltaY:" + deltaY);
* System.out.println("deltaY2:" + deltaY2);
*/
sumDeltaY2 += deltaY2;
// System.out.println("sumDeltaY2:" + sumDeltaY2);
}
sst = sumYY - (sumY * sumY) / pn;
// System.out.println("sst:" + sst);
E = 1 - sumDeltaY2 / sst;
return round(E, 4);
}
// 用于实现精确的四舍五入
public double round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public float round(float v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).floatValue();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
一、源码特点 JSP 工资投放管理系统是一套完善的java web信息管理系统,对理解JSP java编程开发语言有帮助,系统具有完整的源代码和数据库,系统主要采用B/S模式开发。开发环境为 TOMCAT7.0,Myeclipse8.5开发,数据库为Mysql5.0,使用java语言开发。 二、功能介绍 如博客中介绍 三、注意事项 1、管理员账号:admin密码:admin 数据库配置文件DBO.java 2、开发环境为TOMCAT7.0,Myeclipse8.5开发,数据库为Mysql5.0,使用java语言开发。 3、数据库文件名是jspgztf.sql ,系统名称gztf 4、系统首页地址:http://127.0.0.1:8080/gztf/login.jsp 不包含人工调试服务,如果需要联系作者购买
资源推荐
资源详情
资源评论
收起资源包目录
jsp工资投放管理系统Myeclipse开发mysql数据库web结构java编程计算机网页项目源码 (473个子文件)
index.css.bak 7KB
login.css.bak 6KB
demo.css.bak 2KB
pinyin.js.bak 812B
RegressionLine.class 5KB
DBO.class 5KB
Str.class 4KB
LinearRegression.class 4KB
StaticMethod.class 3KB
DataPoint.class 368B
.classpath 927B
sapar.css 34KB
index.css 7KB
zTreeStyle.css 6KB
login.css 6KB
datepicker-dev.css 5KB
datepicker.css 4KB
datepicker.css 4KB
datepicker.css 4KB
datepicker.css 4KB
index_inner.css 4KB
datepicker.css 3KB
demo.css 2KB
common.css 249B
WdatePicker.css 192B
WdatePicker.css 158B
WdatePicker.css 0B
xubox_loading0.gif 6KB
zTreeStandard.gif 5KB
bank_upop.gif 3KB
xubox_loading3.gif 2KB
bank_srcb.gif 2KB
bank_psbc.gif 2KB
bank_bjrcb.gif 2KB
xubox_loading2.gif 2KB
img.gif 2KB
img.gif 2KB
img.gif 2KB
img.gif 2KB
bank_czb.gif 1KB
bank_hsb.gif 1KB
bank_hzb.gif 1KB
bank_nbcb.gif 1KB
bank_gzcb.gif 1KB
datePicker.gif 1KB
datePicker.gif 1KB
bank_ceb.gif 1014B
bank_shb.gif 1010B
bank_pab.gif 965B
bank_post.gif 940B
bank_bcom.gif 910B
bank_cib.gif 902B
bank_abc.gif 813B
bank_cmbc.gif 795B
bank_bob.gif 781B
bank_gzrcc.gif 769B
bank_gdb.gif 761B
bank_icbc.gif 739B
bank_sdb.gif 739B
bank_ccb.gif 729B
bank_hxb.gif 715B
bank_boc.gif 704B
xubox_loading1.gif 701B
bank_njcb.gif 689B
bank_spdb.gif 682B
bank_citic.gif 678B
bank_cmb.gif 659B
bank_bea.gif 626B
bank_cbhb.gif 614B
img.gif 475B
loading.gif 381B
line_conn.gif 45B
mysql-connector-java-5.0.8-bin.jar 528KB
jspsmart.jar 521KB
fastjson-1.1.15.jar 309KB
json-lib-2.4-jdk15.jar 155KB
commons-io-1.4.jar 106KB
commons-fileupload-1.2.1.jar 56KB
json_simple-1.1.jar 16KB
RegressionLine.java 6KB
LinearRegression.java 4KB
DBO.java 4KB
Str.java 4KB
StaticMethod.java 2KB
DataPoint.java 451B
bg.jpg 307B
bg.jpg 307B
jquery.js 247KB
sapar.js 95KB
jquery-1.4.4.min.js 77KB
jquery.ztree.core.js 72KB
JsBarcode.all.min.js 57KB
jquery-barcode.js 55KB
sapar.min.js 50KB
pinyin.js 47KB
calendar.js 22KB
WdatePicker.js 10KB
LodopFuncs.js 7KB
page.js 4KB
common.js 2KB
共 473 条
- 1
- 2
- 3
- 4
- 5
资源评论
qq_251836457
- 粉丝: 4402
- 资源: 580
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功