A fractal econometrics package for python, built with JAX.
Project description
fractrics
Table of Contents
Installation
pip install fractrics
Quick example
The main tool in fractrics is the MSM class, an implementation of the univariate Markov Switching Multifractal Model. The logaritmic difference between observations is modeled as the noise-adjusted square root of the product of a chosen number of latent volatility components, each following the dynamics of discrete first order markov chains, whose transition depends on geometrically-spaced Poisson arrivals, and an unconditional term, effectively being the unconditional volatility.
Such structure effectively captures the behaviour of time series with fat tails, hyperbolic correlation decay, and multifractal moments, such as the returns of many financial assets.
The implementation is made in JAX, simplifying parallelization of the code. Moreover, following from this paper, the memory complexity of the forward algorithm is reduced, due to the factorization of latent states.
To use the model, start with an example time series. Note that the model is only defined for positive time series (as it was created to model prices of financial assets).
from fractrics.time_series.MSM import MSM
import jax.numpy as jnp
import numpy as np
ts_test = jnp.array(np.random.normal(50, 10, 10))
Then initialize the model. It requires the following hyperparameters:
num_latent: how many volatility components, integer.marg_prob_mass: the probability mass of the marginal distribution of the latent states, needs to sum to 1.
model = MSM(ts=ts_test, num_latent=3)
To fit the model to the data, start with an initial guess. The MSM.fit() method then optimizes the parameters using jaxopt's Broyden–Fletcher–Goldfarb–Shanno algorithm.
By assumption, all the parameters need to be positive, and have further individual constrains:
-
marg_support: the support of the marginal probability mass defined in the parameters. It needs to have unity expectation. In the symmetric binomial case, this can be enforced by specifying one value $m_0$, and having the second value be $2 - m_0$. -
unconditional_term: the unconditional distribution of the model, a positive double. -
arrival_gdistance: the geometric distance between the Poisson arrivals of each latent volatility component, a positive double. -
hf_arrival: the highest poisson arrival probability (i.e. the proability of state switch of the highest frequency component).
Note: to maintain the constrains during optimization, the parameters are transformed using mappings.
initial_params = jnp.array([
2, #unconditional term
3.0, #arrival_gdistance
0.98, #hf_arrival
#support
1.5,
0.5
])
fitresult = model.fit(initial_parameters=initial_params, maxiter=1000, verbose=False)
params, current_distribution, transition_tensor, distribution_history, negative_log_likelihood = fitresult
print(params)
[0.22855803 3.1763792 0.44004682 1.0000163 0.9999837 ]
It is also possible to make simulations with the MSM. To avoid tracing issues with JAX, the parameters are to be given as input for the simulation. Follows an example with the parameters of the fitted model above.
num_latent = model.num_latent
unconditional_term = params[0]
arrival_gdistance = params[1]
hf_arrival = params[2]
marg_support = params[3:]
poisson_arrivals = 1 - (1 - hf_arrival) ** (1 / (arrival_gdistance ** (jnp.arange(num_latent, 0, -1) - 1)))
simulation = model.simulation(number_simulations= 1000, unconditional_term = unconditional_term,
poisson_arrivals=poisson_arrivals,
marginal_support = marg_support
)
Finally a 7 period forecast. The transition tensor and current distribution are required as input
forecast = model.forecast(7, current_distribution, *transition_tensor)
License
fractrics is distributed under the terms of the MIT license.
References
-
Calvet, L.E. and Fisher, A.J. (2004). How to Forecast Long-Run Volatility: Regime Switching and the Estimation of Multifractal Processes. Journal of Financial Econometrics, 2(1).
-
Calvet, L.E. and Fisher, A.J. (2008). Multifractal Volatility. Theory, Forecasting, and Pricing. Academic Press.
-
Calvet, L.E., Fisher, A.J. and Thompson, S.B. (2004). Volatility Comovement: A Multifrequency Approach. SSRN Electronic Journal. doi:https://doi.org/10.2139/ssrn.582541.
-
Diodati, A. (2025). Tensor representation of Markov Switching Multifractal Models. doi:https://doi.org/10.2139/ssrn.5276090.
-
Lux, T. (2008). The Markov-Switching Multifractal Model of Asset Returns. Journal of Business & Economic Statistics, 26(2), pp.194–210. doi:https://doi.org/10.1198/073500107000000403.
-
Lux, T. (2020). Inference for Nonlinear State Space Models: A Comparison of Different Methods applied to Markov-Switching Multifractal Models. Econometrics and Statistics. doi:https://doi.org/10.1016/j.ecosta.2020.03.001.
-
Lux, T., Morales-Arias, L. and Sattarhoff, C. (2011). A Markov-switching multifractal approach to forecasting realized volatility. [online] Kiel Working Papers. Available at: https://ideas.repec.org/p/zbw/ifwkwp/1737.html [Accessed 30 May 2025].
-
Murphy, K.P. (2012). Machine learning : a probabilistic perspective. Cambridge (Ma): Mit Press.
-
Rypdal, M. and Løvsletten, O. (2011). Multifractal modeling of short-term interest rates. arXiv (Cornell University).
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fractrics-0.1.0.tar.gz.
File metadata
- Download URL: fractrics-0.1.0.tar.gz
- Upload date:
- Size: 28.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63d46a9adbef58d7328ee4f072f411db24f00390a1cea994d7941bad7c46a899
|
|
| MD5 |
f0c2ef3e55b6d51b75a86f2e8bbf9ff2
|
|
| BLAKE2b-256 |
9762d0906cc382af5795f15ccd36f94634ed5b40294f55080702b36146b4ff80
|
File details
Details for the file fractrics-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fractrics-0.1.0-py3-none-any.whl
- Upload date:
- Size: 29.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c148d6261b950c582057b5cf220bc562c3e4592e68f0cf1bd33ffa419c39587d
|
|
| MD5 |
19348cb821dd55e04447d96bdf0db02d
|
|
| BLAKE2b-256 |
87de19df8056e4fee29507afe8fafb9ec6caf5b7447dbf8fb2d77b7958b3868d
|