java 线性回归_Java线性回归

package com.topsmob.amazon.utils;

import java.util.LinkedHashMap;

import java.util.List;

import java.util.Map;

/**

* Java线性回归实现

* x:是rank

* y: 是销量

*/

public class LinearRegression {

public static Map calculate(List> list){

Map map = new LinkedHashMap<>();

int MAXN = list.size();

int n = 0;

double[] x = new double[MAXN];

double[] y = new double[MAXN];

double sumx = 0.0, sumy = 0.0, sumx2 = 0.0;

for (Map item:list){

//这里是x轴的参数

x[n] = Double.parseDouble(item.get("rank").toString());

//这里是y轴的参数

y[n] = Double.parseDouble(item.get("qty").toString());

sumx += x[n];

sumx2 += x[n] * x[n];

sumy += y[n];

n++;

}

double xbar = sumx / n;

double ybar = sumy / n;

// second pass: compute summary statistics

double xxbar = 0.0, yybar = 0.0, xybar = 0.0;

for (int i = 0; i < n; i++) {

xxbar += (x[i] - xbar) * (x[i] - xbar);

yybar += (y[i] - ybar) * (y[i] - ybar);

xybar += (x[i] - xbar) * (y[i] - ybar);

}

double beta1 = xybar / xxbar;

double beta0 = ybar - beta1 * xbar;

// print results

System.out.println("y = " + beta1 + " * x + " + beta0);

// analyze results

int df = n - 2;

double rss = 0.0; // residual sum of squares

double ssr = 0.0; // regression sum of squares

for (int i = 0; i < n; i++) {

double fit = beta1*x[i] + beta0;

rss += (fit - y[i]) * (fit - y[i]);

ssr += (fit - ybar) * (fit - ybar);

}

double R2 = ssr / yybar;

map.put("r",R2);

map.put("a",beta1);

map.put("b",beta0);

/* double svar = rss / df;

double svar1 = svar / xxbar;

double svar0 = svar/n + xbar*xbar*svar1;

System.out.println("R^2 = " + R2);

System.out.println("std error of beta_1 = " + Math.sqrt(svar1));

System.out.println("std error of beta_0 = " + Math.sqrt(svar0));

svar0 = svar * sumx2 / (n * xxbar);

System.out.println("std error of beta_0 = " + Math.sqrt(svar0));

System.out.println("SSTO = " + yybar);

System.out.println("SSE = " + rss);

System.out.println("SSR = " + ssr);*/

return map;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值