Professional Documents
Culture Documents
Cse6669 HW 1
Cse6669 HW 1
Fall 2021
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.
The plot above was created using Desmos. The link to the Desmos plot
is provided here, https://www.desmos.com/calculator/xgkb4djjje
The feasible region of this problem with the feasible area is shaded in gray.
1
The contours of the objective function is drawn in dashed line (red) for
values 1 through 5
The optimal solutions are shown using green dots. They are (x = 0, y =
- 1) and (x = -1 and y = 0). The optimal objective value of this problem
is 5.
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.
Taking the derivative of x(1 − 2x)2 with respect to x
∂
x(1 − 2x)2 = 0
∂x
∂
⇒ x 1 − 4x + 4x2 = 0
∂x
∂ ∂ ∂
4x2 + 4x3 = 0
⇒ x−
∂x ∂x ∂x
⇒1 − 8x + 12x2 = 0
√
−b± b2 −4ac
Using the quadratic formula, x = 2a
p
8± (−8)2 − 4 × 12
x=
2 × 12
√
8 ± 64 − 48
=
24
8±4
=
24
1 1
x= ,
2 6
In both these cases: 0 ≤ x ≤ 1/2 thus satisfying our constraint.
Using the second derivative test, we can find the local maxima and minima,
∂
1 − 8x + 12x2
∂x
⇒ −8 + 24x
when x = 1/2 ⇒ −8 + 24(1/2) = 4
when x = 1/6 ⇒ −8 + 24(1/6) = −4
Therefore, 1/6 is the optimum solution that maximizes x(1 − 2x)2 , which
is 2/27.
2
The plot above was created using Desmos. The link to the Desmos plot
is provided here, https://www.desmos.com/calculator/qs5vp1vzyr
The feasible region of this problem with the feasible area is shaded in red.
The optimal solution is (x = 1/6, y = 2/27)
3. Consider the following optimization problem:
(P ) max x(y 2 − z 2 )
s.t. |y| + z ≤ 1,
x ∈ {0, 1}, z ≥ 0.
(P ) min − x(y 2 − z 2 )
s.t. |y| + z ≤ 1,
x ∈ {0, 1}, z ≥ 0.
3
(c) Find all the optimal solutions.
The plot above was created using Desmos. The plot encapsulates
the objective function when x = 1 The link to the Desmos plot is
provided here, https://www.desmos.com/calculator/fsbwim1s6t
Here, we are maximizing x(y 2 − z 2 ) but x can only either be 0 or
1.
When x is 0, the maximum value of the objective function is also
0. When x is 1, the maximum value of the objective function is
obtained when (y 2 − z 2 ) is maximum. The smallest value z can take
is 0. Therefore, building on it, the maximum value of the objective
function is obtained when y 2 is maximum. y is the maximum when
y is 1 or -1.
Therefore, the optimal solutions are: x = 1, y = 1, z = 0 and x =
1, y = −1, z = 0
4
4. Recall the portfolio optimization problem solved in Module 2, Lesson 3.
Now, collect the prices of MSFT, V, and WMT from the last 24 months
(data can be collected from e.g. Yahoo Finance). Use the code and data
file format used in the lesson (this will be provided) to solve the exact
same portfolio problem using the new data. Compare and contrast your
solution to the one in the lesson.
Please refer to the full notebook on the next page.
The new data was downloaded from Yahoo Finance using the yf inance
package and pandas datareader. I used the ’Adj Close’. This is the closing
price adjusted for splits and dividend distributions.
The maximum expected return we can hope for is 0.00623 or 0.62% if
we were to invest everything in Visa. Therefore, the threshold used for
expected return is 0.6% or 0.006. This is substantially low than the ex-
pected return threshold (2%) defined on the previous historic data (in the
solution to the one in the lesson).
Optimizing based on this threshold, the results suggest 85.1% to be in-
vested in Visa, and 14.8% in Microsoft, while Walmart is to be ignored
completely. This gives us a risk of 0.06, which is higher than 0.038 (the
solution to the one in the lesson).
The return on investment from stocks of MSFT, V and WMT have gone
down significantly in the last 24 months.
5
In [1]: import pandas as pd
import numpy as np
import pandas as pd
import numpy as np
import yfinance
from pandas_datareader import data as pdr
yfinance.pdr_override()
from cvxpy import *
In [2]: df = pd.DataFrame()
for ticker in ['MSFT', 'V', 'WMT']:
temp = pdr.get_data_yahoo(ticker, start = '2021-01-01', end='2023-01-01', interval='1mo')
df[ticker] = temp['Adj Close']
[*********************100%***********************] 1 of 1 completed
[*********************100%***********************] 1 of 1 completed
[*********************100%***********************] 1 of 1 completed
In [3]: df
In [4]: mp = df.copy()
mp.index = range(1, len(mp)+1)
In [5]: mp
Out[5]: MSFT V WMT
1 227.896393 190.628174 136.150650
2 228.309021 209.508514 125.907120
3 232.173172 209.181702 131.634567
4 248.332809 230.748962 136.154678
5 245.870911 224.564316 138.217758
6 267.383148 231.341370 137.773819
7 281.211304 243.778107 139.268600
8 297.961029 226.671432 144.690842
9 278.792847 220.689163 136.672653
10 327.941559 209.810760 146.517639
11 326.922943 191.977280 137.898376
12 333.197937 215.083023 141.879532
13 308.093109 224.471985 137.646286
14 296.016266 214.497452 133.068268
15 306.079468 220.463120 146.615311
16 275.512238 211.874039 151.204880
17 269.903107 210.919708 127.129112
18 255.565750 196.103561 120.598152
19 279.358093 211.262787 130.983612
20 260.182922 197.916306 131.479568
21 232.245483 177.253799 129.212891
22 231.477661 206.697998 141.795456
23 254.422989 216.516052 151.847565
24 239.820007 207.759995 141.257477
In [6]: mr = pd.DataFrame()
# compute monthly returns
for s in mp.columns:
date = mp.index[0]
pr0 = mp[s][date]
for t in range(1,len(mp.index)):
date = mp.index[t]
pr1 = mp[s][date]
ret = (pr1-pr0)/pr0
#mr.set_value(date,s,ret)
mr.at[date,s]=ret
pr0 = pr1
# covariance
C = np.asmatrix(np.cov(return_data))
----------------------
MSFT: Exp ret = 0.004646, Risk = 0.071930
V: Exp ret = 0.006237, Risk = 0.073021
WMT: Exp ret = 0.003634, Risk = 0.064479
In [7]: r
In [8]: C
----------------------
Optimal portfolio
----------------------
x[MSFT] = 0.148735
x[V] = 0.851265
x[WMT] = -0.000000
----------------------
Exp ret = 0.006000
risk = 0.066631
----------------------