Example #1
0
def portfolio_box(er, s, eta, y, side=1.0, extended_return=True):
    n = er.shape[1]
    N = n+1
    mu = ps.first_moment(er)
    P = ps.second_moment(er)
    Ws = list()
    for i in range(n):
        W = np.zeros((N,N))
        W[n, i] = 1.0
        W[i, n] = 1.0
        W[n, n] = -side / 2.0
        Ws.append(W)
        W = np.zeros((N,N))
        W[n, i] = -1.0
        W[i, n] = -1.0
        W[n, n] = -side / 2.0
        Ws.append(W)
    Ws = np.array(Ws)
    logging.debug('Ws = {0}'.format(Ws))
    
    sol = eval_portfolio_box(y, mu, P, s, eta, Ws)
    
    logging.info('Solution = {0}'.format(sol))
    if extended_return:
        return sol
    else:
        return sol[0]
Example #2
0
def base_portfolio_extended(er, y, s, eta):
    n = er.shape[1]
    N = n+1
    mu = ps.first_moment(er)
    P = ps.second_moment(er)
    init_sol = find_initial_solution(s, mu, P)
    prob = dccbase.DccBaseDetFormProblem(mu, P, s, y, eta)
    sol = logbar.solve_log_barrier(init_sol, prob, 0.05, 9*6, using_finite_differences=True)
    logging.info('Solution = {0}'.format(sol))
    return sol[-n:], prob.eval_f(sol), sol, prob.eval_g(sol)
Example #3
0
def base_portfolio(er, s, eta, y):
    n = er.shape[1]
    N = n+1
    mu = ps.first_moment(er)
    P = ps.second_moment(er)
    init_sol = find_initial_solution(s, mu, P)
    prob = dccbase.DccBaseProblem(mu, P, s, y, eta)
    sol = logbar.solve_log_barrier(init_sol, prob, 0.001, 2*N)
    logging.info('Solution = {0}'.format(sol))
    return sol[-n:]
Example #4
0
def portfolio_unconstrained(er, y, s, eta):
    n = er.shape[1]
    N = n+1
    mu = ps.first_moment(er)
    P = ps.second_moment(er)
    init_sol = gen_init_sol_sphere(y, eta, s, P)
    Ws = np.zeros((N,N)).reshape((1,N,N)) # No support constraint.
    prob = dccsupport.DccSupportProblem(mu, P, s, y, eta, Ws)
    sol = logbar.solve_log_barrier(init_sol, prob, 0.001, 9*6)
    logging.info('Solution = {0}'.format(sol))
    return sol[-n:], prob.eval_f(sol), sol, prob.eval_g(sol)
Example #5
0
def portfolio_sphere_extended_fd(er, y, s, eta):
    n = er.shape[1]
    N = n+1
    mu = ps.first_moment(er)
    P = ps.second_moment(er)
    init_sol = gen_init_sol_sphere(y, eta, s, P)
    Ws = np.eye(N).reshape((1,N,N))
    Ws[-1] *= -1                # Radius of 1.
    prob = dccsupport.DccSupportProblem(mu, P, s, y, eta, Ws)
    sol = logbar.solve_log_barrier(init_sol, prob, 0.05, 9*6, using_finite_differences=True)
    logging.info('Solution = {0}'.format(sol))
    return sol[-n:], prob.eval_f(sol), sol, prob.eval_g(sol)
Example #6
0
def portfolio_logbar(er, variance_bound, extended_return=True):
    logging.info("Initialize parameters and calculate variance bound portfolio.")
    n = er.shape[1]
    mu = ps.first_moment(er)
    sigma = ps.covariance(er)
    sol = eval_portfolio_logbar(mu, sigma, variance_bound)

    logging.info("Solution = {0}".format(sol))
    if extended_return:
        return sol
    else:
        return sol[0]
Example #7
0
def portfolio_bounds(er, variance_bound, u_bounds, extended_return=True):
    logging.info("Initialize and calculate variance bound portfolio with bounded portfolio weights.")
    n = er.shape[1]
    mu = ps.first_moment(er)
    sigma = ps.covariance(er)
    sol = eval_portfolio_bounds(mu, sigma, variance_bound, u_bounds)

    logging.info("Solution = {0}".format(sol))
    if extended_return:
        return sol
    else:
        return sol[0]
Example #8
0
def portfolio(excess_returns, mean_weight, risk_aversion, eta):
    first_moment = ps.first_moment(excess_returns)
    u = np.zeros(excess_returns.shape[1])
    active_samples = msv_active_samples(u, excess_returns, eta)
    has_converged = False
    while not has_converged:
        grad = msv_grad(first_moment, excess_returns, active_samples,
                        mean_weight, risk_aversion, eta)
        u_prime = msv_hess_inv_prod(grad, excess_returns, active_samples, risk_aversion)
        active_samples_prime = msv_active_samples(u_prime, excess_returns, eta)
        has_converged = all(active_samples == active_samples_prime)
        if has_converged:
            break
        u = u_prime
        active_samples = active_samples_prime
    return u_prime
Example #9
0
def portfolio_sphere_bounds(er, s, eta, y, radius, u_bounds,
                            extended_return=True):
    n = er.shape[1]
    N = n+1
    mu = ps.first_moment(er)
    P = ps.second_moment(er)
    Ws = np.eye(N).reshape((1,N,N))

    Ws[0,-1,-1] = -radius
        
    logging.debug('Ws = {0}'.format(Ws))
    
    sol = eval_portfolio_sphere_bounds(y, mu, P, s, eta, Ws, u_bounds)
    
    logging.info('Solution = {0}'.format(sol))
    if extended_return:
        return sol
    else:
        return sol[0]
Example #10
0
def portfolio_sphere(er, semivar_bound, eta, y=0.0, radius=1.0,
                     extended_return=True,
                     verbose=False):
    n = er.shape[1]
    N = n+1
    mu = ps.first_moment(er)
    P = ps.second_moment(er)
    Ws = np.eye(N).reshape((1,N,N))

    Ws[0,-1,-1] = -radius
        
    logging.info('radius: {0}'.format(radius))
    
    sol = eval_portfolio_sphere(y, mu, P, semivar_bound, eta, Ws, verbose)
    
    # print 'Solution = {0}'.format(sol)
    if extended_return:
        return sol
    else:
        return sol[0]
Example #11
0
def bench_exact_hess(x):
    test_prob.eval_hess_bar_g(x)
    

def benchmark_fd_exact_hess_bar_g():
    print(timeit.timeit('bench_fd_hess()', setup='from dccsupport import bench_fd_hess',
                        number=1000))
    print(timeit.timeit('bench_exact_hess()', setup='from dccsupport import bench_exact_hess',
                        number=1000))



# test_xm = np.concatenate([np.array([[ -1.3125 ,  -1.125  ,  -1.0625 ,  -0.5625 ],
#                                     [ -1.125  ,  -1.8125 ,  -1.25   ,  -0.90625],
#                                     [ -1.0625 ,  -1.25   ,  -1.3125 ,  -0.625  ],
#                                     [ -0.5625 ,  -0.90625,  -0.625  ,  -1.025  ]]).reshape(-1),
#                           np.array([0.1667, 0.3333, 0.5])]) / 10.

# test_xm = init_tau(test_xm, 3, 1)/100.
# test_xm_extended = init_tau(test_xm, 3, 2)/100.

if __name__ == '__main__':
    test_er = loaddata.load_fama_french_10_industry().values[-300:, :3]
    mu, sigma = ps.first_moment(test_er), ps.covariance(test_er)
    # mu, P = ps.shrinkage_moments(test_er.values[300:,:3], bootstrap_size=100)
    variance_bound = 0.03
    u_bounds = {'lower': -0.1*np.ones(3), 'upper': 0.5*np.ones(3)}
    test_prob = MVProblem(mu, sigma, variance_bound, u_bounds)
    # test_prob_extended = DccSupportProblem(mu, P, s, y, eta, extended_Ws)
Example #12
0
    bar_hess = np.sum(map(logbar.eval_log_bar_hess_f, g, jac_g, hess_g), axis=0)

def bench_exact_hess(x):
    test_prob.eval_hess_bar_g(x)
    

def benchmark_fd_exact_hess_bar_g():
    print(timeit.timeit('bench_fd_hess()', setup='from dccsupport import bench_fd_hess',
                        number=1000))
    print(timeit.timeit('bench_exact_hess()', setup='from dccsupport import bench_exact_hess',
                        number=1000))
    

if __name__ == '__main__':
    test_er = loaddata.load_fama_french_10_industry().values[-300:, :3]
    mu, P = ps.first_moment(test_er), ps.second_moment(test_er)
    # mu, P = ps.shrinkage_moments(test_er.values[300:,:3], bootstrap_size=100)
    y = 0.1
    eta = 0.004
    s = 0.005
    Ws = np.eye(4).reshape(1, 4, 4)
    radius = 3.0
    Ws[0, 3, 3] = -radius
    extended_Ws = np.concatenate([np.eye(4).reshape(1,4,4),
                                  np.diag(np.linspace(1,4,4)).reshape(1,4,4)])
    u_bounds = {'lower': -0.1*np.ones(3), 'upper': 0.5*np.ones(3)}
    test_prob = DccSupportProblem(mu, P, s, y, eta, Ws, u_bounds)
    # test_prob_extended = DccSupportProblem(mu, P, s, y, eta, extended_Ws)
    
    
    
Example #13
0
    bar_hess = np.sum(map(logbar.eval_log_bar_hess_f, g, jac_g, hess_g), axis=0)

def bench_exact_hess(x):
    test_prob.eval_hess_bar_g(x)
    

def benchmark_fd_exact_hess_bar_g():
    print(timeit.timeit('bench_fd_hess()', setup='from dccsupport import bench_fd_hess',
                        number=1000))
    print(timeit.timeit('bench_exact_hess()', setup='from dccsupport import bench_exact_hess',
                        number=1000))
    

if __name__ == '__main__':    
    test_et = loaddata.load_fama_french_10_industry()
    mu = ps.first_moment(test_et.values[300:,:3])
    P = ps.second_moment(test_et.values[300:,:3])
    
    y = 0.1
    eta = 0.004
    s = 0.005
    test_prob = DccBaseProblem(mu, P, s, y, eta)
    
    
    
        
    # test_xm = np.concatenate([np.array([[ -1.3125 ,  -1.125  ,  -1.0625 ,  -0.5625 ],
    #                                     [ -1.125  ,  -1.8125 ,  -1.25   ,  -0.90625],
    #                                     [ -1.0625 ,  -1.25   ,  -1.3125 ,  -0.625  ],
    #                                     [ -0.5625 ,  -0.90625,  -0.625  ,  -1.025  ]]).reshape(-1),
    #                           np.array([0.1667, 0.3333, 0.5])]) / 10.
Example #14
0
def portfolio(excess_returns, mean_weight):
    mu = ps.first_moment(excess_returns)
    sigma = ps.covariance(excess_returns)

    return mean_weight * 0.5 * la.solve(sigma, mu)