如何使用statsmodels的DynamicFactor方法与外生变量?

huangapple go评论94阅读模式
英文:

How to use statsmodels' DynamicFactor method with exogenous variables?

问题

I have a multivariate dynamic factor model with one common factor that I want to estimate with statsmodels.tsa.statespace.dynamic_factor.DynamicFactor.

The model looks as follows:

Model formulation in LaTeX.

As you can see, I am dealing with a t x 4 matrix of endogenous variables. Each of them has 6 own specific exogenous variables, which they don't share. So the only thing the 4 time series have in common is the common factor.

My question is how to put this in code.

I have attempted the following:

  1. model = DynamicFactor(
  2. endog=y, # nobs x 4
  3. exog=X, # nobs x k_exog
  4. k_factors=1,
  5. factor_order=1,
  6. error_order=0,
  7. error_cov_type='diagonal'
  8. )

But the results seem off, and I know from the documentation that X should have the shape of t x k_exog. I am wondering what k_exog should be in my case, and if I can arrange my matrix so that y_1 only uses W_1, etc.

*EDIT: In the model formulation, at one point the dependent variable is called 'NG,' but it should be 'y.' Apologies.

英文:

I have a multivariate dynamic factor model with one common factor that I want to estimate with statsmodels.tsa.statespace.dynamic_factor.DynamicFactor.

The model looks as follows:
Model formulation in LaTeX.*

As you can see, I am dealing with a t x 4 matrix of endogenous variables. Each of them has 6 own specific exogenous variables, which they don't share. So the only thing the 4 time series have in common, is the common factor.

My question is how to put this in code.

I have attempted the following:

  1. model = DynamicFactor(
  2. endog=y, # nobs x 4
  3. exog=X, # nobs x k_exog
  4. k_factors=1,
  5. factor_order=1,
  6. error_order=0,
  7. error_cov_type='diagonal'
  8. )

But the results seem off, and I know from the documentation that X should have the shape of t x k_exog. I am wondering what k_exog should be in my case, and if I can arange my matrix so that y_1 only uses W_1 etc.

*EDIT: in the model formulation, at one point the dependent variable is called 'NG' but it should be y. Apologies.

答案1

得分: 0

DynamicFactor模型假设每个exog变量影响每个endog变量。但是,您可以告诉模型将某些参数的值设置为固定值(而不是估计它们)。您可以使用这个来实现您想要的目标。

一个简单的示例如下:

  1. import numpy as np
  2. import pandas as pd
  3. import statsmodels.api as sm
  4. # 模拟一些数据
  5. nobs = 100
  6. np.random.seed(1234)
  7. y = pd.DataFrame(np.random.normal(size=(nobs, 2)), columns=['y1', 'y2'])
  8. X_1 = pd.Series(np.random.normal(size=nobs), name='x1')
  9. X_2 = pd.Series(np.random.normal(size=nobs), name='x2')
  10. X = pd.concat([X_1, X_2], axis=1)
  11. # 构建模型
  12. mod = sm.tsa.DynamicFactor(y, exog=X, k_factors=1, factor_order=1)
  13. # 如果需要确定需要设置为0的参数的名称,可以打印参数名称
  14. # print(mod.param_names)
  15. # 使用`fix_params`固定适用的参数...
  16. with mod.fix_params({'beta.x2.y1': 0, 'beta.x1.y2': 0}):
  17. # 然后使用`fit`估计其他参数
  18. res = mod.fit(disp=False)
  19. # 打印结果
  20. print(res.summary())

这将产生上述输出结果。

英文:

The DynamicFactor model assumes that every exog variable affects every endog variable. However, you can tell the model to set the values of certain parameters to fixed values (rather than estimate them). You can use this to do what you want.

A simple example follows:

  1. import numpy as np
  2. import pandas as pd
  3. import statsmodels.api as sm
  4. # Simulate some data
  5. nobs = 100
  6. np.random.seed(1234)
  7. y = pd.DataFrame(np.random.normal(size=(nobs, 2)), columns=['y1', 'y2'])
  8. X_1 = pd.Series(np.random.normal(size=nobs), name='x1')
  9. X_2 = pd.Series(np.random.normal(size=nobs), name='x2')
  10. X = pd.concat([X_1, X_2], axis=1)
  11. # Construct the model
  12. mod = sm.tsa.DynamicFactor(y, exog=X, k_factors=1, factor_order=1)
  13. # You can print the parameter names if you need to determine the
  14. # names of the parameters that you need to set fixed to 0
  15. # print(mod.param_names)
  16. # Fix the applicable parameters with `fix_params`...
  17. with mod.fix_params({'beta.x2.y1': 0, 'beta.x1.y2': 0}):
  18. # And estimate the other parameters with `fit`
  19. res = mod.fit(disp=False)
  20. # Print the results
  21. print(res.summary())

Which gives:

  1. Statespace Model Results
  2. =============================================================================================
  3. Dep. Variable: ['y1', 'y2'] No. Observations: 100
  4. Model: DynamicFactor(factors=1, order=1) Log Likelihood -276.575
  5. + 2 regressors AIC 567.150
  6. Date: Fri, 12 May 2023 BIC 585.386
  7. Time: 22:45:16 HQIC 574.530
  8. Sample: 0
  9. - 100
  10. Covariance Type: opg
  11. ===================================================================================
  12. Ljung-Box (L1) (Q): 0.01, 0.10 Jarque-Bera (JB): 5.22, 0.78
  13. Prob(Q): 0.93, 0.76 Prob(JB): 0.07, 0.68
  14. Heteroskedasticity (H): 2.11, 0.75 Skew: -0.56, -0.17
  15. Prob(H) (two-sided): 0.04, 0.41 Kurtosis: 3.02, 3.26
  16. Results for equation y1
  17. ==============================================================================
  18. coef std err z P>|z| [0.025 0.975]
  19. ------------------------------------------------------------------------------
  20. loading.f1 -0.4278 0.891 -0.480 0.631 -2.174 1.318
  21. beta.x1 -0.0614 0.129 -0.478 0.633 -0.313 0.191
  22. beta.x2 0 nan nan nan nan nan
  23. Results for equation y2
  24. ==============================================================================
  25. coef std err z P>|z| [0.025 0.975]
  26. ------------------------------------------------------------------------------
  27. loading.f1 0.4487 0.888 0.505 0.613 -1.292 2.189
  28. beta.x1 0 nan nan nan nan nan
  29. beta.x2 -0.1626 0.113 -1.442 0.149 -0.384 0.058
  30. Results for factor equation f1
  31. ==============================================================================
  32. coef std err z P>|z| [0.025 0.975]
  33. ------------------------------------------------------------------------------
  34. L1.f1 0.1060 0.323 0.328 0.743 -0.527 0.739
  35. Error covariance matrix
  36. ==============================================================================
  37. coef std err z P>|z| [0.025 0.975]
  38. ------------------------------------------------------------------------------
  39. sigma2.y1 0.6124 0.766 0.800 0.424 -0.889 2.113
  40. sigma2.y2 0.9306 0.818 1.138 0.255 -0.672 2.533
  41. ==============================================================================
  42. Warnings:
  43. [1] Covariance matrix calculated using the outer product of gradients (complex-step).

huangapple
  • 本文由 发表于 2023年5月11日 16:18:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定