Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. import numpy as np
  2. from scipy.optimize import minimize
  3.  
  4. w0 = 0
  5. w1 = 0
  6. w2 = 0
  7. w3 = 0
  8.  
  9. ws = np.array([w0, w1, w2, w3])
  10.  
  11. ## Constraints ################################################################
  12.  
  13. # sum of the weight should be 1
  14. cons = [
  15. {'type': 'eq', 'fun': lambda x: np.array([1 - np.sum(x)])}
  16. ]
  17.  
  18. # # All weights should be >= 0 and <= 1
  19. # for i, w in enumerate(ws):
  20. # cons.extend([
  21. # {'type': 'ineq', 'fun': lambda x: np.array([ws[i]])},
  22. # {'type': 'ineq', 'fun': lambda x: np.array([1 - ws[i]])}
  23. # ])
  24.  
  25.  
  26. ## function to minimize #######################################################
  27. tir = np.array([0.04, 0.045, 0.05, 0.0375])
  28. initial_guess = np.array([0.1, 0.2, 0.5, 0.2])
  29.  
  30. bounds = [(0, 1), ] * len(ws)
  31.  
  32.  
  33. def tir_portfolio(weights):
  34. return (-1) * np.sum(weights * tir)
  35.  
  36. ## Let's minimize!! ##########################################################
  37.  
  38. res = minimize(
  39. fun=tir_portfolio,
  40. x0=initial_guess,
  41. bounds=bounds,
  42. method='SLSQP',
  43. constraints=cons,
  44. options={'disp': True}
  45. )
  46.  
  47. print(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement