import pytest
import linreg_core
class TestDiagnosticTestsNative:
def test_rainbow_test_native(self, diagnostic_y, diagnostic_x):
result = linreg_core.rainbow_test(diagnostic_y, diagnostic_x, 0.5, "r")
assert hasattr(result, "test_name")
assert hasattr(result, "has_r_result")
assert hasattr(result, "interpretation")
def test_white_test_native(self, sample_y, sample_x):
result = linreg_core.white_test(sample_y, sample_x, "r")
assert hasattr(result, "test_name")
assert hasattr(result, "has_r_result")
assert hasattr(result, "interpretation")
def test_breusch_pagan_test_native(self, sample_y, sample_x):
result = linreg_core.breusch_pagan_test(sample_y, sample_x)
assert hasattr(result, "statistic")
assert hasattr(result, "p_value")
def test_cooks_distance_test_native(self, sample_y, sample_x):
result = linreg_core.cooks_distance_test(sample_y, sample_x)
assert hasattr(result, "distances")
assert hasattr(result, "p")
assert hasattr(result, "threshold_4_over_n")
assert hasattr(result, "influential_4_over_n")
def test_breusch_godfrey_test_native(self, sample_y, sample_x):
result = linreg_core.breusch_godfrey_test(sample_y, sample_x, 1, "chisq")
assert hasattr(result, "test_name")
assert hasattr(result, "order")
assert hasattr(result, "interpretation")
assert hasattr(result, "guidance")
def test_vif_test_native(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3, 7.0, 8.1, 9.2]
x1 = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
x2 = [2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0]
x3 = [1.5, 2.3, 3.1, 4.9, 5.2, 6.8, 7.1, 8.5]
result = linreg_core.vif_test(y, [x1, x2, x3])
assert hasattr(result, "max_vif")
assert hasattr(result, "vif_results")
assert hasattr(result, "interpretation")
assert hasattr(result, "guidance")
assert len(result.vif_results) == 3
for detail in result.vif_results:
assert hasattr(detail, "variable")
assert hasattr(detail, "vif")
assert hasattr(detail, "rsquared")
assert hasattr(detail, "interpretation")
def test_vif_test_insufficient_predictors(self):
y = [1.0, 2.0, 3.0, 4.0]
x = [[1.0, 2.0, 3.0, 4.0]]
with pytest.raises(Exception): linreg_core.vif_test(y, x)
def test_vif_test_high_correlation(self):
y = [1.0, 2.0, 3.0, 4.0, 5.0]
x1 = [1.0, 2.0, 3.0, 4.0, 5.0]
x2 = [2.0, 4.0, 6.0, 8.0, 10.0] x3 = [1.0, 2.0, 3.0, 4.0, 5.0]
result = linreg_core.vif_test(y, [x1, x2, x3])
assert result.max_vif > 10
class TestDiagnosticsEdgeCases:
def test_rainbow_test_fraction_boundary(self):
y = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]]
result_0 = linreg_core.rainbow_test(y, x, 0.0, "r")
assert hasattr(result_0, "test_name")
result_1 = linreg_core.rainbow_test(y, x, 1.0, "r")
assert hasattr(result_1, "test_name")
def test_breusch_pagan_insufficient_data(self):
y = [1.0, 2.0, 3.0] x = [[1.0, 2.0, 3.0]]
result = linreg_core.breusch_pagan_test(y, x)
assert hasattr(result, "statistic")
def test_diagnostics_with_constant_y(self):
y = [5.0, 5.0, 5.0, 5.0, 5.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
try:
result = linreg_core.breusch_pagan_test(y, x)
assert hasattr(result, "statistic")
except Exception:
pass
def test_diagnostics_with_perfect_fit(self):
y = [1.0, 2.0, 3.0, 4.0, 5.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.breusch_pagan_test(y, x)
assert hasattr(result, "statistic")
def test_cooks_distance_influential_points(self):
y = [1.0, 2.0, 3.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 hasattr(result, "influential_4_over_n")
def test_cooks_distance_small_sample(self):
y = [1.0, 2.0, 3.0] x = [[1.0, 2.0, 3.0]]
result = linreg_core.cooks_distance_test(y, x)
assert hasattr(result, "distances")
assert len(result.distances) == 3
def test_durbin_watson_bounds(self):
y = [1.0, 2.5, 3.7, 4.8, 6.2] x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.durbin_watson_test(y, x)
assert isinstance(result.statistic, float)
def test_durbin_watson_perfect_positive_autocorrelation(self):
y = [1.0, 2.5, 3.7, 4.8, 6.2] x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.durbin_watson_test(y, x)
assert isinstance(result.statistic, float)
def test_white_test_methods(self):
y = [1.0, 2.0, 3.0, 4.0, 5.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result_r = linreg_core.r_white_test(y, x)
assert hasattr(result_r, "statistic")
result_py = linreg_core.python_white_test(y, x)
assert hasattr(result_py, "statistic")
def test_jarque_bera_normal_distribution(self):
import random
random.seed(42)
y = [sum([random.random() for _ in range(12)]) - 6 for _ in range(100)]
x = [[random.random() for _ in range(100)]]
result = linreg_core.jarque_bera_test(y, x)
assert hasattr(result, "p_value")
def test_reset_test_powers(self):
y = [1.0, 2.0, 3.0, 4.0, 5.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result_single = linreg_core.reset_test(y, x, [2], "fitted")
assert hasattr(result_single, "statistic")
result_multi = linreg_core.reset_test(y, x, [2, 3], "fitted")
assert hasattr(result_multi, "statistic")
def test_breusch_godfrey_higher_order(self):
y = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]]
result_order2 = linreg_core.breusch_godfrey_test(y, x, 2, "chisq")
assert result_order2.order == 2
result_order3 = linreg_core.breusch_godfrey_test(y, x, 3, "chisq")
assert result_order3.order == 3
def test_breusch_godfrey_f_test_type(self):
y = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]]
result_f = linreg_core.breusch_godfrey_test(y, x, 1, "f")
assert hasattr(result_f, "statistic")