Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,607 changes: 46 additions & 1,561 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions crates/ci-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@ ndarray = { workspace = true }
statrs = { workspace = true }
ordered-float = "5.3.0"
rand = "0.9"
nalgebra = "0.33"

[target.'cfg(target_os = "linux")'.dependencies]
ndarray-linalg = { version = "0.18.1", features = ["openblas-system"] }

[target.'cfg(target_os = "macos")'.dependencies]
ndarray-linalg = { version = "0.18.1", features = ["openblas-system"] }

[target.'cfg(windows)'.dependencies]
ndarray-linalg = { version = "0.18.1", features = ["intel-mkl-static"] }

[dev-dependencies]
criterion = { workspace = true }
Expand Down
16 changes: 8 additions & 8 deletions crates/ci-core/src/ci_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod chi_squared;
mod cressie_read;
mod freeman_tukey;
mod log_likelihood;
mod modified_likelihood;
mod pearson_correlation;
mod pearson_equivalence;
pub mod chi_squared;
pub mod cressie_read;
pub mod freeman_tukey;
pub mod log_likelihood;
pub mod modified_likelihood;
pub mod pearson_correlation;
pub mod pearson_equivalence;

use chi_squared::ChiSquared;
use cressie_read::CressieRead;
Expand All @@ -30,7 +30,7 @@ pub fn register_all_tests(registry: &mut Registry) {
.expect("Failed to register Modified Likelihood Test!");

registry
.add_to_registry("pearson_correlation", PearsonCorrelation::new(true, 0.05))
.add_to_registry("pearson_correlation", PearsonCorrelation::new(false, 0.05))
.expect("Failed to register Pearson Correlation test!");

registry
Expand Down
66 changes: 34 additions & 32 deletions crates/ci-core/src/ci_tests/pearson_correlation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::strategy::{CITest, CITestDataType, TestResult};
use anyhow::{ensure, Context};
use ndarray::{Array1, Array2, ArrayView1};
use ndarray_linalg::LeastSquaresSvd;
use nalgebra::{DMatrix, DVector};
use statrs::distribution::{ContinuousCDF, StudentsT};
use statrs::statistics::Statistics;

Expand Down Expand Up @@ -45,38 +45,40 @@ impl CITest for PearsonCorrelation {
///
/// - If `boolean=true`: `TestResult::Boolean(p_value >= SIGNIFICANCE_LEVEL)`
/// - If `boolean=false`: `TestResult::PValue(p_value, coefficient)`
fn run_test(
&self,
x_values: Array1<f64>,
y_values: Array1<f64>,
z: Array2<f64>,
) -> anyhow::Result<TestResult> {
if z.is_empty() {
let (coefficient, p_value) = pearsonr(&x_values.view(), &y_values.view())?;
Ok(wrap_result(
self.boolean,
p_value,
coefficient,
self.significance_level,
))
} else {
// Use linear regression to compute residuals and test independence on it.
let x_coefficient = z.view().least_squares(&x_values.view())?.solution;

let y_coefficient = z.view().least_squares(&y_values.view())?.solution;

let residual_x = x_values - z.dot(&x_coefficient);
let residual_y = y_values - z.dot(&y_coefficient);

let (coefficient, p_value) = pearsonr(&residual_x.view(), &residual_y.view())?;
Ok(wrap_result(
self.boolean,
p_value,
coefficient,
self.significance_level,
))
}
fn run_test(
&self,
x_values: Array1<f64>,
y_values: Array1<f64>,
z: Array2<f64>,
) -> anyhow::Result<TestResult> {
if z.is_empty() {
let (coefficient, p_value) = pearsonr(&x_values.view(), &y_values.view())?;
Ok(wrap_result(self.boolean, p_value, coefficient, self.significance_level))
} else {
// Convert ndarrays into DMatrix and DVector
let z_na = DMatrix::from_row_iterator(z.nrows(), z.ncols(), z.iter().cloned());
let x_na = DVector::from_iterator(x_values.len(), x_values.iter().cloned());
let y_na = DVector::from_iterator(y_values.len(), y_values.iter().cloned());

// Least square computation
let svd = z_na.svd(true, true);
let x_coefficient = svd.solve(&x_na, 1e-10)
.map_err(|e| anyhow::anyhow!("least squares failed for x: {e}"))?;
let y_coefficient = svd.solve(&y_na, 1e-10)
.map_err(|e| anyhow::anyhow!("least squares failed for y: {e}"))?;

// Convert nalgebra resuult into ndarray
let x_coef_nd = Array1::from_vec(x_coefficient.iter().cloned().collect());
let y_coef_nd = Array1::from_vec(y_coefficient.iter().cloned().collect());

// Compute residuals
let residual_x = x_values - z.dot(&x_coef_nd);
let residual_y = y_values - z.dot(&y_coef_nd);

let (coefficient, p_value) = pearsonr(&residual_x.view(), &residual_y.view())?;
Ok(wrap_result(self.boolean, p_value, coefficient, self.significance_level))
}
}

fn data_types(&self) -> &'static [CITestDataType] {
&[CITestDataType::Continuous]
Expand Down
2 changes: 1 addition & 1 deletion crates/ci-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod ci_tests;
pub mod ci_tests;
pub mod registry;
pub mod strategy;
pub mod utils;
12 changes: 9 additions & 3 deletions crates/ci-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ license = ""
description = "wasm-pack bindings for Conditional Independence Testing"
repository = ""

[lib]
crate-type = ["cdylib"]

[dependencies]
ci_core = { path = "../ci-core" }
ci_core = { path = "../ci_core"}
wasm-bindgen = "0.2"
js-sys = "0.3"
ndarray = {workspace = true}

[lints]
workspace = true

[lib]
crate-type = ["cdylib"]
[profile.release]
opt-level = "s"
56 changes: 56 additions & 0 deletions crates/ci-js/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
use ci_core::strategy::TestResult;
use ndarray::{Array1, Array2};
use js_sys::{Float64Array, Array};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct JSCITest {}

#[wasm_bindgen]
impl JSCITest {
#[wasm_bindgen(js_name = "run_test")]
pub fn run_test(
&self,
z_flat: &Float64Array,
z_rows: usize,
z_cols: usize,
x: &Float64Array,
y: &Float64Array,
boolean: bool,
significance_level: f64,
) -> Result<JsValue, JsValue> {
//Converting Float64Array -> Vec<f64> -> ndarray
let z_vec: Vec<f64> = z_flat.to_vec();
let x_vec: Vec<f64> = x.to_vec();
let y_vec: Vec<f64> = y.to_vec();

let z: Array2<f64> = Array2::from_shape_vec((z_rows, z_cols), z_vec)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
let x: Array1<f64> = Array1::from_vec(x_vec);
let y: Array1<f64> = Array1::from_vec(y_vec);

let test = PearsonCorrelation::new(&boolean, &significance_level);

let result = test
.run_test(x, y, z)
.map_err(|e| JsError::new(&e.to_string()))?;

match result {
TestResult::Boolean(b) => Ok(JsValue::from_bool(b)),
TestResult::PValue(p_value, coefficient) => {
let array = Array::new();
array.push(&JsValue::from_f64(p_value));
array.push(&JsValue::from_f64(coefficient));
Ok(array.into())
}

TestResult::Statistic(p_value, statistic, dof) => {
let array = Array::new();
array.push(&JsValue::from_f64(p_value));
array.push(&JsValue::from_f64(statistic));
array.push(&JsValue::from_f64(dof as f64));
Ok(array.into())}

}

}
}
14 changes: 14 additions & 0 deletions crates/ci-js/src/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const response = await fetch("http://localhost:3000/tests/pearson_correlation", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
x: [1.0, 2.0, 3.0, 4.0, 5.0],
y: [1.0, 2.0, 3.0, 4.0, 5.0],
z: [],
boolean: false,
significance_level: 0.05
})
});

const result = await response.json();
console.log(result);