首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Commons Math学习札记——聚类和回归

2012-12-23 
Commons Math学习笔记——聚类和回归看其他篇章到目录选择。聚类可以见我以前写过的聚类分析的文章。回归是一

Commons Math学习笔记——聚类和回归

看其他篇章到目录选择。

聚类可以见我以前写过的聚类分析的文章。

回归是一个统计中非常重要的概念了。在Commons Math库中有一个regression的子包转么实现了线性回归的一些基本类型。在regression包中,有个基本接口就是MultipleLinearRegression,这个接口表达y=X*b+u这样的基本线性回归式。线性回归是利用称为线性回归方程的最小二乘函数对一个或多个自变量和因变量之间关系进行建模的一种回归分析。简单看这个公式,y代表了一个n维的列向量(回归子),X代表了[n,k]大小的观测值矩阵(回归量),b是k维的回归参数,u是一个n维的剩余误差。回归分析干什么用的?具体讲就是预测。我们在数据挖掘中定义,定性的分析叫做分类,而定量的分析叫做回归。回归就是根据已有的观察值去预测未来的一个定量的指标。记得前一段阿里云到学院来做技术交流,讲到阿里和淘宝通过数据分析对中国商品交易(还是具体什么贸易,忘记了,尴尬)的预测就是工程师做的一个简单的线性回归分析,模型虽然简单,但是后来与实际数据一比较,预测值与实际值的曲线基本吻合。

回到Commons Math中的实现,MultipleLinearRegression接口共有5个方法,double estimateRegressandVariance()返回回归子的方差,double[] estimateRegressionParameters()返回回归参数b(当然是一个k维的向量),double[] estimateRegressionParametersStandardErrors()返回回归参数的标准误差,double[][] estimateRegressionParametersVariance()返回回归参数b的方差,double[] estimateResiduals()返回剩余误差数组(n维向量)。AbstractMultipleLinearRegression类实现了这个接口,作为一个抽象类,它没有完全实现这些方法,仍旧作为抽象方法抛给具体的实现类去完成,典型的模板方法设计模式。继承这个类的方法有GLSMultipleLinearRegression和OLSMultipleLinearRegression,前者应该是广义最小二乘法,而后者是普通最小二乘法。

我们拿普通最小二乘法作为测试,看看它的source code如何实现:首先对于x和y的表示,在AbstractMultipleLinearRegression内部,x被定义为一个RealMatrix,y是一个RealVector,这些在第一章都有讲到。而OLSMultipleLinearRegression里用到定义了一个内部的QRDecomposition。通过void newSampleData(double[] y, double[][] x)方法来载入回归模型;接着就可以通过各种借口方法去计算这个回归的统计量了。算出b和u之后,那么根据新的观测回归量x就可以预测y了。

?1Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归/**?*//**
?2Commons Math学习札记——聚类和回归?*?
?3Commons Math学习札记——聚类和回归?*/
?4Commons Math学习札记——聚类和回归package?algorithm.math;
?5Commons Math学习札记——聚类和回归
?6Commons Math学习札记——聚类和回归import?org.apache.commons.math.stat.regression.OLSMultipleLinearRegression;
?7Commons Math学习札记——聚类和回归import?org.apache.commons.math.stat.regression.SimpleRegression;
?8Commons Math学习札记——聚类和回归
?9Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归/**?*//**
10Commons Math学习札记——聚类和回归?*?@author?Jia?Yu
11Commons Math学习札记——聚类和回归?*?@date???2010-12-6
12Commons Math学习札记——聚类和回归?*/
13Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归public?class?RegressionTest?Commons Math学习札记——聚类和回归{
14Commons Math学习札记——聚类和回归
15Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????/**?*//**
16Commons Math学习札记——聚类和回归?????*?@param?args
17Commons Math学习札记——聚类和回归?????*/
18Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????public?static?void?main(String[]?args)?Commons Math学习札记——聚类和回归{
19Commons Math学习札记——聚类和回归????????//?TODO?Auto-generated?method?stub
20Commons Math学习札记——聚类和回归????????regression();
21Commons Math学习札记——聚类和回归????????System.out.println("-------------------------------------");
22Commons Math学习札记——聚类和回归????????simple();
23Commons Math学习札记——聚类和回归????}
24Commons Math学习札记——聚类和回归
25Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????private?static?void?simple()?Commons Math学习札记——聚类和回归{
26Commons Math学习札记——聚类和回归????????//?TODO?Auto-generated?method?stub
27Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????double[][]?data?=?Commons Math学习札记——聚类和回归{?Commons Math学习札记——聚类和回归{?0.1,?0.2?},?Commons Math学习札记——聚类和回归{338.8,?337.4?},?Commons Math学习札记——聚类和回归{118.1,?118.2?},?
28Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????????????Commons Math学习札记——聚类和回归{888.0,?884.6?},?Commons Math学习札记——聚类和回归{9.2,?10.1?},?Commons Math学习札记——聚类和回归{228.1,?226.5?},?Commons Math学习札记——聚类和回归{668.5,?666.3?},?Commons Math学习札记——聚类和回归{998.5,?996.3?},?
29Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????????????Commons Math学习札记——聚类和回归{449.1,?448.6?},?Commons Math学习札记——聚类和回归{778.9,?777.0?},?Commons Math学习札记——聚类和回归{559.2,?558.2?},?Commons Math学习札记——聚类和回归{0.3,?0.4?},?Commons Math学习札记——聚类和回归{0.1,?0.6?},?Commons Math学习札记——聚类和回归{778.1,?775.5?},?
30Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????????????Commons Math学习札记——聚类和回归{668.8,?666.9?},?Commons Math学习札记——聚类和回归{339.3,?338.0?},?Commons Math学习札记——聚类和回归{448.9,?447.5?},?Commons Math学习札记——聚类和回归{10.8,?11.6?},?Commons Math学习札记——聚类和回归{557.7,?556.0?},?
31Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????????????Commons Math学习札记——聚类和回归{228.3,?228.1?},?Commons Math学习札记——聚类和回归{998.0,?995.8?},?Commons Math学习札记——聚类和回归{888.8,?887.6?},?Commons Math学习札记——聚类和回归{119.6,?120.2?},?Commons Math学习札记——聚类和回归{0.3,?0.3?},?
32Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????????????Commons Math学习札记——聚类和回归{0.6,?0.3?},?Commons Math学习札记——聚类和回归{557.6,?556.8?},?Commons Math学习札记——聚类和回归{339.3,?339.1?},?Commons Math学习札记——聚类和回归{888.0,?887.2?},?Commons Math学习札记——聚类和回归{998.5,?999.0?},?
33Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????????????Commons Math学习札记——聚类和回归{778.9,?779.0?},?Commons Math学习札记——聚类和回归{10.2,?11.1?},?Commons Math学习札记——聚类和回归{117.6,?118.3?},?Commons Math学习札记——聚类和回归{228.9,?229.2?},?Commons Math学习札记——聚类和回归{668.4,?669.1?},?
34Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????????????Commons Math学习札记——聚类和回归{449.2,?448.9?},?Commons Math学习札记——聚类和回归{0.2,?0.5?}
35Commons Math学习札记——聚类和回归????????};
36Commons Math学习札记——聚类和回归????????SimpleRegression?regression?=?new?SimpleRegression();
37Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????for?(int?i?=?0;?i?<?data.length;?i++)?Commons Math学习札记——聚类和回归{
38Commons Math学习札记——聚类和回归????????????regression.addData(data[i][1],?data[i][0]);
39Commons Math学习札记——聚类和回归????????}
40Commons Math学习札记——聚类和回归????????System.out.println("slope?is?"+regression.getSlope());
41Commons Math学习札记——聚类和回归????????System.out.println("slope?std?err?is?"+regression.getSlopeStdErr());
42Commons Math学习札记——聚类和回归????????System.out.println("number?of?observations?is?"+regression.getN());
43Commons Math学习札记——聚类和回归????????System.out.println("intercept?is?"+regression.getIntercept());
44Commons Math学习札记——聚类和回归????????System.out.println("std?err?intercept?is?"+regression.getInterceptStdErr());
45Commons Math学习札记——聚类和回归????????System.out.println("r-square?is?"+regression.getRSquare());
46Commons Math学习札记——聚类和回归????????System.out.println("SSR?is?"+regression.getRegressionSumSquares());
47Commons Math学习札记——聚类和回归????????System.out.println("MSE?is?"+regression.getMeanSquareError());
48Commons Math学习札记——聚类和回归????????System.out.println("SSE?is?"+regression.getSumSquaredErrors());
49Commons Math学习札记——聚类和回归????????System.out.println("predict(0)?is?"+regression.predict(0));
50Commons Math学习札记——聚类和回归????????System.out.println("predict(1)?is?"+regression.predict(1));
51Commons Math学习札记——聚类和回归????}
52Commons Math学习札记——聚类和回归
53Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????private?static?void?regression()?Commons Math学习札记——聚类和回归{
54Commons Math学习札记——聚类和回归????????//?TODO?Auto-generated?method?stub
55Commons Math学习札记——聚类和回归????????double[]?y;
56Commons Math学习札记——聚类和回归????????double[][]?x;
57Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????y?=?new?double[]Commons Math学习札记——聚类和回归{11.0,?12.0,?13.0,?14.0,?15.0,?16.0};
58Commons Math学习札记——聚类和回归????????x?=?new?double[6][];
59Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????x[0]?=?new?double[]Commons Math学习札记——聚类和回归{1.0,?0,?0,?0,?0,?0};
60Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????x[1]?=?new?double[]Commons Math学习札记——聚类和回归{1.0,?2.0,?0,?0,?0,?0};
61Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????x[2]?=?new?double[]Commons Math学习札记——聚类和回归{1.0,?0,?3.0,?0,?0,?0};
62Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????x[3]?=?new?double[]Commons Math学习札记——聚类和回归{1.0,?0,?0,?4.0,?0,?0};
63Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????x[4]?=?new?double[]Commons Math学习札记——聚类和回归{1.0,?0,?0,?0,?5.0,?0};
64Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????????x[5]?=?new?double[]Commons Math学习札记——聚类和回归{1.0,?0,?0,?0,?0,?6.0};
65Commons Math学习札记——聚类和回归????????
66Commons Math学习札记——聚类和回归????????OLSMultipleLinearRegression?regression?=?new?OLSMultipleLinearRegression();
67Commons Math学习札记——聚类和回归????????regression.newSampleData(y,?x);
68Commons Math学习札记——聚类和回归????????
69Commons Math学习札记——聚类和回归????????double[]?betaHat?=?regression.estimateRegressionParameters();
70Commons Math学习札记——聚类和回归????????System.out.println("Estimates?the?regression?parameters?b:");
71Commons Math学习札记——聚类和回归????????print(betaHat);
72Commons Math学习札记——聚类和回归????????double[]?residuals?=?regression.estimateResiduals();
73Commons Math学习札记——聚类和回归????????System.out.println("Estimates?the?residuals,?ie?u?=?y?-?X*b:");
74Commons Math学习札记——聚类和回归????????print(residuals);
75Commons Math学习札记——聚类和回归????????double?vary?=?regression.estimateRegressandVariance();
76Commons Math学习札记——聚类和回归????????System.out.println("Returns?the?variance?of?the?regressand?Var(y):");
77Commons Math学习札记——聚类和回归????????System.out.println(vary);
78Commons Math学习札记——聚类和回归????????double[]?erros?=?regression.estimateRegressionParametersStandardErrors();
79Commons Math学习札记——聚类和回归????????System.out.println("Returns?the?standard?errors?of?the?regression?parameters:");
80Commons Math学习札记——聚类和回归????????print(erros);
81Commons Math学习札记——聚类和回归????????double[][]?varb?=?regression.estimateRegressionParametersVariance();
82Commons Math学习札记——聚类和回归????}
83Commons Math学习札记——聚类和回归
84Commons Math学习札记——聚类和回归Commons Math学习札记——聚类和回归????private?static?void?print(double[]?v)?Commons Math学习札记——聚类和回归{
85Commons Math学习札记——聚类和回归????????//?TODO?Auto-generated?method?stub
86Commons Math学习札记——聚类和回归Cod

热点排行