def calc_ev_ret(self, df_inc, ev_cov, P, eta, alpha): if eta.size == 0: ev_ret = df_inc.mean().values else: initialvalue = np.mean(df_inc.T.values, axis=1) ev_ret = fin.black_litterman(initialvalue, ev_cov, P, eta, alpha).reshape(-1) return ev_ret
def markowitz_bl_fixrisk(df_inc, P, eta, alpha, bound, target_risk): w0 = [1 / len(df_inc.columns)] * len(df_inc.columns) bnds = [(bound[i]['lower'], bound[i]['upper']) for i in range(len(bound))] ret = df_inc.mean().values vol = df_inc.cov().values initialvalue = np.mean(df_inc.T.values, axis=1) expected_return = fin.black_litterman(initialvalue, vol, P, eta, alpha) ret = expected_return.reshape(-1) asset_sum1_limit = 0.0 sum1_limit_assets = [] for asset in range(len(bound)): if bound[asset]['sum1'] != 0.0: sum1_limit_assets.append(asset) asset_sum1_limit = bound[asset]['sum1'] cons = [ { 'type': 'eq', 'fun': lambda x: np.sum(x) - 1.0 }, { 'type': 'ineq', 'fun': lambda x: target_risk - np.sqrt(np.dot(x, np.dot(vol, x))) }, ] if asset_sum1_limit > 0.0: cons.append({ 'type': 'ineq', 'fun': lambda x: asset_sum1_limit - np.sum(x[sum1_limit_assets]) }) cons = tuple(cons) res = scipy.optimize.minimize(risk_budget_objective, w0, args=[ret, vol, target_risk], method='SLSQP', bounds=bnds, constraints=cons, options={ 'disp': False, 'eps': 1e-3 }) final_risk = np.sqrt(np.dot(res.x, np.dot(vol, res.x))) final_return = np.dot(res.x, ret) final_ws = res.x final_sharp = (final_return - Const.rf) / final_risk return final_risk, final_return, final_ws, final_sharp
def strategicallocation(delta, weq, V, tau, P, Q): P = np.array(P) Q = np.array(Q) tauV = tau * V Omega = np.dot(np.dot(P, tauV), P.T) * np.eye(Q.shape[0]) res = fin.black_litterman(delta, weq, V, tau, P, Q, Omega) return re
def asset_allocation(start_date, end_date, largecap_fund, smallcap_fund, P, Q): ######################################################################### delta = 2.5 tau = 0.05 ps = [] for p in P: ps.append(np.array(p)) P = np.array(ps) qs = [] for q in Q: qs.append(np.array(q)) Q = np.array(qs) indexdf = data.index_value(start_date, end_date, [const.largecap_code, const.smallcap_code]) indexdfr = indexdf.pct_change().fillna(0.0) indexrs = [] for code in indexdfr.columns: indexrs.append(indexdfr[code].values) #print indexdfr sigma = np.cov(indexrs) #print type(sigma) #print sigma #print np.cov(indexrs) #print indexdfr weq = np.array([0.5, 0.5]) tauV = tau * sigma Omega = np.dot(np.dot(P, tauV), P.T) * np.eye(Q.shape[0]) er, ws, lmbda = fin.black_litterman(delta, weq, sigma, tau, P, Q, Omega) sum = 0 for w in ws: sum = sum + w for i in range(0, len(ws)): ws[i] = 1.0 * ws[i] / sum #print er indexws = ws #print indexws #largecap_fund, smallcap_fund = largesmallcapfunds(fund_tags) #print largecap_fund #risk, returns, ws, sharp = markowitz( #print smallcap_fund funddf = data.fund_value(start_date, end_date) bounds = boundlimit(len(largecap_fund)) risk, returns, ws, sharp = markowitz(funddf[largecap_fund], bounds) largecap_fund_w = {} for i in range(0, len(largecap_fund)): code = largecap_fund[i] largecap_fund_w[code] = ws[i] * indexws[0] bounds = boundlimit(len(smallcap_fund)) risk, returns, ws, sharp = markowitz(funddf[smallcap_fund], bounds) smallcap_fund_w = {} for i in range(0, len(smallcap_fund)): code = smallcap_fund[i] smallcap_fund_w[code] = ws[i] * indexws[1] ''' #平均分配 largecap_fund_w = {} for code in largecap_fund: largecap_fund_w[code] = 1.0 / len(largecap_fund) * indexws[0] smallcap_fund_w = {} for code in smallcap_fund: smallcap_fund_w[code] = 1.0 / len(smallcap_fund) * indexws[1] ''' fundws = {} for code in largecap_fund: w = fundws.setdefault(code, 0) fundws[code] = w + largecap_fund_w[code] for code in smallcap_fund: w = fundws.setdefault(code, 0) fundws[code] = w + smallcap_fund_w[code] ####################################################################### #print largecap #print smallcap #print risefitness #print declinefitness #print oscillafitness #print growthfitness #print valuefitness #print fund_codes = [] ws = [] for k, v in fundws.items(): fund_codes.append(k) ws.append(v) #for code in largecap: return fund_codes, ws