Skip to main content

Python package for defining and interacting with causal models, with a particular emphasis on Structural Causal Models

Project description

causalite

Overview

Causalite is a Python package for defining and interacting with causal models, with a particular emphasis on Structural Causal Models (SCMs). It aims to be an accessible tool that enables users to develop their understanding of causal models and test their own ideas.

Functionality is provided for:

  • Defining an SCM by specifying causal mechanisms for each variable/node in the corresponding Directed Acyclic Graph
  • Sampling from the SCM
  • Simulating Randomised Controlled Trials (RCT) by intervening on a 'treatment' variable in the SCM
  • Applying the do-operator to the SCM to generate interventional samples
  • Computing deterministic Counterfactuals given an SCM and a set of observed data

Disclaimer

Versions 0.y.z of causalite may include breaking changes to the API. Changes to each release are documented.

Installation

We recommend you install causalite using pip. We also recommend that you install it into a virtual environment to avoid potential conflicts with other packages. Once you have created and activated the virtual environment, you can install causalite as follows:

$ pip install causalite

Getting Started

Below we demonstrate how to use causalite to create and sample from a Structural Causal Model. We firstly do some imports:

>>> from causalite import causal_models as cm
>>> from causalite import node_models as nm
>>> import pandas as pd

We define an SCM consisting of 3 variables/nodes A, X and Y by specifying the models representing the causal mechanisms for each node. (We use the words 'variable' and 'node' interchangeably.)

>>> model = cm.StructuralCausalModel(node_models=[
        nm.NodeAdditiveNoiseModel('A'),
        nm.NodeAdditiveNoiseModel('X', parent_polys={'A': [1., 0., -3.]}),
        nm.NodeAdditiveNoiseModel('Y', parent_polys={'X': [-0.5, 0., 1.2], 'A': [1.4], 'XA': [3.]})
    ])

This results in the following SCM:

>>> print(model)
Structural Causal Model
=======================

A <-  U_A

X <-  1.0A - 3.0A^3 + U_X

Y <-  - 0.5X + 1.2X^3 + 1.4A + 3.0XA + U_Y

Here, U_A, U_X and U_Y represent the exogenous noise for variables A, X and Y respectively. We can draw a sample from the SCM and store it in a pandas dataframe as follows:

>>> samples = model.draw_sample(size=50000)
>>> samples.head()
A X Y
0 1.764052 -13.080164 -2746.102137
1 0.400157 -0.403826 0.142060
2 0.978738 -2.362115 -22.336143
3 2.240893 -32.590699 -41737.620108
4 1.867558 -16.807889 -5782.921438

For more detail and demonstrations of functionality such as simulation of interventions and counterfactual computation, please see this notebook.

Documentation

Further illustrations of usage will be added to examples in due course.

Contributing

Please read this if you wish to help or contribute to causalite in any way whatsoever.

License

This project is licensed according to the Apache 2.0 software license.

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

causalite-0.1.0.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

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

causalite-0.1.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file causalite-0.1.0.tar.gz.

File metadata

  • Download URL: causalite-0.1.0.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for causalite-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e4ff4a11bab89ec93cd5be96434ea47b2375317c027838a074503a5e8e6134e2
MD5 1c9022e5412ee9a0b00fb803da1a49bd
BLAKE2b-256 afe1448e190a3ba4c76260dcaad80395b6c8369cfe2e837b54182d644a488fd1

See more details on using hashes here.

File details

Details for the file causalite-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: causalite-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for causalite-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b86832713d0cd9d458fc48d27cc616ade6d20da611482a44561b3065ec13ba1c
MD5 9d4d156d003c1260ae2d560462bcb78f
BLAKE2b-256 f03dbc4094f275f444d5c8522e454f053a5d4daad48dfa97d9008bb7db975b2c

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