Exemplo n.º 1
0
def optimisation_bayesian_riskweights(weekly_returns,
                                      shrinkcorr=0.0,
                                      shrinkSR=0.0,
                                      priorcorroffdiag=0.5,
                                      priorSR=0.25):

    prior_corrmatrix = boring_corr_matrix(len(weekly_returns.columns),
                                          priorcorroffdiag)

    est_corrmatrix = weekly_returns.corr().values

    shrink_corrmatrix = (prior_corrmatrix * shrinkcorr) + (est_corrmatrix *
                                                           (1 - shrinkcorr))

    est_mean_list = list(annualised_mean(weekly_returns).values)
    est_stdev_list = list(annualised_std(weekly_returns).values)

    est_sr_list = [
        each_return / each_std
        for each_return, each_std in zip(est_mean_list, est_stdev_list)
    ]
    prior_sr_list = [priorSR] * len(est_sr_list)

    shrink_sr_list = [(prior * shrinkSR) + (est * (1 - shrinkSR))
                      for prior, est in zip(prior_sr_list, est_sr_list)]

    shrunk_mean_list = [
        each_std * shrunk_sr
        for each_std, shrunk_sr in zip(est_stdev_list, shrink_sr_list)
    ]

    ## apply risk weights
    avg_std = np.nanmean(est_stdev_list)
    riskwt_mean_list = [
        this_mean * avg_std / this_std
        for this_mean, this_std in zip(shrunk_mean_list, est_stdev_list)
    ]
    norm_stdev_list = [avg_std] * len(est_stdev_list)

    return optimisation_with_data(shrink_corrmatrix, riskwt_mean_list,
                                  norm_stdev_list)
Exemplo n.º 2
0
def shrink_corr(corrmatrix, shrinkage_corr):
    """
    >>> sigma=np.array([[1.0,0.0,0.5], [0.0, 1.0, 0.75],[0.5, 0.75, 1.0]]) 
    >>> shrink_corr(sigma, 0.5)
    array([[ 1.        ,  0.20833333,  0.45833333],
           [ 0.20833333,  1.        ,  0.58333333],
           [ 0.45833333,  0.58333333,  1.        ]])
    >>> shrink_corr(sigma, 0.0)
    array([[ 1.  ,  0.  ,  0.5 ],
           [ 0.  ,  1.  ,  0.75],
           [ 0.5 ,  0.75,  1.  ]])
    >>> shrink_corr(sigma, 1.0)
    array([[ 1.        ,  0.41666667,  0.41666667],
           [ 0.41666667,  1.        ,  0.41666667],
           [ 0.41666667,  0.41666667,  1.        ]])
    """
    
    avg_corr=get_avg_corr(corrmatrix)
    prior_corr=boring_corr_matrix(corrmatrix.shape[0], offdiag=avg_corr)

    sigma_corr=shrinkage_corr*prior_corr+(1-shrinkage_corr)*corrmatrix
    
    return sigma_corr
Exemplo n.º 3
0
def shrink_corr(corrmatrix, shrinkage_corr):
    """
    >>> sigma=np.array([[1.0,0.0,0.5], [0.0, 1.0, 0.75],[0.5, 0.75, 1.0]]) 
    >>> shrink_corr(sigma, 0.5)
    array([[ 1.        ,  0.20833333,  0.45833333],
           [ 0.20833333,  1.        ,  0.58333333],
           [ 0.45833333,  0.58333333,  1.        ]])
    >>> shrink_corr(sigma, 0.0)
    array([[ 1.  ,  0.  ,  0.5 ],
           [ 0.  ,  1.  ,  0.75],
           [ 0.5 ,  0.75,  1.  ]])
    >>> shrink_corr(sigma, 1.0)
    array([[ 1.        ,  0.41666667,  0.41666667],
           [ 0.41666667,  1.        ,  0.41666667],
           [ 0.41666667,  0.41666667,  1.        ]])
    """
    
    avg_corr=get_avg_corr(corrmatrix)
    prior_corr=boring_corr_matrix(corrmatrix.shape[0], offdiag=avg_corr)

    sigma_corr=shrinkage_corr*prior_corr+(1-shrinkage_corr)*corrmatrix
    
    return sigma_corr
Exemplo n.º 4
0
def get_shocked_corr_matrix(list_of_instruments):
    return boring_corr_matrix(len(list_of_instruments), offdiag=1.0)
Exemplo n.º 5
0
def optimise_using_correlation(mean_list, avg_correlation, std):
    corr_matrix = boring_corr_matrix(len(mean_list), offdiag=avg_correlation)
    stdev_list = [std] * len(mean_list)
    sigma = sigma_from_corr_and_std(stdev_list, corr_matrix)

    return optimise(sigma, mean_list)