基于Python语言的statsmodels包使用手册_XX.马尔可夫区制转换动态回归模型.3.美国联邦基金的区制转换_具有转换截距和滞后因变量(END)

XX.马尔可夫区制转换动态回归模型(Markov switching dynamic regression models).3.美国联邦基金的区制转换_具有转换截距和滞后因变量(Federal funds rate with switching intercept and lagged dependent variable)

描述

第一个案例将美国联邦利率在恒定截距附近随机变动(噪音),看作一个马尔可夫区制转换动态回归模型,恒定截距具有在不同区制下有一定规律变化的特点。模型如下第一个案例
在这里插入图片描述
第二个美国联邦基金案例从第一个案例进行扩充,增加了美国联邦基金利率的滞后因子,模型如下:
在这里插入图片描述
区制转换规则根据:
在这里插入图片描述
在这里插入图片描述

我们将用最大似然法估计该模型的参数:
在这里插入图片描述

此模型案例的数据来源:美国联邦基金的区制转换案例数据来源

用法

参数

代码①

#pip install pandas_datareader
%matplotlib inline

import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt

#Get the federal funds rate data
#载美国联邦基金数据
from statsmodels.tsa.regime_switching.tests.test_markov_regression import fedfunds

dta_fedfunds = pd.Series(
    fedfunds, index=pd.date_range("1954-07-01", "2010-12-31", freq="QS")
)
# Plot the data
# dta_fedfunds.plot(title="Federal funds rate", figsize=(12, 3))

# Fit the model
mod_fedfunds2 = sm.tsa.MarkovRegression(
    dta_fedfunds.iloc[1:], k_regimes=2, exog=dta_fedfunds.iloc[:-1]
)
res_fedfunds2 = mod_fedfunds2.fit()
res_fedfunds2.summary())

结果①

在这里插入图片描述

从结果可以看出:

(1)

对比第一个案例,信息标准大幅下降,表明第二个案例具有更好的拟合度。

类别第一个案例第二个案例
Dep. Variableyy
No. Observations226225
ModelMarkovRegressionMarkovRegression
Log Likelihood-508.636-264.711
AIC1027.272543.421
BIC1044.375567.334
HQIC1034.174553.073

(2)

通过截距结果,区制转换的解释方式已经改变。第二个案例的截距coef都较低。

代码②

print(res_fedfunds2.expected_durations)

结果②

[2.76105188 7.65529154]
保持Regime0状态2.76/4=0.7年左右,保持Regime1状态7.66/4=2年左右。

代码③

data1 = res_fedfunds2.smoothed_marginal_probabilities.reset_index().rename(columns={"index":"时间",0:"zero-step probabilities",1:"one-step probabilities"})
data2 = dta_fedfunds.reset_index().rename(columns={"index":"时间",0:"Federal funds rate"})
data  = data1.merge(data2)
data.head()

结果③

在这里插入图片描述

代码④

import plotly as py
import plotly.graph_objects as go
import plotly.express as px
import plotly.offline
import plotly.figure_factory as ff  # 画有标注的热力图, go.Heatmap 无标注
from plotly import tools

figname = '20220717'  
x2 ='zero-step probabilities'
x1 ='Federal funds rate'
draw1 = go.Scatter(x=data['时间'], y=data[x1], name=x1, marker=dict(color='rgb(49,130,189)'),yaxis = 'y')
draw2 = go.Scatter(x=data['时间'], y=data[x2], name=x2, marker=dict(color='rgb(190,190,190)'),yaxis = 'y2')
# draw3 = go.Scatter(x=data['时间'], y=data[x33], name=x33, marker=dict(color='rgb(0,0,0)'))

figdata = [draw1,draw2]
fig = go.Figure(data=figdata)
# 尺寸、背景和全局设置:Paper、plot
fig.update_layout(template='simple_white'  # 'plotly',#'simple_white',
        #     ,font_size=16,
        #     ,paper_bgcolor='#E9E7EF'
        #     ,plot_bgcolor='black'
        #     ,width=1000
        #     ,margin=dict(t=100,pad=10)
    )

    # 标题
fig.update_layout(
        title=figname
        # ,title_font_size=22,
        # ,title_font_color='red',
        , title_x=0.5)

    # 图例
fig.update_layout(
        legend_title=''
        , legend_x=0.9
        , legend_y=1.1
        #     ,legend_title_font_color='red',
        #     ,legend_bordercolor='black',
        #     ,legend_valign='top',
        #     ,legend_borderwidth=1
    )

    # 图形系列非数据相关的设置
fig.layout.bargap = 0.5  # (如针对bar图)

    # x轴:轴、网格线、范围滚动条
fig.update_layout(
        xaxis=dict(
            title=''
            , title_font_color='red'
            # ,gridcolor='cyan'
            # ,rangeslider=dict(bgcolor='black',yaxis_rangemode='auto')
            # ,tickmode='array'#str型,设置坐标轴刻度的格式,’auto’表示自动根据输入的数据来决定,’linear’表示线性的数值型,
            # ’array’表示由自定义的数组来表示(用数组来自定义刻度标签时必须选择此项)
            # ,tickvals = np.arange(1,len(xoldticktext))
            # ,ticktext = np.arange(1,len(xoldticktext))
        ),
        yaxis=dict(
            title='',
            title_font_color='red',
            side='left'
        ),
            yaxis2=dict(
            title='',
            title_font_color='red',
            overlaying='y',
            side='right'
        )

    )
    # xaxis = dict(title=dict(text=""),),# "tickformat": '', "zeroline": True},
    # yaxis = dict(title=dict(text=""),zeroline = False),#"autorange": 'reversed'
    # "yaxis2": {"anchor": 'x', "overlaying": 'y', "side": 'right'},

  
plotly.offline.plot(fig, filename=figname + '.html')
fig.show()    

结果④

在这里插入图片描述
通过查看更高截距的区制Regime0的平滑概率,可以看到更多变化。

代码⑤

res_fedfunds2.smoothed_marginal_probabilities[0].plot(
    title="Probability of being in the high regime", figsize=(12, 3)
)

结果⑤

在这里插入图片描述

总结

数据更新

数据更新主页

1.每月中国进出口

1.1.进出口数据 20220531 时间序列 ARMA ARIMA SARIMA

链接: link

2.宏观数据集

2.1.图01:剔除价格波动后工业增加值同比增速较为平滑_金工狗_数据包

链接: link

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金工狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值