linreg-core 0.8.1

Lightweight regression library (OLS, Ridge, Lasso, Elastic Net, WLS, LOESS, Polynomial) with 14 diagnostic tests, cross validation, and prediction intervals. Pure Rust - no external math dependencies. WASM, Python, FFI, and Excel XLL bindings.
Documentation
# ============================================================================
# White Test Reference Implementation (Python)
# ============================================================================
# This script generates reference values for the White test using
# statsmodels.stats.diagnostic.het_white. The White test is a more general
# test for heteroscedasticity that uses squares and cross-products of
# predictors.
#
# Source: statsmodels package, het_white function
# Reference: White (1980), "A Heteroskedasticity-Consistent Covariance
#            Matrix Estimator and a Direct Test for Heteroskedasticity"
#
# Usage:
#   python test_white.py [--csv CSV_PATH] [--output-dir OUTPUT_DIR]
#
#   Args:
#     --csv       Path to CSV file (first col = response, rest = predictors)
#                 Default: ../../datasets/csv/mtcars.csv
#     --output-dir Path to output directory
#                 Default: ../../results/python
# ============================================================================

import argparse
import os
import json
import pandas as pd
import numpy as np
import statsmodels.api as sm
from statsmodels.stats.diagnostic import het_white


def convert_categorical_to_numeric(data, dataset_name):
    """Convert categorical variables to numeric representations."""
    non_numeric_cols = data.select_dtypes(exclude=[np.number]).columns.tolist()

    if not non_numeric_cols:
        return []

    print(f"INFO: Dataset '{dataset_name}' contains non-numeric columns: {non_numeric_cols}")
    print(f"Converting categorical variables to numeric representations...")

    # Process each non-numeric column
    for col in non_numeric_cols:
        if data[col].dtype == 'object':
            # For string/categorical data, use factorize (integer encoding)
            # This preserves the categorical nature without trying to parse strings as numbers
            encoded, categories = pd.factorize(data[col])
            data[col] = encoded
            print(f"  {col}: {len(categories)} unique categories -> integer encoded as 0, 1, 2, ...")
        else:
            # Already numeric or other type
            pass

    return non_numeric_cols


def main():
    parser = argparse.ArgumentParser(
        description="White Test - Check heteroscedasticity using statsmodels"
    )
    parser.add_argument(
        "--csv",
        default="../../datasets/csv/mtcars.csv",
        help="Path to CSV file (first column = response, rest = predictors)"
    )
    parser.add_argument(
        "--output-dir",
        default="../../results/python",
        help="Path to output directory"
    )

    args = parser.parse_args()

    # Validate CSV path
    if not os.path.exists(args.csv):
        raise FileNotFoundError(f"CSV file not found: {args.csv}")

    # Extract dataset name from filename
    dataset_name = os.path.splitext(os.path.basename(args.csv))[0]

    # Read CSV data
    data = pd.read_csv(args.csv)

    # If there are non-numeric columns, convert them to numeric
    non_numeric_cols = data.select_dtypes(exclude=[np.number]).columns.tolist()
    if non_numeric_cols:
        convert_categorical_to_numeric(data, dataset_name)

    # After conversion, re-check if we still have non-numeric columns
    remaining_non_numeric = data.select_dtypes(exclude=[np.number]).columns.tolist()
    if remaining_non_numeric:
        # These could be things like empty strings or other non-numeric types that couldn't be converted
        raise ValueError(f"Could not convert the following non-numeric columns to numeric: {remaining_non_numeric}")

    # Assume first column is response variable, rest are predictors
    response_col = data.columns[0]
    predictor_cols = data.columns[1:]

    # Prepare data for statsmodels (add constant for intercept)
    X = data[predictor_cols]
    X = sm.add_constant(X)
    y = data[response_col]

    # Build formula string for output
    formula_str = f"{response_col} ~ {' + '.join(predictor_cols)}"

    # Fit the model
    model = sm.OLS(y, X).fit()

    # Run White test
    # het_white returns (LM_statistic, p_value, f_statistic, f_p_value)
    white_result = het_white(model.resid, model.model.exog)

    # Print results
    print("White Test (Python - statsmodels)")
    print("=" * 35)
    print(f"Dataset: {dataset_name}")
    print(f"Formula: {formula_str}")
    print(f"LM-statistic: {white_result[0]}")
    print(f"p-value: {white_result[1]}")
    print(f"Passed: {white_result[1] > 0.05}")
    print()

    # Prepare output - use None for NaN values (converts to null in JSON)
    def safe_float(value):
        """Convert to float, returning None for NaN"""
        fval = float(value)
        return None if np.isnan(fval) else fval

    output = {
        "test_name": "White Test (Python - statsmodels)",
        "dataset": dataset_name,
        "formula": formula_str,
        "statistic": float(white_result[0]),
        "p_value": float(white_result[1]),
        "passed": bool(white_result[1] > 0.05),
        "f_statistic": safe_float(white_result[2]),
        "f_p_value": safe_float(white_result[3]),
        "description": "Tests for heteroscedasticity using squares and cross-products of predictors. Uses statsmodels.stats.diagnostic.het_white."
    }

    # Create output directory if it doesn't exist
    os.makedirs(args.output_dir, exist_ok=True)

    # Save to JSON with naming convention: {dataset}_white.json
    output_file = os.path.join(args.output_dir, f"{dataset_name}_white.json")

    with open(output_file, 'w') as f:
        json.dump(output, f, indent=2)

    print(f"Results saved to: {output_file}")


if __name__ == "__main__":
    main()