mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-11 14:56:55 +08:00
Format with black
This commit is contained in:
@@ -16,11 +16,11 @@ class RiskModel(BaseModel):
|
||||
A risk model is used to estimate the covariance matrix of stock returns.
|
||||
"""
|
||||
|
||||
MASK_NAN = 'mask'
|
||||
FILL_NAN = 'fill'
|
||||
IGNORE_NAN = 'ignore'
|
||||
MASK_NAN = "mask"
|
||||
FILL_NAN = "fill"
|
||||
IGNORE_NAN = "ignore"
|
||||
|
||||
def __init__(self, nan_option: str = 'ignore', assume_centered: bool = False, scale_return: bool = True):
|
||||
def __init__(self, nan_option: str = "ignore", assume_centered: bool = False, scale_return: bool = True):
|
||||
"""
|
||||
Args:
|
||||
nan_option (str): nan handling option (`ignore`/`mask`/`fill`)
|
||||
@@ -28,15 +28,19 @@ class RiskModel(BaseModel):
|
||||
scale_return (bool): whether scale returns as percentage
|
||||
"""
|
||||
# nan
|
||||
assert nan_option in [self.MASK_NAN, self.FILL_NAN, self.IGNORE_NAN], \
|
||||
f'`nan_option={nan_option}` is not supported'
|
||||
assert nan_option in [
|
||||
self.MASK_NAN,
|
||||
self.FILL_NAN,
|
||||
self.IGNORE_NAN,
|
||||
], f"`nan_option={nan_option}` is not supported"
|
||||
self.nan_option = nan_option
|
||||
|
||||
self.assume_centered = assume_centered
|
||||
self.scale_return = scale_return
|
||||
|
||||
def predict(self, X: Union[pd.Series, pd.DataFrame, np.ndarray],
|
||||
return_corr: bool = False, is_price: bool = True) -> Union[pd.DataFrame, np.ndarray]:
|
||||
def predict(
|
||||
self, X: Union[pd.Series, pd.DataFrame, np.ndarray], return_corr: bool = False, is_price: bool = True
|
||||
) -> Union[pd.DataFrame, np.ndarray]:
|
||||
"""
|
||||
Args:
|
||||
X (pd.Series, pd.DataFrame or np.ndarray): data from which to estimate the covariance,
|
||||
@@ -53,18 +57,18 @@ class RiskModel(BaseModel):
|
||||
else:
|
||||
if isinstance(X.index, pd.MultiIndex):
|
||||
if isinstance(X, pd.DataFrame):
|
||||
X = X.iloc[:, 0].unstack(level='instrument') # always use the first column
|
||||
X = X.iloc[:, 0].unstack(level="instrument") # always use the first column
|
||||
else:
|
||||
X = X.unstack(level='instrument')
|
||||
X = X.unstack(level="instrument")
|
||||
else:
|
||||
# X is 2D DataFrame
|
||||
pass
|
||||
columns = X.columns # will be used to restore dataframe
|
||||
columns = X.columns # will be used to restore dataframe
|
||||
X = X.values
|
||||
|
||||
# calculate pct_change
|
||||
if is_price:
|
||||
X = X[1:] / X[:-1] - 1 # NOTE: resulting `n - 1` rows
|
||||
X = X[1:] / X[:-1] - 1 # NOTE: resulting `n - 1` rows
|
||||
|
||||
# scale return
|
||||
if self.scale_return:
|
||||
@@ -106,7 +110,7 @@ class RiskModel(BaseModel):
|
||||
N = len(X)
|
||||
if isinstance(X, np.ma.MaskedArray):
|
||||
M = 1 - X.mask
|
||||
N = M.T.dot(M) # each pair has distinct number of samples
|
||||
N = M.T.dot(M) # each pair has distinct number of samples
|
||||
return xTx / N
|
||||
|
||||
def _preprocess(self, X: np.ndarray) -> Union[np.ndarray, np.ma.MaskedArray]:
|
||||
@@ -165,14 +169,14 @@ class ShrinkCovEstimator(RiskModel):
|
||||
[7] https://www.econ.uzh.ch/dam/jcr:ffffffff-935a-b0d6-0000-0000648dfc98/covMarket.m.zip
|
||||
"""
|
||||
|
||||
SHR_LW = 'lw'
|
||||
SHR_OAS = 'oas'
|
||||
SHR_LW = "lw"
|
||||
SHR_OAS = "oas"
|
||||
|
||||
TGT_CONST_VAR = 'const_var'
|
||||
TGT_CONST_CORR = 'const_corr'
|
||||
TGT_SINGLE_FACTOR = 'single_factor'
|
||||
TGT_CONST_VAR = "const_var"
|
||||
TGT_CONST_CORR = "const_corr"
|
||||
TGT_SINGLE_FACTOR = "single_factor"
|
||||
|
||||
def __init__(self, alpha: Union[str, float] = 0.0, target: Union[str, np.ndarray] = 'const_var', **kwargs):
|
||||
def __init__(self, alpha: Union[str, float] = 0.0, target: Union[str, np.ndarray] = "const_var", **kwargs):
|
||||
"""
|
||||
Args:
|
||||
alpha (str or float): shrinking parameter or estimator (`lw`/`oas`)
|
||||
@@ -183,24 +187,26 @@ class ShrinkCovEstimator(RiskModel):
|
||||
|
||||
# alpha
|
||||
if isinstance(alpha, str):
|
||||
assert alpha in [self.SHR_LW, self.SHR_OAS], \
|
||||
f'shrinking method `{alpha}` is not supported'
|
||||
assert alpha in [self.SHR_LW, self.SHR_OAS], f"shrinking method `{alpha}` is not supported"
|
||||
elif isinstance(alpha, (float, np.floating)):
|
||||
assert 0 <= alpha <= 1, 'alpha should be between [0, 1]'
|
||||
assert 0 <= alpha <= 1, "alpha should be between [0, 1]"
|
||||
else:
|
||||
raise TypeError('invalid argument type for `alpha`')
|
||||
raise TypeError("invalid argument type for `alpha`")
|
||||
self.alpha = alpha
|
||||
|
||||
# target
|
||||
if isinstance(target, str):
|
||||
assert target in [self.TGT_CONST_VAR, self.TGT_CONST_CORR, self.TGT_SINGLE_FACTOR], \
|
||||
f'shrinking target `{target} is not supported'
|
||||
assert target in [
|
||||
self.TGT_CONST_VAR,
|
||||
self.TGT_CONST_CORR,
|
||||
self.TGT_SINGLE_FACTOR,
|
||||
], f"shrinking target `{target} is not supported"
|
||||
elif isinstance(target, np.ndarray):
|
||||
pass
|
||||
else:
|
||||
raise TypeError('invalid argument type for `target`')
|
||||
raise TypeError("invalid argument type for `target`")
|
||||
if alpha == self.SHR_OAS and target != self.TGT_CONST_VAR:
|
||||
raise NotImplementedError('currently `oas` can only support `const_var` as target')
|
||||
raise NotImplementedError("currently `oas` can only support `const_var` as target")
|
||||
self.target = target
|
||||
|
||||
def _predict(self, X: np.ndarray) -> np.ndarray:
|
||||
@@ -215,7 +221,7 @@ class ShrinkCovEstimator(RiskModel):
|
||||
|
||||
# shrink covariance
|
||||
if alpha > 0:
|
||||
S *= (1 - alpha)
|
||||
S *= 1 - alpha
|
||||
F *= alpha
|
||||
S += F
|
||||
|
||||
@@ -292,8 +298,8 @@ class ShrinkCovEstimator(RiskModel):
|
||||
alpha = A / B
|
||||
where `n`, `p` are the dim of observations and variables respectively.
|
||||
"""
|
||||
trS2 = np.sum(S**2)
|
||||
tr2S = np.trace(S)**2
|
||||
trS2 = np.sum(S ** 2)
|
||||
tr2S = np.trace(S) ** 2
|
||||
|
||||
n, p = X.shape
|
||||
|
||||
@@ -310,10 +316,10 @@ class ShrinkCovEstimator(RiskModel):
|
||||
"""
|
||||
t, n = X.shape
|
||||
|
||||
y = X**2
|
||||
phi = np.sum(y.T.dot(y) / t - S**2)
|
||||
y = X ** 2
|
||||
phi = np.sum(y.T.dot(y) / t - S ** 2)
|
||||
|
||||
gamma = np.linalg.norm(S - F, 'fro')**2
|
||||
gamma = np.linalg.norm(S - F, "fro") ** 2
|
||||
|
||||
kappa = phi / gamma
|
||||
alpha = max(0, min(1, kappa / t))
|
||||
@@ -331,15 +337,15 @@ class ShrinkCovEstimator(RiskModel):
|
||||
sqrt_var = np.sqrt(var)
|
||||
r_bar = (np.sum(S / np.outer(sqrt_var, sqrt_var)) - n) / (n * (n - 1))
|
||||
|
||||
y = X**2
|
||||
phi_mat = y.T.dot(y) / t - S**2
|
||||
y = X ** 2
|
||||
phi_mat = y.T.dot(y) / t - S ** 2
|
||||
phi = np.sum(phi_mat)
|
||||
|
||||
theta_mat = (X**3).T.dot(X) / t - var[:, None] * S
|
||||
theta_mat = (X ** 3).T.dot(X) / t - var[:, None] * S
|
||||
np.fill_diagonal(theta_mat, 0)
|
||||
rho = np.sum(np.diag(phi_mat)) + r_bar * np.sum(np.outer(1 / sqrt_var, sqrt_var) * theta_mat)
|
||||
|
||||
gamma = np.linalg.norm(S - F, 'fro')**2
|
||||
gamma = np.linalg.norm(S - F, "fro") ** 2
|
||||
|
||||
kappa = (phi - rho) / gamma
|
||||
alpha = max(0, min(1, kappa / t))
|
||||
@@ -357,19 +363,21 @@ class ShrinkCovEstimator(RiskModel):
|
||||
cov_mkt = np.asarray(X.T.dot(X_mkt) / len(X))
|
||||
var_mkt = np.asarray(X_mkt.dot(X_mkt) / len(X))
|
||||
|
||||
y = X**2
|
||||
phi = np.sum(y.T.dot(y)) / t - np.sum(S**2)
|
||||
y = X ** 2
|
||||
phi = np.sum(y.T.dot(y)) / t - np.sum(S ** 2)
|
||||
|
||||
rdiag = np.sum(y**2) / t - np.sum(np.diag(S)**2)
|
||||
rdiag = np.sum(y ** 2) / t - np.sum(np.diag(S) ** 2)
|
||||
z = X * X_mkt[:, None]
|
||||
v1 = y.T.dot(z) / t - cov_mkt[:, None] * S
|
||||
roff1 = np.sum(v1 * cov_mkt[:, None].T) / var_mkt - np.sum(np.diag(v1) * cov_mkt) / var_mkt
|
||||
v3 = z.T.dot(z) / t - var_mkt * S
|
||||
roff3 = np.sum(v3 * np.outer(cov_mkt, cov_mkt)) / var_mkt**2 - np.sum(np.diag(v3) * cov_mkt**2) / var_mkt**2
|
||||
roff3 = (
|
||||
np.sum(v3 * np.outer(cov_mkt, cov_mkt)) / var_mkt ** 2 - np.sum(np.diag(v3) * cov_mkt ** 2) / var_mkt ** 2
|
||||
)
|
||||
roff = 2 * roff1 - roff3
|
||||
rho = rdiag + roff
|
||||
|
||||
gamma = np.linalg.norm(S - F, 'fro')**2
|
||||
gamma = np.linalg.norm(S - F, "fro") ** 2
|
||||
|
||||
kappa = (phi - rho) / gamma
|
||||
alpha = max(0, min(1, kappa / t))
|
||||
@@ -386,11 +394,11 @@ class POETCovEstimator(RiskModel):
|
||||
[2] http://econweb.rutgers.edu/yl1114/papers/poet/POET.m
|
||||
"""
|
||||
|
||||
THRESH_SOFT = 'soft'
|
||||
THRESH_HARD = 'hard'
|
||||
THRESH_SCAD = 'scad'
|
||||
THRESH_SOFT = "soft"
|
||||
THRESH_HARD = "hard"
|
||||
THRESH_SCAD = "scad"
|
||||
|
||||
def __init__(self, num_factors: int = 0, thresh: float = 1.0, thresh_method: str = 'soft', **kwargs):
|
||||
def __init__(self, num_factors: int = 0, thresh: float = 1.0, thresh_method: str = "soft", **kwargs):
|
||||
"""
|
||||
Args:
|
||||
num_factors (int): number of factors (if set to zero, no factor model will be used)
|
||||
@@ -403,25 +411,28 @@ class POETCovEstimator(RiskModel):
|
||||
"""
|
||||
super().__init__(**kwargs)
|
||||
|
||||
assert num_factors >= 0, '`num_factors` requires a positive integer'
|
||||
assert num_factors >= 0, "`num_factors` requires a positive integer"
|
||||
self.num_factors = num_factors
|
||||
|
||||
assert thresh >= 0, '`thresh` requires a positive float number'
|
||||
assert thresh >= 0, "`thresh` requires a positive float number"
|
||||
self.thresh = thresh
|
||||
|
||||
assert thresh_method in [self.THRESH_HARD, self.THRESH_SOFT, self.THRESH_SCAD], \
|
||||
'`thresh_method` should be `soft`/`hard`/`scad`'
|
||||
assert thresh_method in [
|
||||
self.THRESH_HARD,
|
||||
self.THRESH_SOFT,
|
||||
self.THRESH_SCAD,
|
||||
], "`thresh_method` should be `soft`/`hard`/`scad`"
|
||||
self.thresh_method = thresh_method
|
||||
|
||||
def _predict(self, X: np.ndarray) -> np.ndarray:
|
||||
|
||||
Y = X.T # NOTE: to match POET's implementation
|
||||
Y = X.T # NOTE: to match POET's implementation
|
||||
p, n = Y.shape
|
||||
|
||||
if self.num_factors > 0:
|
||||
Dd, V = np.linalg.eig(Y.T.dot(Y))
|
||||
V = V[:, np.argsort(Dd)]
|
||||
F = V[:, -self.num_factors:][:, ::-1] * np.sqrt(n)
|
||||
F = V[:, -self.num_factors :][:, ::-1] * np.sqrt(n)
|
||||
LamPCA = Y.dot(F) / n
|
||||
uhat = np.asarray(Y - LamPCA.dot(F.T))
|
||||
Lowrank = np.asarray(LamPCA.dot(LamPCA.T))
|
||||
@@ -434,12 +445,12 @@ class POETCovEstimator(RiskModel):
|
||||
lamb = rate * self.thresh
|
||||
SuPCA = uhat.dot(uhat.T) / n
|
||||
SuDiag = np.diag(np.diag(SuPCA))
|
||||
R = np.linalg.inv(SuDiag**0.5).dot(SuPCA).dot(np.linalg.inv(SuDiag**0.5))
|
||||
R = np.linalg.inv(SuDiag ** 0.5).dot(SuPCA).dot(np.linalg.inv(SuDiag ** 0.5))
|
||||
|
||||
if self.thresh_method == self.THRESH_HARD:
|
||||
M = R * (np.abs(R) > lamb)
|
||||
elif self.thresh_method == self.THRESH_SOFT:
|
||||
res = (np.abs(R) - lamb)
|
||||
res = np.abs(R) - lamb
|
||||
res = (res + np.abs(res)) / 2
|
||||
M = np.sign(R) * res
|
||||
else:
|
||||
@@ -449,7 +460,7 @@ class POETCovEstimator(RiskModel):
|
||||
M = M1 + M2 + M3
|
||||
|
||||
Rthresh = M - np.diag(np.diag(M)) + np.eye(p)
|
||||
SigmaU = (SuDiag**0.5).dot(Rthresh).dot(SuDiag**0.5)
|
||||
SigmaU = (SuDiag ** 0.5).dot(Rthresh).dot(SuDiag ** 0.5)
|
||||
SigmaY = SigmaU + Lowrank
|
||||
|
||||
return SigmaY
|
||||
|
||||
Reference in New Issue
Block a user