import pytest
import linreg_core
class TestRidgeRegressionNative:
def test_ridge_regression_native_lists(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.ridge_regression(y, x, 1.0, True)
assert hasattr(result, 'intercept')
assert hasattr(result, 'coefficients')
assert hasattr(result, 'lambda')
assert hasattr(result, 'r_squared')
assert getattr(result, 'lambda') == 1.0
def test_ridge_result_object_attributes(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.ridge_regression(y, x, 0.5, True)
assert isinstance(result.intercept, float)
assert isinstance(result.coefficients, list)
assert isinstance(getattr(result, 'lambda'), float)
assert isinstance(result.fitted_values, list)
assert isinstance(result.residuals, list)
assert isinstance(result.r_squared, float)
assert isinstance(result.mse, float)
assert isinstance(result.effective_df, float)
def test_ridge_summary_method(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.ridge_regression(y, x, 1.0, True)
summary = result.summary()
assert isinstance(summary, str)
assert "Ridge Regression Results" in summary
assert "Lambda" in summary
assert "R-squared" in summary
def test_ridge_to_dict_method(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.ridge_regression(y, x, 1.0, True)
d = result.to_dict()
assert isinstance(d, dict)
assert "intercept" in d
assert "coefficients" in d
assert "lambda" in d
assert "r_squared" in d
def test_ridge_repr_and_str(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.ridge_regression(y, x, 1.0, True)
str_result = str(result)
assert "Ridge Regression Results" in str_result
repr_result = repr(result)
assert "RidgeResult" in repr_result
def test_ridge_multiple_predictors(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3, 7.0]
x = [
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
[2.0, 4.0, 5.0, 4.0, 3.0, 2.0]
]
result = linreg_core.ridge_regression(y, x, 0.1, False)
assert len(result.coefficients) == 2
assert len(result.fitted_values) == 6
class TestLassoRegressionNative:
def test_lasso_regression_native_lists(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.lasso_regression(y, x, 0.1, True, 100000, 1e-7)
assert hasattr(result, 'intercept')
assert hasattr(result, 'coefficients')
assert hasattr(result, 'lambda')
assert hasattr(result, 'n_nonzero')
assert hasattr(result, 'converged')
assert getattr(result, 'lambda') == 0.1
def test_lasso_result_object_attributes(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.lasso_regression(y, x, 0.1, True, 100000, 1e-7)
assert isinstance(result.intercept, float)
assert isinstance(result.coefficients, list)
assert isinstance(getattr(result, 'lambda'), float)
assert isinstance(result.n_nonzero, int)
assert isinstance(result.converged, bool)
assert isinstance(result.n_iterations, int)
assert isinstance(result.r_squared, float)
def test_lasso_summary_method(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.lasso_regression(y, x, 0.1, True, 100000, 1e-7)
summary = result.summary()
assert isinstance(summary, str)
assert "Lasso Regression Results" in summary
assert "Lambda" in summary
assert "Non-zero coefficients" in summary
assert "Converged" in summary
def test_lasso_to_dict_method(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.lasso_regression(y, x, 0.1, True, 100000, 1e-7)
d = result.to_dict()
assert isinstance(d, dict)
assert "intercept" in d
assert "coefficients" in d
assert "lambda" in d
assert "n_nonzero" in d
def test_lasso_repr_and_str(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.lasso_regression(y, x, 0.1, True, 100000, 1e-7)
str_result = str(result)
assert "Lasso Regression Results" in str_result
repr_result = repr(result)
assert "LassoResult" in repr_result
def test_lasso_variable_selection(self):
y = [1.0, 2.0, 3.0, 4.0, 5.0]
x = [
[1.0, 2.0, 3.0, 4.0, 5.0],
[0.1, 0.2, 0.3, 0.2, 0.1], ]
result = linreg_core.lasso_regression(y, x, 1.0, True, 100000, 1e-7)
assert result.n_nonzero <= 2
class TestElasticNetRegressionNative:
def test_elastic_net_regression_native_lists(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.elastic_net_regression(y, x, 0.1, 0.5, True, 100000, 1e-7)
assert hasattr(result, 'intercept')
assert hasattr(result, 'coefficients')
assert hasattr(result, 'lambda')
assert hasattr(result, 'alpha')
assert hasattr(result, 'n_nonzero')
assert getattr(result, 'lambda') == 0.1
assert result.alpha == 0.5
def test_elastic_net_result_object_attributes(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.elastic_net_regression(y, x, 0.1, 0.5, True, 100000, 1e-7)
assert isinstance(result.intercept, float)
assert isinstance(result.coefficients, list)
assert isinstance(getattr(result, 'lambda'), float)
assert isinstance(result.alpha, float)
assert isinstance(result.n_nonzero, int)
assert isinstance(result.converged, bool)
assert isinstance(result.n_iterations, int)
assert isinstance(result.r_squared, float)
def test_elastic_net_summary_method(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.elastic_net_regression(y, x, 0.1, 0.5, True, 100000, 1e-7)
summary = result.summary()
assert isinstance(summary, str)
assert "Elastic Net Regression Results" in summary
assert "Lambda" in summary
assert "Alpha" in summary
assert "Non-zero coefficients" in summary
def test_elastic_net_to_dict_method(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.elastic_net_regression(y, x, 0.1, 0.5, True, 100000, 1e-7)
d = result.to_dict()
assert isinstance(d, dict)
assert "intercept" in d
assert "coefficients" in d
assert "lambda" in d
assert "alpha" in d
def test_elastic_net_repr_and_str(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.elastic_net_regression(y, x, 0.1, 0.5, True, 100000, 1e-7)
str_result = str(result)
assert "Elastic Net Regression Results" in str_result
repr_result = repr(result)
assert "ElasticNetResult" in repr_result
def test_elastic_net_alpha_pure_ridge(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.elastic_net_regression(y, x, 0.1, 0.0, True, 100000, 1e-7)
assert result.alpha == 0.0
def test_elastic_net_alpha_pure_lasso(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.elastic_net_regression(y, x, 0.1, 1.0, True, 100000, 1e-7)
assert result.alpha == 1.0
class TestLambdaPathNative:
def test_lambda_path_native_lists(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.make_lambda_path(y, x, 100, 0.01)
assert hasattr(result, 'lambda_path')
assert hasattr(result, 'lambda_max')
assert hasattr(result, 'lambda_min')
assert hasattr(result, 'n_lambda')
assert result.n_lambda == 100
def test_lambda_path_result_attributes(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.make_lambda_path(y, x, 50, 0.1)
assert isinstance(result.lambda_path, list)
assert isinstance(result.lambda_max, float)
assert isinstance(result.lambda_min, float)
assert isinstance(result.n_lambda, int)
assert result.n_lambda == 50
assert len(result.lambda_path) == 50
def test_lambda_path_decreasing_sequence(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.make_lambda_path(y, x, 20, 0.01)
for i in range(1, len(result.lambda_path)):
assert result.lambda_path[i] <= result.lambda_path[i - 1]
def test_lambda_path_max_min(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.make_lambda_path(y, x, 10, 0.1)
assert result.lambda_max > result.lambda_min
assert result.lambda_max == result.lambda_path[0]
assert result.lambda_min == result.lambda_path[-1]
def test_lambda_path_summary_method(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.make_lambda_path(y, x, 100, 0.01)
summary = result.summary()
assert isinstance(summary, str)
assert "Lambda Path Results" in summary
assert "Lambda max" in summary
assert "Lambda min" in summary
assert "Number of values" in summary
def test_lambda_path_to_dict_method(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.make_lambda_path(y, x, 50, 0.1)
d = result.to_dict()
assert isinstance(d, dict)
assert "lambda_path" in d
assert "lambda_max" in d
assert "lambda_min" in d
assert "n_lambda" in d
def test_lambda_path_repr_and_str(self):
y = [2.5, 3.7, 4.2, 5.1, 6.3]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
result = linreg_core.make_lambda_path(y, x, 100, 0.01)
str_result = str(result)
assert "Lambda Path Results" in str_result
repr_result = repr(result)
assert "LambdaPathResult" in repr_result
class TestRegularizedEdgeCases:
def test_ridge_small_lambda(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.ridge_regression(y, x, 0.001, True)
assert len(result.coefficients) == 1
assert hasattr(result, 'intercept')
def test_ridge_large_lambda(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.ridge_regression(y, x, 100.0, True)
assert len(result.coefficients) == 1
assert abs(result.coefficients[0]) < 0.1
def test_ridge_negative_lambda_raises_error(self):
y = [1.0, 2.0, 3.0, 4.0, 5.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
with pytest.raises(Exception):
linreg_core.ridge_regression(y, x, -1.0, True)
def test_lasso_small_lambda(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.lasso_regression(y, x, 0.001, True, 1000, 1e-7)
assert result.n_nonzero >= 1
def test_lasso_large_lambda(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.lasso_regression(y, x, 10.0, True, 1000, 1e-7)
assert hasattr(result, 'n_nonzero')
def test_lasso_convergence_attribute(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.lasso_regression(y, x, 0.1, True, 1000, 1e-7)
assert hasattr(result, 'n_nonzero')
def test_elastic_net_middle_alpha(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.elastic_net_regression(y, x, 0.1, 0.5, True, 1000, 1e-7)
assert hasattr(result, 'n_nonzero')
def test_elastic_net_negative_alpha(self):
y = [1.0, 2.0, 3.0, 4.0, 5.0]
x = [[1.0, 2.0, 3.0, 4.0, 5.0]]
try:
result = linreg_core.elastic_net_regression(y, x, 0.1, -0.5, True, 1000, 1e-7)
assert hasattr(result, 'n_nonzero')
except Exception:
pass
def test_regularized_multiple_predictors(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, 3.0, 4.0, 2.0, 3.0]
result = linreg_core.ridge_regression(y, [x1, x2], 1.0, True)
assert len(result.coefficients) == 2