# Java线性回归

2018/01/02 15:58

package com.topsmob.amazon.utils;

import java.util.List;
import java.util.Map;

/**
* Java线性回归实现
* x：是rank
* y: 是销量
*/
public class LinearRegression {

public static Map<String,Object> calculate(List<Map<String,Object>> list){
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<String,Object> 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);