python中的DDOF参数的人口协方差计算
我有一个带有股票价格的数据框。(实际上是2600行的Excel。我的问题是在创建协方差矩阵时,dataframe.cov中的DDOF参数默认为1个值为1。这对应于样本协方差方法。但是, ,我想创建一个人口方法,即 /n
。也就是说
,
代码
的 | Numpy.cov | 可以 |
---|---|---|
吗 | ? | 您 |
组织 | 我 | 根据 |
| | |
| | 54.2 |
22.02.2022 | A | 56.112 |
21.02.2022 | A | 56 |
28.02.2022 | B | 5 |
25.02.2022 | B | 4 |
24.02.2022 | B | 4 |
23.02.2022 | B | 4.2 |
22.02.2022 | B | 5.1 |
21.02.2022 | B | 5 |
28.02.2022 | C | 7 |
25.02.2022 | C | 7.2 |
24.02.2022 | C | 7 |
23.02.2022 | C | 8 |
22.02.2022 | C | 6.9 |
21.02.2022 | C | 7 |
我的代码;
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)
Date | Code | Price |
---|---|---|
28.02.2022 | A | 55 |
25.02.2022 | A | 55.1 |
24.02.2022 | A | 54 |
23.02.2022 | A | 54.2 |
22.02.2022 | A | 56.112 |
21.02.2022 | A | 56 |
28.02.2022 | B | 5 |
25.02.2022 | B | 4 |
24.02.2022 | B | 4 |
23.02.2022 | B | 4.2 |
22.02.2022 | B | 5.1 |
21.02.2022 | B | 5 |
28.02.2022 | C | 7 |
25.02.2022 | C | 7.2 |
24.02.2022 | C | 7 |
23.02.2022 | C | 8 |
22.02.2022 | C | 6.9 |
21.02.2022 | C | 7 |
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ddoftest = pd.read_csv(“ ddoftest.csv”)
covar = ddoftest.cov()
印刷(Covar)
covar1 = ddoftest.cov(ddof = 0)
打印(COVAR1)
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作为某些随机股票价格
ddoftest=pd.read_csv("ddoftest.csv")
covar=ddoftest.cov()
print(covar)
covar1=ddoftest.cov(ddof=0)
print(covar1)
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