HW 1
HW 1
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 .
= 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)
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)
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)
[8]: # mr = pd.DataFrame()
5
ret = (pr1-pr0)/pr0
#mr.set_value(date,s,ret)
mr.at[date,s]=ret
pr0 = pr1
# covariance
C = np.asarray(np.cov(return_data))
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 ')
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)