| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 928 人关注过本帖
标题:[答案]统计分析与预测程序.回归分析
只看楼主 加入收藏
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
结帖率:100%
收藏
 问题点数:0 回复次数:1 
[答案]统计分析与预测程序.回归分析

各位还是要在理解的基础上再使用啊..
样本输出:

Mx=5.5
My=72.7
方差=82.5
a=67.4667
b=0.951515
回归方程: Y=67.4667 + 0.951515x
相关系数: cor=0.863825
相关系数大于0.81说明预测结果满足要求。
x=15 时的回归方程: Y=81.7394
1995年人均寿命预测约: 81岁

程序代码:

////////////////////////
// 统计分析与预测程序.函数版
// Nobi 2007-7-1
// 开发环境: Visual C++ 6.0 SP6 + Windows Server 2003 Enterprise Edition
// 免责声明: Nobi不对因完全依赖此程序所造成的考试问题承担任何相关责任.
// 敬请各位理解后再使用
////////////////////////
#include <iostream>
#include <cmath>
using namespace std;
//----------------------
// 样本空间
float xi[]={1,2,3,4,5,6,7,8,9,10};
float yi[]={69,70,72,68,73,71,75,74,78,77};
int n=10; //sample count
//----------------------
// 方差
float variance(float *x, float mx, int n);
//----------------------
// 样本均值
float x_aver(float *x, int n);
float y_aver(float *y, int n);
//----------------------
// 回归系数
float a_reg(float mx, float my, float b);
float b_reg(float *x, float *y, int n, float mx, float my);
//----------------------
// 回归方程
float regression(float a, float b, float x);
//----------------------
// 相关系数
float correlation(float *x, float *y, int n, float mx, float my);

//----------------------
// 主程序
void main(){
float mx=x_aver(xi,n);
float my=y_aver(yi,n);
float b=b_reg(xi,yi,n,mx,my);
float a=a_reg(mx,my,b);
cout<<"Mx="<<mx<<endl;
cout<<"My="<<my<<endl;
cout<<"方差="<<variance(xi,mx,n)<<endl;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"回归方程: Y="<<a<<" + "<<b<<"x"<<endl;
cout<<"相关系数: cor="<<correlation(xi,yi,n,mx,my)<<endl;
cout<<"相关系数大于0.81说明预测结果满足要求。"<<endl;
cout<<"x=15 时的回归方程: Y="<<regression(a,b,15)<<endl;
cout<<"1995年人均寿命预测约: "<<int(regression(a,b,15))<<"岁"<<endl;
}
//----------------------
// 方差
float variance(float *x, float mx, int n){
float sum=0, temp;
for(int i=0;i<n;++i){
temp=x[i]-mx;
temp=temp*temp;
sum+=temp;
}
return sum;
}
//----------------------
// 样本均值
float x_aver(float *x, int n){
float sum=0;
for(int i=0;i<n;++i)
sum+=x[i];
sum/=n;
return sum;
}
float y_aver(float *y, int n){
float sum=0;
for(int i=0;i<n;++i)
sum+=y[i];
sum/=n;
return sum;
}
//----------------------
// 回归系数
float a_reg(float mx, float my, float b){
return my-b*mx;
}
float b_reg(float *x, float *y, int n, float mx, float my){
float sum=0;
for(int i=0;i<n;++i)
sum+=(x[i]-mx)*(y[i]-my);
return sum/variance(x,mx,n);
}

//----------------------
// 回归方程
float regression(float a, float b, float x){
return a+b*x;
}
//----------------------
// 相关系数
float correlation(float *x, float *y, int n, float mx, float my){
//和计算b系数类似
float sum=0;
for(int i=0;i<n;++i)
sum+=(x[i]-mx)*(y[i]-my);
sum/=n;
//计算分母
float x_vari=variance(x,mx,n);
float y_vari=variance(y,my,n);
return sum/(sqrt(x_vari/n)*sqrt(y_vari/n));
}

搜索更多相关主题的帖子: 统计分析 回归分析 人均寿命 
2007-07-01 22:41
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
收藏
得分:0 
很简单的一个程序, 没有用啥技巧...

女侠,约吗?
2007-07-01 22:43
快速回复:[答案]统计分析与预测程序.回归分析
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025432 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved