import argparse
import json
import sys
from pathlib import Path
import numpy as np
import pandas as pd
from statsmodels.stats.diagnostic import normal_ad
def convert_categorical_to_numeric(df, dataset_name):
non_numeric_cols = df.select_dtypes(exclude=[np.number]).columns.tolist()
if non_numeric_cols:
print(f"INFO: Dataset '{dataset_name}' contains non-numeric columns: "
f"{', '.join(non_numeric_cols)}")
print("Converting categorical variables to numeric representations...")
for col in non_numeric_cols:
df[col], uniques = pd.factorize(df[col])
print(f" {col}: {len(uniques)} unique values -> integer level encoding")
return df
def main():
parser = argparse.ArgumentParser(
description='Run Anderson-Darling test using statsmodels'
)
parser.add_argument(
'--csv',
type=str,
default='../../datasets/csv/mtcars.csv',
help='Path to CSV file'
)
parser.add_argument(
'--output',
type=str,
default='../../results/python',
help='Path to output directory (deprecated, use --output-dir)'
)
parser.add_argument(
'--output-dir',
type=str,
default='../../results/python',
help='Path to output directory'
)
args = parser.parse_args()
csv_path = Path(args.csv)
output_path = args.output_dir if hasattr(args, 'output_dir') and args.output_dir != '../../results/python' else args.output
output_dir = Path(output_path)
if not csv_path.exists():
print(f"Error: CSV file not found: {csv_path}")
sys.exit(1)
dataset_name = csv_path.stem
data = pd.read_csv(csv_path)
data = convert_categorical_to_numeric(data, dataset_name)
response_col = data.columns[0]
predictor_cols = data.columns[1:].tolist()
formula_str = f"{response_col} ~ {' + '.join(predictor_cols)}"
import statsmodels.api as sm
X = data[predictor_cols]
X = sm.add_constant(X) y = data[response_col]
model = sm.OLS(y, X).fit()
residuals = model.resid
ad_stat, p_value = normal_ad(residuals)
import numpy as np
if np.isinf(ad_stat) or np.isnan(ad_stat):
ad_stat = 999.999 if np.isinf(p_value) or np.isnan(p_value):
p_value = 0.0
print("Anderson-Darling Test (Python - statsmodels.stats.diagnostic.normal_ad)")
print("=" * 70)
print(f"Dataset: {dataset_name}")
print(f"Formula: {formula_str}")
print(f"A-statistic: {ad_stat:.22g}")
print(f"p-value: {p_value:.22g}")
print(f"Passed: {p_value > 0.05}")
print()
output = {
"test_name": "Anderson-Darling Test (Python - statsmodels.stats.diagnostic.normal_ad)",
"dataset": dataset_name,
"formula": formula_str,
"statistic": ad_stat,
"p_value": p_value,
"passed": bool(p_value > 0.05),
"description": "Tests for normality of residuals. The Anderson-Darling test is "
"particularly sensitive to deviations in the tails of the distribution. "
"Uses statsmodels.stats.diagnostic.normal_ad."
}
output_dir.mkdir(parents=True, exist_ok=True)
output_file = output_dir / f"{dataset_name}_anderson_darling.json"
with open(output_file, 'w') as f:
json.dump(output, f, indent=2, allow_nan=False)
print(f"Results saved to: {output_file}")
if __name__ == "__main__":
main()