Learn R Programming

⚠️There's a newer version (0.2.1) of this package.Take me there.

Pattern Causality

Overview

The patterncausality package implements a novel approach for detecting and analyzing causal relationships in complex systems. Key features include:

Core Capabilities

  • Pattern-based causality detection
  • State space reconstruction
  • Multi-dimensional causality analysis
  • Robust cross-validation methods

Applications

  • Financial market analysis
  • Climate system interactions
  • Medical diagnosis
  • Ecological system dynamics

Key Advantages

  • Detects nonlinear causal relationships
  • Quantifies causality strength
  • Identifies hidden patterns
  • Handles noisy data effectively

This algorithm has a lot of advantages.

  • You can find the hidden pattern in the complex system.
  • You can measure the causality in different fields.
  • You can search for the best parameters for the complex system.

Installation

You can install the development version of patterncausality from GitHub with:

# install.packages("devtools")
devtools::install_github("skstavroglou/pattern_causality")

You can also install the package from CRAN with:

install.packages("patterncausality")

Example

Application in climate

We can import the existing data.

library(patterncausality)
data(climate_indices)
head(climate_indices)
#>         Date      AO    AAO   NAO   PNA
#> 1 1979-01-01 -2.2328 0.2088 -1.38 -0.69
#> 2 1979-02-01 -0.6967 0.3563 -0.67 -1.82
#> 3 1979-03-01 -0.8141 0.8992  0.78  0.38
#> 4 1979-04-01 -1.1568 0.6776 -1.71  0.09
#> 5 1979-05-01 -0.2501 0.7237 -1.03  1.35
#> 6 1979-06-01  0.9332 1.7000  1.60 -1.64

This dataset contains 4 famous time series of climate index, we can find the introduction of this dataset in the CRAN and R documment, we could use the patterncausality in this dataset to detect the hidden causality in this climate system.

The climate system is a typical complex system like lorenz system, which are both originating from the climate system, it’s a good example to show how to find the hidden causality in the complex system.

First of all, we need to determine the E and tao, it could be easy to complete by optimalParametersSearch function like this:

dataset <- climate_indices[, -1] # remove the date column
parameter <- optimalParametersSearch(Emax = 5, tauMax = 5, metric = "euclidean", dataset = dataset)
EtauTotalPositiveNegativeDark
210.55038020.55290910.446472390.0006185057
220.56724030.57225290.424611120.0031359329
230.56474360.54714880.451067620.0017836150
240.55383620.54856370.449611870.0018243903
250.56160830.54339070.455130140.0014791531
310.32037750.34608090.246909590.4070094904
320.33624600.40104030.254104460.3448552507
330.33889980.36573690.268570830.3656922393

Of course, we can also change the distance style to calculate the distance matrix or even custom distance function, we can find more inforation on our website. Then according the combo that produces the highest percentages collectively, we can choose the best parameters here.

After the parameters are confirmed, we could calculate the pattern causality.

X <- climate_indices$AO
Y <- climate_indices$AAO
pc <- pcLightweight(X, Y, E = 3, tau = 1, metric = "euclidean", h = 1, weighted = TRUE, verbose = FALSE)
print(pc)
#> Pattern Causality Analysis Results:
#> Total: 0.2336
#> Positive: 0.4471
#> Negative: 0.1380
#> Dark: 0.4150

The percentages of each causality status will be displayed below.

To examine the causality status at each time point, we can run the following code and find the causality strength at each time point by function pcFullDetails, the causality_predict is the predicted causality status at each point, the parameter weighted = TRUE is used to for erf function and if it’s FALSE, then it will just use the 1 or 0 to present the causality strength, however, whatever which one is used, the total causality points will be the same.

X <- climate_indices$AO
Y <- climate_indices$AAO
detail <- pcFullDetails(X, Y, E = 3, tau = 1, metric = "euclidean", h = 1, weighted = TRUE, verbose = FALSE)
predict_status <- detail$causality_predict
real_status <- detail$causality_real

Then the causality strength series will be saved in predict_status and real_status, if we want to plot the causality strength series, we can use the plot_causality function for the pc_full_details class, and it will show the continuous causality strength series in the whole time period, we can find the dynamic pattern causality strength by this way.

Conclusion

After calculating the causality, we can get the result here.

PairstotalpositivenegativedarkDataset
AAPL –> MSFT0.26986650.38812790.13698630.4748858stock
MSFT –> AAPL0.27598870.40758930.13883930.4535714stock
AO –> AAO0.28411210.3260870.23188410.442029climate
AAO –> AO0.28037380.36029410.26470590.375climate
AO –> P0.30841120.11920530.45033110.4304636AUCO
P –> AO0.33084110.33742330.25153370.4110429AUCO

About the authors

Stavros is lecturer in credit risk and fin-tech at the University of Edinburgh Business School and is the main creator for the algorithm of the pattern causality.

Athanasios is professor in econometrics and business statistics of Monash Business School and is the main author of the pattern causality.

Hui is MPhil student in econometrics and business statistics of Monash Business School and is the author and maintainer of the patterncausality package.

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.

Test environments

  • local R installation, R 4.1.0
  • ubuntu 20.04 (on GitHub Actions), R 4.1.0
  • win-builder (devel and release)

R CMD check results

0 errors | 0 warnings | 0 notes

Downstream dependencies

There are currently no downstream dependencies for this package

Copy Link

Version

Install

install.packages('patterncausality')

Monthly Downloads

192

Version

0.2.0

License

GPL-3 | file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Hui Wang

Last Published

January 10th, 2025

Functions in patterncausality (0.2.0)

plot.pc_cv

Plot Pattern Causality Cross Validation Results
print.pc_fit

Print Pattern Causality Results
print.pc_matrix

Print Pattern Causality Matrix
print.summary.pc_accuracy

Print Method for Pattern Causality Accuracy Summary
summary.pc_fit

Summarize Pattern Causality Results
print.pc_effect

Print Pattern Causality Effect
stateSpace

State Space Reconstruction
summary.pc_matrix

Summarize Pattern Causality Matrix
print.pc_params

Print Method for Pattern Causality Parameter Results
print.pc_accuracy

Print Method for Pattern Causality Accuracy Results
plot_total.pc_fit

Plot Total Causality
plot.pc_state

Plot State Space Reconstruction
plot_causality

Plot Pattern Causality Time Series
print.pc_state

Print State Space Reconstruction
plot.pc_effect

Plot Pattern Causality Effect
plot_causality.pc_full_details

Plot Pattern Causality Time Series
validate_custom_fn_output

Validate Custom Function Output for Pattern Causality Analysis
plot_components

Plot Pattern Causality Components
plot_components.pc_fit

Plot Causality Components
summary.pc_effect

Summarize Pattern Causality Effect
summary.pc_cv

Summary of Pattern Causality Cross Validation Results
stateSpaceMethod

State Space Reconstruction Interface
summary.pc_accuracy

Summary Method for Pattern Causality Accuracy Results
plot_total

Plot Total Pattern Causality
summary.pc_params

Summary Method for Pattern Causality Parameter Results
summary.pc_state

Summarize State Space Reconstruction
AUCO

Illapel Ecological Dataset
pcAccuracy

Calculate Pattern Causality Accuracy
optimalParametersSearch

Search for Optimal Parameters in Pattern Causality Analysis
DJS

Dow Jones Stock Price Dataset
distanceMetric

Distance Metric Interface
pc_matrix

Pattern Causality Matrix Object
pcLightweight

Calculate Pattern Causality Using Lightweight Algorithm
pcMatrix

Pattern Causality Matrix Analysis
pc_params

Pattern Causality Parameter Optimization Results
pc_cv

Pattern Causality Cross-Validation Object
pc_effect

Pattern Causality Effect Object
pcEffect

Pattern Causality Effect Analysis
print.pc_cv

Print Pattern Causality Cross Validation Results
pcCrossMatrix

Cross Pattern Causality Matrix Analysis
pcCrossValidation

Pattern Causality Cross-Validation Analysis
climate_indices

Climate Indices Dataset
pcFullDetails

Calculate Full Details Pattern Causality Analysis
plot.pc_matrix

Plot Pattern Causality Matrix
plot.pc_fit

Plot Pattern Causality Results