Breakthroughs in Financial Time Series ====================================== .. include:: /_generated/nav/time_series.rst .. epigraph:: "The series looks like a wandering one, almost as if once a week the Demon of Chance drew a random number from a symmetrical population of fixed dispersion and added it to the current price to determine the next week's price." -- Maurice Kendall, 1953 A price series is a single realization of a random process that can never be rerun, so everything known about it has to be inferred from its own past. Financial time-series analysis began by borrowing the autoregressions that statisticians had built for sunspots and rivers, and then found that prices broke their assumptions one by one. Prices wander like random walks, so their changes, not their levels, are the object of study. The changes are nearly uncorrelated, which is the efficient-markets hypothesis in statistical form, and much of the history is a sequence of ever sharper tests of that claim. Yet the changes are not independent: their size clusters, their tails are fat, and volatility responds more to falls than to rises. Two Nobel prizes, Engle's and Granger's in 2003, went to modelling these features. This chronology traces the ideas behind :mod:`financialkit.time_series`. .. contents:: Timeline :local: :depth: 1 1927 -- Yule's Autoregression ----------------------------- The standard tool for periodic data was Schuster's periodogram, which treats a series as exact sine waves plus measurement error. Udny Yule found it unconvincing for sunspot numbers, whose cycles vary in length and height. He proposed instead a pendulum "bombarded by peas": a damped oscillation that random shocks keep alive. For Wolfer's sunspot numbers he fitted :math:`x_t = 1.34\,x_{t-1} - 0.65\,x_{t-2} + \varepsilon_t`, whose complex characteristic roots give a pseudo-cycle of about ten and a half years. The coefficients solve the *Yule-Walker equations*, a Toeplitz system in the sample autocorrelations. With this paper the autoregressive model, the backbone of time-series analysis, was born. .. math:: \rho_k = \sum_{i=1}^{p}\phi_i\,\rho_{k-i}, \quad k = 1, \dots, p *Implementation:* :func:`financialkit.time_series.systems.linear_models.yule_walker` (via :func:`scipy.linalg.solve_toeplitz`), with :func:`~financialkit.time_series.systems.linear_models.ar_cycle_period` for the pseudo-period and :func:`~financialkit.time_series.systems.linear_models.simulate_arma`. *References:* G. U. Yule, "On a Method of Investigating Periodicities in Disturbed Series, with Special Reference to Wolfer's Sunspot Numbers," *Philosophical Transactions of the Royal Society A* 226 (1927) 267-298. .. minigallery:: ../../examples/time_series/linear_models/plot_01_yule_autoregression.py 1951 -- Hurst Exponent ---------------------- Harold Edwin Hurst spent decades measuring the Nile to size the reservoirs above Aswan. The storage a dam needs to release the average flow every year is the range :math:`R` of cumulative departures from the mean. Divided by the standard deviation :math:`S`, this rescaled range grows like :math:`n^{1/2}` for independent inflows, but Hurst found :math:`n^{0.73}` for the Nile and for hundreds of other natural records. Wet years cluster with wet years over long horizons. Mandelbrot later explained the exponent :math:`H` with fractional Brownian motion and brought it to finance, where :math:`H \ne 1/2` would mean long-range predictability. Anis and Lloyd's (1976) exact expectation of :math:`R/S` for independent data corrects the estimate's small-sample bias. .. math:: E\Bigl[\frac{R}{S}\Bigr]_n \propto n^{H} *Implementation:* :func:`financialkit.time_series.systems.long_memory.hurst_exponent` returns a :class:`~financialkit.time_series.core.base.HurstResult` with the raw and Anis-Lloyd-corrected exponents; :func:`~financialkit.time_series.systems.long_memory.simulate_fgn` generates exact fractional Gaussian noise by circulant embedding (:mod:`scipy.fft`). *References:* H. E. Hurst, "Long-Term Storage Capacity of Reservoirs," *Transactions of the American Society of Civil Engineers* 116 (1951) 770-799; A. A. Anis and E. H. Lloyd, "The Expected Value of the Adjusted Rescaled Hurst Range of Independent Normal Summands," *Biometrika* 63(1) (1976) 111-116; B. B. Mandelbrot and J. W. Van Ness, "Fractional Brownian Motions, Fractional Noises and Applications," *SIAM Review* 10(4) (1968) 422-437. .. minigallery:: ../../examples/time_series/long_memory/plot_01_hurst_exponent.py 1953 -- Kendall: the Random Walk of Prices ------------------------------------------ Maurice Kendall went looking for cycles in 22 British price series: wheat, cotton and industrial share indices, sampled weekly. Instead he found that the serial correlations of the price *changes* were close to zero at every lag, while the levels were almost perfectly correlated. He concluded that prices wander like a random walk. The finding surprised economists who had expected cycles, but it established the empirical fact that efficient-markets theory would explain a decade later: in a well-functioning market, past price changes do not predict future ones. .. math:: \hat\rho_k = \frac{\sum_{t}(x_t - \bar x)(x_{t-k} - \bar x)}{\sum_t (x_t - \bar x)^2}, \qquad |\hat\rho_k| \lesssim \frac{1.96}{\sqrt n}\ \text{under independence} *Implementation:* :func:`financialkit.time_series.systems.efficiency.autocorrelation` (by FFT) and :func:`~financialkit.time_series.systems.efficiency.bartlett_band`; :func:`~financialkit.time_series.visualizers.plots.plot_acf` draws the correlogram. *References:* M. G. Kendall, "The Analysis of Economic Time-Series -- Part I: Prices," *Journal of the Royal Statistical Society A* 116(1) (1953) 11-34. .. minigallery:: ../../examples/time_series/efficiency/plot_01_kendall_autocorrelation.py 1963 -- Mandelbrot: Fat Tails ----------------------------- Benoit Mandelbrot plotted the frequency of large daily and monthly changes in cotton prices on doubly logarithmic paper and found straight lines. The tails decay like a power law, not like the Gaussian that Bachelier's random walk assumed, so extreme moves are vastly more common than the normal distribution predicts. He proposed the stable Paretian laws, with index :math:`\alpha \approx 1.7`, which have infinite variance. He also noted that large changes tend to be followed by large changes. Later work settled on tail indices between 3 and 5 for most assets, so variances are finite, but fat tails and volatility clustering became the first two *stylized facts* of returns. .. math:: P(|X| > u) \sim C\,u^{-\alpha}, \qquad u \to \infty *Implementation:* :func:`financialkit.time_series.systems.stylized_facts.tail_survival` (the log-log tail), :func:`~financialkit.time_series.systems.stylized_facts.return_statistics` (moments and the Jarque-Bera test via :func:`scipy.stats.jarque_bera`) and :func:`~financialkit.time_series.systems.stylized_facts.log_returns`. *References:* B. Mandelbrot, "The Variation of Certain Speculative Prices," *Journal of Business* 36(4) (1963) 394-419; R. Cont, "Empirical Properties of Asset Returns: Stylized Facts and Statistical Issues," *Quantitative Finance* 1(2) (2001) 223-236. .. minigallery:: ../../examples/time_series/stylized_facts/plot_01_mandelbrot_fat_tails.py 1965 -- Fama's Runs Test ------------------------ Eugene Fama's thesis, published in full in the *Journal of Business*, examined the daily prices of the 30 Dow Jones Industrials. Correlation coefficients can be dominated by a few huge changes, which Mandelbrot had just shown to be common. Fama therefore added a distribution-free test that uses only the signs of the changes, counting the *runs* of consecutive changes with the same sign. Trends would produce too few runs and reversals too many. The counts were close to their expected values under independence. The paper, and Fama's 1970 survey, made the efficient-markets hypothesis the organizing idea of empirical finance. .. math:: m = \frac{N(N+1) - \sum_i n_i^2}{N}, \qquad \sigma_m^2 = \frac{\sum_i n_i^2\bigl[\sum_i n_i^2 + N(N+1)\bigr] - 2N\sum_i n_i^3 - N^3}{N^2(N-1)} *Implementation:* :func:`financialkit.time_series.systems.efficiency.runs_test` with Fama's three-sign (+, -, 0) moments, returning a :class:`~financialkit.time_series.core.base.TestResult`. *References:* E. F. Fama, "The Behavior of Stock-Market Prices," *Journal of Business* 38(1) (1965) 34-105; A. Wald and J. Wolfowitz, "On a Test Whether Two Samples Are from the Same Population," *Annals of Mathematical Statistics* 11(2) (1940) 147-162. .. minigallery:: ../../examples/time_series/efficiency/plot_02_fama_runs_test.py 1970 -- Box-Jenkins ARMA ------------------------ George Box and Gwilym Jenkins's book turned mixed autoregressive moving-average models into a practical method with three stages. First, *identify* the orders from the autocorrelation function, which cuts off after lag :math:`q` for a moving average, and the partial autocorrelation function, which cuts off after lag :math:`p` for an autoregression. Second, *estimate* by maximum likelihood. Third, *check* the residuals for leftover structure, and repeat until they are white noise. Then forecast, with error bands from the model's moving-average representation. ARMA models became the benchmark that every economic forecast had to beat. .. math:: \phi(B)(x_t - \mu) = \theta(B)\,\varepsilon_t, \qquad \operatorname{Var}(\hat x_{T+h} - x_{T+h}) = \sigma^2\sum_{j