当前位置:网站首页>Autoregressive model of Lantern Festival
Autoregressive model of Lantern Festival
2022-06-26 08:54:00 【Py little brother】
Autoregressive model (AutoRegression Model) It can be used to predict the future value of time series , The model assumes that the relationship between the front and back values is linear . When using this model , It is usually assumed that the value of a random variable depends on its previous value . What we have to do is fit the data , Find the appropriate parameters for the data . The mathematical formula of the autoregressive model is as follows :
x t x_t xt=c+ ∑ i = 1 p a i ∗ x k \sum_{i=1}^{p}a_i*x_k ∑i=1pai∗xk+ g t g_t gt (k=t-i)
In the above formula ,c For constant , The last term is the random component , Also known as white noise .
In regression analysis , If new data points are introduced when the sample fitting degree is very good , The fitting performance may deteriorate immediately . We can cross verify , Or use the algorithm without fitting problem to solve this problem .
Pass below scipy.optimize.least() Function to build a model , We use sunspot data to train the model . The code is as follows :
'''Xu Yong Kang'''
from scipy.optimize import leastsq
import statsmodels.api as sm
import matplotlib.pyplot as plt
import numpy as np
# Use scipy.optimize.leastp Function to build the model
def model(p,x1,x10):
p1,p10=p
return p1*x1+p10*x10
def error(p,data,x1,x10):
return data-model(p,x1,x10)
# Fitting model
def fit(data):
p0=[0.5,0.5]
params=leastsq(error,p0,args=(data[10:],data[9:-1],data[:-10]))
return params
# Training models on sunspot data sets
data_loader=sm.datasets.sunspots.load_pandas()
sunspots=data_loader.data['SUNACTIVITY'].values
cutoff=0.9*len(sunspots)
params=fit(sunspots[:int(cutoff)])
print('Params',params) # Get parameters
print(params[0].shape)
print(params[1])
# Predict the value and calculate each index
pred=params[0][0]*sunspots[int(cutoff-1):-1]+params[0][1]*sunspots[int(cutoff-10):-10]
actual=sunspots[int(cutoff):]
print('Root mean square error',np.sqrt(np.mean((actual-pred)**2)))
print('Mean absolute error',np.mean(np.abs(actual-pred)))
print('Mean absolute percentage error',100*np.mean(np.abs(actual-pred)/actual))
print('Cofficient of determination',1-((actual-pred)**2).sum()/((actual-actual.mean())**2).sum())
year_range=data_loader.data['YEAR'].values[int(cutoff):]
plt.plot(year_range,actual,'x',label='Actual Sunspots',color='red')
plt.plot(year_range,pred,'o',label='Prediction',color='blue')
plt.grid(True)
plt.xlabel('Year')
plt.ylabel('Activity')
plt.legend()
plt.show()
We can get the parameters from the training results
Params (array([0.67172672, 0.33626295]), 2)
From these, we can get the predicted value 
From the picture we can see , Many predictions are almost successful , But there are also some poor results .
notes :
stay pred There is a problem about numpy The problem of broadcasting
边栏推荐
- Detailed explanation of self attention & transformer
- Slider verification - personal test (JD)
- Whale conference provides digital upgrade scheme for the event site
- 【云原生 | Kubernetes篇】深入万物基础-容器(五)
- Reverse crawling verification code identification login (OCR character recognition)
- Torch model to tensorflow
- Leetcode22 summary of types of questions brushing in 2002 (XII) and collection search
- Google Chrome collection
- [已解决]setOnNavigationItemSelectedListener()被弃用
- Tensor
猜你喜欢

Drawing with MATLAB (2) -- color ring

Transformers loading Roberta to implement sequence annotation task

Trimming_ nanyangjx

Opencv learning notes 3

Selenium 搭建 Cookies池 绕过验证反爬登录

How to realize wireless Ethernet high-speed communication for multiple Mitsubishi PLCs?

Polka lines code recurrence

Corn image segmentation count_ nanyangjx

Reverse crawling verification code identification login (OCR character recognition)

多台三菱PLC如何实现无线以太网高速通讯?
随机推荐
1.26 pytorch learning
How to realize wireless Ethernet high-speed communication for multiple Mitsubishi PLCs?
Steps for ROS to introduce opencv (for cmakelist)
Using MySQL and Qt5 to develop takeout management system (I): environment configuration
nn. Modulelist and nn Sequential
How to correctly PIP install pyscipopt
【云原生 | Kubernetes篇】深入万物基础-容器(五)
框架跳转导致定位失败的解决方法
Segmentation of structured light images using segmentation network
Leetcode notes: binary search simple advanced
Addition of attention function in yolov5
Analysis of Yolo series principle
[QNX Hypervisor 2.2用户手册]12.1 术语(一)
OpenGL display mat image
【MATLAB GUI】 键盘回调中按键识别符查找表
9. code generation
Using transformers of hugging face to realize multi label text classification
三菱PLC若想实现以太网无线通讯,需要具备哪些条件?
隐藏式列表菜单以及窗口转换在Selenium 中的应用
基于SSM的毕业论文管理系统