Metadata-Version: 2.4
Name: causalchange
Version: 0.1.0
Summary: Algorithms for Causal Discovery under Distribution Change
Author: Sarah Mameche
License: MIT License
        
        Copyright (c) 2025 Sarah Mameche
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/srhmm/causalchange
Project-URL: Issues, https://github.com/srhmm/causalchange/issues
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.26
Requires-Dist: pandas>=2.0
Requires-Dist: networkx>=3.0
Requires-Dist: pydantic>=2.0
Requires-Dist: scikit-learn>=1.3
Requires-Dist: scipy>=1.10
Provides-Extra: spacetime
Requires-Dist: ruptures; extra == "spacetime"
Requires-Dist: hyppo; extra == "spacetime"
Requires-Dist: causal-learn; extra == "spacetime"
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: pre-commit; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Provides-Extra: notebooks
Requires-Dist: jupyter; extra == "notebooks"
Requires-Dist: ipykernel; extra == "notebooks"
Requires-Dist: matplotlib; extra == "notebooks"
Dynamic: license-file

## CausalChange

Implementations of causal discovery algorithms for settings in which causal mechanisms may change across contexts, regimes, or time.
Available methods are

* **TOPIC** for score-based causal DAG discovery from tabular data in topological order [2],
* **LINC** for causal discovery from multiple contexts, i.e., multiple tabular datasets with distribution shifts [1],
* **SpaceTime** for temporal causal discovery and changepoint detection in time series or multi-context time series [3].

---

### Setup

#### Conda

Use the following to create a conda environment and install the package,

```bash
conda create -n causalchange python=3.10 -y
conda activate causalchange

pip install -e .
```

For running the notebooks, install a Jupyter kernel,

```bash
pip install ipykernel
python -m ipykernel install --user --name causalchange --display-name "Python (cc)"
```

#### Optional dependencies

Some functionality needs additional packages, `ruptures` for changepoint detection in SpaceTime,
`hyppo, causal-learn` for kernelized tests.


---

### Demos

The notebooks under `notebooks/` show basic usage on small synthetic examples.


---

### Algorithms

#### Tabular causal discovery with TOPIC

TOPIC [2] is a score-based causal discovery method for tabular data that searches over topological orders.
In this library it is implemented as a modular graph search backend and can be combined with different local MDL scores.

```python
import pandas as pd

from causalchange.causal_change import CausalChange
from causalchange.config.cc_types import (
    ContextAggregation,
    DataMode,
    GraphSearch,
    ScoreType,
)

X = pd.DataFrame(...)

cc = CausalChange(
    data_mode=DataMode.IID,
    graph_search=GraphSearch.TOPIC,
    score_type=ScoreType.LIN,
    aggregation=ContextAggregation.SKIP,
)

cc.fit(X)

print(cc.graph_.edges())
```

#### Multi-context tabular data with LINC

For tabular data from multiple contexts, the library supports an extension of TOPIC to multiple contexts
using the ideas described in the LINC paper [1].
For tabular data with multiple contexts, the context column is specified through `context_col`.

```python
cc = CausalChange(
    data_mode=DataMode.CONTEXTS,
    graph_search=GraphSearch.TOPIC,
    score_type=ScoreType.LIN,
    aggregation=ContextAggregation.LINC,
    context_col="context",
)

cc.fit(X)
```

Temporal multi-context data is handled separately through SpaceTime.

#### Time series causal discovery with SpaceTime

SpaceTime is a score-based causal discovery method for time series, optionally with multiple contexts and changepoints.
The current implementation supports both single time series as well as multi-context time series.
For this, use `DataMode.TIME` or `DataMode.TIME_CONTEXTS`.

```python
from causalchange.config.cc_config import ChangepointMode

cc = CausalChange(
    data_mode=DataMode.TIME_CONTEXTS,
    graph_search=GraphSearch.GLOBE,
    score_type=ScoreType.LIN,
    aggregation=ContextAggregation.SKIP,
    context_col="context",
    tau_max=2,
    changepoints=ChangepointMode.DETECT,
    d_min=20,
    pelt_penalty=1.0,
    detect_contexts=True,
    detect_regimes=True,
)

cc.fit(X)
```

SpaceTime uses temporal nodes of the form

```python
("x0", 0)  # current time
("x0", 1)  # lag 1
("x0", 2)  # lag 2
```

and learns directed edges into lag-0 variables. For example, `(("x0", 1), ("x1", 0))` means `x0(t-1) -> x1(t)`.

---

### Tests

Run the full test suite with

```bash
pytest
```

Run linting and formatting with

```bash
ruff check .
ruff format .
```

Before committing, the repository uses pre-commit hooks.

```bash
pre-commit run --all-files
```

---

### References

[1] Mameche, S., Kaltenpoth, D., and Vreeken, J. *Learning Causal Models under Independent Changes.* NeurIPS, 2023.

[2] Xu, S., Mameche, S., and Vreeken, J. *Information-theoretic Causal Discovery in Topological Order.* AISTATS, 2025.

[3] Mameche, S., Cornanguer, L., Ninad, U., and Vreeken, J. *SpaceTime: Causal Discovery from Non-stationary Time Series.* AAAI, 2025.

[4] Mameche, S., Kalofolias, J., and Vreeken, J. *Causal Mixture Models: Characterization and Discovery.* NeurIPS, 2025.
