Math Helpers

quant.common.math_helpers.cal_mdd(netvalue, compound=True)[源代码]

计算最大回撤

Parameters:
netvalue: pd.Series

净值序列

compound: bool, optional

是否为复利制,如果为真,回撤计算方法为 \(\frac{N_j}{N_i}-1\) (j > i); 如果为假,回撤计算方法为 \(N_j-N_i\) (j > i)

Returns:
最大回撤值(正值)

Examples

In [1]: import pandas as pd

In [2]: from quant.common.math_helpers import cal_mdd

In [3]: series = pd.Series([1.0, 1.1, 1.11, 1.04, 1.02, 1.08])

In [4]: cal_mdd(series)
Out[4]: 0.08108108108108114

In [5]: 1.02 / 1.11 - 1
Out[5]: -0.08108108108108114
quant.common.math_helpers.get_ic(table1, table2, method='spearman')[源代码]

求两组数据之间的IC score

Parameters:
table1, table2: pd.DataFrame

要计算IC的两个数据框

method: {‘spearman’, ‘pearson’, ‘kendall’}
  • pearson : standard correlation coefficient
  • kendall : Kendall Tau correlation coefficient
  • spearman : Spearman rank correlation
Returns:
pd.Series

每期的相关系数,求平均可得IC分数。

参见

pd.Series.corr

Examples

In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: from quant.common.math_helpers import get_ic

In [4]: df1 = pd.DataFrame(np.random.randn(10, 10))

In [5]: df2 = pd.DataFrame(np.random.randn(10, 10))

In [6]: get_ic(df1, df2)
Out[6]: 
0    0.103030
1   -0.151515
2   -0.090909
3    0.212121
4    0.333333
5    0.418182
6    0.503030
7    0.442424
8   -0.115152
9    0.151515
dtype: float64
quant.common.math_helpers.get_factor_exposure(position, factor_value, benchmark=None)[源代码]

计算持仓的因子暴露

Parameters:
position: pd.DataFrame

每期的持仓

factor_value: pd.DataFrame

每期的因子值

benchmark: str

要减去的指数的暴露,为None则不减

quant.common.math_helpers.exponential_decay_weight(halflife, truncate_length, reverse=True)[源代码]

产生截断的指数衰减权重

Parameters:
halflife: int

半衰期

truncate_length: int

截断长度

reverse: bool

是否反转序列。如果为False则为升序,如果为True则为降序