linreg-core 0.8.1

Lightweight regression library (OLS, Ridge, Lasso, Elastic Net, WLS, LOESS, Polynomial) with 14 diagnostic tests, cross validation, and prediction intervals. Pure Rust - no external math dependencies. WASM, Python, FFI, and Excel XLL bindings.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import pytest
import linreg_core


class TestRidgeRegressionNative:
    """Tests for native Python type API (Phase 5)."""

    def test_ridge_regression_native_lists(self):
        """Test Ridge regression with Python lists."""
        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)

        # Verify result is a RidgeResult object
        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):
        """Test that all RidgeResult attributes are accessible."""
        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)

        # Test all attributes
        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):
        """Test the summary() method of RidgeResult."""
        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()

        # Verify summary is a string with expected content
        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):
        """Test the to_dict() method of RidgeResult."""
        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()

        # Verify to_dict returns a proper 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):
        """Test __repr__ and __str__ methods."""
        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)

        # Test __str__
        str_result = str(result)
        assert "Ridge Regression Results" in str_result

        # Test __repr__
        repr_result = repr(result)
        assert "RidgeResult" in repr_result

    def test_ridge_multiple_predictors(self):
        """Test Ridge with multiple predictor variables."""
        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)

        # Should have 2 coefficients
        assert len(result.coefficients) == 2
        assert len(result.fitted_values) == 6


class TestLassoRegressionNative:
    """Tests for native Python type API (Phase 5)."""

    def test_lasso_regression_native_lists(self):
        """Test Lasso regression with Python lists."""
        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)

        # Verify result is a LassoResult object
        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):
        """Test that all LassoResult attributes are accessible."""
        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)

        # Test all attributes
        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):
        """Test the summary() method of LassoResult."""
        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()

        # Verify summary is a string with expected content
        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):
        """Test the to_dict() method of LassoResult."""
        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()

        # Verify to_dict returns a proper 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):
        """Test __repr__ and __str__ methods."""
        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)

        # Test __str__
        str_result = str(result)
        assert "Lasso Regression Results" in str_result

        # Test __repr__
        repr_result = repr(result)
        assert "LassoResult" in repr_result

    def test_lasso_variable_selection(self):
        """Test that Lasso can set some coefficients to zero."""
        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],  # Weak predictor
        ]

        # High lambda should shrink weak predictor to zero
        result = linreg_core.lasso_regression(y, x, 1.0, True, 100000, 1e-7)
        assert result.n_nonzero <= 2  # At most 2 non-zero coefficients


class TestElasticNetRegressionNative:
    """Tests for native Python type API (Phase 5)."""

    def test_elastic_net_regression_native_lists(self):
        """Test Elastic Net regression with Python lists."""
        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)

        # Verify result is an ElasticNetResult object
        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):
        """Test that all ElasticNetResult attributes are accessible."""
        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)

        # Test all attributes
        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):
        """Test the summary() method of ElasticNetResult."""
        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()

        # Verify summary is a string with expected content
        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):
        """Test the to_dict() method of ElasticNetResult."""
        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()

        # Verify to_dict returns a proper 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):
        """Test __repr__ and __str__ methods."""
        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)

        # Test __str__
        str_result = str(result)
        assert "Elastic Net Regression Results" in str_result

        # Test __repr__
        repr_result = repr(result)
        assert "ElasticNetResult" in repr_result

    def test_elastic_net_alpha_pure_ridge(self):
        """Test alpha=0 approaches Ridge (L2 only)."""
        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):
        """Test alpha=1 approaches Lasso (L1 only)."""
        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:
    """Tests for native Python type API (Phase 5)."""

    def test_lambda_path_native_lists(self):
        """Test lambda path generation with Python lists."""
        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)

        # Verify result is a LambdaPathResult object
        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):
        """Test that all LambdaPathResult attributes are accessible."""
        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)

        # Test all attributes
        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):
        """Test that lambda path is decreasing."""
        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)

        # Lambda path should be decreasing
        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):
        """Test that lambda_max > lambda_min for reasonable data."""
        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):
        """Test the summary() method of LambdaPathResult."""
        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()

        # Verify summary is a string with expected content
        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):
        """Test the to_dict() method of LambdaPathResult."""
        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()

        # Verify to_dict returns a proper 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):
        """Test __repr__ and __str__ methods."""
        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)

        # Test __str__
        str_result = str(result)
        assert "Lambda Path Results" in str_result

        # Test __repr__
        repr_result = repr(result)
        assert "LambdaPathResult" in repr_result


class TestRegularizedEdgeCases:
    """Edge case and boundary condition tests for regularized regression."""

    def test_ridge_small_lambda(self):
        """Test Ridge with small lambda (close to OLS)."""
        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)
        # RidgeResult returns slope coefficients only (not intercept)
        assert len(result.coefficients) == 1
        # But intercept is available separately
        assert hasattr(result, 'intercept')

    def test_ridge_large_lambda(self):
        """Test Ridge with large lambda (coefficients shrink toward zero)."""
        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)
        # With large lambda, coefficients should shrink
        # RidgeResult returns slope coefficients only (not intercept)
        assert len(result.coefficients) == 1
        # Coefficient should be close to zero
        assert abs(result.coefficients[0]) < 0.1

    def test_ridge_negative_lambda_raises_error(self):
        """Test that negative lambda raises an error."""
        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):
        """Test Lasso with small lambda (most coefficients non-zero)."""
        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)
        # With small lambda, should have non-zero coefficients
        assert result.n_nonzero >= 1

    def test_lasso_large_lambda(self):
        """Test Lasso with large lambda (coefficients shrink to zero)."""
        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)
        # With larger lambda, fewer non-zero coefficients
        assert hasattr(result, 'n_nonzero')

    def test_lasso_convergence_attribute(self):
        """Test that Lasso result has convergence attribute."""
        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)
        # Should have convergence info
        assert hasattr(result, 'n_nonzero')

    def test_elastic_net_middle_alpha(self):
        """Test ElasticNet with middle alpha value."""
        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):
        """Test ElasticNet with negative alpha (current behavior)."""
        y = [1.0, 2.0, 3.0, 4.0, 5.0]
        x = [[1.0, 2.0, 3.0, 4.0, 5.0]]

        # Alpha outside [0, 1] may be accepted or rejected
        # This test documents current behavior
        try:
            result = linreg_core.elastic_net_regression(y, x, 0.1, -0.5, True, 1000, 1e-7)
            # If accepted, verify we get a result
            assert hasattr(result, 'n_nonzero')
        except Exception:
            pass  # Also acceptable if rejected

    def test_regularized_multiple_predictors(self):
        """Test regularized regression handles multiple predictors."""
        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)
        # RidgeResult returns slope coefficients only (not intercept)
        assert len(result.coefficients) == 2