用多因子策略构建强大的加密资产投资组合 #大类因子分析:因子合成篇#
作者:LUCIDA & FALCON
书接上回,关于《用多因子模型构建强大的加密资产投资组合》系列文章中,我们已经发布了三篇:《理论基础篇》、《数据预处理篇》、《因子有效性检验篇》。
前三篇分别解释了多因子策略的理论与单因子测试的步骤。
一、因子相关性检验的原因:多重共线性
我们通过单因子测试部分筛选出一批有效因子,但以上因子不能直接入库。因子本身可以根据具体的经济含义进行大类划分,同类型的因子间存在较强的相关性,若不经相关性筛选直接入库,根据不同因子进行多元线性回归求预期收益率时,会出现多重共线性问题。计量经济学中,多重共线性是指回归模型中的一些或全部解释变量存在“完全”或准确的线性关系(各变量间高度相关)。
因此,有效因子筛选出后,首先需要根据大类对因子的相关性进行T检验,对于相关性较高的因子,要么舍弃显著性较低的因子,要么进行因子合成。
多重共线性的数学解释如下:
多重共线性导致的后果:
1.完全共线性下参数估计量不存在
2.近似共线性下OLS估计量非有效
二、步骤一:同类型因子的相关性检验
检验新求出的因子与已入库因子的相关性。通常来说,有两类数据求相关性:
1.根据所有token在回测期间的因子值求相关
2.根据所有token在回测期间的因子超额收益值求相关
三、步骤二:因子取舍、因子合成
对于相关性较高的因子集合,可以采取两种方式处理:
(1)因子取舍
根据因子本身的ICIR值、收益率、换手率、Sharpe 比率,挑选某维度下最有效的因子进行保留,删除其他因子。
(2)因子合成
对因子集合中的因子进行合成,截面上尽可能多的保留有效信息
假设当前有3个待处理的因子矩阵:
synthesis = pd.concat([a,b,c],axis = 1)
synthesis
a b c
BTC.BN 0.184865 -0.013253 -0.001557
ETH.BN 0.185691 0.022708 0.031793
BNB.BN 0.242072 -0.180952 -0.067430
LTC.BN 0.275923 -0.125712 -0.049596
AAVE.BN 0.204443 -0.000819 -0.006550
... ... ... ...
SOC.BN 0.231638 -0.095946 -0.049495
AVAX.BN 0.204714 -0.079707 -0.041806
DAO.BN 0.194990 0.022095 -0.011764
ETC.BN 0.184236 -0.021909 -0.013325
TRX.BN 0.175118 -0.055077 -0.039513
2.1 等权加权
各因子权重相等(w=1/因子个数),综合因子=各因子值加总求平均。
Eg.动量类因子,一个月收益率、两个月收益率、三个月收益率、六个月收益率、十二个月收益率,这六个因子的因子载荷各占1/6的权重,合成新的动量因子载荷,然后再重新进行标准化处理。
synthesis1 = synthesis.mean(axis=1) # 按行求均值
2.2 历史IC加权、历史ICIR、历史收益加权
用回测期的IC值(ICIR值、历史收益值)对因子进行加权。过去有很多期,每一期都有一个IC值,所以用它们的均值作为因子的权重。通常使用回测期IC的均值(算数平均值)作为权重。
# 权重归一化(后文中的因子加权方式也基本都需要进行权重归一化)
w_IC = ic.mean() / ic.mean().sum()
w_ICIR = icir.mean() / icir.mean().sum()
w_Ret = Return.mean() / Return.mean().sum()
synthesis2 = (synthesis * w_IC).sum(axis=1)
synthesis2 = (synthesis * w_ICIR).sum(axis=1)
synthesis2 = (synthesis * w_Ret).sum(axis=1)
2.3历史IC半衰加权、历史ICIR半衰加权
2.1与2.2都是计算算数平均值,回测期的每一次IC、ICIR对于因子的作用被默认为相同。
但现实中,回测期的每一期对于当期的影响程度不完全相同,存在时间上的衰减。越接近当前期的时期,影响越大,越远影响越小。在此原理,求IC权重前首先定义一个半衰权重,距离当期越近的权重值越大、越远权重越小。
半衰权重数学推导
2.4 最大化ICIR加权
通过求解方程,计算最优因子权重w使得ICIR最大化
协方差矩阵的估计问题:协方差矩阵用于衡量不同资产之间的关联性。统计学中常以样本协方差矩阵代替总体协方差矩阵,但在样本量不足时,样本协方差矩阵与总体协方差矩阵的差异会很大。所以有人提出了压缩估计的方法,原理是使估计协方差矩阵与实际协方差矩阵之间的均方误差最小
方式:
1.样本协方差矩阵
<span class="token comment" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;"># 最大化ICIR加权(样本协方差)</span>
ic_cov <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span> np<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>array<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span>ic<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>cov<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span>
inv_ic_cov <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span> np<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>linalg<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>inv<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span>ic_cov<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span>
ic_vector <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span> np<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>mat<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span>ic<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>mean<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span>
w <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span> inv_ic_cov <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">*</span> ic_vector<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>T
w <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span> w <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">/</span> w<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span><span class="token builtin" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">sum</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span>
synthesis4 <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span> <span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span>synthesis <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">*</span> pd<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>DataFrame<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span>w<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">,</span>index<span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span>synthesis<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>columns<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">[</span><span class="token number" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">0</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">]</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span><span class="token builtin" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">sum</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span>axis<span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span><span class="token number" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">1</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span>
2.Ledoit-Wolf收缩:引入一个缩小系数,将原始的协方差矩阵与单位矩阵进行混合,以减少噪音的影响。
<span class="token comment" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;"># 最大化ICIR加权(Ledoit-Wolf压缩估计协方差)</span>
<span class="token keyword" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">from</span> sklearn<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>covariance <span class="token keyword" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">import</span> LedoitWolf
model<span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span>LedoitWolf<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span>
model<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>fit<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span>ic<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span>
ic_cov_lw <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span> model<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>covariance_
inv_ic_cov <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span> np<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>linalg<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>inv<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span>ic_cov_lw<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span>
ic_vector <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span> np<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>mat<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span>ic<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>mean<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span>
w <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span> inv_ic_cov<span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">*</span>ic_vector<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>T
w <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span> w<span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">/</span>w<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span><span class="token builtin" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">sum</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span>
synthesis4 <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span> <span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span>synthesis <span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">*</span> pd<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>DataFrame<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span>w<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">,</span>index<span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span>synthesis<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span>columns<span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">[</span><span class="token number" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">0</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">]</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">.</span><span class="token builtin" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">sum</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">(</span>axis<span class="token operator" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">=</span><span class="token number" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">1</span><span class="token punctuation" style="font-size: inherit; font-family: PingFang SC,Helvetica Neue,Helvetica,Arial,Hiragino Sans GB,Heiti SC,Microsoft YaHei,WenQuanYi Micro Hei,sans-serif;">)</span>
3.Oracle近似收缩:对Ledoit-Wolf收缩的改进,目标是通过对协方差矩阵进行调整,从而在样本大小较小的情况下更准确地估计真实的协方差矩阵。(编程实现与Ledoit-Wolf收缩同理)
2.5 主成分分析PCA
主成分分析(Principal Component Analysis,PCA)是一种用于降维和提取数据主要特征的统计方法。其目标是通过线性变换,将原始数据映射到一个新的坐标系,使得数据在新坐标系下的方差最大化。
具体而言,PCA首先找到数据中的主成分,也就是数据中方差最大的方向。然后,它找到与第一个主成分正交(无关)且具有最大方差的第二个主成分。这个过程一直重复,直到找到数据中所有的主成分。
# 主成分分析(PCA)
from sklearn.decomposition import PCA
model1 = PCA(n_components=1)
model1.fit(f)
w=model1.components_
w=w/w.sum()
weighted_factor=(f*pd.DataFrame(w,columns=f.columns).iloc[0]).sum(axis=1)