当前位置:网站首页>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=1paixk+ 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
 Sunspot prediction
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

原网站

版权声明
本文为[Py little brother]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170553546704.html