Using TikZ and PGFPlots for a class on Portfolio Investment
December 30, 2020
To use the code just insert it into any LaTeX code with the appropriate tikz libraries; see here for examples.
Optimal Portfolio #
Optimal portfolio allocation lies on the Capital Allocation Line (CAL) such that it maximizes the utility of the investor. Thus investor’s utility curve is tangent on that point to the CAL:
\begin{axis}[
height=7cm, width=12cm,
axis x line=bottom, axis y line=left,
xlabel = Standard Deviation, ylabel = Expected Return,
ymin=0, ymax=25,
xmin=0, xmax=60,
extra x ticks={65}, extra x tick labels={$\sigma$},
extra x tick style={major tick length=0mm, grid=none},
extra y ticks={25}, extra y tick labels={$\mathbf{E}\{R\}$},
extra y tick style={major tick length=0mm, grid=none},
enlargelimits=true,
scatter/classes={
a={mark=o,draw=black, mark size = 3pt},
b={mark=*, mark size = 3pt,draw=red, fill = red}
}
]
\addplot[color=black, domain=0:55, line width=1pt]
{3 + x * 0.4}; % plot the capital allocation line
\addplot[color=red, domain=0:55, line width=1pt]
{5 + 0.5 * (1/23) * x^2}; % plot the utility function
\addplot[scatter,only marks,
scatter src=explicit symbolic] % plot the risk-free rate and the market and the allocation
coordinates {
(0,3) [a]
(20, 11) [a]
(10, 7) [b]
};
\node at (axis cs:0,3) [anchor=north west] {Riskfree, $R_f$};
\node at (axis cs:20,11) [anchor=north west] {Risky, $R_s$};
\node[rotate=27] at (axis cs:50, 23) [anchor=south east] {\textbf{CAL}};
\node[color=red] at (axis cs:27, 25) [anchor=south west] {\textbf{U}};
\node[pin={[pin edge={thick}, pin distance=2cm]90:{Optimal Porfolio}}] at (axis cs:10,7.5) {};
\end{axis}
Efficient Frontier #
Case of two or more than two assets
Two Assets #
\begin{axis}[
height=7cm, width=12cm,
axis x line=bottom, axis y line=left,
xlabel = Standard Deviation, ylabel = Expected Return,
ymin=0, ymax=25, xmin=0, xmax=60,
extra x ticks={65}, extra x tick labels={$\sigma$},
extra x tick style={major tick length=0mm, grid=none},
extra y ticks={25}, extra y tick labels={$\mathbf{E}\{R\}$},
extra y tick style={major tick length=0mm, grid=none},
enlargelimits=true,
scatter/classes={
a={mark=o,draw=black, mark size = 3pt},
b={mark=*, mark size = 3pt,draw=red, fill = red}
}
]
\addplot[scatter,only marks, scatter src=explicit symbolic]
coordinates {
(20, 11) [a]
(12, 5) [a]
};
\node at (axis cs:12, 5) [anchor=north east] {Asset 1};
\node at (axis cs:20,11) [anchor=north west] {Asset 2};
\addplot[red, very thick, domain=-1:2.5, samples=200, variable=\t](
{(20^2*t^2 + 12^2*(1-t)^2)^(0.5) }, %{(t^2 * 20^2 + (1-t)^2 * 12)},
{11 * t + (1-t) * 5} );
\node[rotate=18, color=red] at (axis cs:50, 20) [anchor=south east] {\textbf{Efficient Frontier}};
\end{axis}
More than two Assets #
\begin{axis}[
height=7cm, width=12cm,
axis x line=bottom, axis y line=left,
xlabel = Standard Deviation, ylabel = Expected Return,
ymin=0, ymax=25, xmin=0, xmax=60,
extra x ticks={65}, extra x tick labels={$\sigma$},
extra x tick style={major tick length=0mm, grid=none},
extra y ticks={25}, extra y tick labels={$\mathbf{E}\{R\}$},
extra y tick style={major tick length=0mm, grid=none},
enlargelimits=true,
scatter/classes={
a={mark=o,draw=black, mark size = 3pt},
b={mark=*, mark size = 3pt,draw=red, fill = red},
c={mark=*, mark size = 3pt,draw=black, fill = black}
}
]
\addplot[scatter,only marks, scatter src=explicit symbolic]
coordinates {
(30, 11) [c]
(19, 5) [c]
(40, 15) [c]
(16, 10) [b]
};
\node at (axis cs:19, 5) [anchor=north west] {MSFT};
\node at (axis cs:30,11) [anchor=north west] {AAPL};
\node at (axis cs:40,15) [anchor=north west] {KO};
\addplot[red, very thick, domain=-1:2.5, samples=200, variable=\t](
{(20^2*t^2 + 12^2*(1-t)^2)^(0.5) }, %{(t^2 * 20^2 + (1-t)^2 * 12)},
{11 * t + (1-t) * 5}
);
\node[color=red] at (axis cs:30, 0) [anchor=south west] {\textbf{Efficient Frontier}};
\addplot[black, very thick, domain=-10:50, samples=100, variable=\x](
({x}, {4 + 0.37 * x});
\node[rotate = 25, color=black] at (axis cs:30, 15) [anchor=south west] {\textbf{Optimal CAL}};
\node[pin={[pin edge={thick}, text width=3cm, pin distance=2cm]90:{{\centering Mean Variance Efficient Porfolio}}}] at (axis cs:16, 10.5) {};
\end{axis}
Security Market Line #
\begin{axis}[
height=7cm, width=12cm,
axis x line=bottom, axis y line=left,
xlabel = Market Risk, ylabel = Expected Return,
ymin=0, ymax=24, xmin=0, xmax=2.5,
xtick={0, 0.5, 1, 1.5, 2}, extra x ticks={2.5},
extra x tick labels={$\beta$},
extra x tick style={
major tick length=0mm,
grid=none
},
ytick={3, 7, 11, 15}, yticklabels={$R_f$, $R_1$, $R_M$, $R_2$},
extra y ticks={23}, extra y tick labels={$\mathbf{E}\{R\}$},
extra y tick style={
major tick length=0mm,
grid=none
},
scatter/classes={
a={mark=o,draw=black, mark size = 3pt},
b={mark=*, mark size = 3pt,draw=red, fill = red},
c={mark=*, mark size = 3pt,draw=black, fill = black}
}
]
\addplot[color=black, domain=-10:3, line width=1pt]
{3 + x * 8};
\addplot[scatter,only marks, scatter src=explicit symbolic]
coordinates {
(0,3) [a]
(1, 11) [b]
(0.5, 7) [c]
(1.5, 15) [c]
};
\draw[dashed] (axis cs:0.5,0) -- (axis cs:0.5,7);
\draw[dashed] (axis cs:1.5,0) -- (axis cs:1.5,15);
\draw[dashed, color=red] (axis cs:1,0) -- (axis cs:1,11);
\draw[dotted] (axis cs:0,7) -- (axis cs:0.5,7);
\draw[dotted] (axis cs:0,15) -- (axis cs:1.5,15);
\draw[dotted, color=red] (axis cs:0,11) -- (axis cs:1,11);
\node[rotate=23] at (axis cs:2.4, 23) [anchor=south east] {\textbf{Security Market Line}};
\node[pin={[pin edge={thick}, pin distance=2cm]90:{Market Porfolio}}] at (axis cs:1,11.5) {};
\end{axis}
Capital Market Line and Security Market Line #
\begin{tikzpicture}
\begin{groupplot}[
group style={group size=2 by 1, horizontal sep=2cm}, no markers,
height=7cm, width=10cm,
axis x line=bottom, axis y line=left,
ymin=0, ymax=24,
extra y ticks={23}, extra y tick labels={$\mathbf{E}\{R\}$},
extra y tick style={major tick length=0mm, grid=none},
scatter/classes={
a={mark=o,draw=black, mark size = 3pt},
b={mark=*, mark size = 3pt,draw=red, fill = red},
c={mark=*, mark size = 3pt,draw=black, fill = black}
}
]
% PLOT 1
\nextgroupplot[
xlabel = Standard Deviation,
ylabel = Expected Return,
xmin=0, xmax=55,
extra x ticks={55}, extra x tick labels={$\sigma$},
extra x tick style={major tick length=0mm, grid=none},
xtick={0, 16, 30, 40 },
xticklabels={0, $\sigma_M$, $\sigma_{\text{AAPL}}$, $\sigma_{\text{KO}}$ },
ytick={3, 5, 10, 15},
yticklabels={$R_f$, $R_{\text{MSFT}}$, $R_M$, $R_{\text{KO}}$}
]
\addplot[scatter,only marks, scatter src=explicit symbolic]
coordinates {
(30, 11) [c]
(19, 5) [c]
(40, 15) [c]
(16, 10) [b]
};
\node at (axis cs:19, 5) [anchor=north west] {MSFT};
\node at (axis cs:30,11) [anchor=south west] {AAPL};
\node at (axis cs:40,15) [anchor=north west] {KO};
\addplot[red, very thick, domain=-0.5:2.5, samples=200, variable=\t](
{(18^2*t^2 + 16^2*(1-t)^2)^(0.5) }, %{(t^2 * 20^2 + (1-t)^2 * 12)},
{11 * t + (1-t) * 4}
);
\addplot[black, very thick, domain=-10:50, samples=100, variable=\x](({x}, {3 + 7/16 * x});
\node[rotate = 35, color=black] at (axis cs:30, 16) [anchor=south west]{\textbf{Optimal CAL}};
\node[pin={[pin edge={thick}, text width=3cm, pin distance=2cm]90:{{\centering Mean Variance Efficient Porfolio}}}] at (axis cs:16, 10.5) {};
\draw[dotted, color=red] (axis cs:0, 10) -- (axis cs: 16,10);
\draw[dashed, thick, color=red] (axis cs:16, 10) -- (axis cs: 55,10);
\node[] (market1) at (axis cs:55.,10) {};
\draw[dotted] (axis cs:0, 15) -- (axis cs: 40, 15);
\draw[dashed] (axis cs:40, 15) -- (axis cs: 55,15);
\node[] (ko1) at (axis cs:55.,15) {};
\draw[dotted] (axis cs:0, 5) -- (axis cs: 19, 5);
\draw[dashed] (axis cs:19, 5) -- (axis cs: 55,5);
\node[] (ms1) at (axis cs:55, 5) {};
% PLOT 2
\nextgroupplot[
xlabel = Market Risk,
xmin=0, xmax=2,
xtick={0, 1, 0.2857142857142857, 1.7142857142857142},
xticklabels={0 , $\beta^M$, $\beta^{\text{MSFT}}$, $\beta^{\text{KO}}$},
ytick=\empty
]
\addplot[color=black, domain=-10:3, line width=1pt]{3 + x * 7};
\addplot[scatter,only marks, scatter src=explicit symbolic]
coordinates {
(0,3) [a]
(1, 10) [b]
(2/7, 5) [c]
(12/7, 15) [c]
};
\draw[dotted, color=red] (axis cs:1,0) -- (axis cs:1,10);
\draw[dashed, color=red] (axis cs:0,10) -- (axis cs:1,10);
\draw[dotted] (axis cs:12/7,0) -- (axis cs:12/7, 15);
\draw[dashed] (axis cs:0,15) -- (axis cs:12/7,15);
\draw[dotted] (axis cs:2/7,0) -- (axis cs:2/7, 5);
\draw[dashed] (axis cs:0,5) -- (axis cs:2/7,5);
\node[] (market2) at (axis cs:0.,10) {};
\node[] (ko2) at (axis cs:0,15) {};
\node[] (ms2) at (axis cs:0, 5) {};
\node[rotate=23] at (axis cs:2.4, 23) [anchor=south east] {\textbf{SML}};
\node[pin={[pin edge={thick}, pin distance=2cm]90:{Market Porfolio}}] at (axis cs:1,11.5) {};
\end{groupplot}
\draw[dashed, thick, red] (market1) -- (market2) ;
\draw[dashed] (ko1) -- (ko2) ;
\draw[dashed] (ms1) -- (ms2) ;
\end{tikzpicture}
Treynor-Black Portfolio Selection #
If investors have information on some portfolio, they might want to trade beyond the passive allocation of holding the market portfolio. How much should they weigh the “active” section of their portfolio with respect to their passive investment? Standard mean-variance algebra gives a formula for the maximum Sharpe ratio of a portfolio of two risky assets. Coupled with the “diagonal version” of the market model, we find that the weight corresponds to the ratio of excess returns to variances.
\begin{axis}[
height=7cm, width=12cm,
axis x line=bottom, axis y line=left,
xlabel = Standard Deviation, ylabel = Expected Return,
ymin=0, ymax=35, xmin=0, xmax=35,
xtick={0, 10, 20, 30},
extra x ticks={35}, extra x tick labels={$\sigma$},
extra x tick style={major tick length=0mm, grid=none},
extra y ticks={3, 35}, extra y tick labels={$r_f$, $\mathbf{E}\{R\}$},
extra y tick style={major tick length=0mm, grid=none},
enlargelimits=false,
scatter/classes={
a={mark=o,draw=black, mark size = 3pt},
b={mark=*, mark size = 3pt,draw=red, fill = red},
c={mark=*, mark size = 3pt,draw=black, fill = black}
}
]
\pgfmathsetmacro{\rf}{2}
\pgfmathsetmacro{\muM}{8}
\pgfmathsetmacro{\sigM}{9}
\pgfmathsetmacro{\muA}{20}
\pgfmathsetmacro{\sigA}{15}
\pgfmathsetmacro{\rhoAM}{0.1}
\pgfmathsetmacro{\sigAM}{\sigA * \sigM * \rhoAM}
\addplot[scatter,only marks, scatter src=explicit symbolic]
coordinates {
(\sigM, \muM+\rf) [c]
(\sigA, \muA+\rf) [c]
};
\node at (axis cs:\sigM, \muM+\rf) [anchor=south west] {Market Portfolio};
\node at (axis cs:\sigA, \muA+\rf) [anchor=north west] {Active Portfolio};
\pgfmathsetmacro{\omA}{(\muM*\sigAM - \muA * \sigM^2) / ( (\muA+\muM)*\sigAM - \muA*\sigM^2 - \muM*\sigA^2)}
\pgfmathsetmacro{\muP}{\omA * \muA + (1-\omA) * \muM}
\pgfmathsetmacro{\sigP}{(\omA^2 * \sigA^2 + (1-\omA)^2 * \sigM^2 + 2*\omA*(1-\omA)*\sigAM)^(1/2)}
\addplot[scatter,only marks, scatter src=explicit symbolic]
coordinates {
(\sigP, \muP+2) [b]
};
\addplot[red, very thick, domain=-0.75:1.5, samples=200, variable=\t](
{(\sigA^2 * t^2 + \sigM^2*(1-t)^2 + 2*t*(1-t)*\sigAM)^(0.5)},
{\muA * t + (1-t) * \muM + \rf}
);
\addplot[black, very thick, domain=0:18, samples=100, variable=\x](
({x}, {2 + \muP/\sigP * x});
\node[pin={[pin edge={thick}, text width=3cm, pin distance=1.5cm]90:{{\centering maximum Sharpe Ratio portfolio}}}] at (axis cs:\sigP, \muP+3) {};
\node[text centered, text width = 5cm] at (axis cs:20, 10) [anchor=south west] {{Active portfolio weight: \\$\omega_a = \frac{\alpha_A/\sigma_A^2}{\mu_M / \sigma_M^2}$}};
\end{axis}