2016-07-06 130 views
1

我想用python解決風險平價問題。如何使用Python解決風險平價分配問題

風險平價是金融投資組合構建的經典方法。基本思想是確保每項資產的風險貢獻相等。

例如,假設有正在3個資產,對資產收益的協方差矩陣被稱爲:

(var_11,var_12,var_13 

var_12,var_22,var_23 

var_13,var_23,var_33) 

我想拿出一個投資組合權爲這些資產(W1 ,W2,W3),以便:

w1+w2+w3=1 

w1>=0 
w2>=0 
w3>=0 

和每項資產的風險貢獻等於:

w1^2*var_11+w1*w2*var_12+w1*w3*var_13 

=w2^2*var_22+w1*w2*var_12+w2*w3*var_23 

=w3^2*var_33+w1*w3*var_13+w2*w3*var_23 

我我不知道如何使用python解決這些方程,任何人都可以在這方面找到一些啓示。

+0

這看起來並不是一個編程問題,因此也不是題目。也許更適合更多數學導向的其他StackExchange網站? – EdChum

回答

0

超過一年的時間晚了,但使用numpy和scipy求解器。這傢伙解釋得很好,用python做。

https://thequantmba.wordpress.com/2016/12/14/risk-parityrisk-budgeting-portfolio-in-python/

一切歸功於誰寫的博客文章的人。這是博客中的代碼...

from __future__ import division 
import numpy as np 
from matplotlib import pyplot as plt 
from numpy.linalg import inv,pinv 
from scipy.optimize import minimize 

# risk budgeting optimization 
def calculate_portfolio_var(w,V): 
    # function that calculates portfolio risk 
    w = np.matrix(w) 
    return (w*V*w.T)[0,0] 

def calculate_risk_contribution(w,V): 
    # function that calculates asset contribution to total risk 
    w = np.matrix(w) 
    sigma = np.sqrt(calculate_portfolio_var(w,V)) 
    # Marginal Risk Contribution 
    MRC = V*w.T 
    # Risk Contribution 
    RC = np.multiply(MRC,w.T)/sigma 
    return RC 

def risk_budget_objective(x,pars): 
    # calculate portfolio risk 
    V = pars[0]# covariance table 
    x_t = pars[1] # risk target in percent of portfolio risk 
    sig_p = np.sqrt(calculate_portfolio_var(x,V)) # portfolio sigma 
    risk_target = np.asmatrix(np.multiply(sig_p,x_t)) 
    asset_RC = calculate_risk_contribution(x,V) 
    J = sum(np.square(asset_RC-risk_target.T))[0,0] # sum of squared error 
    return J 

def total_weight_constraint(x): 
    return np.sum(x)-1.0 

def long_only_constraint(x): 
    return x 

x_t = [0.25, 0.25, 0.25, 0.25] # your risk budget percent of total portfolio risk (equal risk) 
cons = ({'type': 'eq', 'fun': total_weight_constraint}, 
{'type': 'ineq', 'fun': long_only_constraint}) 
res= minimize(risk_budget_objective, w0, args=[V,x_t], method='SLSQP',constraints=cons, options={'disp': True}) 
w_rb = np.asmatrix(res.x)