用python画AR模型时序图

本文介绍使用Python绘制AR(自回归)模型时序图的方法。包括平稳与非平稳AR(1)及AR(2)模型,并展示了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景:

用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,title = "AR({0}):x[t]={1}*x[t-1]+e".format(1,a))
plt.plot(x,".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,title = "AR({0}):x[t]={1}*x[t-1]+{2}*x[t-2]+e".format(2,a,b))
plt.plot(x,".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,title = "AR({0}):x[t]={1}*x[t-1]+{2}*x[t-2]+e".format(2,a,b))
plt.plot(x,"+",color="purple")

plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值