import pytest
import random
import linreg_core
class TestHarveyCollierEdgeCases:
def test_harvey_collier_with_high_vif_data(self):
n = 50
random.seed(42)
x1 = [random.gauss(0, 1) for _ in range(n)]
x2 = [0.7 * x1[i] + random.gauss(0, 0.3) for i in range(n)]
y = [2.0 + 1.5 * x1[i] + 0.8 * x2[i] + random.gauss(0, 0.5) for i in range(n)]
try:
result = linreg_core.harvey_collier_test(y, [x1, x2])
assert hasattr(result, "statistic")
assert hasattr(result, "p_value")
assert hasattr(result, "test_name")
except Exception as e:
error_msg = str(e).lower()
assert any(term in error_msg for term in
["singular", "multicollinear", "computation", "failed"])
def test_harvey_collier_with_perfect_collinearity(self):
n = 20
x1 = [float(i) for i in range(1, n + 1)]
x2 = [2.0 * x for x in x1] y = [1.0 + 2.0 * x1[i] + 3.0 for i in range(n)]
with pytest.raises(Exception) as exc_info:
linreg_core.harvey_collier_test(y, [x1, x2])
error_msg = str(exc_info.value).lower()
assert any(term in error_msg for term in
["singular", "multicollinear", "redundant", "linear"])
def test_harvey_collier_minimal_data(self):
y = [1.0, 2.0, 3.5]
x = [1.0, 2.0, 3.0]
try:
result = linreg_core.harvey_collier_test(y, [x])
assert hasattr(result, "test_name") or True
except Exception:
pass
def test_harvey_collier_with_nonlinear_relationship(self):
n = 50
random.seed(42)
x = [float(i) / 10 for i in range(n)]
y = [1.0 + 2.0 * xi + 0.5 * xi ** 2 + random.gauss(0, 0.1) for xi in x]
result = linreg_core.harvey_collier_test(y, [x])
assert hasattr(result, "p_value")
assert result.p_value is not None
class TestWhiteTestCollinearity:
def test_white_test_with_collinear_predictors(self):
n = 50
random.seed(42)
x1 = [random.gauss(0, 1) for _ in range(n)]
x2 = [0.8 * x1[i] + random.gauss(0, 0.1) for i in range(n)]
y = [2.0 + 1.5 * x1[i] + 0.5 * x2[i] + random.gauss(0, 1) for i in range(n)]
try:
result_r = linreg_core.r_white_test(y, [x1, x2])
assert hasattr(result_r, "statistic")
assert hasattr(result_r, "p_value")
result_py = linreg_core.python_white_test(y, [x1, x2])
assert hasattr(result_py, "statistic")
assert hasattr(result_py, "p_value")
except Exception as e:
error_msg = str(e).lower()
assert len(error_msg) > 10
def test_white_test_perfect_collinearity(self):
n = 30
x1 = [float(i) for i in range(1, n + 1)]
x2 = [3.0 * x for x in x1] y = [2.0 + x1[i] + x2[i] for i in range(n)]
try:
result = linreg_core.white_test(y, [x1, x2], "r")
assert hasattr(result, "test_name")
except Exception as e:
error_msg = str(e).lower()
assert any(term in error_msg for term in
["singular", "multicollinear", "redundant"])
def test_white_test_comparison_methods_collinear(self):
n = 100
random.seed(123)
x1 = [random.gauss(10, 2) for _ in range(n)]
x2 = [0.6 * x1[i] + random.gauss(5, 1) for i in range(n)]
y = [5.0 + 2.0 * x1[i] + 1.5 * x2[i] + random.gauss(0, 2) for i in range(n)]
result_r = linreg_core.r_white_test(y, [x1, x2])
result_py = linreg_core.python_white_test(y, [x1, x2])
assert hasattr(result_r, "statistic")
assert hasattr(result_py, "statistic")
assert not (result_r.statistic != result_r.statistic) assert not (result_py.statistic != result_py.statistic)
class TestShapiroWilkBoundary:
def test_shapiro_wilk_at_5000_observations(self):
n = 5000
random.seed(42)
y = [random.gauss(0, 1) for _ in range(n)]
x = [random.gauss(0, 1) for _ in range(n)]
result = linreg_core.shapiro_wilk_test(y, [x])
assert hasattr(result, "statistic")
assert hasattr(result, "p_value")
assert result.test_name == "Shapiro-Wilk"
def test_shapiro_wilk_above_5000_observations(self):
n = 5100
random.seed(42)
y = [random.gauss(0, 1) for _ in range(n)]
x = [random.gauss(0, 1) for _ in range(n)]
try:
result = linreg_core.shapiro_wilk_test(y, [x])
assert hasattr(result, "statistic")
except Exception as e:
error_msg = str(e)
assert len(error_msg) > 0
def test_shapiro_wilk_large_normal_sample(self):
n = 1000
random.seed(42)
y = [random.gauss(0, 1) for _ in range(n)]
x = [random.gauss(0, 1) for _ in range(n)]
result = linreg_core.shapiro_wilk_test(y, [x])
assert hasattr(result, "p_value")
assert 0 < result.statistic <= 1.0
def test_shapiro_wilk_large_non_normal_sample(self):
n = 1000
random.seed(42)
y = [random.expovariate(1.0) for _ in range(n)]
x = [random.random() for _ in range(n)]
result = linreg_core.shapiro_wilk_test(y, [x])
assert hasattr(result, "p_value")
class TestRainbowTestBoundaryConditions:
def test_rainbow_test_with_very_small_fraction(self):
y = [float(i) for i in range(1, 21)]
x = [float(i) for i in range(1, 21)]
result = linreg_core.rainbow_test(y, [x], 0.01, "r")
assert hasattr(result, "test_name")
assert hasattr(result, "has_r_result")
def test_rainbow_test_with_very_large_fraction(self):
y = [float(i) for i in range(1, 21)]
x = [float(i) for i in range(1, 21)]
result = linreg_core.rainbow_test(y, [x], 0.99, "r")
assert hasattr(result, "test_name")
assert hasattr(result, "has_r_result")
def test_rainbow_test_both_methods_comparison(self):
y = [1.0, 2.1, 2.9, 4.2, 5.1, 5.8, 7.1, 8.2, 8.9, 10.1]
x = [float(i) for i in range(1, 11)]
result = linreg_core.rainbow_test(y, [x], 0.5, "both")
assert result.has_r_result or result.has_python_result
def test_rainbow_test_with_nonlinear_data(self):
n = 50
random.seed(42)
x = [float(i) / 10 for i in range(n)]
y = [1.0 + xi + 0.3 * xi ** 2 + random.gauss(0, 0.1) for xi in x]
result = linreg_core.rainbow_test(y, [x], 0.5, "r")
assert hasattr(result, "p_value") or result.has_r_result
class TestRESETTestBoundaryConditions:
def test_reset_test_with_single_power(self):
y = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
x = [[float(i) for i in range(1, 9)]]
result = linreg_core.reset_test(y, x, [2], "fitted")
assert hasattr(result, "statistic")
assert hasattr(result, "p_value")
def test_reset_test_with_high_powers(self):
y = [float(i) ** 2 for i in range(1, 21)]
x = [[float(i) for i in range(1, 21)]]
result = linreg_core.reset_test(y, x, [2, 3, 4], "fitted")
assert hasattr(result, "statistic")
def test_reset_test_regressor_type(self):
y = [1.0 + 2.0 * i + 0.1 * i ** 2 for i in range(1, 11)]
x1 = [float(i) for i in range(1, 11)]
result = linreg_core.reset_test(y, [x1], [2], "regressor")
assert hasattr(result, "statistic")
def test_reset_test_principal_component_type(self):
y = [float(i) for i in range(1, 11)]
x1 = [float(i) for i in range(1, 11)]
x2 = [float(i) * 2 for i in range(1, 11)]
result = linreg_core.reset_test(y, [x1, x2], [2], "princomp")
assert hasattr(result, "statistic")
class TestBreuschGodfreyBoundaryConditions:
def test_breusch_godfrey_order_equals_n(self):
y = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]]
result = linreg_core.breusch_godfrey_test(y, x, 4, "chisq")
assert hasattr(result, "order")
assert result.order == 4
def test_breusch_godfrey_with_autocorrelated_data(self):
n = 50
random.seed(42)
y = []
for i in range(n):
if i == 0:
y.append(random.gauss(0, 1))
else:
y.append(0.7 * y[i-1] + random.gauss(0, 1))
x = [float(i) / 10 for i in range(n)]
result = linreg_core.breusch_godfrey_test(y, [x], 2, "chisq")
assert hasattr(result, "p_value")
def test_breusch_godfrey_f_vs_chisq_comparison(self):
y = [1.0, 2.1, 2.8, 4.2, 4.9, 6.1, 6.8, 8.2, 9.1, 9.9]
x = [[float(i) for i in range(1, 11)]]
result_chisq = linreg_core.breusch_godfrey_test(y, x, 2, "chisq")
result_f = linreg_core.breusch_godfrey_test(y, x, 2, "f")
assert hasattr(result_chisq, "statistic")
assert hasattr(result_f, "statistic")
assert result_chisq.test_type == "chisq" or "chi" in result_chisq.test_type.lower()
assert result_f.test_type == "f" or result_f.test_type.lower() == "f"
class TestDurbinWatsonBoundaryConditions:
def test_durbin_watson_perfect_negative_autocorrelation(self):
y = [10.0, 1.0, 10.0, 1.0, 10.0, 1.0, 10.0, 1.0]
x = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
result = linreg_core.durbin_watson_test(y, [x])
assert hasattr(result, "statistic")
assert 0 <= result.statistic <= 4 or result.statistic > 0
def test_durbin_watson_no_autocorrelation(self):
n = 100
random.seed(42)
y = [random.gauss(0, 1) for _ in range(n)]
x = [random.gauss(0, 1) for _ in range(n)]
result = linreg_core.durbin_watson_test(y, [x])
assert hasattr(result, "statistic")
def test_durbin_watson_minimal_data(self):
y = [1.0, 2.1, 2.9, 4.1]
x = [1.0, 2.0, 3.0, 4.0]
result = linreg_core.durbin_watson_test(y, [x])
assert hasattr(result, "statistic")
class TestAndersonDarlingBoundaryConditions:
def test_anderson_darling_normal_data(self):
n = 100
random.seed(42)
y = [random.gauss(0, 1) for _ in range(n)]
x = [random.gauss(0, 1) for _ in range(n)]
result = linreg_core.anderson_darling_test(y, [x])
assert hasattr(result, "p_value")
def test_anderson_darling_exponential_data(self):
n = 100
random.seed(42)
y = [random.expovariate(1.0) for _ in range(n)]
x = [random.random() for _ in range(n)]
result = linreg_core.anderson_darling_test(y, [x])
assert hasattr(result, "p_value")
def test_anderson_darling_uniform_data(self):
n = 100
random.seed(42)
y = [random.random() for _ in range(n)]
x = [random.random() for _ in range(n)]
result = linreg_core.anderson_darling_test(y, [x])
assert hasattr(result, "statistic")
class TestJarqueBeraBoundaryConditions:
def test_jarque_bera_symmetric_data(self):
n = 200
random.seed(42)
y = [random.gauss(0, 1) for _ in range(n)]
x = [random.gauss(0, 1) for _ in range(n)]
result = linreg_core.jarque_bera_test(y, [x])
assert hasattr(result, "p_value")
def test_jarque_bera_skewed_data(self):
n = 200
random.seed(42)
y = [random.expovariate(1.0) for _ in range(n)]
x = [random.random() for _ in range(n)]
result = linreg_core.jarque_bera_test(y, [x])
assert hasattr(result, "statistic")
def test_jarque_bera_heavy_tailed_data(self):
n = 200
random.seed(42)
y = [random.gauss(0, 1) / (random.random() ** 0.5) for _ in range(n)]
x = [random.gauss(0, 1) for _ in range(n)]
result = linreg_core.jarque_bera_test(y, [x])
assert hasattr(result, "statistic")
class TestBreuschPaganBoundaryConditions:
def test_breusch_pagan_homoscedastic_data(self):
n = 100
random.seed(42)
x = [random.gauss(0, 1) for _ in range(n)]
y = [2.0 + 1.5 * xi + random.gauss(0, 1) for xi in x]
result = linreg_core.breusch_pagan_test(y, [x])
assert hasattr(result, "p_value")
def test_breusch_pagan_heteroscedastic_data(self):
n = 100
random.seed(42)
x = [float(i) / 10 for i in range(n)]
y = [2.0 + 1.5 * xi + random.gauss(0, 0.1 * xi) for xi in x]
result = linreg_core.breusch_pagan_test(y, [x])
assert hasattr(result, "statistic")
def test_breusch_pagan_minimal_heteroscedasticity(self):
n = 200
random.seed(42)
x = [float(i) / 50 for i in range(n)]
y = [2.0 + 1.5 * xi + random.gauss(0, 1 + 0.01 * xi) for xi in x]
result = linreg_core.breusch_pagan_test(y, [x])
assert hasattr(result, "p_value")
class TestCooksDistanceBoundaryConditions:
def test_cooks_distance_all_influential(self):
y = [1.0, 2.0, 3.0, 4.0, 5.0]
x = [-2.0, -1.0, 0.0, 1.0, 2.0]
result = linreg_core.cooks_distance_test(y, [x])
assert len(result.distances) == 5
assert all(d < 1.0 for d in result.distances)
def test_cooks_distance_single_extreme_outlier(self):
y = [1.0, 2.0, 3.0, 4.0, 1000.0]
x = [1.0, 2.0, 3.0, 4.0, 5.0]
result = linreg_core.cooks_distance_test(y, [x])
assert len(result.influential_1) > 0 or result.influential_1 == [4]
def test_cooks_distance_multiple_outliers(self):
y = [1.0, 2.0, 50.0, 4.0, 100.0]
x = [1.0, 2.0, 3.0, 4.0, 5.0]
result = linreg_core.cooks_distance_test(y, [x])
assert len(result.distances) == 5
assert len(result.influential_4_over_n) >= 1
class TestDiagnosticCombinations:
def test_full_diagnostic_suite(self):
n = 100
random.seed(42)
x = [float(i) / 10 for i in range(n)]
y = [2.0 + 1.5 * xi + 0.1 * xi ** 2 + random.gauss(0, 0.5 + 0.05 * xi) for xi in x]
bp = linreg_core.breusch_pagan_test(y, [x])
dw = linreg_core.durbin_watson_test(y, [x])
jb = linreg_core.jarque_bera_test(y, [x])
sw = linreg_core.shapiro_wilk_test(y, [x])
ad = linreg_core.anderson_darling_test(y, [x])
rainbow = linreg_core.rainbow_test(y, [x], 0.5, "r")
hc = linreg_core.harvey_collier_test(y, [x])
reset = linreg_core.reset_test(y, [x], [2, 3], "fitted")
bg = linreg_core.breusch_godfrey_test(y, [x], 2, "chisq")
cooks = linreg_core.cooks_distance_test(y, [x])
assert hasattr(bp, "p_value")
assert hasattr(dw, "statistic")
assert hasattr(jb, "p_value")
assert hasattr(sw, "p_value")
assert hasattr(ad, "p_value")
assert hasattr(rainbow, "test_name")
assert hasattr(hc, "statistic")
assert hasattr(reset, "p_value")
assert hasattr(bg, "p_value")
assert hasattr(cooks, "distances")
def test_diagnostics_with_regularized_results(self):
n = 50
random.seed(42)
x1 = [random.gauss(0, 1) for _ in range(n)]
x2 = [random.gauss(0, 1) for _ in range(n)]
y = [2.0 + 1.5 * x1[i] + 0.8 * x2[i] + random.gauss(0, 0.5) for i in range(n)]
ridge = linreg_core.ridge_regression(y, [x1, x2], 1.0, True)
bp = linreg_core.breusch_pagan_test(y, [x1, x2])
assert hasattr(ridge, "r_squared")
assert hasattr(bp, "p_value")
def test_diagnostics_comparison_linear_vs_nonlinear(self):
n = 50
random.seed(42)
x = [float(i) / 10 for i in range(n)]
y_linear = [2.0 + 1.5 * xi + random.gauss(0, 0.3) for xi in x]
y_quad = [2.0 + 1.5 * xi + 0.3 * xi ** 2 + random.gauss(0, 0.3) for xi in x]
hc_linear = linreg_core.harvey_collier_test(y_linear, [x])
hc_quad = linreg_core.harvey_collier_test(y_quad, [x])
assert hasattr(hc_linear, "p_value")
assert hasattr(hc_quad, "p_value")