document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. In [2]: import mpt
  2. In [3]: p = mpt.Portfolio(symbols=["AAPL", "GT", "MMM", "GNW", "KO"], startdate="2005-1-1", enddate="2008-8-23", dbfilename="data/stocks.db")
  3. Adding: AAPL
  4. Adding: GNW
  5. Adding: GT
  6. Adding: KO
  7. Adding: MMM
  8.  
  9. In [4]: rt = 2.0 # Risk tolerance, equivalent to a 200% annual volatility
  10.  
  11. In [5]: drt = rt/252.0 # Convert to a daily volatility using trading days per year
  12.  
  13. In [6]: lb = 0.0 # lower bound of allowed weighting for an asset
  14.  
  15. In [7]: ub = 1.0 # upper bound of allowed weighting for an asset
  16.  
  17. In [8]: p.optimize_portfolio(rt=drt, lower_bound_weight=lb, upper_bound_weight=ub)   Optimization completed in [ 5 ] iterations.
  18. Ending weights:
  19. {\'MMM\': 0.0, \'AAPL\': 1.0, \'GT\': 0.0, \'KO\': 0.0, \'GNW\': 0.0}
  20.  
  21. Optimized Variance: 0.155 and Portfolio Return: 54.093%
  22.  
  23. In [9]: rt = 0.5 # Risk tolerance, equivalent to a 50% annual volatility
  24.  
  25. In [10]: drt = rt/252.0 # Convert to a daily volatility using trading days per year
  26.  
  27. In [11]: del p.port_opt # Clear out the cached optimized portfolio
  28.  
  29. In [12]: p.optimize_portfolio(rt=drt, lower_bound_weight=lb, upper_bound_weight=ub)
  30. Optimization completed in [ 5 ] iterations.
  31. Ending weights:
  32. {\'MMM\': 0.0, \'AAPL\': 0.79253926811721775, \'GT\': 0.0, \'KO\': 0.20746073188278225, \'GNW\': 0.0}
  33.  
  34. Optimized Variance: 0.103 and Portfolio Return: 44.936%
');