Created
December 3, 2016 23:45
-
-
Save matiasherranz/77242489be7b41124509b64a5e8c6ef1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment