如何计算Python中的滚动窗口以获得多资产矩阵?

发布于 2025-02-09 08:03:18 字数 731 浏览 1 评论 0原文

我对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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文