使用python实现画AR模型时序图

前端之家收集整理的这篇文章主要介绍了使用python实现画AR模型时序图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

背景:

用python画AR模型的时序图。

结果:

使用python实现画AR模型时序图


代码

import numpy as np
import matplotlib.pyplot as plt
"""
AR(1)的时序图:x[t]=a*x[t-1]+e
"""
num = 2000
e = np.random.rand(num)
x = np.empty(num)

"""
平稳AR(1)
"""
a = -0.5
x[0] = 2
for i in range(1,num):
 x[i] = a*x[i-1]+e[i]
plt.subplot(321,title = "AR({0}):x[t]={1}*x[t-1]+e".format(1,a))
plt.plot(x,"or")

"""
非平稳AR(1)
"""
a = -1.01
x[0] = 2
for i in range(1,num):
 x[i] = a*x[i-1]+e[i]
plt.subplot(322,".b")

"""
平稳AR(2)
"""
a = -0.2
b = 0.7
x[0] = 2
for i in range(2,num):
 x[i] = a*x[i-1]+b*x[i-2]+e[i]
plt.subplot(323,title = "AR({0}):x[t]={1}*x[t-1]+{2}*x[t-2]+e".format(2,a,b))
plt.plot(x,"og")

"""
非平稳AR(2)
"""
a = -0.3
b = 0.8
x[0] = 2
for i in range(2,num):
 x[i] = a*x[i-1]+b*x[i-2]+e[i]
plt.subplot(324,".y")

"""
非平稳AR(2)
"""
a = -0.2
b = 0.8
x[0] = 2
for i in range(2,num):
 x[i] = a*x[i-1]+b*x[i-2]+e[i]
plt.subplot(313,"+",color="purple")

plt.show()

以上这篇使用python实现画AR模型时序图就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

原文链接:https://www.f2er.com/python/526763.html

猜你在找的Python相关文章