import pytest
import linreg_core
class TestWLSNative:
def test_wls_regression_equal_weights_matches_ols(self):
y = [1.0, 2.0, 3.0, 4.0, 5.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
weights = [1.0, 1.0, 1.0, 1.0, 1.0]
result = linreg_core.wls_regression(y, x, weights)
assert abs(result.coefficients[0]) < 1e-10, f"Intercept should be ~0, got {result.coefficients[0]}"
assert abs(result.coefficients[1] - 1.0) < 1e-10, f"Slope should be ~1, got {result.coefficients[1]}"
assert result.n_observations == 5
assert result.n_predictors == 1
def test_wls_result_object_attributes(self):
y = [2.0, 4.0, 6.0, 8.0, 10.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
weights = [1.0, 1.0, 1.0, 1.0, 1.0]
result = linreg_core.wls_regression(y, x, weights)
assert isinstance(result.r_squared, float)
assert isinstance(result.r_squared_adjusted, float)
assert isinstance(result.f_statistic, float)
assert isinstance(result.f_p_value, float)
assert isinstance(result.residual_std_error, float)
assert isinstance(result.mse, float)
assert isinstance(result.rmse, float)
assert isinstance(result.mae, float)
assert isinstance(result.coefficients, list)
assert isinstance(result.standard_errors, list)
assert isinstance(result.t_statistics, list)
assert isinstance(result.p_values, list)
assert isinstance(result.fitted_values, list)
assert isinstance(result.residuals, list)
assert isinstance(result.n_observations, int)
assert isinstance(result.n_predictors, int)
assert isinstance(result.df_residuals, int)
assert isinstance(result.df_model, int)
def test_wls_with_weighted_outlier(self):
y = [2.0, 4.0, 6.0, 8.0, 100.0] x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
weights_low = [1.0, 1.0, 1.0, 1.0, 0.01]
result_low = linreg_core.wls_regression(y, x, weights_low)
weights_high = [1.0, 1.0, 1.0, 1.0, 10.0]
result_high = linreg_core.wls_regression(y, x, weights_high)
assert result_low.coefficients[1] < result_high.coefficients[1]
def test_wls_negative_weight_error(self):
y = [1.0, 2.0, 3.0]
x = [[1.0, 2.0, 3.0]]
weights = [1.0, -1.0, 1.0]
with pytest.raises(Exception) as exc:
linreg_core.wls_regression(y, x, weights)
assert "negative" in str(exc.value).lower() or "invalid" in str(exc.value).lower()
def test_wls_mismatched_dimensions_error(self):
y = [1.0, 2.0, 3.0]
x = [[1.0, 2.0]] weights = [1.0, 1.0, 1.0]
with pytest.raises(Exception):
linreg_core.wls_regression(y, x, weights)
def test_wls_multiple_predictors(self):
y = [2.0, 4.0, 6.0, 8.0, 10.0]
x1 = [1.0, 2.0, 3.0, 4.0, 5.0]
x2 = [0.5, 1.0, 1.5, 2.0, 2.5]
weights = [1.0, 1.0, 1.0, 1.0, 1.0]
result = linreg_core.wls_regression(y, [x1, x2], weights)
assert result.n_predictors == 2
assert len(result.coefficients) == 3 assert len(result.fitted_values) == 5
assert len(result.standard_errors) == 3
assert len(result.t_statistics) == 3
assert len(result.p_values) == 3
def test_wls_insufficient_data_error(self):
y = [1.0, 2.0]
x1 = [1.0, 2.0]
x2 = [0.5, 1.0]
weights = [1.0, 1.0]
with pytest.raises(Exception):
linreg_core.wls_regression(y, [x1, x2], weights)
def test_wls_statistics_completeness(self):
y = [2.0, 4.0, 6.0, 8.0, 10.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
weights = [1.0, 1.0, 1.0, 1.0, 1.0]
result = linreg_core.wls_regression(y, x, weights)
assert len(result.coefficients) == 2
assert len(result.standard_errors) == 2
assert len(result.t_statistics) == 2
assert len(result.p_values) == 2
assert 0.0 <= result.r_squared <= 1.0
assert 0.0 <= result.r_squared_adjusted <= 1.0
assert result.f_statistic >= 0.0
assert 0.0 <= result.f_p_value <= 1.0
assert result.residual_std_error >= 0.0
assert result.df_residuals == 3 assert result.df_model == 1
assert len(result.fitted_values) == 5
assert len(result.residuals) == 5
assert result.mse >= 0.0
assert result.rmse >= 0.0
assert result.mae >= 0.0
assert result.n_observations == 5
assert result.n_predictors == 1
def test_wls_summary_method(self):
y = [2.0, 4.0, 6.0, 8.0, 10.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
weights = [1.0, 1.0, 1.0, 1.0, 1.0]
result = linreg_core.wls_regression(y, x, weights)
summary = result.summary()
assert isinstance(summary, str)
assert "WLS" in summary
assert "R-squared" in summary
assert "F-statistic" in summary
assert "Coefficients" in summary
def test_wls_get_fitted_values(self):
y = [2.0, 4.0, 6.0, 8.0, 10.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
weights = [1.0, 1.0, 1.0, 1.0, 1.0]
result = linreg_core.wls_regression(y, x, weights)
fitted = result.get_fitted_values()
assert isinstance(fitted, list)
assert len(fitted) == 5
for i, (actual, pred) in enumerate(zip(y, fitted)):
assert abs(actual - pred) < 1e-10, f"Fitted value at {i} should be close to actual"
def test_wls_get_residuals(self):
y = [2.0, 4.0, 6.0, 8.0, 10.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
weights = [1.0, 1.0, 1.0, 1.0, 1.0]
result = linreg_core.wls_regression(y, x, weights)
residuals = result.get_residuals()
assert isinstance(residuals, list)
assert len(residuals) == 5
for r in residuals:
assert abs(r) < 1e-10, f"Residual should be near zero, got {r}"
def test_wls_to_dict_method(self):
y = [2.0, 4.0, 6.0, 8.0, 10.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
weights = [1.0, 1.0, 1.0, 1.0, 1.0]
result = linreg_core.wls_regression(y, x, weights)
d = result.to_dict()
assert isinstance(d, dict)
assert 'coefficients' in d
assert 'r_squared' in d
assert 'f_statistic' in d
assert 'n_observations' in d