1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-17 17:34:35 +08:00

Black(new version) Format

This commit is contained in:
Young
2022-02-06 22:33:16 +08:00
parent 76b7b5f24b
commit 6a946761cf
14 changed files with 35 additions and 35 deletions

View File

@@ -54,9 +54,9 @@ master_doc = "index"
# General information about the project. # General information about the project.
project = u"QLib" project = "QLib"
copyright = u"Microsoft" copyright = "Microsoft"
author = u"Microsoft" author = "Microsoft"
# The version info for the project you're documenting, acts as replacement for # The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the # |version| and |release|, also used in various other places throughout the
@@ -174,7 +174,7 @@ latex_elements = {
# (source start file, target name, title, # (source start file, target name, title,
# author, documentclass [howto, manual, or own class]). # author, documentclass [howto, manual, or own class]).
latex_documents = [ latex_documents = [
(master_doc, "qlib.tex", u"QLib Documentation", u"Microsoft", "manual"), (master_doc, "qlib.tex", "QLib Documentation", "Microsoft", "manual"),
] ]
@@ -182,7 +182,7 @@ latex_documents = [
# One entry per manual page. List of tuples # One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section). # (source start file, name, description, authors, manual section).
man_pages = [(master_doc, "qlib", u"QLib Documentation", [author], 1)] man_pages = [(master_doc, "qlib", "QLib Documentation", [author], 1)]
# -- Options for Texinfo output ------------------------------------------- # -- Options for Texinfo output -------------------------------------------
@@ -194,7 +194,7 @@ texinfo_documents = [
( (
master_doc, master_doc,
"QLib", "QLib",
u"QLib Documentation", "QLib Documentation",
author, author,
"QLib", "QLib",
"One line description of project.", "One line description of project.",

View File

@@ -130,7 +130,7 @@ class TRAModel(Model):
if prob is not None: if prob is not None:
P = sinkhorn(-L, epsilon=0.01) # sample assignment matrix P = sinkhorn(-L, epsilon=0.01) # sample assignment matrix
lamb = self.lamb * (self.rho ** self.global_step) lamb = self.lamb * (self.rho**self.global_step)
reg = prob.log().mul(P).sum(dim=-1).mean() reg = prob.log().mul(P).sum(dim=-1).mean()
loss = loss - lamb * reg loss = loss - lamb * reg
@@ -547,7 +547,7 @@ def evaluate(pred):
score = pred.score score = pred.score
label = pred.label label = pred.label
diff = score - label diff = score - label
MSE = (diff ** 2).mean() MSE = (diff**2).mean()
MAE = (diff.abs()).mean() MAE = (diff.abs()).mean()
IC = score.corr(label) IC = score.corr(label)
return {"MSE": MSE, "MAE": MAE, "IC": IC} return {"MSE": MSE, "MAE": MAE, "IC": IC}

View File

@@ -21,7 +21,7 @@ class TestClass(unittest.TestCase):
provider_uri = "~/.qlib/qlib_data/yahoo_cn_1min" provider_uri = "~/.qlib/qlib_data/yahoo_cn_1min"
qlib.init( qlib.init(
provider_uri=provider_uri, provider_uri=provider_uri,
mem_cache_size_limit=1024 ** 3 * 2, mem_cache_size_limit=1024**3 * 2,
mem_cache_type="sizeof", mem_cache_type="sizeof",
kernels=1, kernels=1,
expression_provider={"class": "LocalExpressionProvider", "kwargs": {"time2idx": False}}, expression_provider={"class": "LocalExpressionProvider", "kwargs": {"time2idx": False}},

View File

@@ -59,10 +59,10 @@ class ConfigSectionProcessor(Processor):
# Features # Features
cols = df_focus.columns[df_focus.columns.str.contains("^KLEN|^KLOW|^KUP")] cols = df_focus.columns[df_focus.columns.str.contains("^KLEN|^KLOW|^KUP")]
df_focus[cols] = df_focus[cols].apply(lambda x: x ** 0.25).groupby(level="datetime").apply(_feature_norm) df_focus[cols] = df_focus[cols].apply(lambda x: x**0.25).groupby(level="datetime").apply(_feature_norm)
cols = df_focus.columns[df_focus.columns.str.contains("^KLOW2|^KUP2")] cols = df_focus.columns[df_focus.columns.str.contains("^KLOW2|^KUP2")]
df_focus[cols] = df_focus[cols].apply(lambda x: x ** 0.5).groupby(level="datetime").apply(_feature_norm) df_focus[cols] = df_focus[cols].apply(lambda x: x**0.5).groupby(level="datetime").apply(_feature_norm)
_cols = [ _cols = [
"KMID", "KMID",

View File

@@ -160,7 +160,7 @@ class DEnsembleModel(Model, FeatureInt):
h_avg = h.groupby("bins")["h_value"].mean() h_avg = h.groupby("bins")["h_value"].mean()
weights = pd.Series(np.zeros(N, dtype=float)) weights = pd.Series(np.zeros(N, dtype=float))
for i_b, b in enumerate(h_avg.index): for i_b, b in enumerate(h_avg.index):
weights[h["bins"] == b] = 1.0 / (self.decay ** k_th * h_avg[i_b] + 0.1) weights[h["bins"] == b] = 1.0 / (self.decay**k_th * h_avg[i_b] + 0.1)
return weights return weights
def feature_selection(self, df_train, loss_values): def feature_selection(self, df_train, loss_values):

View File

@@ -682,9 +682,9 @@ class MMD_loss(nn.Module):
if fix_sigma: if fix_sigma:
bandwidth = fix_sigma bandwidth = fix_sigma
else: else:
bandwidth = torch.sum(L2_distance.data) / (n_samples ** 2 - n_samples) bandwidth = torch.sum(L2_distance.data) / (n_samples**2 - n_samples)
bandwidth /= kernel_mul ** (kernel_num // 2) bandwidth /= kernel_mul ** (kernel_num // 2)
bandwidth_list = [bandwidth * (kernel_mul ** i) for i in range(kernel_num)] bandwidth_list = [bandwidth * (kernel_mul**i) for i in range(kernel_num)]
kernel_val = [torch.exp(-L2_distance / bandwidth_temp) for bandwidth_temp in bandwidth_list] kernel_val = [torch.exp(-L2_distance / bandwidth_temp) for bandwidth_temp in bandwidth_list]
return sum(kernel_val) return sum(kernel_val)

View File

@@ -742,7 +742,7 @@ def evaluate(pred):
score = pred.score score = pred.score
label = pred.label label = pred.label
diff = score - label diff = score - label
MSE = (diff ** 2).mean() MSE = (diff**2).mean()
MAE = (diff.abs()).mean() MAE = (diff.abs()).mean()
IC = score.corr(label, method="spearman") IC = score.corr(label, method="spearman")
return {"MSE": MSE, "MAE": MAE, "IC": IC} return {"MSE": MSE, "MAE": MAE, "IC": IC}

View File

@@ -27,11 +27,11 @@ def count_parameters(models_or_parameters, unit="m"):
counts = sum(v.numel() for v in models_or_parameters) counts = sum(v.numel() for v in models_or_parameters)
unit = unit.lower() unit = unit.lower()
if unit in ("kb", "k"): if unit in ("kb", "k"):
counts /= 2 ** 10 counts /= 2**10
elif unit in ("mb", "m"): elif unit in ("mb", "m"):
counts /= 2 ** 20 counts /= 2**20
elif unit in ("gb", "g"): elif unit in ("gb", "g"):
counts /= 2 ** 30 counts /= 2**30
elif unit is not None: elif unit is not None:
raise ValueError("Unknown unit: {:}".format(unit)) raise ValueError("Unknown unit: {:}".format(unit))
return counts return counts

View File

@@ -55,7 +55,7 @@ class TemporalConvNet(nn.Module):
layers = [] layers = []
num_levels = len(num_channels) num_levels = len(num_channels)
for i in range(num_levels): for i in range(num_levels):
dilation_size = 2 ** i dilation_size = 2**i
in_channels = num_inputs if i == 0 else num_channels[i - 1] in_channels = num_inputs if i == 0 else num_channels[i - 1]
out_channels = num_channels[i] out_channels = num_channels[i]
layers += [ layers += [

View File

@@ -125,7 +125,7 @@ class EnhancedIndexingOptimizer(BaseOptimizer):
# objective # objective
ret = d @ r # excess return ret = d @ r # excess return
risk = cp.quad_form(v, cov_b) + var_u @ (d ** 2) # tracking error risk = cp.quad_form(v, cov_b) + var_u @ (d**2) # tracking error
obj = cp.Maximize(ret - self.lamb * risk) obj = cp.Maximize(ret - self.lamb * risk)
# weight bounds # weight bounds

View File

@@ -482,7 +482,7 @@ class EnhancedIndexingStrategy(WeightStrategyBase):
r=score, r=score,
F=factor_exp, F=factor_exp,
cov_b=factor_cov, cov_b=factor_cov,
var_u=specific_risk ** 2, var_u=specific_risk**2,
w0=cur_weight, w0=cur_weight,
wb=bench_weight, wb=bench_weight,
mfh=mask_force_hold, mfh=mask_force_hold,

View File

@@ -63,7 +63,7 @@ class POETCovEstimator(RiskModel):
lamb = rate * self.thresh lamb = rate * self.thresh
SuPCA = uhat.dot(uhat.T) / n SuPCA = uhat.dot(uhat.T) / n
SuDiag = np.diag(np.diag(SuPCA)) 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: if self.thresh_method == self.THRESH_HARD:
M = R * (np.abs(R) > lamb) M = R * (np.abs(R) > lamb)
@@ -78,7 +78,7 @@ class POETCovEstimator(RiskModel):
M = M1 + M2 + M3 M = M1 + M2 + M3
Rthresh = M - np.diag(np.diag(M)) + np.eye(p) 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 SigmaY = SigmaU + Lowrank
return SigmaY return SigmaY

View File

@@ -174,7 +174,7 @@ class ShrinkCovEstimator(RiskModel):
alpha = A / B alpha = A / B
where `n`, `p` are the dim of observations and variables respectively. where `n`, `p` are the dim of observations and variables respectively.
""" """
trS2 = np.sum(S ** 2) trS2 = np.sum(S**2)
tr2S = np.trace(S) ** 2 tr2S = np.trace(S) ** 2
n, p = X.shape n, p = X.shape
@@ -192,8 +192,8 @@ class ShrinkCovEstimator(RiskModel):
""" """
t, n = X.shape t, n = X.shape
y = X ** 2 y = X**2
phi = np.sum(y.T.dot(y) / t - S ** 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
@@ -213,11 +213,11 @@ class ShrinkCovEstimator(RiskModel):
sqrt_var = np.sqrt(var) sqrt_var = np.sqrt(var)
r_bar = (np.sum(S / np.outer(sqrt_var, sqrt_var)) - n) / (n * (n - 1)) r_bar = (np.sum(S / np.outer(sqrt_var, sqrt_var)) - n) / (n * (n - 1))
y = X ** 2 y = X**2
phi_mat = y.T.dot(y) / t - S ** 2 phi_mat = y.T.dot(y) / t - S**2
phi = np.sum(phi_mat) 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) 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) rho = np.sum(np.diag(phi_mat)) + r_bar * np.sum(np.outer(1 / sqrt_var, sqrt_var) * theta_mat)
@@ -239,16 +239,16 @@ class ShrinkCovEstimator(RiskModel):
cov_mkt = np.asarray(X.T.dot(X_mkt) / len(X)) cov_mkt = np.asarray(X.T.dot(X_mkt) / len(X))
var_mkt = np.asarray(X_mkt.dot(X_mkt) / len(X)) var_mkt = np.asarray(X_mkt.dot(X_mkt) / len(X))
y = X ** 2 y = X**2
phi = np.sum(y.T.dot(y)) / t - np.sum(S ** 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] z = X * X_mkt[:, None]
v1 = y.T.dot(z) / t - cov_mkt[:, None] * S 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 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 v3 = z.T.dot(z) / t - var_mkt * S
roff3 = ( roff3 = (
np.sum(v3 * np.outer(cov_mkt, cov_mkt)) / var_mkt ** 2 - np.sum(np.diag(v3) * cov_mkt ** 2) / var_mkt ** 2 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 roff = 2 * roff1 - roff3
rho = rdiag + roff rho = rdiag + roff

View File

@@ -321,9 +321,9 @@ class SigAnaRecord(ACRecordTemp):
metrics.update( metrics.update(
{ {
"Long-Short Ann Return": long_short_r.mean() * self.ann_scaler, "Long-Short Ann Return": long_short_r.mean() * self.ann_scaler,
"Long-Short Ann Sharpe": long_short_r.mean() / long_short_r.std() * self.ann_scaler ** 0.5, "Long-Short Ann Sharpe": long_short_r.mean() / long_short_r.std() * self.ann_scaler**0.5,
"Long-Avg Ann Return": long_avg_r.mean() * self.ann_scaler, "Long-Avg Ann Return": long_avg_r.mean() * self.ann_scaler,
"Long-Avg Ann Sharpe": long_avg_r.mean() / long_avg_r.std() * self.ann_scaler ** 0.5, "Long-Avg Ann Sharpe": long_avg_r.mean() / long_avg_r.std() * self.ann_scaler**0.5,
} }
) )
objects.update( objects.update(