Professional Documents
Culture Documents
Plot the feasible region of this problem with the feasible area shaded. Draw
(in dashed lines) the contours of the objective function. Based on your
drawing, find all the optimal solutions and the optimal objective value of
this problem. There may be multiple optimal solutions. Find all optimal
solutions.
Solution: Figure 1 shows the feasible region and the contours of the
objective. We see from the figure that the optimal solutions are (−1, 0)
and (0, −1) with optimal objective value of 5. To find the optimal solution,
we need to graph contours of the objective against the feasible region. All
points on a single contour take on the same objective values. In this case
we need to graph the circles (x − 1)2 + (y − 1)2 = c for various scalars
c. We start off by graphing (x − 1)2 + (y − 1)2 = c, against the feasible
region, for small values of c. We gradually increase c, until the contour
(x − 1)2 + (y − 1)2 = c no longer intersects the feasible region. The
last contour intersecting the feasible region indicates the largest objective
value of any point in the feasible region. Hence, it indicates the optimal
solution of this problem. The last contour intersecting the feasible region
is (x − 1)2 + (y − 1)2 = 5 and it intersects the feasible region at (−1, 0) and
(0, −1). Thus, the optimal solutions are (−1, 0) and (0, −1) with optimal
objective value of 5.
1
Figure 1: Feasible region and contours
2. Recall the maximum volume box from Module 1, Lesson 1. Solve the
following problem using basic calculus:
What is the optimal solution and the optimal objective value? Draw the
objective function over the feasible region.
Solution:
Taking the first and second derivatives of the objective function f (x) =
x(1 − 2x)2 , we get
Solve the equation f 0 (x) = 0, and we get solutions x∗1 = 61 , x∗2 = 12 The
solution x∗2 is clearly not a maximizer because f (x∗2 ) = 0. Notice that
f 00 (x∗1 ) = −4 < 0. Using basic calculus, we know that x∗1 is a maximizer.
Therefore, the optimal solution is x∗1 = 61 and the optimal objective value
is f (x∗1 ) = 27
2
.
2
Figure 2: Graph of a function f (x) = x(1 − 2x)2 for 0 ≤ x ≤ 1/2
(c) Substituting |y| = w, helps eliminate |.| function and simplify the
constraint. (P) is equivalent to the following problem,
max x(w2 − z 2 )
s.t. w + z ≤ 1,
x ∈ {0, 1}, w ≥ 0, , z ≥ 0.
3
Since x is independent of w and z, the two factors can be maximized
independently. Thus, x = 1, w = 1, z = 0 results in the optimal
objective function. Equivalently in terms of y,
----------------------
MSFT: Exp ret = 0.014568, Risk = 0.069790
V: Exp ret = 0.011459, Risk = 0.065633
WMT: Exp ret = 0.010790, Risk = 0.059176
----------------------
Optimal portfolio
----------------------
x[MSFT] = 0.290864
x[V] = 0.182256
x[WMT] = 0.526880
----------------------
Exp ret = 0.012011
risk = 0.050847
----------------------
We use adjusted closing prices near the end of each month. Please see
the attached monthly prices spring2024.csv to see the values used.
Students may have used different return value depending on the data they
used. All reasonable solutions should be accepted.
Any reasonable comparison should be awarded full points.
Data source for Microsoft: https://finance.yahoo.com/quote/MSFT/
history?period1=1642291200&period2=1705363200&interval=1mo&filter=
history&frequency=1mo&includeAdjustedClose=true
4
Data source for Visa: https://finance.yahoo.com/quote/V/history?
period1=1642291200&period2=1705363200&interval=1mo&filter=history&
frequency=1mo&includeAdjustedClose=true
Data source for Walmart: https://finance.yahoo.com/quote/WMT/history?
period1=1674089666&period2=1705625666&interval=1mo&filter=history&
frequency=1mo&includeAdjustedClose=true
Code:
10pt
1 #! / u s r / b i n / env python
2 # c o d i n g : u t f −8
3
4 # In [ 1 1 ] :
5
6
7 ”””
8 P o r t f o l i o o p t i m i z a t i o n w i t h CVXPY
9 See e x a m p l e s a t h t t p : / / c v x p y . o r g
10 Author : S h a b b i r Ahmed
11 ”””
12
13 import pandas a s pd
14 import numpy a s np
15 from cvxpy import ∗
16
17 # r ead m o n t h l y p r i c e s . c s v
18 mp = pd . r e a d c s v ( ” m o n t h l y p r i c e s s p r i n g 2 0 2 4 . c s v ” ,
i n d e x c o l =0)
19 mr = pd . DataFrame ( )
20
21 # compute monthly r e t u r n s
22 f o r s in mp. columns :
23 d a t e = mp. i n d e x [ 0 ]
24 pr0 = mp[ s ] [ d a t e ]
25 f o r t in range ( 1 , len (mp. i n d e x ) ) :
26 d a t e = mp. i n d e x [ t ]
27 pr1 = mp[ s ] [ d a t e ]
28 r e t = ( pr1−pr0 ) / pr0
29 #mr . s e t v a l u e ( date , s , r e t )
30 mr . a t [ date , s ]= r e t
31 pr0 = pr1
32
33 # g e t symbol names
34 symbols = mr . columns
35
36 # c o n v e r t monthly r e t u r n d a t a frame t o a numpy m a t r i x
5
37 #r e t u r n d a t a = mr . a s m a t r i x ( ) .T
38 r e t u r n d a t a = mr . v a l u e s . T
39
40 # compute mean r e t u r n
41 r = np . a s a r r a y ( np . mean ( r e t u r n d a t a , a x i s =1) )
42
43 # covariance
44 C = np . a s m a t r i x ( np . cov ( r e t u r n d a t a ) )
45
46 # p r i n t o u t e x p e c t e d r e t u r n and s t d d e v i a t i o n
47 print ( ”−−−−−−−−−−−−−−−−−−−−−−” )
48 f o r j in range ( len ( symbols ) ) :
49 print ( ’%s : Exp r e t = %f , Risk = %f ’ %(symbols [ j ] ,
r [ j ] , C[ j , j ] ∗ ∗ 0 . 5 ) )
50
51
52 # s e t up o p t i m i z a t i o n model
53 n = len ( symbols )
54 x = Variable (n)
55 req return = 0.01
56 r e t = r . T@x
57 r i s k = quad form ( x , C)
58 prob = Problem ( Minimize ( r i s k ) ,
59 [sum( x ) == 1 , r e t >= r e q r e t u r n , x >=
0])
60
61 # s o l v e problem and w r i t e s o l u t i o n
62 try :
63 prob . s o l v e ( )
64 print ( ”−−−−−−−−−−−−−−−−−−−−−−” )
65 print ( ” Optimal p o r t f o l i o ” )
66 print ( ”−−−−−−−−−−−−−−−−−−−−−−” )
67 f o r s in range ( len ( symbols ) ) :
68 #p r i n t ( ’ x[% s ] = %f ’%( s y m b o l s [ s ] , x . v a l u e [ s , 0 ] )
)
69 print ( ’ x[% s ] = %f ’ %(symbols [ s ] , x . v a l u e [ s ] ) )
70 print ( ”−−−−−−−−−−−−−−−−−−−−−−” )
71 print ( ’ Exp r e t = %f ’ %( r e t . v a l u e ) )
72 print ( ’ r i s k = %f ’ %(( r i s k . v a l u e ) ∗ ∗ 0 . 5 ) )
73 print ( ”−−−−−−−−−−−−−−−−−−−−−−” )
74 except :
75 print ( ’ E r r o r ’ )
76
77
78 # In [ 7 ] :
79
6
80
81 symbols
82
83
84 # In [ 8 ] :
85
86
87 x[0]
88
89
90 # In [ 1 0 ] :
91
92
93 x . value [ 0 ]
94
95
96 # In [ ] :