如何计算Python中的滚动窗口以获得多资产矩阵?
我对3个资产有1610个观察值,我想计算一个协方差矩阵σ=σdσ
其中σ
是我3个资产方差的3x3的对角线矩阵,而<代码> d 是我3个资产的相关矩阵。我想要这个矩阵σ
,对于60个观测值的滚动窗口,因此我最终可以将其作为风险公式值的sigma作为sigma来计算风险的价值。
问题是我不确定如何编程滚动窗口,我只知道如何使用滚动窗口编程默认相关矩阵d
,但我想用对角线σ矩阵预先和发布乘。到目前为止,我所做的是:
for I in range (T):
a = sigmabtc[‘gas’].iat[i]
b = sigmaeth[‘gas’].iat[i]
c = sigmaxlm[‘gas’].iat[i]
X = (a,b,c)
σ = np.diag(X)
D = returns.corr
Σ = σ*D*σ
这种方法并没有一个滚动窗口对所有观测值进行一个滚动窗口,我尝试使用函数rolling(σ)
,但是它不起作用。
我只能计算矩阵d
:
returns.rolling(60).corr(pairwise=True)
任何人都可以提出一种方法,也可以在此相关矩阵之前和之后使用滚动窗口喂食对角线矩阵σ
?
I have 1610 observations for 3 assets and I want to compute a covariance matrix Σ = σDσ
where σ
is a diagonal matrix of 3x3 of the variances of my 3 assets, and D
is the correlation matrix of my 3 assets. I wanted this matrix Σ
for a rolling window of 60 observations so I can eventually feed this as the sigma in the value at risk formula to compute the value at risk.
The problem is I'm not sure how to program the rolling window, I only know how to program the default correlation matrix D
with rolling window but I want to pre and post multiply it with the diagonal σ matrix. What I have done so far is:
for I in range (T):
a = sigmabtc[‘gas’].iat[i]
b = sigmaeth[‘gas’].iat[i]
c = sigmaxlm[‘gas’].iat[i]
X = (a,b,c)
σ = np.diag(X)
D = returns.corr
Σ = σ*D*σ
This approach doesn’t take the rolling window one by one for all observations, I tried to use the function rolling(Σ)
however it doesn’t work.
I could only compute the matrix D
:
returns.rolling(60).corr(pairwise=True)
Could anyone suggest a method to also feed the diagonal matrix σ
before and after this correlation matrix also with rolling window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论