{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Risk"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The future return that you will get is obviously unknown unless you have a crystal ball. We can, however, look at the past data of the return and get an idea of the distribution of the return. In this way we can calculate the expected return (average return) and how much the data variate from this value (standard deviation of the return). \n",
"\n",
"An investment therefore has an **expected return**, which is the value that we expect to have moreorless, and a **volatility**, which is a measure of how much we can get less or more that the expected return.\n",
"\n",
"Therefore, looking at past data and supposing that our investment in the future will behave in the same way, we can calculate the exected return as the average and the risk as the standard deviation, i.e. the square root of the variance."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For example, consider the returns of these two stocks with 4 years of past history."
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"# as an example, suppose ve have\n",
"stock1 = pd.Series([+0.15,+0.20,-0.05,+0.10])\n",
"stock2 = pd.Series([+0.35,-0.30,+0.40,-0.05])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"They have the same expected return, but definitely not the same volatility."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Real example"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import yfinance as yf"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [],
"source": [
"tickers=[\"PG\",\"MSFT\"] # Procter & Gamble and Microsoft"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Calculating the portfolio's expected return knowing only the stock's average returns (and not all the daily stock returns)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is very easy to calculate a portfolio's expected returns without knowing the stock's daily returns, because the expected return is an average and the average of a weighted sum is the weighted sum of the averages! Therefore instead of calculating the daily portfolio returns, then the daily portfolio returns and then do the average, we calculate the average of the daily returns of each stock and then calculate the portfolio yearly return.\n",
"$$\\text{portfolio return}=w_1\\cdot r_1+w_2\\cdot r_2 = \\sum_{i=1}^n w_i\\cdot r_i$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Calculating the portfolio's volatility knowing only variances and covariances of stocks' returns (and not all the daily stock returns)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The risk is the standard deviation and unfortunatly for the standard deviation this formula does not work!\n",
"$$\\text{portfolio volatility}\\neq w_1\\cdot v_1+w_2\\cdot v_2 = \\sum_{i=1}^n w_i\\cdot v_i$$\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The formula, using the variance instead of the standard deviation (remember that variance is the square of the standard deviation) is\n",
"$$\\text{variance portfolio}=w_1^2\\cdot \\text{var}_1+w_2^2\\cdot \\text{var}_2 + 2\\cdot w_1\\cdot w_2\\cdot \\text{cov}_{1,2}$$\n",
"Se we can get the variance, and thus the standard deviation, using the covariance and the variances of the stocks' returns. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, this easy formula becomes much more complicated for more than two stocks and it is better to express it in matrix format. If we consider the covariance matrix above and the weights vector\n",
"$$\n",
"\\begin{matrix}\n",
"\\begin{pmatrix}\n",
"\\text{var}_1&\\text{cov}_{12}\\\\\n",
"\\text{cov}_{21}&\\text{var}_2\n",
"\\end{pmatrix}&&&&&\n",
"\\begin{pmatrix}\n",
"w_1\\\\\n",
"w_2\n",
"\\end{pmatrix}\n",
"\\end{matrix}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and we multiply them\n",
"$$\n",
"\\begin{pmatrix}\n",
"\\text{var}_1\\cdot w_1+\\text{cov}_{12}\\cdot w_2\\\\\n",
"\\text{cov}_{21}\\cdot w_1+\\text{var}_2\\cdot w_2\n",
"\\end{pmatrix}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and if we multiply by the vector in this way, remembering that $\\text{cov}_{12}=\\text{cov}_{21}$\n",
"$$\n",
"\\begin{pmatrix}\n",
"w_1&w_2\n",
"\\end{pmatrix}\n",
"\\cdot\n",
"\\begin{pmatrix}\n",
"\\text{var}_1\\cdot w_1+\\text{cov}_{12}\\cdot w_2\\\\\n",
"\\text{cov}_{21}\\cdot w_1+\\text{var}_2\\cdot w_2\n",
"\\end{pmatrix}\n",
"=w_1\\cdot (\\text{var}_1\\cdot w_1+\\text{cov}_{12}\\cdot w_2)+w_2\\cdot (\\text{cov}_{21}\\cdot w_1+\\text{var}_2\\cdot w_2)\n",
"=\\text{var}_1\\cdot w_1^2+2\\cdot\\text{cov}_{12}\\cdot w_2\\cdot w_1+\\text{var}_2\\cdot w_2^2\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Thus the volatility of the portfolio of $n$ stocks can be calculated using the square root of $\\text{variance portfolio}=\\text{weights}^T\\cdot\\text{Cov Matrix}\\cdot\\text{weights}$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Systematic versus diversifiable risk"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given the formula for the variance $\\text{weights}^T\\cdot\\text{Cov Matrix}\\cdot\\text{weights}$, we can split this risk into:\n",
"\n",
" - systematic risk as $\\sum_{i=1}^n w_i^2\\cdot \\text{var}_i$, i.e. for the two stocks example $\\text{var}_1\\cdot w_1^2+\\text{var}_2\\cdot w_2^2$\n",
"
- diversifiable risk as $\\text{variance portfolio}-\\text{systematic risk}$, i.e. for the two stocks example $2w_2 w_1\\cdot \\text{cov}_{12}$\n",
"

\n",
"The diversifiable risk is the part which can be strongly reduced choosing uncorrelated stocks, i.e. stocks with small correlation and thus small covariance. On the other hand, the systematic risk is intrinsic to the stocks and cannot be removed.\n",
"\n",
"Beware that here we are talking about variance and not standard deviation."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}