Crate linreg_core

Crate linreg_core 

Source
Expand description

Linear Regression Library with WASM bindings.

This library provides Ordinary Least Squares (OLS) regression and statistical diagnostic tests. When the wasm feature is enabled, it exposes JavaScript bindings for use in web browsers.

§Features

  • OLS regression with QR decomposition for numerical stability
  • Comprehensive diagnostic tests (Rainbow, Harvey-Collier, Breusch-Pagan, White, Jarque-Bera, Durbin-Watson, Shapiro-Wilk, Anderson-Darling)
  • Variance Inflation Factor (VIF) analysis
  • Custom implementations of all linear algebra and statistical functions

§Module Structure

  • core - OLS regression implementation
  • diagnostics - Statistical diagnostic tests
  • distributions - Statistical distributions (t, F, chi-squared, normal)
  • linalg - Matrix operations and QR decomposition
  • error - Error types

§WASM API

When the wasm feature is enabled, the library exposes JavaScript bindings that accept and return JSON strings for easy integration. By default, all domains are allowed. To restrict usage to specific domains, set the LINREG_DOMAIN_RESTRICT environment variable at build time:

LINREG_DOMAIN_RESTRICT=example.com,mysite.com wasm-pack build

§Example (Native Rust)

use linreg_core::core::ols_regression;
use linreg_core::Error;

let y = vec![2.5, 3.7, 4.2, 5.1, 6.3, 7.0];
let x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let x2 = vec![2.0, 4.0, 5.0, 4.0, 3.0, 2.0];
let names = vec![
    "Intercept".to_string(),
    "Temperature".to_string(),
    "Pressure".to_string(),
];

let result = ols_regression(&y, &[x1, x2], &names)?;
println!("R²: {}", result.r_squared);

Re-exports§

pub use core::RegressionOutput;
pub use core::VifResult;
pub use diagnostics::DiagnosticTestResult;
pub use diagnostics::RainbowTestOutput;
pub use diagnostics::RainbowSingleResult;
pub use diagnostics::RainbowMethod;
pub use diagnostics::WhiteTestOutput;
pub use diagnostics::WhiteSingleResult;
pub use diagnostics::WhiteMethod;
pub use diagnostics::CooksDistanceResult;
pub use diagnostics::rainbow_test as rainbow_test_core;
pub use diagnostics::white_test as white_test_core;
pub use error::Error;
pub use error::Result;
pub use error::error_json;
pub use error::error_to_json;

Modules§

core
Core OLS regression implementation.
diagnostics
distributions
Custom implementation of statistical distributions to replace statrs dependency. Includes Log Gamma, Incomplete Beta, Incomplete Gamma, Student’s T, F-distribution, Chi-Squared, and Normal distribution functions.
error
Error types for the linear regression library.
linalg
Minimal Linear Algebra module to replace nalgebra dependency.

Functions§

anderson_darling_test
Performs the Anderson-Darling test for normality via WASM.
breusch_pagan_test
Performs the Breusch-Pagan test for heteroscedasticity via WASM.
cooks_distance_test
Computes Cook’s distance for identifying influential observations via WASM.
durbin_watson_test
Performs the Durbin-Watson test for autocorrelation via WASM.
get_normal_inverse
Computes the inverse of the standard normal CDF (probit function).
get_t_cdf
Computes the Student’s t-distribution cumulative distribution function.
get_t_critical
Computes the critical t-value for a given significance level.
get_version
Returns the current version of the library.
harvey_collier_test
Performs the Harvey-Collier test for linearity via WASM.
jarque_bera_test
Performs the Jarque-Bera test for normality via WASM.
ols_regression
Performs OLS regression via WASM.
parse_csv
Parses CSV data and returns it as a JSON string.
python_white_test
Performs the Python method White test for heteroscedasticity via WASM.
r_white_test
Performs the R method White test for heteroscedasticity via WASM.
rainbow_test
Performs the Rainbow test for linearity via WASM.
shapiro_wilk_test
Performs the Shapiro-Wilk test for normality via WASM.
test
Simple test function to verify WASM is working.
test_ci
Test function for confidence interval computation.
test_housing_regression
Test function for regression validation against R reference values.
test_r_accuracy
Test function for R accuracy validation.
test_t_critical
Test function for t-critical value computation.
white_test
Performs the White test for heteroscedasticity via WASM.