This note computes log returns, annualized volatility, and visualizes the distribution for a liquid asset.
Definitions
Given a price series \(P_t\), the log return is:
\[
r_t = \ln\left(\frac{P_t}{P_{t-1}}\right)
\]
Annualized volatility (assuming 252 trading days):
\[
\sigma_{\text{ann}} = \sqrt{252} \cdot \text{std}(r_t)
\]
Compute (Python)
Code
import numpy as np
import pandas as pd
import yfinance as yf
import plotly.express as px
ticker = "SPY"
period = "2y"
df = yf.download(ticker, period=period, auto_adjust=True, progress=False)
df.columns = df.columns.droplevel(1) # flatten MultiIndex columns
df = df[["Close"]].dropna()
df["r"] = np.log(df["Close"] / df["Close"].shift(1))
df = df.dropna()
sigma_daily = df["r"].std()
sigma_ann = np.sqrt(252) * sigma_daily
mu_daily = df["r"].mean()
mu_ann = 252 * mu_daily
summary = pd.DataFrame({
"ticker": [ticker],
"period": [period],
"mu_daily": [mu_daily],
"mu_ann": [mu_ann],
"sigma_daily": [sigma_daily],
"sigma_ann": [sigma_ann],
"n_obs": [len(df)]
})
summary
| 0 |
SPY |
2y |
0.000799 |
0.201399 |
0.010242 |
0.162592 |
501 |
Distribution
Code
fig = px.histogram(
df,
x="r",
nbins=80,
title=f"{ticker} log return distribution ({period})",
marginal="box"
)
fig.update_layout(xaxis_title="log return", yaxis_title="count")
fig.show()