2018MCM/ICM-5

概述

  在part 1的D问中需要建立一个模型用于预测相关资源关于时间序列的增长趋势。观察部分资源1960年到2009的数据,发现其比较接近生长曲线模型,于是先利用生长曲线相关函数进行一下拟合,观察一下拟合效果。

理论基础

生长曲线函数模型

  生长曲线函数模型,也被称为 Logistic 函数模型,即为我们高中生物中所学习的“S”型曲线。其一般表达式如下:

  其中$k,a,b,$为模型的参数,实现对于种群初期,中期,末期三个阶段的描述。
  

皮尔模型

  初期用于研究人口增长规律,也可以用于研究部分经济变量的发展变化规律,一般形式如下:

  我们所选择是其中一个常用的模型,表达式如下:

  其中参数为$K,a,b$。

实现

代码实现

  利用scipy.optimize的curve_fit进行已经确定函数模型的拟合,即函数参数的优化,具体实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: UTF-8 -*-
import numpy as np
import xlrd
import matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# get the dataset
data = xlrd.open_workbook('ProblemCData2.xlsx')
sheet1 = data.sheet_by_index(0) #获得表格
sheet2 = data.sheet_by_index(1) #获得表格
sheet3 = data.sheet_by_index(4) #获得表格
nrows = sheet1.nrows # 拿到总共行数
ncols = sheet1.ncols
source = 'data1/'

# the fun use to fit
def func(x,a,b,k):
return k/(1 + b*np.exp(-a*x))

# get the data of (stateNum,MSNNum) from 1960 to 2009
# stateNum is the num of state {0:AZ,1:CA,2:NM,3:TX}
# MSNNum is the num of MSN {1:NGTCB,2:NUETN,3:WYTCB,4:HTTCB,5:GETCB,6:BMTCB}
def getDataNeedFit(stateNum,MSNNum):
MSNNum = MSNNum - 1
beginNum = 1 + stateNum*50
endNum = beginNum + 49
dataNeedFit = []
for i in range(beginNum,endNum):
dataNeedFit.append(sheet3.cell(i,MSNNum).value)
return dataNeedFit

# enter the main
if __name__ =="__main__":
x = np.arange(1, 50, 1)
y = getDataNeedFit(3,2)
y = np.array(y)

popt, pcov = curve_fit(func, x, y)
a=popt[0]
b=popt[1]
k=popt[2]

print popt

yvals=func(x,a,b,k)
plot1=plt.plot(x, y, '*',label='original values')
plot2=plt.plot(x, yvals, 'r',label='fit values')
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.legend(loc=4)
plt.title('GETCB')
plt.show()

  其中我把数据的提取封装为一个函数getDataNeedFit(stateNum,MSNNum),输入对应的州序号stateNum和指标序号MSNNum即可提取需要拟合的数据。拟合的模型即为上述的皮尔模型。
  源数据为原始数据稍做修改后得到的。

拟合效果

  对于德克萨斯州1960到2009年“GETCB”的拟合参数输出如下:

1
2
$ python curveFitting1.py 
[ 4.78504567e-01 6.24579218e+06 4.06732530e+05]

  拟合效果如下:
photo

小结

  此生长模型函数模型对于筛选后的部分资源数据的拟合效果并不是很好,可能是由于外部因素影响导致的。