Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

ISyE 6669 HW 1

Fall 2021

1. Consider the following maximization problem

max (x − 1)2 + (y − 1)2


s.t. |x| + |y| ≤ 1.

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:

max{x(1 − 2x)2 : 0 ≤ x ≤ 1/2}

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.

Answer the questions:


(a) Is (P) a linear program, a mixed integer nonlinear program, or a
mixed integer quadratic program? Choose all descriptions that apply.
Here the objective function is quadratic and it is constrained to take
an integer value. Therefore, it is a mixed integer quadratic program.
(b) Write a minimization problem that is equivalent to (P).
The minimization problem that is equivalent to (P) is

(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

Out[3]: MSFT V WMT


Date
2021-01-01 00:00:00-05:00 227.896393 190.628174 136.150650
2021-02-01 00:00:00-05:00 228.309021 209.508514 125.907120
2021-03-01 00:00:00-05:00 232.173172 209.181702 131.634567
2021-04-01 00:00:00-04:00 248.332809 230.748962 136.154678
2021-05-01 00:00:00-04:00 245.870911 224.564316 138.217758
2021-06-01 00:00:00-04:00 267.383148 231.341370 137.773819
2021-07-01 00:00:00-04:00 281.211304 243.778107 139.268600
2021-08-01 00:00:00-04:00 297.961029 226.671432 144.690842
2021-09-01 00:00:00-04:00 278.792847 220.689163 136.672653
2021-10-01 00:00:00-04:00 327.941559 209.810760 146.517639
2021-11-01 00:00:00-04:00 326.922943 191.977280 137.898376
2021-12-01 00:00:00-05:00 333.197937 215.083023 141.879532
2022-01-01 00:00:00-05:00 308.093109 224.471985 137.646286
2022-02-01 00:00:00-05:00 296.016266 214.497452 133.068268
2022-03-01 00:00:00-05:00 306.079468 220.463120 146.615311
2022-04-01 00:00:00-04:00 275.512238 211.874039 151.204880
2022-05-01 00:00:00-04:00 269.903107 210.919708 127.129112
2022-06-01 00:00:00-04:00 255.565750 196.103561 120.598152
2022-07-01 00:00:00-04:00 279.358093 211.262787 130.983612
2022-08-01 00:00:00-04:00 260.182922 197.916306 131.479568
2022-09-01 00:00:00-04:00 232.245483 177.253799 129.212891
2022-10-01 00:00:00-04:00 231.477661 206.697998 141.795456
2022-11-01 00:00:00-04:00 254.422989 216.516052 151.847565
2022-12-01 00:00:00-05:00 239.820007 207.759995 141.257477

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

# get symbol names


symbols = mr.columns

# convert monthly return data frame to a numpy matrix


#return_data = mr.as_matrix().T
return_data = mr.values.T

# compute mean return


r = np.asarray(np.mean(return_data, axis=1))

# covariance
C = np.asmatrix(np.cov(return_data))

# print out expected return and std deviation


print("----------------------")
for j in range(len(symbols)):
print('%s: Exp ret = %f, Risk = %f' %(symbols[j],r[j], C[j,j]**0.5))

----------------------
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

Out[7]: array([0.00464582, 0.00623661, 0.00363392])

In [8]: C

Out[8]: matrix([[0.00517394, 0.00182214, 0.00237539],


[0.00182214, 0.00533205, 0.00163299],
[0.00237539, 0.00163299, 0.0041575 ]])

The maximum expected return we can hope for is 0.00623 or 0.62%


Let us set the expected return to 0.6% or 0.006
In [9]: # set up optimization model
n = len(symbols)
x = Variable(n)
req_return = 0.0060
ret = r.T@x
risk = quad_form(x, C)
prob = Problem(Minimize(risk),
[sum(x) == 1, ret >= req_return, x >= 0])

# solve problem and write solution


try:
prob.solve()
print("----------------------")
print("Optimal portfolio")
print("----------------------")
for s in range(len(symbols)):
#print('x[%s] = %f'%(symbols[s],x.value[s,0]))
print('x[%s] = %f'%(symbols[s],x.value[s]))
print("----------------------")
print('Exp ret = %f' %(ret.value))
print('risk = %f' %((risk.value)**0.5))
print("----------------------")
except:
print('Error')

----------------------
Optimal portfolio
----------------------
x[MSFT] = 0.148735
x[V] = 0.851265
x[WMT] = -0.000000
----------------------
Exp ret = 0.006000
risk = 0.066631
----------------------

For the "monthly_prices.csv" dataset provided, the results were:


----------------------
MSFT: Exp ret = 0.024611, Risk = 0.058040
V: Exp ret = 0.018237, Risk = 0.042807
WMT: Exp ret = 0.009066, Risk = 0.044461
----------------------
Optimal portfolio
----------------------
x[MSFT] = 0.582818
x[V] = 0.204324
x[WMT] = 0.212858
----------------------
Exp ret = 0.020000
risk = 0.038256
----------------------

You might also like