年化从19.1%提升到22.5%,全球大类资产轮动,加上RSRS择时,RSRS性能优化70倍。(附策略源码)

今天优化一下RSRS指标,并刷新一下策略。

大家知道,numpy的rolling apply性能不好,我们来优化一下:

import numpy as np

from numpy.lib.stride_tricks import as_strided as strided


def rolling_window(a: np.array, window: int):
'生成滚动窗口,以三维数组的形式展示'
shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
strides = a.strides + (a.strides[-1],)
return strided(a, shape=shape, strides=strides)


def numpy_rolling_regress(x1, y1, window: int = 18, array: bool = False):
'在滚动窗口内进行,每个矩阵对应进行回归'
x_series = np.array(x1)
y_series = np.array(y1)
# 创建一个一维数组
dd = x_series
x = rolling_window(dd, window)
yT = rolling_window(y_series, window)
y = np.array([i.reshape(window, 1) for i in yT])
ones_vector = np.ones((1, x.shape[1]))
XT = np.stack([np.vstack([ones_vector, row]) for row in x]) # 加入常数项
X = np.array([matrix.T for matrix in XT]) # 以行数组表示
reg_result = np.linalg.pinv(XT @ X) @ XT @ y # 线性回归公示

if array:
return reg_result else: frame = pd.DataFrame() result_const = np.zeros(x_series.shape[0]) const = reg_result.reshape(-1, 2)[:, 0] result_const[-const.shape[0]:] = const frame['const'] = result_const frame.index = x1.index for i in range(1, reg_result.shape[1]): result = np.zeros(x_series.shape[0]) beta = reg_result.reshape(-1, 2)[:, i] result[-beta.shape[0]:] = beta frame[f'factor{i}'] = result return frame @calc_by_symbol def RSRS(low: pd.Series, high: pd.Series, N: int = 18, M: int = 600): beta_series = numpy_rolling_regress(low, high, window=N, array=True) beta = beta_series.reshape(-1, 2)[:, 1] beta_rollwindow = rolling_window(beta, M) beta_mean = np.mean(beta_rollwindow, axis=1) beta_std = np.std(beta_rollwindow, axis=1) zscore = (beta[M - 1:] - beta_mean) / beta_std len_to_pad = len(low.index) - len(zscore) # print(len_to_pad) pad = [np.nan for i in range(len_to_pad)] pad.extend(zscore) zscore = pd.Series(pad, index=low.index) len_to_pad = len(low.index) - len(beta) pad = [np.nan for i in range(len_to_pad)] pad.extend(beta) beta = pd.Series(pad,index=low.index) return beta

优化前的时间:

图片

优化之后性能可以接受(性能优化70倍!):

图片

当然我们需要确保计算结果是一致的:

图片

我们来测试一下策略——全球大类资产配置,加上RSRS择时后,

图片

图片

年化从19.1%提升到22.5%

图片

源码在这个位置:

图片

吾日三省吾身

之前聊的“ABCZ”计划人生计划之”ABCZ”

Z计划作为投资系统,这个保持不变——应该是建议所有人都建立起这一样的系统,既是保底方案,在你本金大了之后,更新财富自由的动力。

但Z计划需要其他计划的助力,助力越快,则自由越早。

C计划是你理想中的生活,也许有各种各样的版本,但是,你喜欢什么样的感觉你是知道的。

内向型的人喜欢自己能独立完成事情,外向型的人喜欢张罗一堆人去完成一件事情,这完成不一样。

比如“做一个独立的研究者,创作者。”

万维刚在得到App上的课程火了之后,他辞掉了物理学者的工作,专心写作。

因为写作的收益已经远超他的本职工作。

你可以认为,这其实是他的plan B,而且非常稳妥。

ABCZ本是要降低对于A计划的依赖,B计划要有积累,可持续,有复利,持续积累之后,其实就是C计划。

C计划不可能凭空出现,哪怕你中了彩票头奖,也是补充Z计划的本金。C计划肯定是B计划里一步步“生长”出来的。

只是B计划有了正反馈后,一步步积累,一点点调整,迭代走出来的。

因此,这里最大的变化,是强化B计划的可积累和复利,架设被动收入管道,这看来是当下最重要的事情。

发布者:股市刺客,转载请注明出处:https://www.95sca.cn/archives/265116
站内所有文章皆来自网络转载或读者投稿,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!

(0)
股市刺客的头像股市刺客
上一篇 55分钟前
下一篇 53分钟前

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注