λ€λ³λ μκ³μ΄ λ°μ΄ν° 2 (Multivariate Time Series Data)
220930 μμ±
<λ³Έ λΈλ‘κ·Έλ ysyblog λμ λΈλ‘κ·Έλ₯Ό μ°Έκ³ ν΄μ 곡λΆνλ©° μμ±νμμ΅λλ€>
https://ysyblog.tistory.com/298
[μκ³μ΄λΆμ] λ€λ³λ μ ν νλ₯ κ³Όμ - VAR & IRP (λ°±ν°μκΈ°νκ·κ³Όμ , μνμ€μλ΅ν¨μ)
λ€λ³λ μ ν νλ₯ κ³Όμ νμμ± λ¨λ³λ μκ³μ΄(Simple/Multipleν¬ν¨)μ μ’ μλ³μ(Y_t)κ° λ 립λ³μλ€μλ§! μν₯μ λ°λλ€λ ν° κ°μ μ‘΄μ¬ νμ€μ μΌλ‘ μ’ μλ³μμ λ 립λ³μλ μνΈ μν₯μ μ£Όκ³ λ°μ μμ:
ysyblog.tistory.com
1οΈβ£ λ€λ³λ μκ³μ΄
- μ’ μλ³μ(Y_t)κ° λ 립λ³μλ€μλ§ μν₯ λ°μ
- 2μ°¨μ(μλ, μ§μΆ : μ’ μλ³μ) κ³Όκ±° 1μμ κ°μ§λ§μ κ³ λ €νλ 벑ν°μκΈ°νκ· μκ³ λ¦¬μ¦
π 벑ν°μκΈ°νκ· λͺ¨ν
1) VAR μκ³ λ¦¬μ¦
- νκ· λ²‘ν°μ 곡λΆμ° 벑ν°κ° μμ°¨μλ§ μμ‘΄νκ³ κ°κ°μ μ λμμΉμ λ 립μ μΈ μ μμ± μκ³μ΄
2) μνμ€ μλ΅ ν¨μ
- μ¬λ¬κ°μ μκ³μ΄ μνΈ μκ΄κ΄κ³λ₯Ό κΈ°λ°μΌλ‘ κ°κ°μ λ³μκ° λ€λ₯Έ λ³μμ μ΄λ€ μν₯μ μ£Όλμ§ μνμ€ λ°μ ν¨μλ‘ μ μ μμ
- μνμ€ : μ΄λ€ μκ³μ΄μ΄ t = 0 μΌ λ 1μ΄κ³ , t < 0 or t > 0 μΌ λ 0
- μνμ€ ννμ μκ³μ΄μ΄ λ€λ₯Έ μκ³μ΄μ λ―ΈμΉλ μν₯μ μκ°μ λ°λΌ νμ
2οΈβ£ μ½λ ꡬν
π λ°μ΄ν° λ‘λ© λ° μκ°ν
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels
import statsmodels.api as sm
raw = sm.datasets.macrodata.load_pandas().data
dates_info = raw[['year', 'quarter']].astype(int).astype(str)
raw.index = pd.DatetimeIndex(sm.tsa.datetools.dates_from_str(dates_info['year'] + 'Q'+ dates_info['quarter']))
raw_use = raw.iloc[:, 2:5]
- μ€μ GDP = μ€μ CON(μλΉ) + μ€μ INV(ν¬μ)
raw_use.plot(subplots = True, figsize = (12, 5))
plt.tight_layout()
plt.show()
raw_use.diff(1).dropna().plot(subplots = True, figsize = (12, 5))
plt.tight_layout()
plt.show()
π VAR λͺ¨ν
- realgdp : realgdp, realcon, realinv μ μμ°¨ L1 λͺ¨λμ realconμ μμ°¨ L2μ μν₯
- realcon : realgdp, realcon, realinv μ μμ°¨ L1, L2 λͺ¨λ μν₯
- realinv : realgdp, realcon, realinv μ μμ°¨ L1 λ§ μν₯
- realcon, realgdp, realinv μμΌλ‘ κ°μ₯ λ€λ₯Έ λ³μμ μν₯ λ§μ΄ λ°μ
raw_use_return = raw_use.diff(1).dropna()
fit = sm.tsa.VAR(raw_use_return).fit(maxlags = 2)
display(fit.summary())
π λͺ¨ν μμΈ‘ λ° μκ°ν
forecast_num = 20
# μ μΆμ
# pre_var = fit.forecast(fit.model.endog[-1:], steps = forecast_num)
# ꡬκ°μΆμ
# pre_var_ci = fit.forecast_interval(fit.model.endog[-1:], steps = forecast_num)
fit.plot_forecast(forecast_num)
plt.tight_layout()
plt.show()
- μνμ€ λ°μ ν¨μ
- realgdp -> realgdp : λ¨κΈ°μ μΌλ‘ μμλ‘ λ°λμ§λ§ 0μΌλ‘ μλ ΄
- realgdp -> realcons : gdpκ° μ¦κ°, μλΉκ° κ°μνλ€κ° λμ΄λ¨
- realinv -> realcons : μ€μ ν¬μκ° μ¦κ°νλ©΄μ μ€μ μλΉ μ¦κ°νλ€κ° 0μΌλ‘ μλ ΄
- realcon -> realgdp : μλΉκ° μ¦κ°νλ©΄ μ€μ gdpκ° μλΉν μ¦κ°νλ€κ° 0μΌλ‘ μλ ΄
- realinv -> realgdp : ν¬μκ° μ¦κ°νλ©΄ μ€μ gdpκ° μν μ¦κ°νλ€κ° 0μΌλ‘ μλ ΄
fit.irf(forecast_num).plot()
plt.tight_layout()
plt.show()
π μμ°¨μ§λ¨
fit.plot_acorr()
plt.tight_layout()
plt.show()