MoonBit port of DoWhy 0.14 (PyWhy). Causal graph construction and analysis, effect identification (backdoor / frontdoor / complete adjustment), estimation (linear regression, generalized linear model, propensity score weighting / matching / stratification, doubly robust, instrumental variable, two stage, regression discontinuity), refutation (bootstrap, placebo treatment, data subset, dummy outcome, unobserved common cause), intervention sampling (do samplers), temporal graphs, causal discovery (the PC algorithm with the Fisher-z test) and the portable core of the graphical causal models (the p-value merging, the bootstrap confidence intervals, the Kullback-Leibler divergences, the density estimators, the Shapley values, the independence tests, the prediction models, the causal mechanisms, the fitting of a causal model and the what-if queries (the interventional samples, the counterfactual samples and the average causal effect). Pure MoonBit with zero external runtime dependencies.
| Area | Upstream | This port |
|---|---|---|
| Causal graph, d-separation | dowhy.graph, dowhy.causal_graph | ported |
| Identification | dowhy.causal_identifier | ported (backdoor, frontdoor, general adjustment, IV, mediation) |
| Estimation | dowhy.causal_estimators | ported (linear regression, generalised linear model, propensity score weighting / matching / stratification, doubly robust, instrumental variable, two stage, regression discontinuity) |
| Refutation | dowhy.causal_refuters | ported (bootstrap, placebo treatment, data subset, dummy outcome, random common cause, unobserved common cause) |
| Synthetic data | dowhy.datasets | ported (linear_dataset and the fixtures the upstream tests use) |
| Categorical encoding | dowhy.utils.encoding | ported (the one-hot encoder and the encoder registry of the estimators) |
| Dimensionality reduction | dowhy.data_transformers | ported (the PCA reducer) |
| Intervention sampling | dowhy.do_sampler, dowhy.do_samplers | ported (the weighting, multivariate weighting and kernel density samplers) -- the MCMC sampler is not ported, it needs an inference engine upstream takes from PyMC3 |
| Time series | dowhy.timeseries | ported (the temporal graph and the temporal shift) |
| Causal discovery | dowhy.graph_learners | ported (the PC algorithm with the Fisher-z test of conditional independence) |
| Causal discovery | dowhy.graph_learners (GES, CDT, LiNGAM) | not ported -- upstream delegates those to causal-learn / cdt / lingam, which this dependency-free port does not wrap |
| Graphical causal models | dowhy.gcm | partially ported -- the configuration, the general helpers, the p-value merging, the F-test, the marginal expectation, the bootstrap confidence intervals, the Kullback-Leibler divergences, the kernel density estimator, the Shapley values, the independence tests, the prediction models, the causal mechanisms, the probabilistic causal models, the fitting of a causal model, the automatic assignment of the mechanisms and the what-if queries are ported; the model evaluation, the uncertainty estimates, the anomaly scorers and the distribution change tests are not ported |
| Causal prediction | dowhy.causal_prediction | not ported -- upstream depends on PyTorch |
moon add riantr/moonbit_dowhyimport "riantr/moonbit_dowhy"
import "riantr/moonbit_dowhy/datasets"
/// Build a confounded data set: `W0 -> v0`, `W0 -> y`, `v0 -> y`.
let dataset = @datasets.linear_dataset(
42,
1000,
@datasets.LinearConfig::confounded(),
)
/// Build the model. The graph is derived from the semantic roles of the
/// variables when no explicit graph is given.
let model = @moonbit_dowhy.CausalModel::new(
dataset.frame(),
dataset.treatments(),
[dataset.outcome()],
None,
dataset.common_causes(),
dataset.instruments(),
dataset.effect_modifiers(),
dataset.frontdoor_variables(),
)
/// Identify the effect with the backdoor criterion.
let estimand = model.identify_effect("backdoor")
/// Estimate the average treatment effect.
let estimate = model.estimate_effect(
"backdoor.linear_regression",
1.0,
0.0,
"ate",
[],
)
println("ATE: " + estimate.estimate().to_string())
println("95% CI: [" + estimate.confidence_interval_lower().to_string() +
", " + estimate.confidence_interval_upper().to_string() + "]")| Package | Import path | Ports |
|---|---|---|
| api | riantr/moonbit_dowhy/api | the subset of pandas.DataFrame the estimators use, plus the one-hot encoder |
| utils | riantr/moonbit_dowhy/utils | matrices, least squares, generalised linear models, distributions, descriptive statistics, a deterministic generator |
| graph | riantr/moonbit_dowhy/graph | dowhy.graph, dowhy.causal_graph |
| identifiers | riantr/moonbit_dowhy/identifiers | dowhy.causal_identifier |
| model | riantr/moonbit_dowhy/model | dowhy.causal_model, dowhy.causal_estimator (the estimate objects) |
| estimators | riantr/moonbit_dowhy/estimators | dowhy.causal_estimators |
| refuters | riantr/moonbit_dowhy/refuters | dowhy.causal_refuters |
| datasets | riantr/moonbit_dowhy/datasets | dowhy.datasets and the deterministic test fixtures |
| data_transformers | riantr/moonbit_dowhy/data_transformers | dowhy.data_transformers (the PCA reducer) |
| do_samplers | riantr/moonbit_dowhy/do_samplers | dowhy.do_sampler, dowhy.do_samplers |
| timeseries | riantr/moonbit_dowhy/timeseries | dowhy.timeseries (the temporal graph and the temporal shift) |
| cm | riantr/moonbit_dowhy/cm | the PC causal discovery algorithm and the Fisher-z independence test |
| gcm | riantr/moonbit_dowhy/gcm | the portable core of dowhy.gcm: the statistics, the confidence intervals, the divergences, the density estimators, the Shapley values, the independence tests, the prediction models, the causal mechanisms, the fitting of a causal model and the what-if queries |
| tests | riantr/moonbit_dowhy/tests | the cross-package integration tests |
| examples | riantr/moonbit_dowhy/examples | the runnable quick start |
moon check --deny-warn # a warning is a failure
moon test --deny-warn
moon fmt --check
typospub struct CausalModel {
data : DataFrame
treatment : Array[String]
outcome : Array[String]
graph : DirectedGraph
observed_nodes : Array[String]
estimand : IdentifiedEstimand
}fn CausalModel::estimate_effect(self : CausalModel, method_name : String, treatment_value : Double, control_value : Double, target_units : String, effect_modifier_names : Array[String]) -> CausalEstimatefn CausalModel::new(data : DataFrame, treatment : Array[String], outcome : Array[String], graph : DirectedGraph?, common_causes : Array[String], instruments : Array[String], effect_modifiers : Array[String], mediators : Array[String]) -> CausalModelfn CausalModel::refute_estimate(self : CausalModel, method_name : String, estimate : CausalEstimate, refuter_name : String, num_simulations : Int, random : Random) -> CausalRefutationInstall
Download zipMoonBit port of DoWhy 0.14 (PyWhy). Causal graph construction and analysis, effect identification (backdoor / frontdoor / complete adjustment), estimation (linear regression, generalized linear model, propensity score weighting / matching / stratification, doubly robust, instrumental variable, two stage, regression discontinuity), refutation (bootstrap, placebo treatment, data subset, dummy outcome, unobserved common cause), intervention sampling (do samplers), temporal graphs, causal discovery (the PC algorithm with the Fisher-z test) and the portable core of the graphical causal models (the p-value merging, the bootstrap confidence intervals, the Kullback-Leibler divergences, the density estimators, the Shapley values, the independence tests, the prediction models, the causal mechanisms, the fitting of a causal model and the what-if queries (the interventional samples, the counterfactual samples and the average causal effect). Pure MoonBit with zero external runtime dependencies.