{"id":29165119,"url":"https://github.com/lrydin/mfdfa","last_synced_at":"2025-07-01T07:10:40.566Z","repository":{"id":41067140,"uuid":"224135077","full_name":"LRydin/MFDFA","owner":"LRydin","description":"Multifractal Detrended Fluctuation Analysis in Python","archived":false,"fork":false,"pushed_at":"2022-08-15T14:36:29.000Z","size":3028,"stargazers_count":147,"open_issues_count":7,"forks_count":31,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-06-28T06:42:58.085Z","etag":null,"topics":["detrended-fluctuation-analysis","fractional-gaussian-noise","multifractal-analysis","self-similarity","stochastic-processes"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LRydin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-26T08:01:06.000Z","updated_at":"2025-06-21T23:48:36.000Z","dependencies_parsed_at":"2022-08-10T01:29:45.891Z","dependency_job_id":null,"html_url":"https://github.com/LRydin/MFDFA","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/LRydin/MFDFA","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LRydin%2FMFDFA","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LRydin%2FMFDFA/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LRydin%2FMFDFA/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LRydin%2FMFDFA/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LRydin","download_url":"https://codeload.github.com/LRydin/MFDFA/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LRydin%2FMFDFA/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262916694,"owners_count":23383891,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["detrended-fluctuation-analysis","fractional-gaussian-noise","multifractal-analysis","self-similarity","stochastic-processes"],"created_at":"2025-07-01T07:10:39.603Z","updated_at":"2025-07-01T07:10:40.536Z","avatar_url":"https://github.com/LRydin.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![DOI:10.1016/j.cpc.2021.108254](http://img.shields.io/badge/DOI-10.1016/j.cpc.2021.108254-00ff00.svg)](https://doi.org/10.1016/j.cpc.2021.108254)\n[![arXiv](https://img.shields.io/badge/arXiv-2104.10470-00ff00.svg)](https://arxiv.org/abs/2104.10470)\n[![zenodo](https://zenodo.org/badge/224135077.svg)](https://zenodo.org/badge/latestdoi/224135077)\n![PyPI - License](https://img.shields.io/pypi/l/MFDFA)\n![PyPI](https://img.shields.io/pypi/v/MFDFA)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/MFDFA)\n[![Build Status](https://github.com/LRydin/MFDFA/actions/workflows/CI.yml/badge.svg)](https://github.com/LRydin/MFDFA/actions/workflows/CI.yml)\n[![codecov](https://codecov.io/gh/LRydin/MFDFA/branch/master/graph/badge.svg)](https://codecov.io/gh/LRydin/MFDFA)\n[![Documentation Status](https://readthedocs.org/projects/mfdfa/badge/?version=latest)](https://mfdfa.readthedocs.io/en/latest/?badge=latest)\n\n\n# MFDFA\nMultifractal Detrended Fluctuation Analysis `MFDFA` is a model-independent method to uncover the self-similarity of a stochastic process or auto-regressive model.\n`DFA` was first developed by Peng *et al.*\u003csup\u003e1\u003c/sup\u003e and later extended to study multifractality `MFDFA` by Kandelhardt *et al.*\u003csup\u003e2\u003c/sup\u003e.\n\nIn the latest release there is as well added a moving window system, especially useful for short timeseries, a recent extension to DFA called *extended DFA*, and the extra feature of Empirical Mode Decomposition as detrending method.\n\n# Installation\nTo install MFDFA you can simply use\n\n```\npip install MFDFA\n```\nAnd on your favourite editor simply import `MFDFA` as\n```python\nfrom MFDFA import MFDFA\n```\nThere is an added library `fgn` to generate fractional Gaussian noise.\n\nYou can find the latest published paper of this library in Computer Physics Communications [L. Rydin Gorjão, G. Hassan, J. Kurths, and D. Witthaut, _MFDFA: Efficient multifractal detrended fluctuation analysis in python_, Computer Physics Communications *273*, 108254 2022](https://doi.org/10.1016/j.cpc.2021.108254). You can find the paper [here](https://github.com/LRydin/MFDFA/blob/master/paper/paper.pdf).\n\n# The `MFDFA` library\n`MFDFA` basis is solely dependent on `numpy`, especially `numpy`'s `polynomial`. In version 0.3 a [Empirical Mode Decomposition](https://en.wikipedia.org/wiki/Hilbert%E2%80%93Huang_transform) method was added for an alternative method of detrending timeseries, relying on [Dawid Laszuk's](https://github.com/laszukdawid/PyEMD) `PyEMD`.\n\n# Employing the `MFDFA` library\n\n## An exemplary one-dimensional fractional Ornstein–Uhlenbeck process\nThe rationale here is simple: Numerically integrate a stochastic process in which we know exactly the fractal properties, characterised by the Hurst coefficient, and recover this with MFDFA.\nWe will use a fractional Ornstein–Uhlenbeck, a commonly employ stochastic process with mean-reverting properties.\nFor a more detailed explanation on how to integrate an Ornstein–Uhlenbeck process, see the [kramersmoyal's package](https://github.com/LRydin/KramersMoyal#a-one-dimensional-stochastic-process).\nYou can also follow the [fOU.ipynb](/examples/fOU.ipynb)\n\n### Generating a fractional Ornstein–Uhlenbeck process\nThis is one method of generating a (fractional) Ornstein–Uhlenbeck process with *H=0.7*, employing a simple Euler–Maruyama integration method\n\n```python\n# Imports\nfrom MFDFA import MFDFA\nfrom MFDFA import fgn\n# where this second library is to generate fractional Gaussian noises\n\n# integration time and time sampling\nt_final = 2000\ndelta_t = 0.001\n\n# Some drift theta and diffusion sigma parameters\ntheta = 0.3\nsigma = 0.1\n\n# The time array of the trajectory\ntime = np.arange(0, t_final, delta_t)\n\n# The fractional Gaussian noise\nH = 0.7\ndB = (t_final ** H) * fgn(N = time.size, H = H)\n\n# Initialise the array y\ny = np.zeros([time.size])\n\n# Integrate the process\nfor i in range(1, time.size):\n    y[i] = y[i-1] - theta * y[i-1] * delta_t + sigma * dB[i]\n```\nAnd now you have a fractional process with a self-similarity exponent *H=0.7*\n\n### Using the `MFDFA`\nTo now utilise the `MFDFA`, we take this exemplary process and run the (multifractal) detrended fluctuation analysis. For now lets consider only the monofractal case, so we need only `q=2`.\n```python\n# Select a band of lags, which usually ranges from\n# very small segments of data, to very long ones, as\nlag = np.unique(np.logspace(0.5, 3, 100).astype(int))\n# Notice these must be ints, since these will segment\n# the data into chucks of lag size\n\n# Select the power q\nq = 2\n\n# The order of the polynomial fitting\norder = 1\n\n# Obtain the (MF)DFA as\nlag, dfa = MFDFA(y, lag = lag, q = q, order = order)\n```\n\nNow we need to visualise the results, which can be understood in a log-log scale. To find *H* we need to fit a line to the results in the log-log plot\n```python\n# To uncover the Hurst index, lets get some log-log plots\nplt.loglog(lag, dfa, 'o', label='fOU: MFDFA q=2')\n\n# And now we need to fit the line to find the slope. Don't\n# forget that since you are plotting in a double logarithmic\n# scales, you need to fit the logs of the results\nH_hat = np.polyfit(np.log(lag)[4:20],np.log(dfa[4:20]),1)[0]\n\n# Now what you should obtain is: slope = H + 1\nprint('Estimated H = '+'{:.3f}'.format(H_hat[0]))\n```\n\n\u003cimg src=\"docs/_static/fig1.png\" title=\"MFDFA of a fractional Ornstein–Uhlenbeck process\" height=\"250\"/\u003e\n\n\n## Uncovering multifractality in stochastic processes\nYou can find more about multifractality in the [documentation](https://mfdfa.readthedocs.io/en/latest/1dLevy.html).\n\n# Changelog\n- Version 0.4.3 - Reverting negative values in the estimation of the singularity strenght α.\n- Version 0.4.2 - Corrected spectral plots. Added [examples](https://github.com/LRydin/MFDFA/tree/master/examples) from the paper.\n- Version 0.4.1 - Added conventional spectral plots as _h(q)_ vs _q_, _τ(q)_ vs _q_, and _f(α)_ vs _α_.\n- Version 0.4 - EMD is now optional. Restored back compatibility: py3.3 to py3.9. For EMD py3.6 or larger is needed.\n- Version 0.3 - Adding EMD detrending. First release. PyPI code.\n- Version 0.2 - Removed experimental features. Added documentation\n- Version 0.1 - Uploaded initial working code\n\n# Contributions\nI welcome reviews and ideas from everyone. If you want to share your ideas or report a bug, open an [issue](https://github.com/LRydin/KramersMoyal/issues) here on GitHub, or contact me directly.\nIf you need help with the code, the theory, or the implementation, do not hesitate to reach out, I am here to help.\nThis package abides to a [Conduct of Fairness](contributions.md).\n\n# Literature and Support\n### Submission history\nThis library has been submitted for publication at [The Journal of Open Source Software](https://joss.theoj.org/) in December 2019. It was rejected. The review process can be found [here on GitHub](https://github.com/openjournals/joss-reviews/issues/1966). The plan is to extend the library and find another publisher.\n\n### History\nThis project was started in 2019 at the [Faculty of Mathematics, University of Oslo](https://www.mn.uio.no/math/english/research/groups/risk-stochastics/) in the Risk and Stochastics section by Leonardo Rydin Gorjão and is supported by Dirk Witthaut and the [Institute of Energy and Climate Research Systems Analysis and Technology Evaluation](https://www.fz-juelich.de/iek/iek-ste/EN/Home/home_node.html). I'm very thankful to all the folk in Section 3 in the Faculty of Mathematics, University of Oslo, for helping me getting around the world of stochastic processes: Dennis, Anton, Michele, Fabian, Marc, Prof. Benth and Prof. di Nunno. In April 2020 Galib Hassan joined in extending `MFDFA`, particularly the implementation of `EMD`.\n\n\n### Funding\nHelmholtz Association Initiative *Energy System 2050 - A Contribution of the Research Field Energy* and the grant No. VH-NG-1025; *STORM - Stochastics for Time-Space Risk Models* project of the Research Council of Norway (RCN) No. 274410, and the *E-ON Stipendienfonds*.\n\n### References\n\u003csup\u003e1\u003c/sup\u003ePeng, C.-K., Buldyrev, S. V., Havlin, S., Simons, M., Stanley, H. E., \u0026 Goldberger, A. L. (1994). *Mosaic organization of DNA nucleotides*. [Physical Review E, 49(2), 1685–1689](https://doi.org/10.1103/PhysRevE.49.1685)\\\n\u003csup\u003e2\u003c/sup\u003eKantelhardt, J. W., Zschiegner, S. A., Koscielny-Bunde, E., Havlin, S., Bunde, A., \u0026 Stanley, H. E. (2002). *Multifractal detrended fluctuation analysis of nonstationary time series*. [Physica A: Statistical Mechanics and Its Applications, 316(1-4), 87–114](https://doi.org/10.1016/S0378-4371(02)01383-3)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flrydin%2Fmfdfa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flrydin%2Fmfdfa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flrydin%2Fmfdfa/lists"}