def compute_covariance_matrix(self, bool_print=False): # compute variance-covariance matrix by pairwise covariances scale = 252 # annualised size = len(self.rics) mtx_covar = np.zeros([size, size]) mtx_correl = np.zeros([size, size]) vec_returns = np.zeros([size, 1]) vec_volatilities = np.zeros([size, 1]) returns = [] for i in range(size): ric1 = self.rics[i] temp_ret = [] for j in range(i + 1): ric2 = self.rics[j] ret1, ret2, t = stream_functions.synchronise_timeseries( ric1, ric2) returns = [ret1, ret2] # covariances temp_mtx = np.cov(returns) temp_covar = scale * temp_mtx[0][1] temp_covar = np.round(temp_covar, self.nb_decimals) mtx_covar[i][j] = temp_covar mtx_covar[j][i] = temp_covar # correlations temp_mtx = np.corrcoef(returns) temp_correl = temp_mtx[0][1] temp_correl = np.round(temp_correl, self.nb_decimals) mtx_correl[i][j] = temp_correl mtx_correl[j][i] = temp_correl if j == 0: temp_ret = ret1 # returns temp_mean = np.round(scale * np.mean(temp_ret), self.nb_decimals) vec_returns[i] = temp_mean # volatilities temp_volatility = np.round( np.sqrt(scale) * np.std(temp_ret), self.nb_decimals) vec_volatilities[i] = temp_volatility self.covariance_matrix = mtx_covar self.correlation_matrix = mtx_correl self.returns = vec_returns self.volatilities = vec_volatilities if bool_print: print('-----') print('Portfolio Manager details:') print('Securities:') print(self.rics) print('Returns (annualised):') print(self.returns) print('Volatilities (annualised):') print(self.volatilities) print('Variance-covariance matrix (annualised):') print(self.covariance_matrix) print('Correlation matrix:') print(self.correlation_matrix)
import bollinger_bands importlib.reload(bollinger_bands) # inputs backtest = bollinger_bands.backtest() backtest.ric_long = 'SGREN.MC' backtest.ric_short = 'VWS.CO' backtest.rolling_days = 20 backtest.level_1 = 1. backtest.level_2 = 2. backtest.data_cut = 0.7 backtest.data_type = 'in-sample' # in-sample out-of-sample # load data _, _, t = stream_functions.synchronise_timeseries(backtest.ric_long, backtest.ric_short) cut = int(backtest.data_cut * t.shape[0]) if backtest.data_type == 'in-sample': df1 = t.head(cut) elif backtest.data_type == 'out-of-sample': df1 = t.tail(t.shape[0] - cut) df1 = df1.reset_index(drop=True) # spread at current close df1['spread'] = df1['price_1'] / df1['price_2'] base = df1['spread'][0] df1['spread'] = df1['spread'] / base # spread at previous close df1['spread_previous'] = df1['price_1_previous'] / df1['price_2_previous'] df1['spread_previous'] = df1['spread_previous'] / base
def load_timeseries(self): self.returns_benchmark, self.returns_ric, self.dataframe\ = stream_functions.synchronise_timeseries(self.benchmark, self.ric)
def load_timeseries(self): # load timeseries and synchronise them self.x, self.y, self.t = stream_functions.synchronise_timeseries( self.ric, self.benchmark)
# x, x_str, t = stream_functions.load_timeseries(ric) # returns.append(x) # mtx_covar = np.cov(returns) # cov = covariance # mtx_correl = np.corrcoef(returns) # corrcoef = correlation # # this did not work, we need to synchronise the timeseries before computing the covar # compute variance-covariance matrix by pairwise covariances size = len(rics) mtx_covar = np.zeros([size, size]) mtx_correl = np.zeros([size, size]) returns = [] for i in range(size): ric1 = rics[i] for j in range(size): ric2 = rics[j] ret1, ret2, t = stream_functions.synchronise_timeseries(ric1, ric2) returns = [ret1, ret2] # covariances temp_mtx = np.cov(returns) temp_covar = scale * temp_mtx[0][1] mtx_covar[i][j] = np.round(temp_covar, nb_decimals) # correlations temp_mtx = np.corrcoef(returns) temp_correl = temp_mtx[0][1] mtx_correl[i][j] = np.round(temp_correl, nb_decimals) # # compute eigenvalues and eigenvectors # eigenvalues, eigenvectors = LA.eig(mtx_covar) # compute eigenvalues and eigenvectors for symmetric matrices eigenvalues, eigenvectors = LA.eigh(mtx_covar)