| OS | Status |
|---|---|
| Linux | |
| Windows | |
| Mac |
A Python package implementing Structural Causal Models (SCM).
The library uses the CAS library SymPy to allow the user to state arbitrary assignment functions and noise distributions as supported by SymPy and builds the DAG with networkx.
It supports the features:
- Sampling
- Intervening
- Plotting
- Printing
and by extension all methods on a DAG provided by networkx after accessing the member variable dag
Installation
Git clone the repository and run the setup.py file
git clone https://github.com/maichmueller/scm
cd scm
python setup.py install
Example usage
To build the DAG
with the assignments
one can describe the assignments as strings
from scm import SCM
myscm = SCM(
[
"Z = N, N ~ LogLogistic(alpha=1, beta=1)",
"X = N * 3 * Z ** 2, N ~ LogNormal(mean=1, std=1)",
"Y = N + 2 * Z + sqrt(X), N ~ Normal(mean=2, std=1)"
]
)
or build the assignments piecewise themselves via an assignment map
from sympy.stats import LogLogistic, LogNormal, Normal
assignment_map = {
"Z": (
"N",
LogLogistic("N", alpha=1, beta=1)
),
"X": (
"N * 3 * Z ** 2",
LogNormal("N", mean=1, std=1),
),
"Y": (
"N + 2 * Z + sqrt(X)",
Normal("N", mean=2, std=1),
),
}
myscm = SCM(assignment_map)
The SCM supports a form of pretty printing its current setup, which includes mentioning active interventions and the assignments
print(myscm)
Structural Causal Model of 3 variables: Z, X, Y
Following variables are actively intervened on: []
Current Assignments are:
Z := f(N) = N [ N ~ LogLogistic(alpha=1, beta=1) ]
X := f(N, Z) = N * 3 * Z ** 2 [ N ~ LogNormal(mean=1, std=1) ]
Y := f(N, X, Z) = N + 2 * Z + sqrt(X) [ N ~ Normal(mean=2, std=1) ]
One can easily perform interventions on the variables, e.g. a Do-intervention
myscm.do_intervention([("X", 1)])
and sample as many samples from it as desired
myscm.sample(5)
| X | Z | Y | |
|---|---|---|---|
| 0 | 1 | 0.204204 | 2.412121 |
| 1 | 1 | 0.038627 | 2.035116 |
| 2 | 1 | 0.627638 | 4.767240 |
| 3 | 1 | 0.620331 | 4.497902 |
| 4 | 1 | 0.143817 | 4.723647 |
The current intervention is also noted in its string representation
print(myscm)
Structural Causal Model of 3 variables: Z, X, Y
Following variables are actively intervened on: ['X']
Current Assignments are:
Z := f(N) = N [ N ~ LogLogistic(alpha=1, beta=1) ]
X := f(N) = 1 [ N ~ LogNormal(mean=1, std=1) ]
Y := f(N, X, Z) = N + 2 * Z + sqrt(X) [ N ~ Normal(mean=2, std=1) ]
which can be restored to the initial status via undoing the intervention
myscm.undo_intervention()
If you have graphviz installed, you can also use it to plot the DAG easily
myscm.plot(node_size=1000, alpha=1)
No history capture as of yet.
Metadata
Release files for scmodels 0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| scmodels-0.1.tar.gz | 19.2 kB | Without provenance | Details for scmodels-0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| scmodels-0.1-py3-none-any.whl | Python 3 | none | any | Details for scmodels-0.1-py3-none-any.whl |
Total release size: 35.3 kB
Release files / scmodels-0.1.tar.gz
| Download URL | scmodels-0.1.tar.gz |
|---|---|
| Size | 19.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e0ac65f9ae0a56a20de6df2526bc2e46753cfee859109db6d3a70e768e07a7d6
|
|
BLAKE2b-256 checksum How to use checksums |
e61a1c831048e245d0e8156735150f5aa21bb6e90ed05e20d2abd42ddb01a93b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0.post20201207 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5
|
Release files / scmodels-0.1-py3-none-any.whl
| Download URL | scmodels-0.1-py3-none-any.whl |
|---|---|
| Size | 16.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
12ecdbed708ac31085604dbd328d3b1158ec6c6ebd37db8e9683e0f7f46cc41a
|
|
BLAKE2b-256 checksum How to use checksums |
4c1f0f4c0397ace4d6010ae65d66a07ae5e2ed2f15295158fa529d5735e41c11
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0.post20201207 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5
|
Security for scmodels 0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.