Code Monkey home page Code Monkey logo

hurst's Introduction

hurst

Hurst exponent evaluation and R/S-analysis

Python 2.7 Python 3x Build Status pypi Downloads

hurst is a small Python module for analysing random walks and evaluating the Hurst exponent (H).

H = 0.5 — Brownian motion,
0.5 < H < 1.0 — persistent behavior,
0 < H < 0.5 — anti-persistent behavior.

Installation

Install hurst module with
pip install hurst
or
pip install -e git+https://github.com/Mottl/hurst#egg=hurst

Usage

import numpy as np
import matplotlib.pyplot as plt
from hurst import compute_Hc, random_walk

# Use random_walk() function or generate a random walk series manually:
# series = random_walk(99999, cumprod=True)
np.random.seed(42)
random_changes = 1. + np.random.randn(99999) / 1000.
series = np.cumprod(random_changes)  # create a random walk from random changes

# Evaluate Hurst equation
H, c, data = compute_Hc(series, kind='price', simplified=True)

# Plot
f, ax = plt.subplots()
ax.plot(data[0], c*data[0]**H, color="deepskyblue")
ax.scatter(data[0], data[1], color="purple")
ax.set_xscale('log')
ax.set_yscale('log')
ax.set_xlabel('Time interval')
ax.set_ylabel('R/S ratio')
ax.grid(True)
plt.show()

print("H={:.4f}, c={:.4f}".format(H,c))

R/S analysis

H=0.4964, c=1.4877

Kinds of series

The kind parameter of the compute_Hc function can have the following values:
'change': a series is just random values (i.e. np.random.randn(...))
'random_walk': a series is a cumulative sum of changes (i.e. np.cumsum(np.random.randn(...)))
'price': a series is a cumulative product of changes (i.e. np.cumprod(1+epsilon*np.random.randn(...))

Brownian motion, persistent and antipersistent random walks

You can generate random walks with random_walk() function as following:

Brownian

brownian = random_walk(99999, proba=0.5)

Brownian motion

Persistent

persistent = random_walk(99999, proba=0.7)

Persistent random walk

Antipersistent

antipersistent = random_walk(99999, proba=0.3)

Antipersistent random walk

hurst's People

Contributors

mottl avatar vido avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hurst's Issues

Finite differencing step at the beginning when kind='random_walk'?

When I look at the code for the compute_Hc function, there is an initial finite differencing step. Everything else after that agrees with Wikipedia's description of the Hurst exponent except it works with the derivative of the series, instead of the original series.

The random_walk function, which is supposed to use Fractional Brownian Motion, seems to be in agreement with their compute_Hc function. If you remove the differencing step, then there ends up being disagreement. But then is the random_walk function correctly implemented? Because if it is correctly implemented, then the Wikipedia article needs to be corrected.

kind in compute_Hc

I have a cycle data, how I choose the "random_walk","change" and "price"? My data looks like Sin function.

Series length condition

Why do you require series length to be above 100 and not 50? Is this an arbitrary or universal condition? thttps://github.com/Mottl/hurst/blob/5ca5005485a679e6ce11a2769c948915ae27b2da/hurst/init.py#L146

Fix

Hello, Sir,
I would like to question you about this part in "hurst":
"
elif kind == 'change':
incs = series
_series = np.hstack([[0.],np.cumsum(incs)])
mean_inc = (series[-1] - series[0]) / len(incs)
deviations = incs - mean_inc
Z = np.cumsum(deviations)
R = max(Z) - min(Z)
S = np.std(incs, ddof=1)
"
shouldn't it be there like this
"mean_inc = (_series[-1] - _series[0])/len(incs)"?
Sorry in advance if I am wrong.

Hurst exponent less than 0 or greater than 1?

I noticed that the compute_Hc result for some time series is less than 0 or greater than 1. Is this behavior intended?

MWE to reproduce this bug:

import hurst
import numpy as np

print("numpy", np.__version__)
print("hurst", hurst.__version__)
print()

np.random.seed(988)
H, _, _ = hurst.compute_Hc(np.random.uniform(size=100), kind="random_walk", simplified=True)
print(H) # -0.017687382184009826

np.random.seed(916)
H, _, _ = hurst.compute_Hc(np.random.uniform(size=100), kind="random_walk", simplified=False)
print(H) # -0.011722357538317393

np.random.seed(164)
H, _, _ = hurst.compute_Hc(np.random.exponential(1, size=100), kind="change", simplified=True)
print(H) # 1.0118591069505447

Random series should return 0.5?

Random series should return 0.5, but:

a=np.random.randint(100,size=(300))
H, c, data = compute_Hc(a)
print(sname,"H={:.4f}, c={:.4f}".format(H,c))

Hurst exponent

Hello! Just working with your code on a time series analysis, to compute the Hurst exponent of a equity, e.g. Google.

Here is the code:

import matplotlib.pyplot as plt
from hurst import compute_Hc
from datetime import datetime
import pandas_datareader as web

series = web.DataReader("GOOG", "yahoo", datetime(2000,1,1), datetime(2013,1,1))

H, c, data = compute_Hc(series['Adj Close'], kind='price', simplified=True)

Output: H 0.82083 C 0.5268

Following this resource for the Google stock a Hurst of 0.50788 was calculated -> https://www.quantstart.com/articles/basics-of-statistical-mean-reversion-testing

In your script c is the equivalent to the Hurst Exponent being calculated in above link? Or are the two methods not comparable?

Hurst Exponents Above 1.0?

Hi Dmitry,

I was working with the latest release of your Hurst Exponent implementation (https://pypi.org/project/hurst/) and am seeing a bit of strangeness. Specifically my crypto price series seems to be getting HE values > 1.0, which obviously is incorrect.

I did email you the notebook as I am unable to attach it here.

Thanks in advance for any help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.