如何降低基金投资的风险?

如何降低基金投资的风险?

李力用量化投资方法验证低相关性投资组合能降低投资风险,并提供源代码。
瑞达利欧说低相关性的投资组合,能大幅降低投资风险

根据桥水基金创始人瑞达利欧的经验(视频),选择15-20个低相关性的投资品,进行组合,能大幅降低投资风险。

什么是相关性

相关性是指两个变量的关联程度。无相关性基金组合,是指组合中各基金的日收益率水平没有关联性,用统计概念表达就是组合中各基金的日收益率水平的相关系数接近于0。

在中国证券市场能找到低相关性的基金吗?

李力用2016-10-11至2022-10-11共6年的所有4000支公募基金的545万条历史数据进行分析,筛选出与沪深300ETF基金相关系数小于0.1,并且两两相关系数也小于0.1,同时累计收益率为正的16支基金。
以下是这16支基金的相关系数热力图:

以下是这16支基金的累计收益率图:

李力把低相关性基金组合的权重设为等权重。以下是基金组合的权重图:

这16支基金的底层资产类别有债券型、货币型、股票型、混合型和基金型,以下是基金组合的底层资产类别权重图:

债券型占比最大,为54.6%,股票型占比只有9.1%。

低相关性基金组合真的能大幅降低投资风险吗?

以下为等权重的低相关性基金组合的累计收益率图:

从图上看,等权重的低相关性基金组合的波动性大幅低于沪深300ETF,累计收益率略低于沪深300ETF,说明在中国证券市场,低相关性基金组合真的能大幅降低投资风险。


你也克隆下面的“无相关性基金”研究,试运行一下吧。
你对无相关性基金组合有什么想法,评论一下吧。
听说点赞的人赚得更多哟!

源代码

下面是在聚宽量化平台-策略研究-研究环境中研究的源代码:

1.导入库¶
In [1]:


from datetime import date, datetime
import pandas as pd
from jqdata import finance
from sqlalchemy import and_, or_
import seaborn as sns
import matplotlib.pyplot as plt

2.设定基金交易的起止日期¶
In [2]:


start_date = date(2016, 10, 11)
end_date = date(2022, 10, 11)

3.获取基金信息¶
3.1获取在起止日期内交易的基金信息¶
In [3]:


fund_info_0 = finance.run_query(query(finance.FUND_MAIN_INFO).\
filter(or_
(and_(finance.FUND_MAIN_INFO.end_date >= end_date, finance.FUND_MAIN_INFO.start_date <= start_date),\
and_(finance.FUND_MAIN_INFO.end_date == None, finance.FUND_MAIN_INFO.start_date == None),\
and_(finance.FUND_MAIN_INFO.end_date >= end_date, finance.FUND_MAIN_INFO.start_date == None).\
and_(finance.FUND_MAIN_INFO.end_date == None, finance.FUND_MAIN_INFO.start_date <= start_date)\
)
)
)
ins1 = fund_info_0['main_code'].values.tolist()

3.2查询已获取的基金数量¶
In [4]:

len(fund_info_0)

Out[4]:
4000

3.3展示已获取的基金信息¶
In [5]:

fund_info_0.head()

Out[5]:


id main_code statistics_main_code name advisor trustee operate_mode_id operate_mode underlying_asset_type_id underlying_asset_type invest_style_id invest_style start_date end_date
0 1 000001 None 华夏成长 华夏基金管理有限公司 中国建设银行股份有限公司 401001 开放式基金 402004 混合型 005005 偏股混合型 2001-12-18 None
1 2 000003 000003 中海可转债债券 中海基金管理有限公司 中国农业银行股份有限公司 401001 开放式基金 402003 债券型 005011 二级债基 2013-03-20 None
2 3 000005 None 嘉实增强信用 嘉实基金管理有限公司 中国工商银行股份有限公司 401001 开放式基金 402003 债券型 005009 长期纯债型 2013-03-08 None
3 5 000008 000008 嘉实中证500ETF联接 嘉实基金管理有限公司 中国建设银行股份有限公司 401001 开放式基金 402005 基金型 005013 基金型 2013-03-22 None
4 6 000009 000009 易方达天天理财货币 易方达基金管理有限公司 中国工商银行股份有限公司 401001 开放式基金 402002 货币型 005014 货币型 2013-03-04 None


4.根据已获取的基金代码获取基金累计净值¶
4.1获取基金累计净值¶
In [ ]:


i = 0
for code in ins1:
unit_fund_price = finance.run_query(query(finance.FUND_NET_VALUE).filter(finance.FUND_NET_VALUE.code == code, finance.FUND_NET_VALUE.day >= start_date).with_entities(finance.FUND_NET_VALUE.day, finance.FUND_NET_VALUE.code, finance.FUND_NET_VALUE.refactor_net_value))
if i == 0:
fund_price = unit_fund_price.copy()
else:
fund_price = pd.concat([fund_price, unit_fund_price])
i += 1

4.2查询基金累计净值数据量¶
In [159]:


len(fund_price)

Out[159]:

5445126

4.3按照代码展示基金累计净值¶
In [160]:


fund_price[fund_price['code'] == '000001'].head()

Out[160]:

day code refactor_net_value
0 2016-10-11 000001 5.904695
1 2016-10-12 000001 5.899515
2 2016-10-13 000001 5.920234
3 2016-10-14 000001 5.915054
4 2016-10-17 000001 5.878797

4.4按照日期展示基金累计净值¶
In [161]:


fund_price[fund_price['day'] == start_date]

Out[161]:

day code refactor_net_value
0 2016-10-11 000001 5.904695
0 2016-10-11 000003 1.008778
0 2016-10-11 000005 1.204291
0 2016-10-11 000008 1.747600
0 2016-10-11 000009 1.151615
0 2016-10-11 000011 18.248833
0 2016-10-11 000014 1.187000
0 2016-10-11 000015 1.216130
0 2016-10-11 000017 2.188291
0 2016-10-11 000020 1.771000
0 2016-10-11 000021 3.423294
0 2016-10-11 000024 1.328000
0 2016-10-11 000028 1.370776
0 2016-10-11 000029 1.537943
0 2016-10-11 000030 1.873511
0 2016-10-11 000031 1.887000
0 2016-10-11 000032 1.237000
0 2016-10-11 000039 1.967100
0 2016-10-11 000041 0.784000
0 2016-10-11 000042 1.408000
0 2016-10-11 000043 1.338000
0 2016-10-11 000045 1.398820
0 2016-10-11 000047 1.399576
0 2016-10-11 000049 0.911000
0 2016-10-11 000051 1.058000
0 2016-10-11 000053 1.405342
0 2016-10-11 000054 1.282300
0 2016-10-11 270042 1.806844
0 2016-10-11 000056 1.686000
0 2016-10-11 000057 1.373000
... ... ... ...
0 2016-10-11 730103 1.162621
0 2016-10-11 740602 1.158393
0 2016-10-11 750003 1.327954
0 2016-10-11 750007 1.159515
0 2016-10-11 960000 0.994500
0 2016-10-11 960001 0.821000
0 2016-10-11 960002 1.208000
0 2016-10-11 960003 1.030400
0 2016-10-11 960004 1.801000
0 2016-10-11 960005 0.843500
0 2016-10-11 960006 1.781000
0 2016-10-11 960007 2.275000
0 2016-10-11 960008 2.228000
0 2016-10-11 960010 1.054700
0 2016-10-11 960011 0.461600
0 2016-10-11 960012 1.383700
0 2016-10-11 960016 4.417700
0 2016-10-11 960018 2.389000
0 2016-10-11 960020 1.435000
0 2016-10-11 960021 1.030000
0 2016-10-11 960022 0.988400
0 2016-10-11 960023 1.021900
0 2016-10-11 960026 0.975000
0 2016-10-11 960027 1.014000
0 2016-10-11 960028 1.799000
0 2016-10-11 960029 1.235220
0 2016-10-11 968001 10.671567
0 2016-10-11 968003 10.580000
0 2016-10-11 968004 10.434954
0 2016-10-11 968011 12.020000

3386 rows × 3 columns

5.生成基金累计净值透视表¶
5.1生成基金累计净值透视表¶
In [162]:


fund_price_pivot = pd.pivot_table(fund_price, index = 'day', values = 'refactor_net_value', columns = 'code')

5.3用前值填充非交易日期累计净值¶
In [164]:


fund_price_pivot = fund_price_pivot.dropna(how = 'all')
fund_price_pivot = fund_price_pivot.fillna(method='ffill')

5.4删除起始日期有缺失值的基金¶
In [165]:


fund_price_pivot1 = fund_price_pivot.dropna(axis = 1)

5.5查询基金累计净值透视表中基金数量¶
In [166]:


len(fund_price_pivot1.iloc[0])

Out[166]:


3386

5.6展示基金累计净值透视表¶
In [167]:


fund_price_pivot1.head()

Out[167]:

          code
day  000001 000003 000004 000005 000008 000009 000010 000011 000013 000014 000015 000016 000017 000020 000021 000024 000025 000028 000029 000030 000031 000032 000033 000039 000041 000042 000043 000044 000045 000046 000047 000048 000049 000051 000053 000054 000055 000056 000057 000058 ... 740101 740601 740602 750001 750002 750003 750005 750006 750007 762001 770001 960000 960001 960002 960003 960004 960005 960006 960007 960008 960010 960011 960012 960016 960018 960020 960021 960022 960023 960026 960027 960028 960029 968000 968001 968003 968004 968006 968010 968011

2016-10-11 5.904695 1.008778 1.017008 1.204291 1.7476 1.151615 1.161591 18.248833 1.161981 1.187 1.216130 1.199036 2.188291 1.771 3.423294 1.328 1.324 1.370776 1.537943 1.873511 1.887 1.237 1.218 1.9671 0.784 1.408 1.338 1.224 1.398820 1.3771 1.399576 1.386085 0.911 1.058 1.405342 1.2823 0.267144 1.686 1.373 1.175065 ... 1.414816 1.148507 1.158393 1.720773 1.351953 1.327954 1.562 1.149311 1.159515 3.002851 1.532830 0.9945 0.821 1.208 1.0304 1.801 0.8435 1.781 2.275 2.228 1.0547 0.4616 1.3837 4.4177 2.389 1.435 1.030 0.9884 1.0219 0.975 1.014 1.7990 1.23522 10.83 10.671567 10.58 10.434954 1.1754 12.10 12.02
2016-10-12 5.899515 1.007602 1.014657 1.204291 1.7467 1.151697 1.161682 18.188326 1.162072 1.187 1.216130 1.199036 2.177681 1.760 3.413008 1.329 1.324 1.370776 1.548751 1.873511 1.888 1.237 1.219 1.9654 0.782 1.408 1.346 1.227 1.398820 1.3771 1.399576 1.386085 0.912 1.056 1.405544 1.2828 0.266782 1.686 1.372 1.175065 ... 1.413743 1.148585 1.158480 1.718510 1.351953 1.327954 1.562 1.149385 1.159597 3.003962 1.532479 0.9934 0.824 1.207 1.0288 1.799 0.8431 1.780 2.266 2.230 1.0498 0.4610 1.3843 4.3983 2.395 1.430 1.026 0.9864 1.0182 0.972 1.012 1.7937 1.23420 10.82 10.661176 10.57 10.424783 1.1621 12.02 11.94
2016-10-13 5.920234 1.008778 1.015832 1.205461 1.7475 1.151778 1.161771 18.190055 1.162161 1.188 1.217156 1.200063 2.172376 1.764 3.421237 1.329 1.325 1.370776 1.558478 1.873511 1.884 1.238 1.219 1.9734 0.775 1.410 1.342 1.223 1.399886 1.3771 1.400735 1.386085 0.905 1.057 1.406464 1.2833 0.265574 1.685 1.368 1.176236 ... 1.416963 1.148662 1.158565 1.727561 1.353121 1.329123 1.562 1.149457 1.159678 3.005390 1.532479 0.9926 0.827 1.211 1.0299 1.800 0.8439 1.782 2.270 2.232 1.0507 0.4600 1.3831 4.4043 2.411 1.431 1.023 0.9859 1.0165 0.975 1.012 1.7876 1.23522 10.81 10.661176 10.56 10.414612 1.1484 11.96 11.88
2016-10-14 5.915054 1.007602 1.014657 1.205461 1.7437 1.151859 1.161860 18.158937 1.162250 1.188 1.218182 1.201089 2.164419 1.749 3.415065 1.330 1.325 1.370776 1.557397 1.873511 1.889 1.238 1.219 1.9564 0.779 1.412 1.338 1.222 1.399886 1.3771 1.401895 1.387246 0.902 1.058 1.407675 1.2835 0.266178 1.685 1.362 1.175065 ... 1.420184 1.148739 1.158650 1.709459 1.353121 1.329123 1.563 1.149528 1.159758 2.999517 1.532479 0.9928 0.826 1.208 1.0234 1.802 0.8442 1.784 2.267 2.231 1.0489 0.4612 1.3851 4.4407 2.388 1.422 1.024 0.9882 1.0138 0.980 1.013 1.7816 1.23726 10.83 10.671567 10.57 10.424783 1.1469 11.98 11.89
2016-10-15 5.915054 1.007602 1.014657 1.205461 1.7437 1.151940 1.161860 18.158937 1.162250 1.188 1.218182 1.201089 2.164419 1.749 3.415065 1.330 1.325 1.370776 1.557397 1.873511 1.889 1.238 1.219 1.9564 0.779 1.412 1.338 1.222 1.399886 1.3771 1.401895 1.387246 0.902 1.058 1.407675 1.2835 0.266178 1.685 1.362 1.175065 ... 1.420184 1.148739 1.158650 1.709459 1.353121 1.329123 1.563 1.149528 1.159758 2.999517 1.532479 0.9928 0.826 1.208 1.0234 1.802 0.8442 1.784 2.267 2.231 1.0489 0.4612 1.3851 4.4407 2.388 1.422 1.024 0.9882 1.0138 0.980 1.013 1.7816 1.23726 10.83 10.671567 10.57 10.424783 1.1469 11.98 11.89

6.计算基金日收益率¶
6.1计算基金日收益率¶
In [168]:


fund_daily_return = fund_price_pivot1.pct_change()
fund_daily_return = fund_daily_return.dropna(how = 'all')

6.2剔除日收益率大于20%的异常基金¶
In [169]:


daily_return_nan = fund_daily_return.applymap(lambda x: np.nan if x > 0.2 else x)
daily_return_nonan = daily_return_nan.dropna(axis = 1)

6.3查询日收益率小于20%的基金数量¶
In [170]:


len(daily_return_nonan.iloc[0])

Out[170]:


3367

6.4展示基金日收益率¶
In [171]:


daily_return_nonan.head()

Out[171]:

         code
day 000001 000003 000004 000005 000008 000009 000010 000011 000013 000014 000015 000016 000017 000020 000021 000024 000025 000028 000029 000030 000031 000032 000033 000039 000041 000042 000043 000044 000045 000046 000047 000048 000049 000051 000053 000054 000055 000056 000057 000058 ... 740101 740601 740602 750001 750002 750003 750005 750006 750007 762001 770001 960000 960001 960002 960003 960004 960005 960006 960007 960008 960010 960011 960012 960016 960018 960020 960021 960022 960023 960026 960027 960028 960029 968000 968001 968003 968004 968006 968010 968011

2016-10-12 -0.000877 -0.001166 -0.002312 0.000000 -0.000515 0.000071 0.000078 -0.003316 0.000078 0.000000 0.000000 0.000000 -0.004849 -0.006211 -0.003005 0.000753 0.000000 0.0 0.007028 0.0 0.000530 0.000000 0.000821 -0.000864 -0.002551 0.000000 0.005979 0.002451 0.000000 0.0 0.000000 0.000000 0.001098 -0.001890 0.000144 0.000390 -0.001355 0.000000 -0.000728 0.000000 ... -0.000758 0.000068 0.000075 -0.001315 0.000000 0.00000 0.00000 0.000064 0.000071 0.000370 -0.000229 -0.001106 0.003654 -0.000828 -0.001553 -0.001110 -0.000474 -0.000561 -0.003956 0.000898 -0.004646 -0.001300 0.000434 -0.004391 0.002512 -0.003484 -0.003883 -0.002023 -0.003621 -0.003077 -0.001972 -0.002946 -0.000826 -0.000923 -0.000974 -0.000945 -0.000975 -0.011315 -0.006612 -0.006656
2016-10-13 0.003512 0.001167 0.001158 0.000972 0.000458 0.000070 0.000077 0.000095 0.000077 0.000842 0.000844 0.000857 -0.002436 0.002273 0.002411 0.000000 0.000755 0.0 0.006281 0.0 -0.002119 0.000808 0.000000 0.004070 -0.008951 0.001420 -0.002972 -0.003260 0.000762 0.0 0.000828 0.000000 -0.007675 0.000947 0.000655 0.000390 -0.004528 -0.000593 -0.002915 0.000997 ... 0.002278 0.000067 0.000073 0.005267 0.000864 0.00088 0.00000 0.000063 0.000070 0.000475 0.000000 -0.000805 0.003641 0.003314 0.001069 0.000556 0.000949 0.001124 0.001765 0.000897 0.000857 -0.002169 -0.000867 0.001364 0.006681 0.000699 -0.002924 -0.000507 -0.001670 0.003086 0.000000 -0.003401 0.000826 -0.000924 0.000000 -0.000946 -0.000976 -0.011789 -0.004992 -0.005025
2016-10-14 -0.000875 -0.001166 -0.001157 0.000000 -0.002175 0.000070 0.000077 -0.001711 0.000077 0.000000 0.000843 0.000855 -0.003663 -0.008503 -0.001804 0.000752 0.000000 0.0 -0.000694 0.0 0.002654 0.000000 0.000000 -0.008615 0.005161 0.001418 -0.002981 -0.000818 0.000000 0.0 0.000828 0.000838 -0.003315 0.000946 0.000861 0.000156 0.002274 0.000000 -0.004386 -0.000996 ... 0.002273 0.000067 0.000073 -0.010478 0.000000 0.00000 0.00064 0.000062 0.000069 -0.001954 0.000000 0.000201 -0.001209 -0.002477 -0.006311 0.001111 0.000355 0.001122 -0.001322 -0.000448 -0.001713 0.002609 0.001446 0.008265 -0.009540 -0.006289 0.000978 0.002333 -0.002656 0.005128 0.000988 -0.003356 0.001652 0.001850 0.000975 0.000947 0.000977 -0.001306 0.001672 0.000842
2016-10-15 0.000000 0.000000 0.000000 0.000000 0.000000 0.000070 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.0 0.000000 0.0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ... 0.000000 0.000000 0.000000 0.000000 0.000000 0.00000 0.00000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
2016-10-16 0.000000 0.000000 0.000000 0.000000 0.000000 0.000070 0.000153 0.000000 0.000153 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.0 0.000000 0.0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ... 0.000000 0.000134 0.000148 0.000000 0.000000 0.00000 0.00000 0.000121 0.000134 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000


7.计算并筛选低相关性基金¶
7.1计算并筛选低相关性基金¶
In [172]:


df_columns = daily_return_nonan.columns.tolist()
hs300_corr = pd.DataFrame(0, index = df_columns, columns = ['hs300_corr'])
for label, item in daily_return_nonan.iteritems():
hs300_corr.loc[label, 'hs300_corr'] = daily_return_nonan['159919'].corr(item)
hs300_corr = hs300_corr.sort_values(by = ['hs300_corr'])
hs300_corr1 = hs300_corr[(hs300_corr['hs300_corr'] <= 0.2) & (hs300_corr['hs300_corr'] >= 0)]
hs300_corr1_ins = hs300_corr1.index.tolist()
hs300_corr1_ins.append('159919')
hs300_corr_df = daily_return_nonan[hs300_corr1_ins]
corr_high = hs300_corr_df.corr().applymap(lambda x: np.nan if x > 0.2 else x).isnull()
col_all = corr_high.columns.tolist()
del_col = []
i = 0
while i < len(col_all) - 1:
ex_index = corr_high.iloc[:,i][i+1:].index[np.where(corr_high.iloc[:,i][i+1:])].tolist()
for var in ex_index:
col_all.remove(var)
corr_high = corr_high.loc[col_all, col_all]
i += 1
col_all_corr = hs300_corr_df[col_all].corr()
hs300_nocorr_df = hs300_corr_df[col_all]
ins = hs300_nocorr_df.columns.tolist()
name = []
for value in ins:
is_named = False
for index1, row1 in fund_info_0.iterrows():
if value == row1['main_code']:
name.append(row1['name'])
is_named = True
break
hs300_nocorr_df.columns = name

7.2展示低相关性基金的日收益率¶
In [173]:


hs300_nocorr_df.head()

Out[173]:

day 易方达天天R 华安黄金易ETF联接C 上投天添盈B 新华纯债添利债券发起 工银灵活配置混合B 汇添富纯债LOF 华夏恒利3个月定开债券 华泰量化绝对 富国天时D 广发理财年年红 华安票息C 工银稳健成长混合H 融通岁岁添利B 国联安增利债券 工银核心价值混合H 沪深300ETF
2016-10-12 0.000078 -0.002463 0.000051 0.0 -0.000887 0.000000 0.000000 0.000098 0.000046 0.0 0.000947 -0.003621 0.000000 0.000000 -0.004646 -0.001990
2016-10-13 0.000077 0.001975 0.000058 0.0 0.000000 0.000000 0.000000 0.000196 0.000046 0.0 0.000000 -0.001670 0.000914 0.000000 0.000857 0.000786
2016-10-14 0.000077 -0.001380 0.000059 0.0 0.000000 0.000672 0.000965 0.000490 0.000048 0.0 -0.001892 -0.002656 0.000000 0.000744 -0.001713 0.000954
2016-10-15 0.000000 0.000000 0.000000 0.0 0.000000 0.000000 0.000000 0.000000 0.000000 0.0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
2016-10-16 0.000153 0.000000 0.000113 0.0 0.000000 0.000000 0.000000 0.000000 0.000091 0.0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

7.3展示低相关性基金的相关系数¶
In [174]:


col_all_corr

Out[174]:

           code
code 000013 000217 000856 519152 001428 164703 002552 001073 000863 270043 002429 960023 161619 253020 960010 159919

000013 1.000000 -0.005544 -0.013574 0.017853 0.000730 0.002062 0.011783 -0.000934 0.011466 -0.007135 -0.010136 0.004817 0.003147 -0.008214 -0.003716 0.000006
000217 -0.005544 1.000000 0.000171 0.050870 0.009196 0.028532 0.063671 0.004718 -0.018663 -0.008593 -0.047860 0.010465 0.041407 0.046659 -0.021267 0.000285
000856 -0.013574 0.000171 1.000000 0.005899 -0.000680 -0.000497 0.001417 0.000953 0.000063 0.000113 -0.000467 0.000068 0.000696 -0.000791 -0.000761 0.000368
519152 0.017853 0.050870 0.005899 1.000000 0.005357 -0.028618 0.062086 0.046239 -0.006507 0.008920 0.016492 -0.040671 0.053378 0.046880 -0.011642 0.001671
001428 0.000730 0.009196 -0.000680 0.005357 1.000000 -0.000173 0.002084 -0.000041 0.000153 0.000684 -0.008877 0.022740 0.000889 0.000223 0.071225 0.004625
164703 0.002062 0.028532 -0.000497 -0.028618 -0.000173 1.000000 0.013711 -0.009758 -0.001890 0.001613 -0.028334 0.014445 0.009814 0.015928 -0.058509 0.004641
002552 0.011783 0.063671 0.001417 0.062086 0.002084 0.013711 1.000000 -0.013879 0.003249 0.037259 0.017825 -0.016153 0.062216 0.071207 0.003867 0.012899
001073 -0.000934 0.004718 0.000953 0.046239 -0.000041 -0.009758 -0.013879 1.000000 0.001706 0.014764 -0.006398 -0.006135 -0.003865 0.000338 0.005417 0.021970
000863 0.011466 -0.018663 0.000063 -0.006507 0.000153 -0.001890 0.003249 0.001706 1.000000 0.004900 -0.012068 0.000033 -0.002788 -0.003382 0.000113 0.022139
270043 -0.007135 -0.008593 0.000113 0.008920 0.000684 0.001613 0.037259 0.014764 0.004900 1.000000 -0.012416 0.000041 -0.004624 0.003047 0.000538 0.026187
002429 -0.010136 -0.047860 -0.000467 0.016492 -0.008877 -0.028334 0.017825 -0.006398 -0.012068 -0.012416 1.000000 0.004823 0.005055 0.015702 0.027882 0.031484
960023 0.004817 0.010465 0.000068 -0.040671 0.022740 0.014445 -0.016153 -0.006135 0.000033 0.000041 0.004823 1.000000 0.005414 0.006924 0.032634 0.050141
161619 0.003147 0.041407 0.000696 0.053378 0.000889 0.009814 0.062216 -0.003865 -0.002788 -0.004624 0.005055 0.005414 1.000000 0.076854 0.026716 0.051711
253020 -0.008214 0.046659 -0.000791 0.046880 0.000223 0.015928 0.071207 0.000338 -0.003382 0.003047 0.015702 0.006924 0.076854 1.000000 0.017754 0.063169
960010 -0.003716 -0.021267 -0.000761 -0.011642 0.071225 -0.058509 0.003867 0.005417 0.000113 0.000538 0.027882 0.032634 0.026716 0.017754 1.000000 0.096514
159919 0.000006 0.000285 0.000368 0.001671 0.004625 0.004641 0.012899 0.021970 0.022139 0.026187 0.031484 0.050141 0.051711 0.063169 0.096514 1.000000

7.4展示低相关性基金的相关系数热力图¶
In [175]:


sns.heatmap(col_all_corr, cmap = "YlGnBu")

Out[175]:


<matplotlib.axes._subplots.AxesSubplot at 0x7f0009e61a20>


8计算低相关性基金的累计收益率¶
8.1计算低相关性基金的累计收益率¶
In [176]:


df_cumu_return = hs300_nocorr_df.copy()
lenth = len(hs300_nocorr_df.iloc[0])
for i in range(lenth):
df_cumu_return.iloc[:, i] = (hs300_nocorr_df.iloc[:, i] + 1).cumprod() - 1

8.2展示低相关性基金累计收益率¶
In [177]:


df_cumu_return.tail()

Out[177]:

day 易方达天天R 华安黄金易ETF联接C 上投天添盈B 新华纯债添利债券发起 工银灵活配置混合B 汇添富纯债LOF 华夏恒利3个月定开债券 华泰量化绝对 富国天时D 广发理财年年红 华安票息C 工银稳健成长混合H 融通岁岁添利B 国联安增利债券 工银核心价值混合H 沪深300ETF

2022-11-03 0.196230 0.368473 -0.048512 0.234380 -0.000887 -0.178061 0.205514 0.224756 0.148520 0.090025 0.070076 -0.021431 0.231841 0.042336 -0.051863 0.210734
2022-11-04 0.196282 0.370542 -0.048512 0.233938 -0.000887 -0.178253 0.205070 0.220852 0.148540 0.090025 0.071023 -0.021431 0.230747 0.043080 -0.051863 0.250325
2022-11-05 0.196282 0.370542 -0.048512 0.233938 -0.000887 -0.178253 0.205070 0.220852 0.148540 0.090025 0.071023 -0.021431 0.230747 0.043080 -0.051863 0.250325
2022-11-06 0.196387 0.370542 -0.048512 0.233938 -0.000887 -0.178253 0.205070 0.220852 0.148579 0.090025 0.071023 -0.021431 0.230747 0.043080 -0.051863 0.250325
2022-11-07 0.196440 0.376847 -0.048512 0.234048 -0.000887 -0.178349 0.204848 0.222696 0.148562 0.090025 0.071023 -0.021431 0.230747 0.043080 -0.051863 0.252934

8.3删除累计收益率为负数的基金¶
In [182]:


df_cumu_return_last = df_cumu_return.iloc[-1,:]
columns_del = []
for index, value in df_cumu_return_last.items():
if value < 0:
columns_del.append(index)
df_cumu_return0 = df_cumu_return.drop(columns_del, axis = 1)
low_corr_fund_name = df_cumu_return0.columns.tolist()

8.4绘制低相关性基金累计收益஍#29575;图¶
In [183]:


plt.figure(dpi = 100, figsize = (20, 8))
sns.lineplot(data = df_cumu_return0, dashes = False)
plt.show()
plt.tight_layout()

<Figure size 432x288 with 0 Axes>

9低相关性基金等权重组合¶
9.1计算低相关性基金等权重组合的累计收益率¶
In [184]:


df_cumu_return0['低相关性基金等权重组合'] = df_cumu_return0.mean(axis = 1)

9.2获取低相关性基金组合的底层资产类别¶
In [187]:


fund_count = len(low_corr_fund_name)
j = 0
for item in low_corr_fund_name:
one_low_corr_fund_info = fund_info_0[fund_info_0['name'] == item][['name', 'underlying_asset_type']]
if j == 0:
low_corr_fund_info = one_low_corr_fund_info.copy()
else:
low_corr_fund_info = pd.concat([low_corr_fund_info, one_low_corr_fund_info])
j += 1

9.3展示低相关性基金组合的底层资产类别¶
In [188]:


low_corr_fund_info

Out[188]:

name underlying_asset_type
2691 易方达天天R 货币型
2732 华安黄金易ETF联接C 基金型
2361 新华纯债添利债券发起 债券型
1206 华夏恒利3个月定开债券 债券型
547 华泰量化绝对 混合型
2852 富国天时D 货币型
2040 广发理财年年红 债券型
3167 华安票息C 债券型
3451 融通岁岁添利B 债券型
2000 国联安增利债券 债券型
1689 沪深300ETF 股票型

9.4计算低相关性基金组合等权重¶
In [190]:


low_corr_fund_info['weight'] = 1 / len(low_corr_fund_info)

9.5展示低相关性基金组合权重¶
In [196]:


low_corr_fund_weight = low_corr_fund_info[['name', 'weight']]
low_corr_fund_weight = low_corr_fund_weight.set_index('name')
low_corr_fund_weight['weight'].plot.pie()
plt.show()

9.6展示低相关性基金组合的底层资产类别权重¶
In [200]:


low_corr_fund_asset_class_weight = low_corr_fund_info[['underlying_asset_type', 'weight']]
low_corr_fund_asset_class_weight1 = low_corr_fund_asset_class_weight.groupby('underlying_asset_type').sum()
plt.pie(x = low_corr_fund_asset_class_weight1['weight'],
labels = low_corr_fund_asset_class_weight1.index, autopct = '%.2f%%')
plt.show()

9.2绘制低相关性基金等权重组合的累计收益率图¶
In [181]:


df_cumu_return1 = df_cumu_return0[['低相关性基金等权重组合', '沪深300ETF']]
plt.figure(dpi = 100, figsize = (20, 8))
sns.lineplot(data = df_cumu_return1)
plt.show()
plt.tight_layout()

<Figure size 432x288 with 0 Axes>

ブログに戻る

コメントを残す