{"id":13739751,"url":"https://github.com/Boulder-Investment-Technologies/lppls","last_synced_at":"2025-05-08T19:35:03.381Z","repository":{"id":39647219,"uuid":"256555314","full_name":"Boulder-Investment-Technologies/lppls","owner":"Boulder-Investment-Technologies","description":"Library for fitting the LPPLS model to data.","archived":false,"fork":false,"pushed_at":"2024-08-06T15:33:11.000Z","size":11998,"stargazers_count":356,"open_issues_count":18,"forks_count":111,"subscribers_count":25,"default_branch":"master","last_synced_at":"2024-11-09T02:18:36.340Z","etag":null,"topics":["finance","python"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","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/Boulder-Investment-Technologies.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-04-17T16:31:23.000Z","updated_at":"2024-11-08T17:53:53.000Z","dependencies_parsed_at":"2024-04-17T09:53:10.246Z","dependency_job_id":"9b949dcd-2311-492b-85cb-a77247c834ce","html_url":"https://github.com/Boulder-Investment-Technologies/lppls","commit_stats":{"total_commits":88,"total_committers":12,"mean_commits":7.333333333333333,"dds":0.7045454545454546,"last_synced_commit":"a42221a7c5d4fac38f0c92f411676e8f972a4167"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Boulder-Investment-Technologies%2Flppls","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Boulder-Investment-Technologies%2Flppls/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Boulder-Investment-Technologies%2Flppls/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Boulder-Investment-Technologies%2Flppls/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Boulder-Investment-Technologies","download_url":"https://codeload.github.com/Boulder-Investment-Technologies/lppls/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224758213,"owners_count":17364970,"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":["finance","python"],"created_at":"2024-08-03T04:00:37.188Z","updated_at":"2024-11-15T09:30:52.419Z","avatar_url":"https://github.com/Boulder-Investment-Technologies.png","language":"Jupyter Notebook","funding_links":[],"categories":["Python","Python：量化金融第一生态"],"sub_categories":["Indicators","三、技术指标与因子分析"],"readme":"![PyPI 📦   ](https://github.com/Boulder-Investment-Technologies/lppls/workflows/PyPI%20%F0%9F%93%A6%20%20%20/badge.svg?branch=master)\n![PyTests](https://github.com/Boulder-Investment-Technologies/lppls/workflows/PyTests/badge.svg?branch=master)\n[![Downloads](https://pepy.tech/badge/lppls)](https://pepy.tech/project/lppls)\n\n# Log Periodic Power Law Singularity (LPPLS) Model \n`lppls` is a Python module for fitting the LPPLS model to data.\n\n\n## Overview\nThe LPPLS model provides a flexible framework to detect bubbles and predict regime changes of a financial asset. A bubble is defined as a faster-than-exponential increase in asset price, that reflects positive feedback loop of higher return anticipations competing with negative feedback spirals of crash expectations. It models a bubble price as a power law with a finite-time singularity decorated by oscillations with a frequency increasing with time. \n\nTry the demo: \n\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1Qvbdj4DGNcC9Oop9mA6Vzdvsoec6k2I0?usp=sharing)\n\nHere is the model:\n\n```math\nE[ln\\ p(t)] = A + B(t_c-t)^{m}+C(t_c-t)^{m}\\cos(\\omega\\ ln(t_c-t) - \\phi)\n```\n\n  where:\n\n  - $E[ln\\ p(t)]$: expected log price at the date of the termination of the bubble\n  - $t_c$: critical time (date of termination of the bubble and transition in a new regime) \n  - $A$: expected log price at the peak when the end of the bubble is reached at $t_c$\n  - $B$: amplitude of the power law acceleration\n  - $C$: amplitude of the log-periodic oscillations\n  - $m$: degree of the super exponential growth\n  - $\\omega$: scaling ratio of the temporal hierarchy of oscillations\n  - $\\phi$: time scale of the oscillations\n    \nThe model has three components representing a bubble. The first, $A+B(t_c-t)^{m}$, handles the hyperbolic power law. For $m$ \u003c 1 when the price growth becomes unsustainable, and at $t_c$ the growth rate becomes infinite. The second term, $C(t_c-t)^{m}$, controls the amplitude of the oscillations. It drops to zero at the critical time $t_c$. The third term, $\\cos(\\omega\\ ln(t_c-t) - \\phi)$, models the frequency of the oscillations. They become infinite at $t_c$.\n\n## Important links\n - Official source code repo: https://github.com/Boulder-Investment-Technologies/lppls\n - Download releases: https://pypi.org/project/lppls/\n - Issue tracker: https://github.com/Boulder-Investment-Technologies/lppls/issues\n\n## Installation\nDependencies\n\n`lppls` requires:\n - Python (\u003e= 3.7)\n - Matplotlib (\u003e= 3.1.1)\n - Numba (\u003e= 0.51.2)\n - NumPy (\u003e= 1.17.0)\n - Pandas (\u003e= 0.25.0)\n - SciPy (\u003e= 1.3.0)\n - Pytest (\u003e= 6.2.1)\n\nUser installation\n```\npip install -U lppls\n```\n\n## Example Use\n```python\nfrom lppls import lppls, data_loader\nimport numpy as np\nimport pandas as pd\nfrom datetime import datetime as dt\n%matplotlib inline\n\n# read example dataset into df \ndata = data_loader.nasdaq_dotcom()\n\n# convert time to ordinal\ntime = [pd.Timestamp.toordinal(dt.strptime(t1, '%Y-%m-%d')) for t1 in data['Date']]\n\n# create list of observation data\nprice = np.log(data['Adj Close'].values)\n\n# create observations array (expected format for LPPLS observations)\nobservations = np.array([time, price])\n\n# set the max number for searches to perform before giving-up\n# the literature suggests 25\nMAX_SEARCHES = 25\n\n# instantiate a new LPPLS model with the Nasdaq Dot-com bubble dataset\nlppls_model = lppls.LPPLS(observations=observations)\n\n# fit the model to the data and get back the params\ntc, m, w, a, b, c, c1, c2, O, D = lppls_model.fit(MAX_SEARCHES)\n\n# visualize the fit\nlppls_model.plot_fit()\n\n# should give a plot like the following...\n```\n\n![LPPLS Fit to the Nasdaq Dataset](https://raw.githubusercontent.com/Boulder-Investment-Technologies/lppls/master/img/dotcom_lppls_fit.png)\n\n```python\n# compute the confidence indicator\nres = lppls_model.mp_compute_nested_fits(\n    workers=8,\n    window_size=120, \n    smallest_window_size=30, \n    outer_increment=1, \n    inner_increment=5, \n    max_searches=25,\n    # filter_conditions_config={} # not implemented in 0.6.x\n)\n\nlppls_model.plot_confidence_indicators(res)\n# should give a plot like the following...\n```\n![LPPLS Confidnce Indicator](https://raw.githubusercontent.com/Boulder-Investment-Technologies/lppls/master/img/dotcom_confidence_indicator.png)\n\nIf you wish to store `res` as a pd.DataFrame, use `compute_indicators`.\n\u003cdetails\u003e\n  \u003csummary\u003eExample\u003c/summary\u003e\n\n  ```python\n  res_df = lppls_model.compute_indicators(res)\n  res_df\n  # gives the following...\n  ```\n  \u003cimg src=\"https://raw.githubusercontent.com/Boulder-Investment-Technologies/lppls/master/img/compute_indicator_df.png\"  width=\"500\"/\u003e\n  \n\u003c/details\u003e\n\n## Quantile Regression\nBased on the work in Zhang, Zhang \u0026 Sornette 2016, quantile regression for LPPLS uses the L1 norm (sum of absolute differences) instead of the L2 norm\nand applies the q-dependent loss function during calibration. Please refer to the example usage [here](https://github.com/Boulder-Investment-Technologies/lppls/blob/master/notebooks/quantile_regression.ipynb). \n\n## Other Search Algorithms\nShu and Zhu (2019) proposed [CMA-ES](https://en.wikipedia.org/wiki/CMA-ES) for identifying the best estimation of the three non-linear parameters ($t_c$, $m$, $\\omega$).\n\u003e The CMA-ES rates among the most successful evolutionary\nalgorithms for real-valued single-objective optimization and is typically applied to difficult\nnonlinear non-convex black-box optimization problems in continuous domain and search space\ndimensions between three and a hundred. Parallel computing is adopted to expedite the fitting\nprocess drastically.\n\nThis approach has been implemented in a subclass and can be used as follows...\nThanks to @paulogonc for the code.\n```python\nfrom lppls import lppls_cmaes\nlppls_model = lppls_cmaes.LPPLSCMAES(observations=observations)\ntc, m, w, a, b, c, c1, c2, O, D = lppls_model.fit(max_iteration=2500, pop_size=4)\n```\nPerformance Note: this works well for single fits but can take a long time for computing the confidence indicators. More work needs to be done to speed it up. \n## References\n - Filimonov, V. and Sornette, D. A Stable and Robust Calibration Scheme of the Log-Periodic Power Law Model. Physica A: Statistical Mechanics and its Applications. 2013\n - Shu, M. and Zhu, W. Real-time Prediction of Bitcoin Bubble Crashes. 2019.\n - Sornette, D. Why Stock Markets Crash: Critical Events in Complex Financial Systems. Princeton University Press. 2002.\n - Sornette, D. and Demos, G. and Zhang, Q. and Cauwels, P. and Filimonov, V. and Zhang, Q., Real-Time Prediction and Post-Mortem Analysis of the Shanghai 2015 Stock Market Bubble and Crash (August 6, 2015). Swiss Finance Institute Research Paper No. 15-31.\n - Zhang, Q., Zhang, Q., and Sornette, D. Early Warning Signals of Financial Crises with Multi-Scale Quantile Regressions of Log-Periodic Power Law Singularities. PLOS ONE. 2016. DOI:10.1371/journal.pone.0165819","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBoulder-Investment-Technologies%2Flppls","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FBoulder-Investment-Technologies%2Flppls","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBoulder-Investment-Technologies%2Flppls/lists"}