Skip to main content

Pattern Causality Algorithm in Python

Project description

pattern_causality_py

PyPI version PyPI Downloads Tests Lint License Python Code style: black

Overview

pattern_causality is a comprehensive Python library that implements 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 a particular focus on nonlinear complex systems.

Key Features

  • Efficient C++ Implementation: Core algorithms implemented in C++ for maximum performance
  • Comprehensive Analysis Tools:
    • Basic pattern causality analysis
    • Multivariate time series analysis
    • Cross-validation capabilities
    • Parameter optimization
    • Effect metrics calculation
  • Built-in Dataset: Includes climate indices dataset for demonstration
  • OpenMP Support: Parallel processing for improved performance
  • Extensive Testing: Comprehensive test suite with high coverage

System Requirements

  • Python 3.8 or later
  • C++ compiler with C++11 support
  • OpenMP support (for parallel processing)
  • NumPy 1.19.0 or later
  • Pandas 1.0.0 or later

Changelog

Version 1.0.3 (2024-02-15)

  • Fixed integer type conversion issue in natureOfCausality function for Windows compatibility
  • Improved type handling for array data in pattern causality calculations
  • Enhanced cross-platform compatibility for integer types

Version 1.0.2 (2024-02-15)

  • Changed default behavior to use relative differences (relative=True by default)
  • Added relative parameter to signaturespace for choosing between relative and absolute differences
  • Enhanced documentation for the new parameter
  • Improved backward compatibility with absolute difference mode (relative=False)

Version 1.0.1 (2024-02-14)

  • Fixed type conversion issue in natureOfCausality function
  • Improved compatibility with different system architectures by using np.int_
  • Enhanced stability for array data type handling
  • Fixed Python 3.8 compatibility issue with numpy integer types

Installation

Via pip (Recommended)

pip install pattern-causality

Via pip + git

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

From Source

Prerequisites

On Ubuntu/Debian:

sudo apt-get update
sudo apt-get install -y g++ python3-dev libomp-dev build-essential

On macOS:

brew install libomp

Installing the Package

# Install required Python packages
python -m pip install numpy pandas

# Install pattern-causality
python -m pip install -e .

Usage Examples

Basic Usage

from pattern_causality import pattern_causality, load_data

# Load the included climate indices dataset
data = load_data()

# Initialize pattern causality analyzer
pc = pattern_causality(verbose=True)

# Analyze causality between NAO and AAO indices
result = pc.pc_lightweight(
    X=data["NAO"].values,
    Y=data["AAO"].values,
    E=3,          # embedding dimension
    tau=1,        # time delay
    metric="euclidean",
    h=1,          # prediction horizon
    weighted=True, # use weighted calculations
    relative=True  # use relative differences (default)
)

print(result)

Multivariate Analysis

# Analyze causality patterns across multiple variables
matrix_result = pc.pc_matrix(
    dataset=data.drop(columns=["Date"]),
    E=3,
    tau=1,
    metric="euclidean",
    h=1,
    weighted=True,
    relative=True  # Using relative differences (default)
)

print("Pattern Causality Matrix Results:")
print(matrix_result)

Parameter Optimization

# Find optimal parameters
optimal_params = pc.optimal_parameters_search(
    Emax=5,
    tau_max=3,
    metric="euclidean",
    h=1,
    dataset=data.drop(columns=["Date"])
)

print("Optimal Parameters:")
print(optimal_params)

Cross Validation

# Perform cross-validation
cv_results = pc.pc_cross_validation(
    X=data["NAO"].values,
    Y=data["AAO"].values,
    E=3,
    tau=1,
    metric="euclidean",
    h=1,
    weighted=True,
    numberset=[100, 200, 300]
)

print("Cross-validation Results:")
print(cv_results)

Development

Setting Up Development Environment

  1. Clone the repository:
git clone https://github.com/skstavroglou/pattern_causality_py.git
cd pattern_causality_py
  1. Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate  # On Unix/macOS
# or
.\venv\Scripts\activate  # On Windows
  1. Install development dependencies:
python -m pip install -e ".[dev]"

Running Tests

# Run tests with coverage
python -m pytest tests/ --cov=pattern_causality -v

Code Style

The project uses:

  • Black for code formatting
  • isort for import sorting
  • flake8 for linting
  • mypy for type checking

To check code style:

black .
isort .
flake8 .
mypy pattern_causality

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run the test suite
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

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-1.0.3.tar.gz (357.6 kB view details)

Uploaded Source

Built Distribution

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

pattern_causality-1.0.3-cp39-cp39-macosx_10_9_universal2.whl (178.5 kB view details)

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

File details

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

File metadata

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

File hashes

Hashes for pattern_causality-1.0.3.tar.gz
Algorithm Hash digest
SHA256 99d42253d559992eb0137356ad52036789f00dd87ee7f5b1e4e6ebbad5e55141
MD5 f2b61cf66386abbb960ab7fb4a1784ca
BLAKE2b-256 23b62af55572ddc1fd996ac730784a9c3cbed8cc14226079eb526fda10aa1b7d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pattern_causality-1.0.3-cp39-cp39-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 cff37cbe1423dda318a7cbb3cc05ddd93d526679a2cab38a85cce69d7f2b6a5d
MD5 1bdc63571f75c066383a1267391d49e8
BLAKE2b-256 137c8596108d18b59d45d6e382563685bf5262ec5ca58e759e93658b308d0a14

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