
人口预测的模型,主要有阻滞增长模型(logistic),灰色模型GM(1,1),BP网络模型来做。
对于少量的数据,一般用灰色模型GM(1,1)来预测比较多。例如:已知2004-2007的数据48.7, 57.17,68.76,92.15,预测2008、2009、2010的数据。
在命令窗口下运行程序
GM11
得到如下结果
代码如下:
clear all,clc,clear all,clf
X0=input('请输入序列矩阵X: ')%输入数据请用如例所示形式:[48.7 57.17 68.76 92.15],该向量为原始向量X0
n=length(X0)
for i=2:n%开始进行建模可行性分析
Q(i)=X0(i-1)/X0(i)
end
Q(1)=[]
ma=max(Q)
mi=min(Q)
if ma>exp(2/(n+1))
disp(['序列无法进行灰色预测'])
return
elseif mi<exp(-2/(n+1))
disp(['序列无法进行灰色预测'])
return
else
disp(['序列可以进行灰色预测'])
end
%检验结束
X1=cumsum(X0)%累加生成算子向量X1
Z1=ones(n-1,2)
for i=1:(n-1)
Z1(i,1)=-(X1(i)+X1(i+1))/2
Z1(i,2)=1%均值生成算子Z1
end
Z1T=Z1'%均值生成算子矩阵Z1的转置Z1T
for j=1:n-1
Y(j)=X0(j+1)
end
YT=Y'
A=inv(Z1T*Z1)*Z1T*YT%最小二乘估计计算参数a、u
a=A(1)%Z1参数a
u=A(2)%系统给定参数u
disp(['参数a:',num2str(a),' 参数u:',num2str(u)])
t=u/a
t_test=input('请输入需要预测个数:')
i=1:t_test+n
X1S(i+1)=(X0(1)-t).*exp(-a.*i)+t%计算时间响应序列,得出估计累加向量X1S
X1S(1)=X0(1)
X0S(1)=X0(1)
for j=n+t_test:-1:2
X0S(j)=X1S(j)-X1S(j-1)%计算X1S的逆累加向量X0S
end
for i=1:n
Q(i)=X0S(i)-X0(i)%求残差
E(i)=abs(Q(i))/X0(i)%求相对误差
end
AVG=sum(E)/(n-1)%求平均相对误差
av=input('请输入允许的平均相对误差,如0.1,或0.05:')%输入如0.1,或0.05等形式,不要用5%这类形式
if AVG>=av%如果平均相对误差大于av%,则进入残差GM模型!!!!!!!
cn=length(Q)
Q1=cumsum(Q)%累加生成算子向量Q1
CZ1=ones(cn-1,2)
for i=1:(n-1)
CZ1(i,1)=-(Q1(i)+Q1(i+1))/2
CZ1(i,2)=1%均值生成算子CZ1
end
CZ1T=CZ1'%均值生成算子矩阵CZ1的转置CZ1T
for j=1:cn-1
CY(j)=Q(j+1)
end
CYT=Y'
CA=inv(CZ1T*CZ1)*CZ1T*CYT%最小二乘估计计算参数ca、cu
ca=CA(1)%CZ1参数a
cu=CA(2)%系统给定参数cu
ct=cu/ca
i=1:t_test+cn
Q1S(i+1)=(Q(1)-ct).*exp(-ca.*i)+ct%计算时间响应序列,得出估计累加向量Q1S
X1S=X1S+Q1S%将残差拟合值加入,提高精度
for j=n+t_test:-1:2
X0S(j)=X1S(j)-X1S(j-1)%计算X1S的逆累加向量X0S
end
for i=1:cn
Q(i)=X0S(i)-X0(i)%求残差
E(i)=abs(Q(i))/X0(i)%求相对误差
end
AVG=sum(E)/(n-1)%求平均相对误差
end
x=1:n
xs=2:n+t_test
yn=X0S(2:n+t_test)
plot(x,X0,'^r',xs,yn,'*-b')%作图
disp(['百分平均相对误差为:',num2str(AVG*100),'%'])
disp(['拟合值为: ',num2str(X0S(1:n+t_test))])
disp(['预测值为: ',num2str(X0S(n+1:n+t_test))])
使用cftool
logistic人口模型进行拟合
General model:
f(x) = a/(1+b*exp(-k*(x-1790)))
Coefficients (with 95% confidence bounds):
a = 446.6 (371.1, 522)
b = 57.01 (48.93, 65.09)
k = 0.02155 (0.01945, 0.02365)
Goodness of fit:
SSE: 457.7
R-square: 0.9972
Adjusted R-square: 0.9969
RMSE: 4.908
x(t)=xm+(-xm+x0)exp(-rt)
Fit computation did not converge:
Maximum number of function evaluations exceeded. Increasing
MaxFunEvals (in fit options) may allow for a better fit, or
the current equation may not be a good model for the data.
Fit found when optimization terminated:
General model:
f(x) = ym+(-ym+3.9)*exp(-r*(x-1790))
Coefficients (with 95% confidence bounds):
r = 3.682e-006 (-0.005956, 0.005964)
ym = 2.66e+005 (-4.301e+008, 4.307e+008)
Goodness of fit:
SSE: 2.485e+004
R-square: 0.8455
Adjusted R-square: 0.8378
RMSE: 35.25
matlab程序如下:
----------------------------------------------
x=[1790 1800 1810 1820 1830 1840 1850 1860 1870 1880 1890 1900 1910 1920 1930 1940 1950 1960 1970 1980 1990 2000]
y=[3.9 5.3 7.2 9.6 12.9 17.1 23.2 31.4 38.6 50.2 62.9 76.0 92.0 106.5 123.2 131.7 150.7 179.3 204.0 226.5 251.4 281.4]
x=x'y=y'
st_ = [500 20 0.2]
ft_ = fittype('a/(1+b*exp(-k*(x-1790)))' ,...
'dependent',{'y'},'independent',{'x'},...
'coefficients',{'a', 'b', 'k'})
cf_ = fit(x,y,ft_ ,'Startpoint',st_)
----------------------------------------------
运行结果
cf_ =
General model:
cf_(x) = a/(1+b*exp(-k*(x-1790)))
Coefficients (with 95% confidence bounds):
a = 446.6 (371.1, 522)
b = 57.01 (48.93, 65.09)
k = 0.02155 (0.01945, 0.02365)
clearclc
%这是一个一元线性回归问题,用matlab编写如下程序:
x=[1960,1961,1962,1963,1964,1965,1966,1967,1968]'
y=[29.72,30.61,31.51,32.13,32.34,32.85,33.56,34.20,34.83]'
subplot(221)
plot(x,y)
%假设模型 y=a0+a1*x+e
x=[ones(9,1),x]
a=x\y%a(1)=a0,a(2)=a1.
X=1960:2222
Y=a(1)+a(2)*X
subplot(222)
plot(X,Y)
%2003开始 人口增长一倍 43年 2077 年超过一百亿
%2002年的世界人口数 55.2357亿
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)