quanta_lib.py 文件源码

python
阅读 51 收藏 0 点赞 0 评论 0

项目:algorithm-component-library 作者: quantopian 项目源码 文件源码
def compute(self, today, assets, out, close):

            # get returns dataset
            returns = ((close - np.roll(close, 1, axis=0)) / np.roll(close, 1, axis=0))[1:]

            # get index of benchmark
            benchmark_index = np.where((assets == 8554) == True)[0][0]

            # get returns of benchmark
            benchmark_returns = returns[:, benchmark_index]

            # prepare X matrix (x_is - x_bar)
            X = benchmark_returns
            X_bar = np.nanmean(X)
            X_vector = X - X_bar
            X_matrix = np.tile(X_vector, (len(returns.T), 1)).T

            # prepare Y matrix (y_is - y_bar)
            Y_bar = np.nanmean(close, axis=0)
            Y_bars = np.tile(Y_bar, (len(returns), 1))
            Y_matrix = returns - Y_bars

            # prepare variance of X
            X_var = np.nanvar(X)

            # multiply X matrix an Y matrix and sum (dot product)
            # then divide by variance of X
            # this gives the MLE of Beta
            out[:] = (np.sum((X_matrix * Y_matrix), axis=0) / X_var) / (len(returns))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号