Skip to main content

Pattern Causality Algorithm in Python

Project description

pattern_causality_py

PyPI version Tests Coverage License

pattern_causality is a powerful Python library implementing the Pattern Causality algorithm for analyzing causal relationships in time series data. This package provides efficient tools for detecting and quantifying causality patterns between multiple time series, with particular emphasis on nonlinear complex systems.

Key Features

  • Lightweight Analysis: Fast causality detection between pairs of time series
  • Parameter Optimization: Automated search for optimal embedding parameters
  • Cross-validation Support: Robust validation of causality results
  • Matrix Analysis: Efficient computation of causality matrices for multiple time series
  • Effect Analysis: Advanced tools for analyzing causal effects in complex systems

Installation

Using pip (Recommended)

The easiest way to install the package is via pip:

pip install pattern_causality

From Source

For the latest development version, you can install directly from GitHub:

pip install git+https://github.com/wanghui5801/pattern_causality_py.git

Quick Start Guide

Loading Sample Data

The package comes with a built-in climate indices dataset for testing and demonstration:

from pattern_causality import load_data

# Load the included climate indices dataset
data = load_data()
print("Available climate indices:", data.columns.tolist())

Basic Causality Analysis

Perform a basic causality analysis between two time series using the lightweight implementation:

from pattern_causality import pc_lightweight

# Load data
data = load_data()

# Example using two climate indices
X = data['NAO'].values  # North Atlantic Oscillation
Y = data['AAO'].values  # Arctic Oscillation

# Run lightweight pattern causality analysis
# Parameters:
# - E: embedding dimension
# - tau: time delay
# - h: prediction horizon
# - metric: distance metric, default is "euclidean"
# - weighted: whether to use weighted causality, default is True
result = pc_lightweight(X=X, Y=Y, E=3, tau=1, h=1)
print("Causality strength:\n", result)

Parameter Optimization

Find the optimal parameters for your specific dataset:

from pattern_causality import optimal_parameters_search

data = load_data()
# Search for best parameters up to Emax and tau_max
result = optimal_parameters_search(
    Emax=5, 
    tau_max=5, 
    metric="euclidean", 
    dataset=data.drop(columns=['Date'])
)
print("Optimal parameters:\n", result)

Cross-Validation Analysis

Validate your causality results using different sample sizes:

from pattern_causality import pc_cross_validation

data = load_data()
X = data['NAO'].values
Y = data['AAO'].values

# Perform cross-validation with different sample sizes
cv_results = pc_cross_validation(
    X=X,
    Y=Y,
    E=3,
    tau=1,
    metric="euclidean",
    h=1,
    weighted=True,
    numberset=[100, 200, 300, 400, 500]  # Different sample sizes
)
print("Cross-validation results:\n", cv_results)

Multi-Series Analysis

Analyze causality patterns between multiple time series simultaneously:

from pattern_causality import pc_matrix

data = load_data()
results = pc_matrix(
    dataset=data.drop(columns=['Date']),
    E=3,
    tau=1,
    metric="euclidean",
    h=1,
    weighted=True
)

print("Pattern Causality Matrix Results:")
print("Positive causality matrix:", results['positive'])
print("\nNegative causality matrix:", results['negative'])
print("\nDark causality matrix:", results['dark'])
print("\nVariable names:", results['items'])

Effect Analysis

Calculate and analyze the causal effects between different time series:

from pattern_causality import pc_matrix, pc_effect

# Load data and calculate pc_matrix
data = load_data()
pc_matrix_results = pc_matrix(
    dataset=data.drop(columns=['Date']),
    E=3,
    tau=1,
    metric="euclidean",
    h=1,
    weighted=True
)

# Calculate effects
effects = pc_effect(pc_matrix_results)
print("Pattern Causality Effects:")
print("\nPositive effects:", effects['positive'])
print("\nNegative effects:", effects['negative'])
print("\nDark effects:", effects['dark'])

Testing

This package includes a comprehensive test suite. To run the tests:

## Install test dependencies
pip install pytest pytest-cov
## Run tests
python -m pytest tests/
## Run tests with coverage report
python -m pytest tests/ --cov=pattern_causality

Test Coverage

The test suite covers:

  • Basic functionality tests
  • Advanced functionality tests
  • Utility function tests

Current test coverage: 79%

Contributing

We welcome contributions! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Development

  1. Clone the repository
  2. Install development dependencies:
    pip install -e ".[dev]"
    
  3. Run tests:
    pytest
    

References

  • Stavroglou, S. K., Pantelous, A. A., Stanley, H. E., & Zuev, K. M. (2019). Hidden interactions in financial markets. Proceedings of the National Academy of Sciences, 116(22), 10646-10651.

  • Stavroglou, S. K., Pantelous, A. A., Stanley, H. E., & Zuev, K. M. (2020). Unveiling causal interactions in complex systems. Proceedings of the National Academy of Sciences, 117(14), 7599-7605.

  • Stavroglou, S. K., Ayyub, B. M., Kallinterakis, V., Pantelous, A. A., & Stanley, H. E. (2021). A novel causal risk‐based decision‐making methodology: The case of coronavirus. Risk Analysis, 41(5), 814-830.

License

This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.

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

pattern_causality-0.0.1.tar.gz (33.5 kB view details)

Uploaded Source

Built Distribution

pattern_causality-0.0.1-cp39-cp39-macosx_10_9_universal2.whl (151.3 kB view details)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file pattern_causality-0.0.1.tar.gz.

File metadata

  • Download URL: pattern_causality-0.0.1.tar.gz
  • Upload date:
  • Size: 33.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.6

File hashes

Hashes for pattern_causality-0.0.1.tar.gz
Algorithm Hash digest
SHA256 bd0ef28f577ac2c96f422a9cba8c67f7b8aa93895c402986685d14392f67dbff
MD5 6fe764e28e9538cff73d678a29266465
BLAKE2b-256 8da223c86da24fab43c217a1182ebee8295aab69bdc63c58647b7aff7e780040

See more details on using hashes here.

File details

Details for the file pattern_causality-0.0.1-cp39-cp39-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for pattern_causality-0.0.1-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 e3e9d69b3bd0a12f12f7880ac2ba1edc4afeda89d3bd2bced8912c0c996673a4
MD5 1c0f4a5b2de0c4bcf1b4c4b3344f01cd
BLAKE2b-256 640f3d6d62f73503442fb8385b8eff10c899b782db2848e2f83c7fe36a2d3a8c

See more details on using hashes here.

Supported by

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