Without doubt, the Markowitz (1952) mean-variance portfolio theory is a cornerstone of modern financial theory. This section illustrates the use of the mean_variance_portfolio
class to implement this approach.
In [1]:
from dx import *
from pylab import plt
plt.style.use('seaborn')
We start by instantiating a market environment
object which in particular contains a list of ticker symbols in which we are interested in.
In [11]:
ma = market_environment('ma', dt.date(2010, 1, 1))
ma.add_list('symbols', ['AAPL.O', 'INTC.O', 'MSFT.O', 'GS.N'])
ma.add_constant('source', 'google')
ma.add_constant('final date', dt.date(2014, 3, 1))
Using pandas under the hood, the class retrieves historial stock price data from either Yahoo! Finance of Google.
In [13]:
%%time
port = mean_variance_portfolio('am_tech_stocks', ma)
# instantiates the portfolio class
# and retrieves all the time series data needed
In [14]:
port.get_available_symbols()
Out[14]:
Since no portfolio weights have been provided, the class defaults to equal weights.
In [15]:
port.get_weights()
# defaults to equal weights
Out[15]:
Given these weights you can calculate the portfolio return via the method get_portfolio_return
.
In [16]:
port.get_portfolio_return()
# expected (= historical mean) return
Out[16]:
Analogously, you can call get_portfolio_variance
to get the historical portfolio variance.
In [17]:
port.get_portfolio_variance()
# expected (= historical) variance
Out[17]:
The class also has a neatly printable string
representation.
In [18]:
print(port)
# ret. con. is "return contribution"
# given the mean return and the weight
# of the security
Via the method set_weights
the weights of the single portfolio components can be adjusted.
In [19]:
port.set_weights([0.6, 0.2, 0.1, 0.1])
In [21]:
print(port)
You cal also easily check results for different weights with changing the attribute values of an object.
In [22]:
port.test_weights([0.6, 0.2, 0.1, 0.1])
# returns av. return + vol + Sharp ratio
# without setting new weights
Out[22]:
Let us implement a Monte Carlo simulation over potential portfolio weights.
In [23]:
# Monte Carlo simulation of portfolio compositions
rets = []
vols = []
for w in range(500):
weights = np.random.random(4)
weights /= sum(weights)
r, v, sr = port.test_weights(weights)
rets.append(r)
vols.append(v)
rets = np.array(rets)
vols = np.array(vols)
And the simulation results visualized.
In [29]:
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(10, 6))
plt.scatter(vols, rets, c=rets / vols, marker='o', cmap='coolwarm')
plt.xlabel('expected volatility')
plt.ylabel('expected return')
plt.colorbar(label='Sharpe ratio');
One of the major application areas of the mean-variance portfolio theory and therewith of this DX Analytics class it the optimization of the portfolio composition. Different target functions can be used to this end.
The first target function might be the portfolio return.
In [30]:
port.optimize('Return')
# maximizes expected return of portfolio
# no volatility constraint
In [31]:
print(port)
Instead of maximizing the portfolio return without any constraints, you can also set a (sensible/possible) maximum target volatility level as a constraint. Both, in an exact sense ("equality constraint") ...
In [32]:
port.optimize('Return', constraint=0.225, constraint_type='Exact')
# interpretes volatility constraint as equality
In [33]:
print(port)
... or just a an upper bound ("inequality constraint").
In [34]:
port.optimize('Return', constraint=0.4, constraint_type='Bound')
# interpretes volatility constraint as inequality (upper bound)
In [35]:
print(port)
The class also allows you to minimize portfolio risk.
In [36]:
port.optimize('Vol')
# minimizes expected volatility of portfolio
# no return constraint
In [37]:
print(port)
And, as before, to set constraints (in this case) for the target return level.
In [38]:
port.optimize('Vol', constraint=0.175, constraint_type='Exact')
# interpretes return constraint as equality
In [39]:
print(port)
In [40]:
port.optimize('Vol', constraint=0.20, constraint_type='Bound')
# interpretes return constraint as inequality (upper bound)
In [41]:
print(port)
Often, the target of the portfolio optimization efforts is the so called Sharpe ratio. The mean_variance_portfolio
class of DX Analytics assumes a risk-free rate of zero in this context.
In [42]:
port.optimize('Sharpe')
# maximize Sharpe ratio
In [43]:
print(port)
Another application area is to derive the efficient frontier in the mean-variance space. These are all these portfolios for which there is no portfolio with both lower risk and higher return. The method get_efficient_frontier
yields the desired results.
In [44]:
%%time
evols, erets = port.get_efficient_frontier(100)
# 100 points of the effient frontier
The plot with the random and efficient portfolios.
In [46]:
plt.figure(figsize=(10, 6))
plt.scatter(vols, rets, c=rets / vols, marker='o')
plt.scatter(evols, erets, c=erets / evols, marker='o', cmap='coolwarm')
plt.xlabel('expected volatility')
plt.ylabel('expected return')
plt.colorbar(label='Sharpe ratio')
Out[46]:
The capital market line is another key element of the mean-variance portfolio approach representing all those risk-return combinations (in mean-variance space) that are possible to form from a risk-less money market account and the market portfolio (or another appropriate substitute efficient portfolio).
In [47]:
%%time
cml, optv, optr = port.get_capital_market_line(riskless_asset=0.05)
# capital market line for effiecient frontier and risk-less short rate
In [48]:
cml # lambda function for capital market line
Out[48]:
The following plot illustrates that the capital market line has an ordinate value equal to the risk-free rate (the safe return of the money market account) and is tangent to the efficient frontier.
In [49]:
plt.figure(figsize=(10, 6))
plt.plot(evols, erets, lw=2.0, label='efficient frontier')
plt.plot((0, 0.4), (cml(0), cml(0.4)), lw=2.0, label='capital market line')
plt.plot(optv, optr, 'r*', markersize=10, label='optimal portfolio')
plt.legend(loc=0)
plt.ylim(0)
plt.xlabel('expected volatility')
plt.ylabel('expected return')
Out[49]:
Portfolio return and risk of the efficient portfolio used are:
In [50]:
optr
Out[50]:
In [51]:
optv
Out[51]:
The portfolio composition can be derived as follows.
In [52]:
port.optimize('Vol', constraint=optr, constraint_type='Exact')
In [53]:
print(port)
Or also in this way.
In [54]:
port.optimize('Return', constraint=optv, constraint_type='Exact')
In [55]:
print(port)
As a larger, more realistic example, consider a larger set of assets.
In [71]:
symbols = list(port.get_available_symbols())[:7]
symbols
Out[71]:
In [72]:
ma = market_environment('ma', dt.date(2010, 1, 1))
ma.add_list('symbols', symbols)
ma.add_constant('source', 'google')
ma.add_constant('final date', dt.date(2014, 3, 1))
Data retrieval in this case takes a bit.
In [73]:
%%time
djia = mean_variance_portfolio('djia', ma)
# defining the portfolio and retrieving the data
In [74]:
%%time
djia.optimize('Vol')
print(djia.variance, djia.variance ** 0.5)
# minimium variance & volatility in decimals
Given the larger data set now used, efficient frontier ...
In [75]:
%%time
evols, erets = djia.get_efficient_frontier(25)
# efficient frontier of DJIA
... and capital market line derivations take also longer.
In [76]:
%%time
cml, optv, optr = djia.get_capital_market_line(riskless_asset=0.01)
# capital market line and optimal (tangent) portfolio
In [77]:
plt.figure(figsize=(10, 6))
plt.plot(evols, erets, lw=2.0, label='efficient frontier')
plt.plot((0, 0.4), (cml(0), cml(0.4)), lw=2.0, label='capital market line')
plt.plot(optv, optr, 'r*', markersize=10, label='optimal portfolio')
plt.legend(loc=0)
plt.ylim(0)
plt.xlabel('expected volatility')
plt.ylabel('expected return')
Out[77]:
Copyright, License & Disclaimer
© Dr. Yves J. Hilpisch | The Python Quants GmbH
DX Analytics (the "dx library" or "dx package") is licensed under the GNU Affero General Public License version 3 or later (see http://www.gnu.org/licenses/).
DX Analytics comes with no representations or warranties, to the extent permitted by applicable law.
http://tpq.io | dx@tpq.io | http://twitter.com/dyjh
Quant Platform | http://pqp.io
Python for Finance Training | http://training.tpq.io
Certificate in Computational Finance | http://compfinance.tpq.io
Derivatives Analytics with Python (Wiley Finance) | http://dawp.tpq.io
Python for Finance (2nd ed., O'Reilly) | http://py4fi.tpq.io