Coder Social home page Coder Social logo

tesla1060 / lead-lag Goto Github PK

View Code? Open in Web Editor NEW

This project forked from philipperemy/lead-lag

0.0 0.0 0.0 861 KB

Estimation of the lead-lag parameter from non-synchronous data.

License: MIT License

Python 22.79% Makefile 0.17% Jupyter Notebook 76.53% Shell 0.51%

lead-lag's Introduction

Estimation of the lead-lag from non-synchronous data

Implementation of the paper https://arxiv.org/pdf/1303.4871.pdf

Complexity: N O(LOG N)

Limitations: Only supports up to the second. Everything labeled in milliseconds is not correctly handled (at the moment).

Abstract

We propose a simple continuous time model for modeling the lead-lag effect between two financial assets. A two-dimensional process (Xt, Yt) reproduces a lead-lag effect if, for some time shift ϑ ∈ R, the process (Xt, Yt+ϑ) is a semi-martingale with respect to a certain filtration. The value of the time shift ϑ is the lead-lag parameter. Depending on the underlying filtration, the standard no-arbitrage case is obtained for ϑ = 0. We study the problem of estimating the unknown parameter ϑ ∈ R, given randomly sampled non-synchronous data from (Xt) and (Yt). By applying a certain contrast optimization based on a modified version of the Hayashi–Yoshida covariation estimator, we obtain a consistent estimator of the lead-lag parameter, together with an explicit rate of convergence governed by the sparsity of the sampling design.

Get started

You have to install the library as a package first by running those commands:

Method 1

pip install Cython
pip install git+ssh://[email protected]/philipperemy/lead-lag

Method 2

git clone [email protected]:philipperemy/lead-lag.git && cd lead-lag
virtualenv -p python3.6 venv3.6
source venv3.6/bin/activate
make

A way to test that the library has been correctly installed.

python -c "import lead_lag; print('success')"

Then you can run one of those Jupyter Notebooks:

pip install jupyter
cd notebooks
jupyter notebook lead_lag_example_1.ipynb
jupyter notebook lead_lag_example_2.ipynb

Numerical Illustrations (cf. Jupyter Notebook files)

Non synchronous data (generated from the Brownian Bachelier model)

We simulate a lead-lag Bachelier model without drift with:

  • N = 10,000 (grid on which we sample random arriving times for both X and Y).
  • #I = 500
  • #J = 3,000
  • ρ = 0.80, x0 = 1.0, y0 = 2.1, s1 = 1.0, s2 = 1.5
  • lead_lag = 200 (X is the leader, Y the lagger)
  • finite grid Gn = [0, 400]

We show a realization of the process (Xt, Yt) and its corresponding Constrast vs Lag plot:

The contrast is just a positive definitive cross correlation quantity.

Clearly, the argmax of the constrast is located around the correct value (lead_lag = 200). We also observe some persistence in the constrast (I may have forgotten an extra term in the modified HY estimator). Even though X has a sampling rate 7x lower than Y, the estimator can still pick up the correct value. We can also normalize the contrast to have an unbiased estimation of the cross correlation function rho for different lags. In theory this function should be a Dirac centered around the lead_lag parameter with ρ(lead_lag) = 0.8 and 0 elsewhere.

We can also look at negative lags and define the LLR (standing for Lead/Lag Ratio) to measure the lead/lag relationships. If LLR > 1, then X is the leader and Y the lagger and vice versa for LLR <= 1. In our case, for the realization of our process (X,Y), we find LLR ~ 8.03.

Non synchronous data (Bitcoin markets)

We now consider a real world use case where we have two Japanese bitcoin exchanges: bitflyer and btcbox. The former has higher liquidity hence we expect it to lead the latter. If we plot the prices of BTC/JPY for both exchanges for a specific day, we get:

So which one leads? We apply the same lead lag procedure using the constrast quantity computed on a grid Gn = ]-40,40[ (unit is second here).

The contrast is maximized for ϑ = 15 seconds. This promptly means that bitflyer is the leader as expected and that btcbox takes on average 15 seconds to reflect any changes on its price.

Realtime example

Limitations of this current implementation

  • Only supports up to the second. Everything labeled in milliseconds is not correctly handled.

References

lead-lag's People

Contributors

ohenrik avatar philipperemy avatar psatyajeet avatar

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.