python中的DDOF参数的人口协方差计算

发布于 2025-01-23 15:38:09 字数 3040 浏览 2 评论 0原文

我有一个带有股票价格的数据框。(实际上是2600行的Excel。我的问题是在创建协方差矩阵时,dataframe.cov中的DDOF参数默认为1个值为1。这对应于样本协方差方法。但是, ,我想创建一个人口方法,即 /n

。也就是说

代码

Numpy.cov可以
组织根据
54.2
22.02.2022A56.112
21.02.2022A56
28.02.2022B5
25.02.2022B4
24.02.2022B4
23.02.2022B4.2
22.02.2022B5.1
21.02.2022B5
28.02.2022C7
25.02.2022C7.2
24.02.2022C7
23.02.2022C8
22.02.2022C6.9
21.02.2022C7

我的代码;

dfr = dfr.set_index(['Date', 'Code']).Price.unstack(['Code'])
df1 = dfr.pct_change()
df1.to_excel(r'C:\sample\df1.xlsx', index = True)  

#Covar
df2 = df1.cov()
df2.to_excel(r'C:\sample\df2.xlsx', index = True)

I have a dataframe with stock prices.(actually its a excel with 2600 rows. My problem is when creating the covariance matrix, the ddof parameter in dataframe.cov has a value of 1 by default. This corresponds to the sample covariance method. However, I want to create a covar with the method of population, namely /N. I have tried =df1.cov(ddof=0) but it had no effect.

Can you help me to create a dataframe.cov with the bias true, that is, the population method? Or can you organize my code according to numpy.cov?

Thank you,

My dataframe; (dfr)

DateCodePrice
28.02.2022A55
25.02.2022A55.1
24.02.2022A54
23.02.2022A54.2
22.02.2022A56.112
21.02.2022A56
28.02.2022B5
25.02.2022B4
24.02.2022B4
23.02.2022B4.2
22.02.2022B5.1
21.02.2022B5
28.02.2022C7
25.02.2022C7.2
24.02.2022C7
23.02.2022C8
22.02.2022C6.9
21.02.2022C7

My code;

dfr = dfr.set_index(['Date', 'Code']).Price.unstack(['Code'])
df1 = dfr.pct_change()
df1.to_excel(r'C:\sample\df1.xlsx', index = True)  

#Covar
df2 = df1.cov()
df2.to_excel(r'C:\sample\df2.xlsx', index = True)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆梦 2025-01-30 15:38:10

我不确定您的意思是您没有使用ddof = 0吗?我得到了差异。


ddoftest = pd.read_csv(“ ddoftest.csv”)
covar = ddoftest.cov()
印刷(Covar)
covar1 = ddoftest.cov(ddof = 0)
打印(COVAR1)


      A         B         C

A 0.060795 -0.007887 -0.018725
B -0.007887 0.092322 0.022308
C -0.018725 0.022308 0.075867
ABC
A 0.057418 -0.007448 -0.017685
B -0.007448 0.087193 0.021068
C -0.017685 0.021068 0.071652


i选择A,B和C作为某些随机股票价格

enter image description hereI am not sure what you mean you are not getting with ddof=0? I get the differences.


ddoftest=pd.read_csv("ddoftest.csv")
covar=ddoftest.cov()
print(covar)
covar1=ddoftest.cov(ddof=0)
print(covar1)


      A         B         C

A 0.060795 -0.007887 -0.018725
B -0.007887 0.092322 0.022308
C -0.018725 0.022308 0.075867
A B C
A 0.057418 -0.007448 -0.017685
B -0.007448 0.087193 0.021068
C -0.017685 0.021068 0.071652


I chose A, B and C as some random stock prices

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文