| Crates.io | why_rs |
| lib.rs | why_rs |
| version | 0.2.1 |
| created_at | 2025-12-08 16:33:35.720989+00 |
| updated_at | 2025-12-16 15:03:44.607155+00 |
| description | A Causal Inference library for Rust. |
| homepage | |
| repository | https://github.com/jwhogg/why-rs |
| max_upload_size | |
| id | 1974021 |
| size | 142,933 |
To use the crate, you can include it in your cargo.toml:
[dependencies]
why_rs = "0.2.1"
Or, get the latest version directly from github:
[dependencies]
my_crate = { git = "https://github.com/jwhogg/why-rs.git" }
let dag: DAG = dag!( //macro for defining DAGs similar to DOT files
"A" => "C",
"B" => "C",
"C" => "D"
);
let mut fcm = FCM::from_dag(dag);
let df = generate_data(500); //simulate some practice data
//fit the LR mechanism given training data
let mut c_mechanism = LinearRegression::new();
c_mechanism.fit(df.clone(), Variable::from("C"), &fcm);
//there is also the option to manually define coefficients:
let noise_d = rand::thread_rng().gen_range(0..5);
let mut d_mechanism = LinearRegression::from(vec![0.8], 1, noise_d)
fcm = fcm //for this to actually run, you would need to define rules for A and B- see examples/linear_regression
.rule("C", c_mechanism)
.rule("D", d_mechanism);
let result = fcm.sample(15); //sample 15 rows
println!("normal sampling: {}", result);
let intervened_df = fcm.interventional_samples(
intervene!('B': 0), // The Macro in action
5 // n_samples
);
println!("Intervened Graph: {}", intervened_df);
Currently, there is support for the DAG and FCM (Functional Causal Model) data types. The following functionality is supported:
First, clone the repository:
git clone https://github.com/jwhogg/why-rs.git
cd why-rs
Sampling:
cargo run --example fcm_test
PC algorithm:
cargo run --example pc_test
Interventions:
cargo run --example intervention
Parsing .DOT files:
cargo run --example io_test