0% found this document useful (0 votes)
118 views8 pages

HW 1

The portfolio optimization problem was solved again using stock price data from Yahoo Finance for MSFT, V, and WMT from January 2019 to January 2021 instead of the data provided in the lesson. The results of the optimization were significantly different due to changes in the expected returns and risks of the individual stocks, especially for V. The optimized portfolio from the Yahoo data eliminated V and increased the weighting of WMT compared to the solution from the lesson data. The expected return of the optimized Yahoo portfolio was higher but with slightly more risk.

Uploaded by

Malik Khaled
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views8 pages

HW 1

The portfolio optimization problem was solved again using stock price data from Yahoo Finance for MSFT, V, and WMT from January 2019 to January 2021 instead of the data provided in the lesson. The results of the optimization were significantly different due to changes in the expected returns and risks of the individual stocks, especially for V. The optimized portfolio from the Yahoo data eliminated V and increased the weighting of WMT compared to the solution from the lesson data. The expected return of the optimized Yahoo portfolio was higher but with slightly more risk.

Uploaded by

Malik Khaled
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

hw1

January 24, 2021

1 Circle Optimization
Consider the following optimization problem
max (x − 1)2 + (y − 2)2
s.t. x + y ≤ 4;
x − y ≥ −2;
x ≥ 0; y ≥ 0;
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, and all
the optimal solutions and the optimal objective value of this problem. There may be
multiple optimal solutions. Please and all optimal solutions.

1
The equation for the objective function (x − 1)2 + (y − 2)2 represents a circle with center @ (1,2).
As shown in the plot above the greyed area is the feasible region determined by the constraints,
color coded and labeled to the right.
We can see from the diagram that the feasible set is bounded, so this problem will have an optimal
solution for the maximum as well as for the minimum. The vertices (corner points) of the feasible
set are (0, 0) , (0, 2) , (1, 3) and (4, 0) .
To find the optimal solutions at which the maximum occurs, we substitute each corner point into
the objective function, (x − 1)2 + (y − 2)2 .
Vertex of Feasible Set Value of (x − 1)2 + (y − 2)2
(0, 0) (0 − 1)2 + (0 − 2)2 = 5
(0, 2) (0 − 1)2 + (2 − 2)2 = 1
(1, 3) (1 − 1)2 + (3 − 2)2 = 1
(4, 0) (4 − 1)2 + (0 − 2)2 = 13
as shown in the table above the highest value function occurs @ (4, 0). Hence the optimal solution
is at (4, 0) .
And the optimal objective value is 13 .

2 Maximum volume box


Recall the maximum volume box from Module 1, Lesson 1. Solve the following problem
using basic calculus:
max {x(1 − 2x)2 : 0 ≤ x ≤ 12 }
What is the optimal solution and the optimal objective value?
First we will take the first derivative of the objective function and equate it to 0 to find the extrema.
δ
thus δx (x(1 − 2x)2 )
= δx (4x3 − 4x2 + x)
δ

= 12x2 − 8x + 1
setting the first order derivative to zero we get the quadratic equation
$ 12 x^2 - 8x + 1 = 0 $
solving this using the quadratic formula we get

−(−8)± (−8) −(4121)
2

212
gives the extrema set { 12 , 16 }
To determine the maxima which we need to maximize the objective funxtion we can take the second
order derivative and substitue the value of x in the second order derivative. If the value of second
order derivaive is -ve at a specific extrema then we have a maxima.
∴ δx
δ
(12x2 − 8x + 1)
= 24x − 8
Substituting the extrema set { 21 , 16 } we get
extrema Value of 24x - 8
1
2 24( 21 ) − 8 = 4
1
6 24( 16 ) − 8 = −4

2
1
Thus from the table above we see that @ 6 the objective function is maximized.
1
Optimal solution : 6
Optimal Objective value : 0.074

3 Portfolio optimization
Recall the portfolio optimization problem solved in Module 2, Lesson 3.
Collect the prices of MSFT, V, and WMT from the last 24 months.
Use the code 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.
Code for solving the portfolio optimization problem along with collecting the data is provided
below.

|
As can be seen from the results above, the results are very different. As can be seen from
the results of the code below the data retrieved from Yahoo is very different from the data in
monthly_prices.csv. This has caused the risk and the expected return for each of the securities to
change considerably - especially for Visa[V].
The risk on Visa (v) is higher by 67% approx. and the exptected return is lower by 23% approx.
This has resulted in the complete elimination of Visa[V] from the optimized portfolio.
Moreover the Walmart[WMT] expected return has increased by 125% for the Yahoo data with risk
remaining approximately the same and thus has caused WMT to be given a higher weighting in
the optimized portfolio of the Yahoo data.
The Expected return of the Yahoo optimized portfolio is higher @ 2.42% vs 2.0% for the
monthly_prices.csv.
The risk for the optimized portfolio is also higher for the Yahoo data @ 0.041 vs 0.039 for the
monthly_prices.csv data.
[11]: ## get ticker data
import yfinance as yf
import pandas as pd

3
import numpy as np
from yahoofinancials import YahooFinancials
from matplotlib import pyplot as plt
from cvxpy import *
import warnings as w

w.simplefilter('ignore')
w.filterwarnings('ignore')
w.simplefilter(action='ignore', category=Warning)

assets = [ 'MSFT', 'V', 'WMT' ]

yahoo_financials = YahooFinancials(assets)

data = yahoo_financials.get_historical_price_data(start_date='2019-01-01',
end_date='2021-01-31',
time_interval='monthly')

prices_df = pd.DataFrame({
a: {x['formatted_date']: x['adjclose'] for x in data[a]['prices']} for a in␣
,→assets

})

prices_df = prices_df.tail(24)
prices_df.head(n=24)

[11]: MSFT V WMT


2019-03-01 115.494392 154.513962 94.024399
2019-04-01 127.891884 162.665512 99.676994
2019-05-01 121.115356 159.598801 98.320053
2019-06-01 131.667801 171.951736 107.660606
2019-07-01 133.938263 176.360748 107.553413
2019-08-01 135.501053 179.154785 111.334061
2019-09-01 137.106079 170.671829 116.210098
2019-10-01 141.385986 174.789551 116.621368
2019-11-01 149.285172 183.074570 116.611572
2019-12-01 156.046890 186.750488 116.366768
2020-01-01 168.445557 197.752747 112.610161
2020-02-01 160.311722 180.648041 105.911972
2020-03-01 156.483047 160.366241 111.754448
2020-04-01 177.815781 177.883911 120.083237
2020-05-01 181.824356 194.326614 122.562943
2020-06-01 202.489105 192.588226 118.855125
2020-07-01 203.981583 189.826569 128.400848
2020-08-01 224.398651 211.351547 137.777878
2020-09-01 209.780777 199.669144 139.400452
2020-10-01 201.941315 181.436630 138.244675

4
2020-11-01 213.511017 210.033539 152.233536
2020-12-01 222.419998 218.729996 143.625000
2021-01-01 225.949997 202.020004 146.330002
2021-01-22 225.949997 202.020004 146.330002

[3]: mp = pd.read_csv("monthly_prices.csv",index_col=0)
mp.head(n=24)

[3]: MSFT V WMT


1 44.259998 69.660004 64.839996
2 52.639999 77.580002 57.240002
3 54.349998 79.010002 58.840000
4 55.480000 77.550003 61.299999
5 55.090000 74.489998 66.360001
6 50.880001 72.389999 66.339996
7 55.230000 76.480003 68.489998
8 49.869999 77.239998 66.870003
9 53.000000 78.940002 70.779999
10 51.169998 74.169998 73.019997
11 56.680000 78.050003 72.970001
12 57.459999 80.900002 71.440002
13 57.599998 82.699997 72.120003
14 59.919998 82.510002 70.019997
15 60.259998 77.320000 70.430000
16 62.139999 78.019997 69.120003
17 64.650002 82.709999 66.739998
18 63.980000 87.940002 70.930000
19 65.860001 88.870003 72.080002
20 68.459999 91.220001 75.180000
21 69.839996 95.230003 78.599998
22 68.930000 93.779999 75.680000
23 72.699997 99.559998 79.989998
24 74.769997 103.519997 78.070000

[8]: # mr = pd.DataFrame()

def findOptimizedPortfolio(mp, title='Yahoo data'):


# # compute monthly returns
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]

5
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.asarray(np.cov(return_data))

# print out expected return and std deviation


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

# set up optimization model


n = len(symbols)
x = Variable(n)
req_return = 0.02
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))

6
print('risk = %f' %((risk.value)**0.5))
print("----------------------")
except:
print('Error')

[9]: w.simplefilter('ignore')
findOptimizedPortfolio(prices_df, title='Yahoo data ')

Yahoo data
----------------------
MSFT: Exp ret = 0.031055, Risk = 0.055871
V: Exp ret = 0.014130, Risk = 0.071313
WMT: Exp ret = 0.020411, Risk = 0.046081
----------------------
Optimal portfolio
----------------------
x[MSFT] = 0.357574
x[V] = 0.000000
x[WMT] = 0.642426
----------------------
Exp ret = 0.024217
risk = 0.040933
----------------------
C:\ProgramData\Anaconda3\lib\site-packages\cvxpy\expressions\expression.py:550:
UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.

warnings.warn(__STAR_MATMUL_WARNING__, UserWarning)

[10]: w.simplefilter('ignore')
findOptimizedPortfolio(mp, title='Data from Module 2 - monthly_prices.csv ')

Data from Module 2 - monthly_prices.csv


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

7
----------------------
Exp ret = 0.020000
risk = 0.038256
----------------------
C:\ProgramData\Anaconda3\lib\site-packages\cvxpy\expressions\expression.py:550:
UserWarning:
This use of ``*`` has resulted in matrix multiplication.
Using ``*`` for matrix multiplication has been deprecated since CVXPY 1.1.
Use ``*`` for matrix-scalar and vector-scalar multiplication.
Use ``@`` for matrix-matrix and matrix-vector multiplication.
Use ``multiply`` for elementwise multiplication.

warnings.warn(__STAR_MATMUL_WARNING__, UserWarning)

You might also like