Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from scipy.optimize import minimize
- w0 = 0
- w1 = 0
- w2 = 0
- w3 = 0
- ws = np.array([w0, w1, w2, w3])
- ## Constraints ################################################################
- # sum of the weight should be 1
- cons = [
- {'type': 'eq', 'fun': lambda x: np.array([1 - np.sum(x)])}
- ]
- # # All weights should be >= 0 and <= 1
- # for i, w in enumerate(ws):
- # cons.extend([
- # {'type': 'ineq', 'fun': lambda x: np.array([ws[i]])},
- # {'type': 'ineq', 'fun': lambda x: np.array([1 - ws[i]])}
- # ])
- ## function to minimize #######################################################
- tir = np.array([0.04, 0.045, 0.05, 0.0375])
- initial_guess = np.array([0.1, 0.2, 0.5, 0.2])
- bounds = [(0, 1), ] * len(ws)
- def tir_portfolio(weights):
- return (-1) * np.sum(weights * tir)
- ## Let's minimize!! ##########################################################
- res = minimize(
- fun=tir_portfolio,
- x0=initial_guess,
- bounds=bounds,
- method='SLSQP',
- constraints=cons,
- options={'disp': True}
- )
- print(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement