Real Life Example: Portfolio Optimization¶
Consider an asset whose price is evaluated continuously in time, and denoted by $P(t)$. In general, we are interested in buying the asset at some time $t_0$ and selling it at some later time $t_1$, hoping to make some profit. The profit -- called "return" -- is of course
\begin{align}
R(t_1,\Delta t) := P(t_1) - P(t_0)
\end{align}
where $\Delta t = t_1 - t_0$ is the period of time we held onto the asset. Now, the return is a quantity with dimensions of currency, so it is not good if we want to compare it with the returns of other assets. For this reason, we divide it by the initial price $P(t_0)$, thus obatining the "rate of return" for the period $\Delta t$:
\begin{align}
r(t,\Delta t) := \dfrac{R(t,t - \Delta t)}{P(t - \Delta t)} = \dfrac{P(t)}{P(t - \Delta t)} - 1
\end{align}
The rate of return can be thought of as a percentage: it tells us how much we have earned/lost after a time period $\Delta t$, as a percentage of our initial investment. For example, suppose $r(\text{today},1 \ \text{year}) = 0.06$, that is, our investment had a yearly rate of return of $6\%$ in the year just past: this means that, if we sold today the asset that we bought one year ago for $V$ dollars, we would be making a profit of $0.06 \times V$ dollars.
How to estimate an asset's $r(t,\Delta t)$ for future $t$? First, we observe that $r(t,\Delta t)$ is derived from time series $P(t)$ by differencing: as such, it is expected to be an (approximately) stationary time series. Hence, we can imagine that historical data -- i.e., values $x_\mu := r(t_\mu,\Delta t)$ with $\mu = 1,..,m$ and $t_\mu := \mu \Delta t$ (for a total period of length $T := m \Delta t$, where $T = \text{today}$) -- are $m$ outcomes of a stochastic process $X$ with mean and variance independent of $t$. Hence, the fundamental mean $\mathbb E[X]$ and variance $\text{Var}[X] = \mathbb E[X^2] - \mathbb E[X]^2$ can be estimated by the sample mean and variance:
\begin{align}
\mathbb E[X] \approx \bar r := \dfrac{1}{m} \sum_{\mu = 1}^m x_\mu, \ \ \ \ \ \text{Var}[X] \approx \bar \sigma^2 := \dfrac{1}{m} \sum_{\mu = 1}^m x_\mu^2 - \bar r^2
\end{align}
More generally, if we have $n$ different assets, each can be associated with a stochastic process $X_{(i)}$ (with $i = 1,2,..,n$). We can then estimate the rate of return $r_i$ of $i$th asset as
\begin{align}
\mathbb E[X_{(i)}] \approx \bar r_i = \dfrac{1}{m} \sum_{\mu = 1}^m x_{\mu,i}
\end{align}
where $x_{\mu,i}$ is the historical rate of return of $i$th asset at time $t_\mu$. The covariance matrix $\text{Cov}[X_{(i)},X_{(j)}] = \mathbb E[X_{(i)}X_{(j)}] - \mathbb E[X_{(i)}] \mathbb E[X_{(j)}]$ can be estimated as
\begin{align}
\text{Cov}[X_{(i)},X_{(j)}] \approx \bar \Sigma_{ij} := \dfrac{1}{m} \sum_{\mu=1}^m x_{\mu, i} x_{\mu, j} - \bar r_i \bar r_j
\end{align}
and it is easy to see that $\bar \Sigma_{ii} = \bar \sigma_i^2$, the sample variance of the $i$th asset. Using matrix notation, we can write
\begin{align}
\mathbb E[X] \approx \bar r, \ \ \ \ \ \text{Cov}[X^T,X] \approx \bar \Sigma = \dfrac{1}{m} x^T x - \bar r^T \bar r
\end{align}
where we think of $X$ (resp., $\bar r$) as a $n$-dimensional row-vector with $i$th element $X_{(i)}$ (resp., $\bar r_i$), and of $x$ as the $m \times n$ matrix of historical data. (Note that the vector $\bar r$ is given by the mean value of each column of $x$.)
Now, a portfolio $P$ of the assets $X_{(1)},..,X_{(n)}$ consists of a linear combination
\begin{align}
P = \sum_{i=1}^n X_{(i)} w_i
\end{align}
with weights $0 \leq w_i \leq 1$ such that
\begin{align}
\sum_{i=1}^n w_i = 0
\end{align}
Collecing the weights into an $n$-dimensional column-vector $w$, we can write compactly
\begin{align}
P = X \cdot w
\end{align}
It is now easy to see that the rate of return $r_P$ and the variance $\sigma_P^2$ (also called "volatility") of the portfolio are
\begin{align}
r_P(w) = \mathbb E[P] = \mathbb E[X] \cdot w
\end{align}
and
\begin{align}
\sigma_P^2(w) = \mathbb E[P^T P] - \mathbb E[P^T] \mathbb E[P] = w^T \left(\mathbb E[X^T X] - \mathbb E[X^T] \mathbb E[X]\right) w = w^T \text{Cov}[X^T,X] w
\end{align}
They can therefore be estimated from the historical data as
\begin{align}
r_P(w) \approx \bar r \cdot w, \ \ \ \ \ \ \ \ \ \ \sigma_P^2(w) \approx w^T \bar \Sigma w = \dfrac{1}{m} w^T x^T x w - (\bar r \cdot w)^2
\end{align}
We can now use the methods described above to optimize the portfolio: for example, we can find the weights $w$ that
minimize the volatility $\sigma_P^2$ while producing at least a certain rate of return, $r_P \geq r_*$
maximize the rate of return $r_P$ while keeping the volatility below an acceptable threshold, $\sigma_P^2 \leq \sigma_*^2$
maximize the Sharpe ratio $r_P/\sigma_P$
all under the constraints $0 \leq w_i \leq 1$ for all $i=1,2,..,n$ and $\sum_{i=1}^n w_i = 1$. We can also identify the **efficient frontier**, i.e., the curve $C$ in the $\sigma_P^2 - r_P$ plane of "best portfolios":
\begin{align}
C = \left\{(\sigma^2,r) \in \mathbb R^2 : r = \max_{w} r_P(w) \ \text{subject to} \ \sigma_P^2(w) = \sigma^2\right\}
\end{align}