Skip to content

Commit

Permalink
Quantile Regression (#101)
Browse files Browse the repository at this point in the history
* add subclass for quantile regression.

* add usage quantile regression example notebook.

* update readme.

* bump version
  • Loading branch information
Joshwani authored Jan 31, 2024
1 parent 40d6dcd commit 2c48a26
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ If you wish to store `res` as a pd.DataFrame, use `compute_indicators`.

</details>

## Quantile Regression
Based on the work in Zhang, Zhang & Sornette 2016, quantile regression for LPPLS uses the L1 norm (sum of absolute differences) instead of the L2 norm
and 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).

## Other Search Algorithms
Shu 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$).
> The CMA-ES rates among the most successful evolutionary
Expand All @@ -141,3 +145,4 @@ Performance Note: this works well for single fits but can take a long time for c
- Shu, M. and Zhu, W. Real-time Prediction of Bitcoin Bubble Crashes. 2019.
- Sornette, D. Why Stock Markets Crash: Critical Events in Complex Financial Systems. Princeton University Press. 2002.
- 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.
- 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
35 changes: 35 additions & 0 deletions lppls/lppls_q.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from lppls.lppls import LPPLS
import numpy as np


class QLPPLS(LPPLS):
def __init__(self, observations, q=0.5):
super().__init__(observations)
self.q = q

def func_restricted(self, x, *args):
"""
Finds the least absolute differences adjusted for the q-dependent loss function.
Args:
x(np.ndarray): 1-D array with shape (n,).
args: Tuple of the fixed parameters needed to completely specify the function.
Returns:
(float)
"""

tc = x[0]
m = x[1]
w = x[2]
observations = args[0]

rM = self.matrix_equation(observations, tc, m, w)
a, b, c1, c2 = rM[:, 0].tolist()

delta = [self.lppls(t, tc, m, w, a, b, c1, c2) for t in observations[0, :]]
delta = np.subtract(delta, observations[1, :])

# Use the L1 norm (sum of absolute differences) instead of the L2 norm
# Apply the q-dependent loss function using the given quantile
loss = np.sum([-(1 - self.q) * e if e < 0 else self.q * e for e in np.abs(delta)])

return loss
123 changes: 123 additions & 0 deletions notebooks/quantile_regression.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
long_description = fh.read()

setuptools.setup(name='lppls',
version='0.6.11',
version='0.6.12',
description='A Python module for fitting the LPPLS model to data.',
packages=['lppls'],
author='Josh Nielsen',
Expand Down

0 comments on commit 2c48a26

Please sign in to comment.