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]
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)
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:]
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)
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)
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]
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]
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)
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.