Volatility An interactive monograph on the quant-finance Python ecosystem

Volatility

The quantities the financial industry tracks daily, the mathematics that defines them, and the Python libraries that put both within reach of a working analyst.

An interactive monograph in ten sections, with seven live figures. The figures are operable by mouse, touch and keyboard, and the document conforms to WCAG 2.2 AA. Built as a single static page in vanilla HTML, CSS and JavaScript, with no framework and no build step.

Abstract

Quantitative finance asks a precise question of an imprecise object: given a record of prices that flutter unpredictably, what can be said about the future returns and the future losses of an investment, what mixture of investments is best, and how should the methods used to answer those questions be tested. This document develops the modern answers from the foundation, starting with the definition of returns and volatility, moving through portfolio construction, time-varying volatility, downside risk measurement, backtesting and drawdowns, and ending with the joint risk of several assets. Throughout, it traces the Python libraries that implement each step: NumPy and pandas as the substrate; statsmodels and arch for econometrics; QuantLib for instrument pricing; riskfolio-lib, PyPortfolioOpt and CVXPY for portfolio optimisation; vectorbt, zipline and backtrader for backtesting; pyfolio and empyrical for performance attribution.

The figures run the genuine computation over synthetic data. The rolling volatility is a real moving-window standard deviation; the mean-variance frontier is the upper envelope of an exact sample of portfolio variances; the GARCH series is simulated by the real recursion; Value at Risk and Conditional Value at Risk are the empirical quantile and the conditional mean below it; the backtest runs a moving-average crossover with positions resolved daily and compounding correctly; the drawdown is the difference between the running maximum and the current value; the risk decomposition uses the exact covariance-based marginal contributions. Nothing on display is approximated by a trained model or hidden inside an opaque library, so the reader can see how each Python function actually does its work.

1Prices, returns and what is and is not random

A financial price series is, on first inspection, a record of the values at which an asset has changed hands over time. It is far more variable than most quantities encountered in the natural sciences, and unlike them it admits no closed-form generating process: the prices reflect the trades of many participants whose information, mood and constraints are not in the modeller's notebook. The methods of quantitative finance are nonetheless built on the orderly study of these series, because two transformations render them tractable. The first is to look not at the prices but at the returns, the proportional change from one price to the next; the second is to assume, with caveats, that the returns are approximately independent draws from some distribution whose parameters change slowly.

The choice between simple returns and logarithmic returns is the first technical decision. Simple returns are differences in price divided by the previous price, and they have the property that the return of a portfolio is the weighted average of the returns of its components. Logarithmic returns are differences in the natural logarithm of the price, and they have the property that they add across time: the log return over a week is the sum of the log returns of the days within the week. Each is preferred for one of the two operations, and a careful library, NumPy and pandas included, exposes both rather than forcing a choice.

The distribution of daily returns, examined empirically, departs in two ways from the normal distribution that the basic models assume. The tails are heavier than a normal predicts, so extreme moves happen more often than a Gaussian model says they should; this is the so-called fat-tail or leptokurtic property of financial returns, and it is the reason that risk measures based on the normal underestimate tail loss in real data. The variance is not constant: periods of low and high volatility cluster, with each large move tending to be followed by another large move and each quiet day by another quiet day. The two phenomena together make the practice of risk measurement an exercise in modelling time-varying, fat-tailed distributions, and several of the methods treated below exist precisely to capture them.

Return
The relative change in a price from one period to the next. The simple return at time t equals (Pt − Pt-1) / Pt-1; the logarithmic return equals log(Pt) − log(Pt-1). Returns are the unit of analysis in quantitative finance because they are approximately stationary in a way that prices are not.

2Returns and rolling volatility

Volatility is the standard deviation of returns. As a parameter attached to a particular distribution it is constant; as a quantity estimated from data over a finite window it varies. The difference matters in practice because a rolling estimate, the standard deviation computed over the last n days of returns, is what a real risk dashboard displays. A long window gives a stable estimate but is slow to reflect a change in regime; a short window reacts to regime changes quickly but is itself noisy.

The figure below shows a synthetic price series with a regime change in its middle, and a band of plus or minus two rolling standard deviations drawn around the current price. The window slider changes the trade-off between smoothness and reaction time visibly: with a five-day window the band shivers in response to every noisy day; with a sixty-three-day window it is calm but slow. The drift slider changes the expected return, leaving the rolling volatility largely unchanged.

The same computation in Python is a single pandas method: returns.rolling(window=21).std() returns the same rolling estimate the figure draws. The Series object that pandas uses for time-indexed data is the most-used object in quantitative finance work, and a great deal of practitioners' code is a composition of these single-method calls. The volatility scale reported in industry is annualised: the daily volatility is multiplied by the square root of approximately 252, the number of trading days in a year, so that a sixteen-per-cent annual volatility corresponds to a daily standard deviation of about one per cent. The square-root scaling assumes independent returns and is approximately right when returns are nearly independent, which they nearly are on daily horizons in liquid markets.

3A history of the quant-finance Python ecosystem: a literature review

The Python ecosystem for quantitative finance is a layered stack, and each layer was introduced to remove a definite limitation of the layer beneath it. The account that follows is causal: in each case the question is which earlier shortcoming the next library or method was built to answer.

The methodological substrate predates Python. Harry Markowitz's 1952 paper on portfolio selection [1] introduced the idea that the risk of a portfolio is not the average of the risks of its components but a quantity that depends on their covariances, and that for a given level of risk there is a portfolio of maximum expected return. William Sharpe's capital-asset-pricing model [2] made the structure of those expectations more specific. Fischer Black and Myron Scholes's option-pricing formula [3] gave quantitative finance its first widely used closed-form result. The treatment of time-varying volatility came with Robert Engle's ARCH paper [4], generalised by Tim Bollerslev into GARCH [5], which became the standard parametric model for the volatility clustering observed in real returns. The risk-management vocabulary that the present industry uses, in particular Value at Risk and its variants, was consolidated by Philippe Jorion [6]. The mathematical formalisation of what makes a risk measure well-behaved, with the discovery that Value at Risk fails one of the desirable axioms and that Conditional Value at Risk does not, came from Artzner and colleagues' coherent measures of risk [7].

The transition into Python began with the array library. NumPy [8] gave Python efficient n-dimensional arrays, broadcasting and the linear-algebra routines that make a covariance computation a single function call. The early Python finance code, however, lacked first-class support for time-indexed data: every analysis began with index alignment, missing-data handling and resampling, all of which had to be written by hand. Wes McKinney's pandas [9], released in 2008 and described in a 2010 paper, removed that limitation. The DataFrame and Series, with their time-aware index and method chains for windowing and grouping, became the central currency of quantitative-finance work in Python, and the methods of NumPy were used through pandas at least as often as directly.

A further word on stationarity is worth adding because it controls almost everything that follows. A time series is stationary if its statistical properties do not change over time; financial return series are approximately stationary on short horizons and measurably non-stationary on long ones. The methods that assume stationarity, including rolling estimators of mean and variance, GARCH models with fixed parameters, and most backtests, work well on a stretch of data over which the underlying market regime did not change and degrade quickly when it did. A great deal of practical quantitative-finance discipline is the management of this non-stationarity: choosing window lengths short enough to adapt and long enough to estimate; using exponentially weighted averages instead of equal-weighted ones; refitting GARCH and factor models on a rolling basis; and segmenting backtests by regime so that a strategy's performance in one regime is not taken as evidence of its performance in another.

Econometrics in Python followed. The statsmodels library [10], described by Seabold and Perktold in a 2010 paper, brought the classical regressions, tests and time-series models that R users had long taken for granted. The arch library, by Kevin Sheppard, specialised in GARCH-family volatility models because statsmodels' coverage of conditional heteroscedasticity was thin; arch was introduced to remove that gap, and its API became the standard for fitting and forecasting volatility models from a pandas Series of returns. For derivative pricing the bridge to the long-established C++ QuantLib library was the SWIG-generated Python binding, which gave Python users access to interest-rate term-structure construction, option pricing and Monte-Carlo simulation without rewriting QuantLib's two decades of validated numerical code.

Portfolio optimisation acquired several Python implementations as the convex-optimisation tooling matured. The CVXPY library [11] let users write a mean-variance or risk-parity programme in a few lines of code as a convex problem, and modern portfolio-construction libraries such as PyPortfolioOpt and riskfolio-lib, by Robert Andrew Martin and Dany Cajas respectively, layered ready-to-use objectives on top of CVXPY together with covariance-shrinkage estimators and factor-model utilities. The proliferation of these libraries was driven by the gap between what statsmodels covered and what a practitioner needed for the day's actual portfolio: a covariance shrinkage, a constraint on weight bounds, an objective that balanced expected return against risk and that handled corner cases when the covariance matrix was singular.

The backtesting frameworks formed the most fragmented layer of the stack. Zipline, released by Quantopian in 2012, took the event-driven simulation approach used in trading systems and made it scriptable in Python. Backtrader, by Daniel Rodriguez, offered a similar abstraction with a different API. Vectorbt, by Oleg Polakov, took a fundamentally different approach: rather than simulating events one at a time, it vectorised the entire backtest into NumPy operations so that thousands of parameter combinations could be evaluated in a single pass. The fragmentation reflected the lack of a canonical answer to questions of speed, flexibility and ergonomic preference. The performance-attribution piece was supplied by pyfolio and empyrical, both originating at Quantopian, which standardised the calculations of Sharpe ratio, Sortino ratio, maximum drawdown and the rest of the performance-tear-sheet vocabulary.

The most recent thread, and the one with the most visible activity, is the integration of machine learning. Marcos López de Prado's Advances in Financial Machine Learning [12] codified a set of practices for applying machine-learning techniques to financial data in a way that respects its peculiarities, in particular the small effective sample size produced by overlapping returns and the non-stationarity that violates the iid assumptions of most learning theory. The libraries that have grown around this thread integrate the scikit-learn, statsmodels and pandas conventions with the financial-specific corrections López de Prado names, and they sit alongside the older quant-finance stack as a parallel tradition rather than a replacement for it.

Two threads run through this history. The first is a movement from low-level numerical primitives toward domain-specific abstractions: pandas was an abstraction over NumPy that knew about time, riskfolio-lib was an abstraction over CVXPY that knew about portfolios, pyfolio was an abstraction over pandas that knew about backtests. The second is the steady absorption of methods from outside finance, especially from the mathematical-statistics and machine-learning communities, into a stack that began by recreating mid-twentieth-century actuarial calculations in modern code.

Return to contents

4Portfolio construction and the efficient frontier

The portfolio construction problem in its simplest form is the one Markowitz [1] stated. Several assets have estimated expected returns and an estimated covariance matrix of returns. A portfolio is a choice of weights summing to one, and its expected return is the weighted average of the asset returns while its variance is the weighted quadratic form of the weights against the covariance matrix. The mean-variance frontier is the set of portfolios that achieve the maximum expected return at each level of variance. Below the frontier are dominated portfolios that could be improved at no cost in risk; the frontier itself is the set of efficient portfolios.

The figure below illustrates the geometry for three assets. Each of the three weight sliders sets the corresponding weight; the weights are renormalised to sum to one so that the figure always shows a valid portfolio. The cloud of grey dots is two hundred and twenty random portfolios sampled by drawing weights from the uniform simplex; the solid line is the frontier traced by the upper envelope of the cloud. The current portfolio, fixed by the sliders, is the highlighted square.

Two practical points are worth making. The frontier is computed here by sampling and taking the upper envelope, which is fine for three assets and rapidly degrades with the asset count. Real portfolio-optimisation libraries solve a small quadratic programme for each target return, and obtain the frontier in a fraction of the time. CVXPY [11] formulates the optimisation as a few lines of declarative code; PyPortfolioOpt and riskfolio-lib wrap it with sensible defaults for the inputs. The second point is that the expected returns and the covariance matrix are themselves estimates, and small errors in them produce large changes in the optimal portfolio. The standard responses are shrinkage estimators of the covariance (Ledoit-Wolf is widely used and is available through scikit-learn's covariance module), Bayesian priors on the expected returns (Black-Litterman), and outright robustification of the optimisation against parameter uncertainty.

The covariance estimation question deserves a section in its own right because it dominates the practical performance of any portfolio-optimisation library. The sample covariance matrix is the maximum-likelihood estimator under a Gaussian assumption, but for a portfolio of more than a handful of assets it is also notoriously badly behaved: its eigenvalues are biased, the smallest ones are pushed toward zero, and the corresponding eigenvectors are noisy. A mean-variance optimiser fed the sample covariance gladly leverages those noisy small-eigenvalue directions, producing a portfolio that looks excellent on the estimation set and performs poorly out of sample. Ledoit and Wolf's shrinkage estimator, the most widely used remedy, blends the sample covariance with a structured target (the identity matrix scaled by the average variance, or a constant-correlation model) at a data-driven weight that minimises the expected squared error. The estimator is in scikit-learn's covariance module as LedoitWolf, and the modern practice is to pass its output to the portfolio optimiser rather than the sample covariance directly. Factor-model covariance estimates, based on the principal components of returns or on observed factor exposures, are a parallel route that scales better to large universes; both routes share the goal of reducing the estimation error before it propagates into the optimisation.

5Volatility clustering and GARCH

Real return series exhibit volatility clustering: large absolute returns tend to be followed by large absolute returns and small absolute returns by small. A constant-variance model cannot reproduce this and underestimates the probability of consecutive large moves. The GARCH family of models, introduced by Bollerslev [5] in 1986 as a generalisation of Engle's ARCH [4], lets the variance be its own time series, with the next period's variance depending on the most recent observed squared return and on the previous variance.

The GARCH(1,1) recursion is short. The conditional variance at time t equals a constant omega, plus alpha times the squared return at time t − 1, plus beta times the conditional variance at time t − 1. The parameters alpha and beta jointly govern persistence: their sum is the proportion of last period's variance that is carried forward in expectation, and stationarity requires their sum to be less than one. Values of alpha near a tenth and beta near nine tenths are typical for liquid markets, giving a persistence of about ninety-five per cent: a shock decays slowly and a quiet period is also slow to end.

The figure above simulates a GARCH(1,1) series at the alpha-and-beta values the sliders specify, drawing the returns in the upper panel and the conditional standard deviation that the recursion produces in the lower. The clustering is visible: stretches of large positive and negative returns are accompanied by elevated conditional sigma, and the recovery to quieter regimes is gradual. Fitting a GARCH model to real data, rather than simulating one as the figure does, is the task the arch library handles by maximum likelihood. The user passes a pandas Series of returns and a model specification, and the library returns fitted parameters, standardised residuals for diagnostic tests and forecasts of future conditional variance.

The GARCH framework has been extended in several directions. EGARCH models the logarithm of the variance rather than the variance itself, so that the volatility can respond asymmetrically to positive and negative returns, capturing the leverage effect by which a market drop is followed by higher volatility than a symmetric rise. The TGARCH or GJR-GARCH model captures the same asymmetry with a threshold term. Multivariate GARCH models extend the recursion to the covariance matrix of several assets simultaneously, at substantial computational expense. All are available through the arch library or, for the multivariate cases, through specialised packages such as the rmgarch wrapper.

A related class of models takes seriously the long-memory property of squared returns: the autocorrelation of squared returns at lag k decays as a power of k rather than exponentially, slowly enough that even at a hundred days it is measurably non-zero. The fractionally integrated GARCH model, FIGARCH, replaces the integer differencing of the standard model with a fractional differencing parameter and captures this slow decay. The realised-volatility literature, started by Andersen and Bollerslev in the late 1990s, took a different route: rather than infer the unobserved variance from daily returns, it computed the realised variance directly from intraday high-frequency returns and modelled that observed quantity as a time series in its own right. The two routes converge on similar forecasts at daily horizons and diverge at shorter ones, and the choice between them in production depends on whether high-frequency data is available and on the horizon at which the forecast is needed.

6Value at Risk and Conditional Value at Risk

The Value at Risk at confidence level alpha is the smallest loss such that the probability of a worse loss is at most one minus alpha. Numerically it is a quantile of the loss distribution. Reported as "the daily VaR at 95 per cent is two per cent" it tells the reader that, under the model from which the distribution comes, the loss on any given day will exceed two per cent of the portfolio's value on no more than five per cent of days.

Two limitations of VaR are well known. The first is that VaR gives no information about the size of the losses that occur beyond it. A position whose tail risk consists of a small chance of catastrophic loss and a position whose tail risk is a small chance of moderate loss can have the same VaR. The second is mathematical: VaR is not subadditive, meaning that the VaR of a portfolio can exceed the sum of the VaRs of its components, in violation of the diversification principle. Artzner and colleagues' coherent-risk-measure axioms [7] were proposed specifically because VaR fails subadditivity, and they led to the adoption of Conditional Value at Risk, also called Expected Shortfall, which is the expected loss given that the loss exceeds VaR.

The figure above shows the empirical distribution of six thousand synthetic returns drawn from a Gaussian mixture that mimics fat tails. The Confidence slider moves the VaR threshold to the left or right; the dashed line marks VaR and the dotted line marks CVaR. CVaR is always at least as large as VaR, and it is substantially larger for fat-tailed distributions, because the average of the worst five per cent of returns is much further out than the worst-but-one of those returns. The same shape carries over to real return data: empirical CVaR estimates are larger than what a normal model would predict, and the gap is one of the standard diagnostics of fat-tail behaviour.

Computing VaR and CVaR in Python is straightforward when the assumption is a fitted parametric model, but the empirical version, which the figure uses, is what most production dashboards report. The arch and scipy.stats libraries supply the parametric paths; pandas's quantile method supplies the empirical one. The harder question is the underlying distribution: the assumption of independent identically distributed returns understates risk during regime changes, and the practical fix, used in industry, is to apply the rolling GARCH volatility from the previous section to filter the returns into standardised residuals before computing the quantile, then scale the result back by the latest volatility estimate. The composite measure is the conditional VaR of the filtered series, and it tracks regime changes far better than the unfiltered version.

The regulatory treatment of these measures has shifted noticeably over the last decade. Under the Basel II market-risk framework, banks reported a ten-day VaR at the 99 per cent level as the basis of their market-risk capital. The Basel Committee's Fundamental Review of the Trading Book, published in 2016 and implemented progressively since, replaced VaR with Expected Shortfall at the 97.5 per cent level as the regulatory risk measure, citing the coherent-measure-of-risk axiom failures of VaR and the better tail behaviour of Expected Shortfall. The calibration to a 97.5 per cent ES roughly matches the magnitude of a 99 per cent VaR for normally distributed returns and is more conservative for fat-tailed ones. The Python libraries for risk reporting, including riskfolio-lib and the bespoke risk systems built on top of pandas, all expose both measures; the choice between them is in practice driven by the regulatory environment rather than by the analyst's preference.

7Backtesting a strategy

A trading strategy is a rule that maps the historical record up to time t into a position at time t. A backtest is the counterfactual application of that rule to a historical price series, in order to estimate how the strategy would have performed. Backtesting is the only way to evaluate a strategy before committing capital, and it is the most error-prone step in the entire workflow.

The figure below runs a simple moving-average crossover: the strategy is long the asset when the fast moving average is above the slow one and flat otherwise. The fast and slow window sliders control the two moving averages. The cumulative return of the strategy is drawn against the buy-and-hold benchmark on the same axes; the difference between the two curves is the excess return the strategy produces.

The figure is honest about a structural simplification. It executes the position change on the same day the moving averages cross, which is an approximation: in a real backtest the decision is made at the close and executed at the next open, and the execution price is not the close. It assumes zero transaction costs, where a real backtest deducts commission and slippage on every change of position. And it uses the entire historical record to compute the moving averages, which is fine for a backtest but obscures the look-ahead problem that often inflates academic strategy results: a strategy that uses the full-sample mean as one of its inputs is not implementable in real time, and a backtest that does so is reporting a number that the strategy could not have produced. The dedicated libraries enforce point-in-time data: zipline, backtrader and vectorbt each ensure that the rule at time t can use only the data available at time t, and the burden of avoiding look-ahead falls on the framework rather than on the user.

The vectorbt library is worth a separate note. It departs from the event-driven simulation of zipline and backtrader by formulating the entire backtest as a vectorised NumPy operation over a parameter grid, evaluating thousands of strategy variants in seconds. This makes hyperparameter sweeps tractable and, more importantly, it makes the look-ahead failures of careless code visible by surfacing the variance of returns across parameter combinations: a strategy whose performance depends sensitively on its parameter values is one whose backtest is not robust, and a sweep reveals that immediately.

The discipline that protects a backtest from the related subtleties of overfitting is walk-forward analysis. Rather than fitting all of a strategy's hyperparameters on the full sample and reporting the in-sample performance, walk-forward analysis divides the data into a sequence of training windows and the out-of-sample evaluation windows that follow them. On each training window the strategy chooses its hyperparameters; on the immediately following evaluation window it is run with those values fixed, and the returns from that window are appended to the out-of-sample track record. Sliding both windows forward and repeating produces an out-of-sample track record over the entire period, free of look-ahead. The implicit assumption is that the optimal hyperparameters do not change too quickly, and the diagnostic that reveals violations is the variance of those hyperparameters across the rolling fits: if they jump from one window to the next the strategy is overfitting to local noise and the walk-forward result is the honest measure of what to expect. The major Python backtesting libraries support this pattern, and the more recent vectorbt makes the parameter sweep that walk-forward analysis requires essentially free.

8Drawdowns and downside risk

A drawdown is the difference between the running maximum of a portfolio's value and its current value, expressed as a fraction of the running maximum. It is never positive: when the portfolio is at a new high the drawdown is zero, and otherwise it is the fraction by which the portfolio has fallen from its peak. The maximum drawdown over a period is the worst the drawdown got during it, and it is the headline downside-risk number quoted alongside the Sharpe ratio in nearly every performance attribution.

The drawdown matters because investors care about peak-to-trough losses, not just about end-to-end returns. Two portfolios with the same total return but very different drawdowns are not the same investment: the one with the worse drawdown carries more behavioural risk, because investors are likelier to capitulate near the trough than near the peak, and more career risk for the manager, because reporting a large interim loss to clients is generally career-limiting. Backtests that report only the final return understate the difference between a smooth and a jagged path to it.

The figure above shows the cumulative return in the upper panel and the drawdown in the lower, computed by a running-maximum scan. The drawdown band is hatched as well as shaded so that the meaning of the band, the region between zero and the drawdown curve, survives in a monochrome rendering. The Series control switches between the strategy and the buy-and-hold path. The maximum drawdown for each is reported in the stats below the figure, and the difference between them is one of the genuine ways a hedged or trend-following strategy can earn its keep: even if the total return is similar, a smaller maximum drawdown is a less harrowing path to it.

Several derived statistics live downstream of drawdown. The Calmar ratio is the annualised return divided by the absolute value of the maximum drawdown, and it gives a return-per-pain measure that institutional investors use alongside the Sharpe ratio. The recovery time of a drawdown is the number of days from trough to the recovery of the previous high water mark, and it is the duration analogue of the drawdown's magnitude. The pyfolio and empyrical libraries compute these statistics from a pandas Series of returns in a few lines, and they are the standard tear-sheet output of any quantitative strategy.

9Correlation, diversification and risk decomposition

The variance of a portfolio of several assets is not the average of the variances of its components. It is the quadratic form of the weights against the covariance matrix, and the off-diagonal terms of the covariance reflect the correlations among assets. When correlations are low or negative the portfolio variance is much lower than the average of the component variances, which is the precise statement of the diversification benefit; when correlations approach one the portfolio behaves like a single highly-volatile asset, and the diversification benefit disappears.

The risk contribution of an individual asset to a portfolio is not simply its weight times its variance. It is the asset's weight multiplied by the covariance of its return with the portfolio's return, divided by the portfolio variance. The sum of these contributions across assets equals one by construction, and so the contributions partition the portfolio's risk among its members. An asset whose return is highly correlated with the portfolio contributes more to the portfolio's risk than its weight alone would suggest, and an asset whose return is uncorrelated or negatively correlated contributes less.

The figure above shows the correlation matrix of five synthetic assets at a configurable average correlation, alongside the risk contributions of those assets in an equal-weight portfolio. As the correlation rises the risk contributions tilt toward the most volatile asset; as the correlation falls the contributions even out. The same calculation in code is a one-liner over the covariance matrix returned by returns.cov() in pandas, and riskfolio-lib exposes it as one of several risk-budgeting objectives that can be set as the optimisation target in place of mean-variance.

The risk-decomposition view leads naturally to risk parity, an alternative portfolio-construction objective. In a risk-parity portfolio the weights are chosen so that every asset contributes equally to the portfolio's total risk, regardless of its weight. The result is, in practice, a portfolio that is heavily invested in the lower-volatility assets and lightly invested in the higher-volatility ones, often with leverage applied at the portfolio level to scale the total risk to the target. Bridgewater's All Weather strategy popularised risk parity at the institutional level, and a number of Python libraries, including riskfolio-lib and PyPortfolioOpt, implement the risk-parity optimisation as a one-line call.

10Conclusion

The quantitative-finance Python ecosystem has matured into a stack in which each layer is well defined and broadly stable. NumPy and pandas form the bottom: arrays and time-indexed series, with operations broadcasting across them. statsmodels and arch sit above as the econometrics layer: regressions, stationarity tests, ARIMA, and the GARCH-family models. CVXPY, riskfolio-lib and PyPortfolioOpt occupy the optimisation layer: mean-variance, Black-Litterman, risk parity, and a growing list of robust and Bayesian extensions. Zipline, backtrader and vectorbt provide the backtesting layer, with pyfolio and empyrical handling the performance attribution. QuantLib's Python bindings supply the pricing routines for fixed income and derivatives. The dependencies among these layers are orderly, and a working analyst can move between them by knowing which library to reach for at each step.

The literature review's two threads explain why the stack looks the way it does. The methodological backbone, from Markowitz through Sharpe, Black-Scholes, ARCH, GARCH, VaR and the coherent-risk-measure axioms, was substantially in place before Python existed, and the libraries are explicit implementations of those methods. The library backbone, from NumPy through pandas, statsmodels, arch, CVXPY, riskfolio-lib and the backtesters, climbs from low-level numerical primitives toward high-level abstractions that know about time, portfolios and backtests respectively. The figures in this document have run the corresponding computations on synthetic data so that the arithmetic each library performs can be seen plainly; in production code each is a single function call against real data, and the function is the one the corresponding library provides.

The reader who has followed the development is positioned to start writing the corresponding Python. The book that ties the methodological and library threads together at length is Yves Hilpisch's Python for Finance, now in its second edition, and the practical-machine-learning angle is treated by López de Prado [12]. The rest is, increasingly, the practitioner's domain rather than the library's: the libraries supply the calculations, the analyst supplies the data, the choice of model, and the discipline to refuse to overfit.

Return to contents

RReferences

  1. Markowitz, H. (1952). Portfolio selection. The Journal of Finance, 7(1), 77–91.
  2. Sharpe, W. F. (1964). Capital asset prices: a theory of market equilibrium under conditions of risk. The Journal of Finance, 19(3), 425–442.
  3. Black, F. and Scholes, M. (1973). The pricing of options and corporate liabilities. Journal of Political Economy, 81(3), 637–654.
  4. Engle, R. F. (1982). Autoregressive conditional heteroscedasticity with estimates of the variance of United Kingdom inflation. Econometrica, 50(4), 987–1007.
  5. Bollerslev, T. (1986). Generalized autoregressive conditional heteroskedasticity. Journal of Econometrics, 31(3), 307–327.
  6. Jorion, P. (1996). Value at Risk: The New Benchmark for Managing Financial Risk. New York: McGraw-Hill.
  7. Artzner, P., Delbaen, F., Eber, J.-M. and Heath, D. (1999). Coherent measures of risk. Mathematical Finance, 9(3), 203–228.
  8. Harris, C. R., Millman, K. J., van der Walt, S. J., and 23 others (2020). Array programming with NumPy. Nature, 585, 357–362.
  9. McKinney, W. (2010). Data structures for statistical computing in Python. Proceedings of the 9th Python in Science Conference, 56–61.
  10. Seabold, S. and Perktold, J. (2010). Statsmodels: econometric and statistical modeling with Python. Proceedings of the 9th Python in Science Conference, 92–96.
  11. Diamond, S. and Boyd, S. (2016). CVXPY: a Python-embedded modeling language for convex optimization. Journal of Machine Learning Research, 17(83), 1–5.
  12. López de Prado, M. (2018). Advances in Financial Machine Learning. Hoboken: Wiley.