python - 如何计算两列内两组数据之间的相关性?
问题描述
我想先创建两列,然后用它LOG()
来计算 columnPrice
和 column的定期每日收益Adjusted Close
。此后使用周期回报来计算周期每日回报之间的相关性。
我试过了
Combine_data['log_return'] = np.log(1 + Combine_data.pct_change)
Combine_data.head()
但它不工作。
Combine_data= pd.merge(XAU_USD,SP500, on='Date',suffixes=
('(GOLD)','(SP500)'))
Combine_data.set_index('Date',inplace=True)
Combine_data.head()
这是我的输出的样子:
解决方案
您可以尝试如下:
ser1= (df['gold']+1).apply(np.log)
ser2= (df['silver']+1).apply(np.log)
np.corrcoef(ser1, ser2)
结果如下所示:
Out[431]:
array([[1. , 0.30121126],
[0.30121126, 1. ]])
考虑到数据是随机生成的,0.301 的相关性还不错:-)
Out[430]:
gold silver date
0 793.559641 19.112793 2019-08-23
1 1428.329390 17.758924 2019-08-24
2 1044.061092 17.962435 2019-08-25
3 1222.397539 17.638691 2019-08-26
4 890.945841 11.593497 2019-08-27
5 1224.616916 15.759736 2019-08-28
6 1059.684075 12.900665 2019-08-29
7 1147.011421 20.274250 2019-08-30
8 929.638993 12.244630 2019-08-31
9 515.545695 14.609073 2019-09-01
推荐阅读
- python - DataFrame of frequencies by list comprehension?
- c# - if URL contains, then execute
- r - How to plot regression or LOWESS lines over data in coplot
- docker - 詹金斯声明性管道多个奴隶
- sql-server - SSMS 和 Active Directory 身份验证不适用于 Azure SQL 和非 Azure SQL
- r - 使用 rhandsontable 具有类似 excel 功能的 R Shiny 应用程序
- import - LESS Mixins 在其他导入的 LESS 文件中不可用
- sql - SQL - 语法错误(PK 和 FK 的问题)
- angular - Angular Nativescript http 请求停止
- bash - 抑制 Bash 中 readarray 命令中的错误