Skip to main content

Python package for Granger causality test with nonlinear (neural networks) forecasting methods.

Project description

nonlincausality

Python package for Granger causality test with nonlinear forecasting methods (neural networks).

This package contains two types of functions.

As a traditional Granger causality test is using linear regression for prediction it may not capture more complex causality relations. The first type of presented functions are using nonlinear forecasting methods (neural networks) for prediction instead of linear regression. For each tested lag this function is creating 2 models. The first one is forecasting the present value of X based on n=current lag past values of X, while the second model is forecasting the same value based on n=current lag past values of X and Y time series. If the prediction error of the second model is statistically significantly smaller than the error of the first model then it means that Y is G-causing X (Y➔X). It is also possible to test conditional causality using those functions. The functions based on neural networks can test the causality on the given test set (which is recomended). The first type of function contains: nonlincausalityMLP(), nonlincausalityLSTM(), nonlincausalityGRU(), nonlincausalityNN() (and additionally nonlincausalityARIMA(), as there is no other Python implementation of ARIMA/ARIMAX models for causality analysis). NOTE: currently MLP, GRU and LSTM functions might not work properly, but by using NN function can replace all those three.

The second type of functions is for measuring the change of causality over time. Those functions are using first type functions to create the forecasting models. They calculate the measure of the causality in a given time window (w1) with a given step (w2). The measure of change of the causality over time is expressed by the equation:

Equation 1

Those functions can operate with multiple time series and test causal relations for each pair of signals. The second type of function contains: nonlincausalitymeasureMLP(), nonlincausalitymeasureLSTM(), nonlincausalitymeasureGRU(), nonlincausalitymeasureNN() and nonlincausalitymeasureARIMA().

Author

Maciej Rosoł mrosol5@gmail.com, maciej.rosol.dokt@pw.edu.pl
Warsaw University of Technology

Reference

Maciej Rosoł, Marcel Młyńczak, Gerard Cybulski
Granger causality test with nonlinear neural-network-based methods: Python package and simulation study.
Computer Methods and Programs in Biomedicine, Volume 216, 2022
https://doi.org/10.1016/j.cmpb.2022.106669

Example usage

Assume that there are two signals X and Y, which are stored in the variable data, where X is in the first column and Y in the second. The variable data was split into data_train (first 70% of the data) and data_test (last 30% of the data). Then to test the presence of causality Y➔X for the given lag values (defined as a list e.g. [50, 150]) the following functions can be used (all arguments are examples and may vary depending on the data.).

MLP

results = nonlincausalityMLP(x=data_train, maxlag=lags, Dense_layers=2, Dense_neurons=[100, 100], x_test=data_test, run=1, add_Dropout=True, Dropout_rate=0.01, epochs_num=[50, 100], learning_rate=[0.001, 0.0001], batch_size_num=128, verbose=True, plot=True)

GRU

results_GRU = nonlincausalityGRU(x=data_train, maxlag=lags, GRU_layers=2, GRU_neurons=[25, 25], Dense_layers=2, Dense_neurons=[100, 100], x_test=data_test, run=3, add_Dropout=True, Dropout_rate=0.01, epochs_num=[50, 100], learning_rate=[0.001, 0.0001], batch_size_num=128, verbose=False, plot=True)

LSTM

results_LSTM = nonlincausalityLSTM(x=data_train, maxlag=lags, LSTM_layers=2, LSTM_neurons=[25, 25], Dense_layers=2, Dense_neurons=[100, 100], x_test=data_test, run=3, add_Dropout=True, Dropout_rate=0.01, epochs_num=[50, 100], learning_rate=[0.001, 0.0001], batch_size_num=128, verbose=False, plot=True)

NN

results_NN = nonlincausalityNN(x=data_train, maxlag=lags, NN_config=["l", "dr", "g", "dr", "d", "dr"], NN_neurons=[5, 0.1, 5, 0.1, 5, 0.1], x_test=data_test, run=3, epochs_num=[50, 100], learning_rate=[0.001, 0.0001], batch_size_num=128, verbose=False, plot=True)

ARIMA

results_ARIMA = nonlincausalityARIMA(x=data_train, maxlag=lags, x_test=data_train)

Change of causality over time

For a deeper understanding of the dependency between the signals, the change of causality over time might be studied using the above-mentioned functions. The example usage for MLP neural networks:

results = nlc.nonlincausalitymeasureMLP(x=data_train, maxlag=lags, window=100, step=1, Dense_layers=2, Dense_neurons=[100, 100], x_test=data_test, run=5, add_Dropout=True, Dropout_rate=0.01, epochs_num=[50, 100], learning_rate=[0.001, 0.0001], batch_size_num=128, verbose=False, plot=True)

Conditional causality

nonlincausality package also allows to study conditional causality (with signal Z).

results_conditional = nlc.nonlincausalityMLP(x=data_train, maxlag=lags, Dense_layers=2, Dense_neurons=[100, 100], x_test=data_test, run=5, add_Dropout=True, Dropout_rate=0.01, z=z_train, z_test=z_test, epochs_num=[50, 100], learning_rate=[0.001, 0.0001], batch_size_num=128, verbose=True, plot=True)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nonlincausality-1.1.11.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nonlincausality-1.1.11-py3-none-any.whl (21.0 kB view details)

Uploaded Python 3

File details

Details for the file nonlincausality-1.1.11.tar.gz.

File metadata

  • Download URL: nonlincausality-1.1.11.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for nonlincausality-1.1.11.tar.gz
Algorithm Hash digest
SHA256 6d6ce73483f9e89cba1b2a5c04f721b3fc94341125de30eea03dce2aef1debe0
MD5 79d4e2c37591bd622757ef6b50fd717f
BLAKE2b-256 c6a6f5dd05fbab99970873249d27c20dc3dde44e89f0ed732a9b49022b5cc5f4

See more details on using hashes here.

File details

Details for the file nonlincausality-1.1.11-py3-none-any.whl.

File metadata

File hashes

Hashes for nonlincausality-1.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 65630e71c56a8cd936d96eeb7a0f8594d7d2d2603a921d0da332f1882ac1d8fc
MD5 3a95bad4e5dd2e9fc6ec7ce3147dfc14
BLAKE2b-256 6382bf4acce7f6d8128af59b1a8a0c61ab8f91a0827fb00d78c1f9ab7485533b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page