One model for the daily return
Every risk number starts with a model of the daily return. The simplest model is a normal curve with the average and the standard deviation of the sample. The Student t model adds one number, the degrees of freedom ν: a small ν means heavy tails, and when ν is large the t curve turns into the normal one. scipy.stats gives every distribution the same four methods: fit finds the parameters by maximum likelihood, logpdf gives the log of the density, cdf gives the probability of falling below a value and ppf is its inverse, the quantile. Learn the four once and every distribution in SciPy works the same way, so changing the model is a one-word change.
To choose between models you need a score and a test. The log-likelihood log L=Σᵢlog f(rᵢ) adds up how likely the whole sample is under the model, and the Akaike criterion AIC=2k-2log L, with k the number of fitted parameters, charges a price for every extra parameter: lower is better. The Kolmogorov–Smirnov test measures the largest gap between the sample's cumulative curve and the model's. stats.kstest(r, dist.cdf, args=params) returns it with a p-value, and a tiny p-value says the sample does not look like a draw from that model. Because the parameters were fitted on the same sample the p-value is flattering, so use it to compare models, not as a final verdict. Finally the model becomes a risk number: the one-day value at risk is minus the quantile, -dist.ppf(alpha, *params), and the expected number of bad days is the sample size times dist.cdf(-level, *params).
- Same four methods everywhere:
fit,logpdf,cdfandppfwork for everyscipy.statsdistribution, so models are interchangeable. - Score, then test: AIC compares models with a price for parameters, the KS test asks whether the sample could come from a model at all.
- Tail counts expose the model: count the days below -5% and compare them with
n * cdf; a normal curve misses by a factor of hundreds.
Lab · what's graded
- Daily returns of a column10 pts
- Two fitted models15 pts
- A log-likelihood10 pts
- Comparing models with the AIC10 pts
- A Kolmogorov–Smirnov p-value15 pts
- A value at risk from a quantile10 pts
- Observed and expected extreme days10 pts
- The whole study20 pts