25.11.2015 Views

The MOSEK Python optimizer API manual Version 7.0 (Revision 141)

Optimizer API for Python - Documentation - Mosek

Optimizer API for Python - Documentation - Mosek

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

8.1. PORTFOLIO OPTIMIZATION 107<br />

8.1.3.1 Example code<br />

<strong>The</strong> following example code demonstrates how to compute the efficient portfolios for several values of<br />

α.<br />

1 """<br />

2 File : case portfolio 2.py<br />

3<br />

[ case portfolio 2.py ]<br />

4 Copyright : Copyright (c) <strong>MOSEK</strong> ApS, Denmark. All rights reserved.<br />

5<br />

6 Description : Implements a basic portfolio optimization model.<br />

7 """<br />

8<br />

10<br />

9 import mosek<br />

11 try:<br />

12 from numpy import zeros<br />

13 except ImportError:<br />

14 from mosek.array import zeros<br />

15<br />

16 def streamprinter(text):<br />

17 print("%s" % text),<br />

18<br />

19 if name == ’ main ’:<br />

20<br />

21 n = 3<br />

22 gamma = 0.05<br />

23 mu = [0.1073, 0.0737, 0.0627]<br />

24 GT = [[0.1667, 0.0232, 0.0013],<br />

25 [0.0000, 0.1033, -0.0022],<br />

26 [0.0000, 0.0000, 0.0338]]<br />

27 x0 = [0.0, 0.0, 0.0]<br />

28 w = 1.0<br />

29 alphas = [0.0, 0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]<br />

30<br />

31 inf = 0.0 # This value has no significance<br />

32<br />

33 with mosek.Env() as env:<br />

34 with env.Task(0,0) as task:<br />

35 task.set Stream(mosek.streamtype.log,streamprinter)<br />

36<br />

37 rtemp = w<br />

38 for j in range(0,n):<br />

39 rtemp += x0[j]<br />

40<br />

41 # Constraints.<br />

42 task.appendcons(1+n)<br />

43 task.putconbound(0,mosek.boundkey.fx,rtemp,rtemp)<br />

44 task.putconname(0,"budget")<br />

45<br />

46 task.putconboundlist(range(1+0,1+n),n*[mosek.boundkey.fx],n*[0.0],n*[0.0])<br />

47 for j in range(1,1+n) :<br />

48 task.putconname(j,"GT[%d]" % j)<br />

49<br />

50 # Variables.<br />

51 task.appendvars(1+2*n)<br />

52

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!