股票多因子选股模型 —— 数据去极值

data_extreme

#为什么要做去极值的工作(Why)


在做回归分析的时候,因为过大或过小的数据可能会影响到分析结果,离群值会严重影响因子和收益率之间的相关性估计结果,因此需要对那些离群值进行处理

## 有哪些去极值的方法(What)


根据不同的距离判断标准,去极值有以下三种方法:
* MAD法
* 3𝜎法
* 百分位法

## 去极值怎么做(How)


一般去极值的处理方法就是先确定该项指标的上下限,然后找出超出限值的数据,并将它们的值统统变为限值。如下图:

   

 

1.去极值 — MAD


去极值-MAD法的距离判断标准是因子与中位数之间的距离,因此MAD法又称为绝对值差中位数法

步骤:
1. 需要找出所有因子的中位数,记作𝐹_𝑚𝑒𝑑𝑖𝑎𝑛

2. 求每个因子与中位数的绝对偏差值,再求因子绝对偏差值的中位数

3. 根据因子的中位数与绝对偏差值的中位数确定阈值范围,对超出阈值范围的因子值做调整

4. 令超出阈值范围的因子值等于阈值,处在阈值范围内的因子的值保持不变

#代码实现
from atrader import *
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# 获取因子数据
factor = get_factor_by_factor(factor='PB', target_list=list(get_code_list('hs300').code), begin_date='2019-01-01', end_date='2019-03-01')
factors = factor.set_index('date').T

# MAD:中位数去极值
def extreme_MAD(dt,n):
    median = dt.quantile(0.5)   # 找出中位数
    new_median = (abs((dt - median)).quantile(0.5))   # 偏差值的中位数
    dt_up = median + n*new_median    # 上限
    dt_down = median - n*new_median  # 下限
    return dt.clip(dt_down, dt_up, axis=1)    # 超出上下限的值,赋值为上下限

ex_MAD = extreme_MAD(factors,5.2)

 

#去极值前
factors.tail()

#去极值后
ex_MAD.tail()

 

# 核密度分布画图
fig = plt.figure()
factors.iloc[:,-1].plot(kind = 'kde',label='PB' )
extreme_MAD(factors,5.2).iloc[:,-1].plot(kind = 'kde',label = 'MAD')
plt.legend()
plt.show()

### conclude:使用MAD去极值后,因子数据的取值范围明显缩小了

 

2.去极值 — 3𝜎法


去极值-3𝜎法使用标准差来设置阈值范围

步骤:
1. 先计算出因子的平均值𝐹_𝑚𝑒𝑎𝑛与标准差𝜎

2. 确定阈值参数 𝑛,n通常默认为3,最后对超出范围 [𝐹_𝑚𝑒𝑎𝑛−𝑛𝜎,  𝐹_𝑚𝑒𝑎𝑛+𝑛𝜎]  的因子值做调整

3. 令超出阈值范围的因子值等于阈值,处在阈值范围内的因子的值保持不变

# 3sigma 去极值
def extreme_3sigma(dt,n=3):
    mean = dt.mean()           # 截面数据均值
    std = dt.std()             # 截面数据标准差
    dt_up = mean + n*std       # 上限
    dt_down = mean - n*std     # 下限
    return dt.clip(dt_down, dt_up, axis=1)   # 超出上下限的值,赋值为上下限

ex_3 = extreme_3sigma(factors)

# 核密度分布画图
fig = plt.figure()
factors.iloc[:,-1].plot(kind = 'kde',label='PB' )
extreme_3sigma(factors).iloc[:,-1].plot(kind = 'kde',label = '3sigma')
plt.legend()
plt.show()

3.去极值 — 百分位法


百分位法利用所有因子值的某个百分位作为因子的合理范围,然后超出范围的因子值按照上下限值处理,一般情况下,合理范围设为2.5%-97.5%之间

步骤:

1. 找到因子值的97.5%和2.5%分位数

2. 对大于97.5%分位数的因子值,或小于2.5%分位数的因子值进行调整

def extreme_percentile(dt,min=0.025,max=0.975):
    p = dt.quantile([min,max])                    # 得到上下限的值
    return dt.clip(p.iloc[0],p.iloc[1], axis=1)  # 超出上下限的值,赋值为上下限

ex_p = extreme_percentile(factors)

# 核密度分布画图
fig = plt.figure()
factors.iloc[:,-1].plot(kind = 'kde',label='PB' )
extreme_percentile(factors).iloc[:,-1].plot(kind = 'kde',label = 'percen')
plt.legend()
plt.show()

4. 三种方法比较

# 核密度分布画图
fig = plt.figure()
factors.iloc[:,-1].plot(kind = 'kde',label='PB' )
extreme_MAD(factors,5.2).iloc[:,-1].plot(kind = 'kde',label = 'MAD')
extreme_3sigma(factors).iloc[:,-1].plot(kind = 'kde',label = '3sigma')
extreme_percentile(factors).iloc[:,-1].plot(kind = 'kde',label = 'percen')
plt.legend()
plt.show()

### MAD法处理后的因子值范围最小,3sigma法随后,百分位法最大。这个结果与参数的设置有关
### 在实际运用中,投资者可以从三种方法中任意选择一种对因子数据进行去极值处理。

  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值