scm2cpp-learn 0.23.1__tar.gz → 0.24.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/PKG-INFO +156 -2
  2. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/README.md +155 -1
  3. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/pyproject.toml +1 -1
  4. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/setup.py +19 -1
  5. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/__init__.py +475 -6
  6. scm2cpp_learn-0.24.0/src/scm2cpp_learn/_generated/_loader_changepoint.py +57 -0
  7. scm2cpp_learn-0.24.0/src/scm2cpp_learn/_generated/_loader_lagops.py +22 -0
  8. scm2cpp_learn-0.24.0/src/scm2cpp_learn/_generated/changepoint.cpp +8 -0
  9. scm2cpp_learn-0.24.0/src/scm2cpp_learn/_generated/changepoint.hpp +262 -0
  10. scm2cpp_learn-0.24.0/src/scm2cpp_learn/_generated/changepoint_capi.cpp +28 -0
  11. scm2cpp_learn-0.24.0/src/scm2cpp_learn/_generated/lagops.cpp +4 -0
  12. scm2cpp_learn-0.24.0/src/scm2cpp_learn/_generated/lagops.hpp +92 -0
  13. scm2cpp_learn-0.24.0/src/scm2cpp_learn/_generated/lagops_capi.cpp +16 -0
  14. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/scm2cpp-blas.hpp +5 -3
  15. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_libfind.py +25 -0
  16. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn.egg-info/PKG-INFO +156 -2
  17. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn.egg-info/SOURCES.txt +8 -0
  18. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/setup.cfg +0 -0
  19. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_compat.py +0 -0
  20. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/__init__.py +0 -0
  21. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/_loader.py +0 -0
  22. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/_loader_accel_cublas.py +0 -0
  23. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/_loader_classifiers.py +0 -0
  24. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/_loader_derived_blas.py +0 -0
  25. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/_loader_derived_cublas.py +0 -0
  26. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/_loader_derived_loops.py +0 -0
  27. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/_loader_gpu_cuda.py +0 -0
  28. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/_loader_gramops_blas.py +0 -0
  29. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/_loader_gramops_loops.py +0 -0
  30. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/_loader_nmf_blas.py +0 -0
  31. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/_loader_nmf_cublas.py +0 -0
  32. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/_loader_nmf_loops.py +0 -0
  33. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/accel_cublas/accel.cpp +0 -0
  34. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/accel_cublas/accel.hpp +0 -0
  35. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/accel_cublas/accel_capi.cpp +0 -0
  36. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/classifiers.cpp +0 -0
  37. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/classifiers.hpp +0 -0
  38. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/classifiers_capi.cpp +0 -0
  39. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/derived_blas/lasso_design.cpp +0 -0
  40. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/derived_blas/lasso_design.hpp +0 -0
  41. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/derived_blas/lasso_design_capi.cpp +0 -0
  42. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/derived_cublas/lasso_design.cpp +0 -0
  43. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/derived_cublas/lasso_design.hpp +0 -0
  44. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/derived_cublas/lasso_design_capi.cpp +0 -0
  45. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/derived_loops/lasso_design.cpp +0 -0
  46. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/derived_loops/lasso_design.hpp +0 -0
  47. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/derived_loops/lasso_design_capi.cpp +0 -0
  48. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/gpu_cuda/gpu.cpp +0 -0
  49. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/gpu_cuda/gpu.hpp +0 -0
  50. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/gpu_cuda/gpu_capi.cpp +0 -0
  51. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/gramops_blas/gramops.cpp +0 -0
  52. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/gramops_blas/gramops.hpp +0 -0
  53. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/gramops_blas/gramops_capi.cpp +0 -0
  54. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/gramops_loops/gramops.cpp +0 -0
  55. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/gramops_loops/gramops.hpp +0 -0
  56. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/gramops_loops/gramops_capi.cpp +0 -0
  57. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/lasso_cov.cpp +0 -0
  58. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/lasso_cov.hpp +0 -0
  59. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/lasso_cov_capi.cpp +0 -0
  60. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/nmf_blas/nmf_cd.cpp +0 -0
  61. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/nmf_blas/nmf_cd.hpp +0 -0
  62. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/nmf_blas/nmf_cd_capi.cpp +0 -0
  63. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/nmf_cublas/nmf_cd.cpp +0 -0
  64. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/nmf_cublas/nmf_cd.hpp +0 -0
  65. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/nmf_cublas/nmf_cd_capi.cpp +0 -0
  66. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/nmf_loops/nmf_cd.cpp +0 -0
  67. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/nmf_loops/nmf_cd.hpp +0 -0
  68. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/nmf_loops/nmf_cd_capi.cpp +0 -0
  69. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/scm2cpp-cublas.hpp +0 -0
  70. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/scm2cpp-cuda.hpp +0 -0
  71. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/_generated/scm2cpp.hpp +0 -0
  72. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn/batch_capi.cu +0 -0
  73. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn.egg-info/dependency_links.txt +0 -0
  74. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn.egg-info/requires.txt +0 -0
  75. {scm2cpp_learn-0.23.1 → scm2cpp_learn-0.24.0}/src/scm2cpp_learn.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scm2cpp-learn
3
- Version: 0.23.1
3
+ Version: 0.24.0
4
4
  Summary: scikit-learn estimators (lasso, elastic net, ridge, RidgeCV, ARD, SVM, SVR, logistic regression, GLMs, Huber, Theil-Sen, F-score selection, RFE, NMF, Gaussian mixture, FastICA, PCA, KernelPCA, LDA, TruncatedSVD, PLS, CCA, graphical lasso, RANSAC, SGD, OMP, LARS) translated from Scheme to C++ by scm2cpp; GPU batch solver and CUDA kernels included
5
5
  Author: Hirotaka Niitsuma
6
6
  License: MIT
@@ -130,7 +130,7 @@ them first; this solver does not do it for you.
130
130
  From 0.14.0 every estimator with a `fit(X, y)` (or `fit(X)`) derives
131
131
  from scikit-learn's `BaseEstimator` and the matching mixin, so `clone`,
132
132
  `get_params` / `set_params`, `GridSearchCV`, `Pipeline` and
133
- `cross_val_score` take them, and all 44 pass
133
+ `cross_val_score` take them, and all 55 pass
134
134
  `sklearn.utils.estimator_checks.check_estimator` (`check-sklearn.py`
135
135
  runs it; the one expected failure is `LBFGSLogisticRegressionCV`'s
136
136
  `n_iter_` of 0 for a C whose warm start is already converged, which
@@ -217,6 +217,9 @@ package picks the fastest one present.
217
217
  | `GramRidge` | Ridge, fit(X, y) with the intercept, one or many targets, dense or sparse X | `gram.scm` (Cholesky; dposv) | loops / BLAS | Ridge |
218
218
  | `GramLinearRegression` | LinearRegression, dense or sparse X, sample_weight | `linreg.scm` (Cholesky; dposv, dpocon) | loops / BLAS | no |
219
219
  | `FrischNewtonQuantileRegressor` | QuantileRegressor, the L1 penalty and sample_weight included, dense X | `quantile.scm` (Frisch-Newton interior point; dsyrk, dposv) | loops / BLAS | no |
220
+ | `LagRidge` | Ridge on the windows of a series (n_lags values against the next horizon), fit(series), the windows never formed | `lag-gram.scm` (`--derive`: prefix and lag tables), `gram.scm` | CPU | no |
221
+ | `Pelt` | (none; ruptures `Pelt`, least-squares cost), fit(signal) then predict(pen), the optimum for any min_size and jump | `changepoint.scm` (`-I`: running sums) | CPU | - |
222
+ | `Binseg` | (none; ruptures `Binseg`, least-squares cost), fit(signal) then predict(n_bkps / pen / epsilon) | `changepoint.scm` (`-I`: running sums) | CPU | - |
220
223
  | `GramRidgeCV` | RidgeCV (cv=None), leave-one-out over every alpha from one eigenproblem | `ridge-gcv.scm` | loops / BLAS | no |
221
224
  | `GramMultiTaskLasso` | MultiTaskLasso, fit(X, Y) with the intercept | `lasso-cov.scm` | CPU | no |
222
225
  | `GramMultiTaskElasticNet` | MultiTaskElasticNet, fit(X, Y) with the intercept | `lasso-cov.scm` | CPU | no |
@@ -926,6 +929,157 @@ scikit-learn's times barely change with the cores (225 s and 28 s for
926
929
  on threads. In every row the objectives agree to 4e-16 of their value
927
930
  and the coefficients to 5e-14.
928
931
 
932
+ ## Forecasting from lags: `LagRidge`, `lag_moments`
933
+
934
+ A linear forecaster of a series from its own past is a ridge
935
+ regression on windows: row i of the design is `y[i:i+L]`, its target
936
+ the next H values `y[i+L:i+L+H]`. Formed as a matrix
937
+ (`sliding_window_view`), the T rows cost O(T L (L + H)) for X'X and
938
+ X'Y, and the matrix itself is T x L. But every entry of both is a sum
939
+ of products `y[t] * y[t+d]` over a run of t, and the runs of one lag d
940
+ differ only in where they start: a difference of two entries of that
941
+ lag's running sum.
942
+
943
+ `examples/kernel-only/lag-gram.scm` writes the moments as they are
944
+ defined -- `g[j][k] = sum_i a[j+i] * b[k+i]`, three loops -- and
945
+ `--derive` turns them into that form: prefix tables for the two column
946
+ sums, one running sum a lag for the Gram, O((n + wj)(wj + wk)) and no
947
+ window anywhere, behind a run-time comparison of the two costs that
948
+ keeps the loops when there are too few windows for the tables to pay.
949
+ `lag_moments(a, wj, wk, b=None, n=None)` is that function;
950
+ `LagRidge` is the ridge on top of it:
951
+
952
+ ```python
953
+ import numpy as np
954
+ from scm2cpp_learn import LagRidge
955
+
956
+ y = np.cumsum(np.random.default_rng(0).standard_normal(20000))
957
+ model = LagRidge(n_lags=336, horizon=96, alpha=1.0).fit(y)
958
+ model.forecast() # the 96 values after the end of y
959
+ model.predict(windows) # (m, 336) -> (m, 96)
960
+ model.coef_ # (96, 336): horizon by lag
961
+ ```
962
+
963
+ The fit is scikit-learn's `Ridge(alpha)` on the windows -- the same
964
+ objective, the intercept not penalised, every horizon a target -- and
965
+ agrees with it to rounding. A (T, C) series has C channels:
966
+ `channels="pooled"` fits one model to the windows of all of them (no
967
+ channel is regressed on another), `"separate"` one model a channel.
968
+ `center="last"` takes the window's newest value off the window and its
969
+ targets before the regression and puts it back on the forecast, and
970
+ `center="mean"` does so with the window's mean; both are linear in the
971
+ window, so they act on the moments, and `coef_` / `intercept_` are
972
+ still those of the map from the window as it is.
973
+
974
+ Against `Ridge(alpha=1)` on the window matrix, the time to make the
975
+ matrix included, and against this package's `GramRidge` on the same
976
+ matrix (`bench/lag-ridge-compare.py`; synthetic series at the lengths
977
+ of the hourly and the 15-minute ETT files; one thread on both sides,
978
+ on a machine busy with other work):
979
+
980
+ | series x channels | lags | horizons | windows | Ridge | GramRidge | LagRidge | ratio | difference |
981
+ |---|---|---|---|---|---|---|---|---|
982
+ | 17,420 x 1 | 96 | 96 | 17,229 | 0.0355 s | 0.0251 s | 0.0055 s | 6x | 5e-14 |
983
+ | 17,420 x 1 | 336 | 96 | 16,989 | 0.116 s | 0.105 s | 0.0157 s | 7x | 1e-13 |
984
+ | 17,420 x 1 | 720 | 720 | 15,981 | 0.525 s | 0.426 s | 0.0681 s | 8x | 2e-13 |
985
+ | 17,420 x 7 | 336 | 96 | 118,923 | 0.931 s | 0.875 s | 0.0982 s | 9x | 1e-12 |
986
+ | 69,680 x 1 | 96 | 96 | 69,489 | 0.133 s | 0.108 s | 0.0201 s | 7x | 4e-13 |
987
+ | 69,680 x 1 | 336 | 96 | 69,249 | 0.46 s | 0.429 s | 0.0544 s | 8x | 8e-13 |
988
+ | 69,680 x 1 | 720 | 96 | 68,865 | 1.18 s | 1.21 s | 0.117 s | 10x | 1e-12 |
989
+ | 69,680 x 1 | 720 | 720 | 68,241 | 3.14 s | 1.99 s | 0.178 s | 18x | 1e-12 |
990
+
991
+ The difference is the largest between the two models' forecasts over
992
+ the last 200 windows, relative to the largest value forecast. With
993
+ four BLAS threads on the same machine `Ridge` was no faster (0.05 to
994
+ 3.0 s); the kernel has one thread either way. The moments are
995
+ differences of running sums over the whole series, so `LagRidge` takes
996
+ the series' mean off first (it goes back into the intercept); with
997
+ `fit_intercept=False` and no centring the sums are those of the series
998
+ as given, as X'X of the windows would be.
999
+
1000
+ ## Change points: `Pelt`, `Binseg`
1001
+
1002
+ Where the level of a series changes. A segment costs the squared
1003
+ distance of its samples from their mean (the `"l2"` cost of the
1004
+ [ruptures](https://github.com/deepcharles/ruptures) library), and the
1005
+ two classes are ruptures' `Pelt` and `Binseg` on that cost, with
1006
+ ruptures' interface:
1007
+
1008
+ ```python
1009
+ import numpy as np
1010
+ from scm2cpp_learn import Pelt, Binseg
1011
+
1012
+ rng = np.random.default_rng(0)
1013
+ signal = np.repeat([0.0, 4.0, 1.0, 5.0], 500) + rng.standard_normal(2000)
1014
+ Pelt(min_size=2, jump=5).fit(signal).predict(pen=20) # [500, 1000, 1500, 2000]
1015
+ b = Binseg(min_size=2, jump=5).fit(signal)
1016
+ b.predict(n_bkps=3) # [500, 1000, 1500, 2000]
1017
+ b.cuts_ # [500, 1500, 1000]: the order made
1018
+ ```
1019
+
1020
+ `Pelt` returns the segmentation of least cost plus `pen` a change
1021
+ point (PELT, Killick, Fearnhead and Eckley 2012: the dynamic program
1022
+ over the segments' ends, without the starts that can no longer win).
1023
+ `Binseg` begins with one segment and each round makes the cut that
1024
+ lowers the cost most, until `n_bkps` cuts, a gain of `pen` or less, or
1025
+ a remaining cost of `epsilon` or less. Both return the ends of the
1026
+ segments in order, the last one n, as ruptures does; a (n, d) signal
1027
+ has d values a sample; a segment holds at least `min_size` samples and
1028
+ a cut sits at a multiple of `jump`. Parameters ruptures refuses
1029
+ (`BadSegmentationParameters`) raise `ValueError`.
1030
+
1031
+ `examples/kernel-only/changepoint.scm` writes both searches with each
1032
+ cost as it is defined, the sums over the segment as loops. Translated
1033
+ with `-I y,r`, every sum is the difference of two entries of a running
1034
+ sum formed once before the search: a cost is d + 1 subtractions
1035
+ whatever the segment's length, where ruptures' `Pelt` and `Binseg` go
1036
+ over the segment's samples for each (numpy's `var`, one call a
1037
+ candidate). ruptures' `KernelCPD` with the linear kernel is the same
1038
+ cost in C over running sums, `jump` fixed at 1.
1039
+
1040
+ Against ruptures 1.1.9 (`bench/changepoint-compare.py`; piecewise
1041
+ constant signals with noise, penalty 3 d log n; one thread, on a
1042
+ machine busy with other work). The segmentation of least penalised
1043
+ cost, ruptures' `Pelt` and `Pelt` at `min_size=2, jump=5`, `KernelCPD`
1044
+ and `Pelt` at `min_size=2, jump=1`:
1045
+
1046
+ | samples x values | change points | ruptures.Pelt | Pelt | ratio | same | KernelCPD | Pelt, jump 1 | ratio | same |
1047
+ |---|---|---|---|---|---|---|---|---|---|
1048
+ | 2,000 x 1 | 10 | 0.264 s | 0.000164 s | 1611x | yes | 0.00582 s | 0.00194 s | 3.0x | yes |
1049
+ | 5,000 x 1 | 25 | 0.909 s | 0.000374 s | 2429x | yes | 0.0194 s | 0.00628 s | 3.1x | yes |
1050
+ | 5,000 x 3 | 25 | 1.19 s | 0.00068 s | 1748x | yes | 0.018 s | 0.00696 s | 2.6x | yes |
1051
+ | 20,000 x 1 | 100 | 8.06 s | 0.00275 s | 2937x | yes | 0.153 s | 0.0513 s | 3.0x | yes |
1052
+ | 20,000 x 1 | 5 | 55.4 s | 0.0177 s | 3124x | yes | 1.16 s | 0.394 s | 2.9x | yes |
1053
+ | 20,000 x 3 | 100 | 8.06 s | 0.00396 s | 2037x | yes | 0.145 s | 0.0615 s | 2.4x | yes |
1054
+ | 100,000 x 1 | 500 | - | 0.0133 s | - | - | 0.728 s | 0.231 s | 3.1x | yes |
1055
+ | 100,000 x 3 | 500 | - | 0.0263 s | - | - | 0.762 s | 0.339 s | 2.2x | yes |
1056
+ | 100,000 x 1 | 5 | - | 0.397 s | - | - | 29.2 s | 10.2 s | 2.9x | yes |
1057
+
1058
+ Binary segmentation to the true number of change points,
1059
+ `min_size=2, jump=5`:
1060
+
1061
+ | samples x values | cuts | ruptures.Binseg | Binseg | ratio | same |
1062
+ |---|---|---|---|---|---|
1063
+ | 2,000 x 1 | 10 | 0.0953 s | 0.000188 s | 508x | yes |
1064
+ | 5,000 x 1 | 25 | 0.417 s | 0.000238 s | 1751x | yes |
1065
+ | 5,000 x 3 | 25 | 0.763 s | 0.000473 s | 1614x | yes |
1066
+ | 20,000 x 1 | 100 | 2.7 s | 0.000894 s | 3016x | yes |
1067
+ | 20,000 x 1 | 5 | 1.02 s | 0.000516 s | 1975x | yes |
1068
+ | 20,000 x 3 | 100 | 16 s | 0.00183 s | 8752x | yes |
1069
+ | 100,000 x 1 | 500 | 40.9 s | 0.00587 s | 6980x | yes |
1070
+ | 100,000 x 3 | 500 | 254 s | 0.0142 s | 17907x | yes |
1071
+ | 100,000 x 1 | 5 | 11.2 s | 0.00185 s | 6037x | yes |
1072
+
1073
+ "same": the two return the same change points. `Pelt` drops a start
1074
+ only from the first end at which a cut there has become possible, so
1075
+ its result is the optimum for every `min_size` and `jump` (equal to
1076
+ the dynamic program without pruning on 1,475 random signals);
1077
+ ruptures' `Pelt` drops it at once, and on 1,471 such signals returned
1078
+ a segmentation of higher objective in 17, by up to 1.5%. `Binseg`
1079
+ makes ruptures' cuts, equal gains included (1,373 of 1,373). The
1080
+ running sums are over the whole signal, so `fit` takes each column's
1081
+ mean off first; the cost does not see a level.
1082
+
929
1083
  ## Sparse X
930
1084
 
931
1085
  `GramLasso`, `GramElasticNet`, `GramRidge`, `GramLinearRegression` and
@@ -112,7 +112,7 @@ them first; this solver does not do it for you.
112
112
  From 0.14.0 every estimator with a `fit(X, y)` (or `fit(X)`) derives
113
113
  from scikit-learn's `BaseEstimator` and the matching mixin, so `clone`,
114
114
  `get_params` / `set_params`, `GridSearchCV`, `Pipeline` and
115
- `cross_val_score` take them, and all 44 pass
115
+ `cross_val_score` take them, and all 55 pass
116
116
  `sklearn.utils.estimator_checks.check_estimator` (`check-sklearn.py`
117
117
  runs it; the one expected failure is `LBFGSLogisticRegressionCV`'s
118
118
  `n_iter_` of 0 for a C whose warm start is already converged, which
@@ -199,6 +199,9 @@ package picks the fastest one present.
199
199
  | `GramRidge` | Ridge, fit(X, y) with the intercept, one or many targets, dense or sparse X | `gram.scm` (Cholesky; dposv) | loops / BLAS | Ridge |
200
200
  | `GramLinearRegression` | LinearRegression, dense or sparse X, sample_weight | `linreg.scm` (Cholesky; dposv, dpocon) | loops / BLAS | no |
201
201
  | `FrischNewtonQuantileRegressor` | QuantileRegressor, the L1 penalty and sample_weight included, dense X | `quantile.scm` (Frisch-Newton interior point; dsyrk, dposv) | loops / BLAS | no |
202
+ | `LagRidge` | Ridge on the windows of a series (n_lags values against the next horizon), fit(series), the windows never formed | `lag-gram.scm` (`--derive`: prefix and lag tables), `gram.scm` | CPU | no |
203
+ | `Pelt` | (none; ruptures `Pelt`, least-squares cost), fit(signal) then predict(pen), the optimum for any min_size and jump | `changepoint.scm` (`-I`: running sums) | CPU | - |
204
+ | `Binseg` | (none; ruptures `Binseg`, least-squares cost), fit(signal) then predict(n_bkps / pen / epsilon) | `changepoint.scm` (`-I`: running sums) | CPU | - |
202
205
  | `GramRidgeCV` | RidgeCV (cv=None), leave-one-out over every alpha from one eigenproblem | `ridge-gcv.scm` | loops / BLAS | no |
203
206
  | `GramMultiTaskLasso` | MultiTaskLasso, fit(X, Y) with the intercept | `lasso-cov.scm` | CPU | no |
204
207
  | `GramMultiTaskElasticNet` | MultiTaskElasticNet, fit(X, Y) with the intercept | `lasso-cov.scm` | CPU | no |
@@ -908,6 +911,157 @@ scikit-learn's times barely change with the cores (225 s and 28 s for
908
911
  on threads. In every row the objectives agree to 4e-16 of their value
909
912
  and the coefficients to 5e-14.
910
913
 
914
+ ## Forecasting from lags: `LagRidge`, `lag_moments`
915
+
916
+ A linear forecaster of a series from its own past is a ridge
917
+ regression on windows: row i of the design is `y[i:i+L]`, its target
918
+ the next H values `y[i+L:i+L+H]`. Formed as a matrix
919
+ (`sliding_window_view`), the T rows cost O(T L (L + H)) for X'X and
920
+ X'Y, and the matrix itself is T x L. But every entry of both is a sum
921
+ of products `y[t] * y[t+d]` over a run of t, and the runs of one lag d
922
+ differ only in where they start: a difference of two entries of that
923
+ lag's running sum.
924
+
925
+ `examples/kernel-only/lag-gram.scm` writes the moments as they are
926
+ defined -- `g[j][k] = sum_i a[j+i] * b[k+i]`, three loops -- and
927
+ `--derive` turns them into that form: prefix tables for the two column
928
+ sums, one running sum a lag for the Gram, O((n + wj)(wj + wk)) and no
929
+ window anywhere, behind a run-time comparison of the two costs that
930
+ keeps the loops when there are too few windows for the tables to pay.
931
+ `lag_moments(a, wj, wk, b=None, n=None)` is that function;
932
+ `LagRidge` is the ridge on top of it:
933
+
934
+ ```python
935
+ import numpy as np
936
+ from scm2cpp_learn import LagRidge
937
+
938
+ y = np.cumsum(np.random.default_rng(0).standard_normal(20000))
939
+ model = LagRidge(n_lags=336, horizon=96, alpha=1.0).fit(y)
940
+ model.forecast() # the 96 values after the end of y
941
+ model.predict(windows) # (m, 336) -> (m, 96)
942
+ model.coef_ # (96, 336): horizon by lag
943
+ ```
944
+
945
+ The fit is scikit-learn's `Ridge(alpha)` on the windows -- the same
946
+ objective, the intercept not penalised, every horizon a target -- and
947
+ agrees with it to rounding. A (T, C) series has C channels:
948
+ `channels="pooled"` fits one model to the windows of all of them (no
949
+ channel is regressed on another), `"separate"` one model a channel.
950
+ `center="last"` takes the window's newest value off the window and its
951
+ targets before the regression and puts it back on the forecast, and
952
+ `center="mean"` does so with the window's mean; both are linear in the
953
+ window, so they act on the moments, and `coef_` / `intercept_` are
954
+ still those of the map from the window as it is.
955
+
956
+ Against `Ridge(alpha=1)` on the window matrix, the time to make the
957
+ matrix included, and against this package's `GramRidge` on the same
958
+ matrix (`bench/lag-ridge-compare.py`; synthetic series at the lengths
959
+ of the hourly and the 15-minute ETT files; one thread on both sides,
960
+ on a machine busy with other work):
961
+
962
+ | series x channels | lags | horizons | windows | Ridge | GramRidge | LagRidge | ratio | difference |
963
+ |---|---|---|---|---|---|---|---|---|
964
+ | 17,420 x 1 | 96 | 96 | 17,229 | 0.0355 s | 0.0251 s | 0.0055 s | 6x | 5e-14 |
965
+ | 17,420 x 1 | 336 | 96 | 16,989 | 0.116 s | 0.105 s | 0.0157 s | 7x | 1e-13 |
966
+ | 17,420 x 1 | 720 | 720 | 15,981 | 0.525 s | 0.426 s | 0.0681 s | 8x | 2e-13 |
967
+ | 17,420 x 7 | 336 | 96 | 118,923 | 0.931 s | 0.875 s | 0.0982 s | 9x | 1e-12 |
968
+ | 69,680 x 1 | 96 | 96 | 69,489 | 0.133 s | 0.108 s | 0.0201 s | 7x | 4e-13 |
969
+ | 69,680 x 1 | 336 | 96 | 69,249 | 0.46 s | 0.429 s | 0.0544 s | 8x | 8e-13 |
970
+ | 69,680 x 1 | 720 | 96 | 68,865 | 1.18 s | 1.21 s | 0.117 s | 10x | 1e-12 |
971
+ | 69,680 x 1 | 720 | 720 | 68,241 | 3.14 s | 1.99 s | 0.178 s | 18x | 1e-12 |
972
+
973
+ The difference is the largest between the two models' forecasts over
974
+ the last 200 windows, relative to the largest value forecast. With
975
+ four BLAS threads on the same machine `Ridge` was no faster (0.05 to
976
+ 3.0 s); the kernel has one thread either way. The moments are
977
+ differences of running sums over the whole series, so `LagRidge` takes
978
+ the series' mean off first (it goes back into the intercept); with
979
+ `fit_intercept=False` and no centring the sums are those of the series
980
+ as given, as X'X of the windows would be.
981
+
982
+ ## Change points: `Pelt`, `Binseg`
983
+
984
+ Where the level of a series changes. A segment costs the squared
985
+ distance of its samples from their mean (the `"l2"` cost of the
986
+ [ruptures](https://github.com/deepcharles/ruptures) library), and the
987
+ two classes are ruptures' `Pelt` and `Binseg` on that cost, with
988
+ ruptures' interface:
989
+
990
+ ```python
991
+ import numpy as np
992
+ from scm2cpp_learn import Pelt, Binseg
993
+
994
+ rng = np.random.default_rng(0)
995
+ signal = np.repeat([0.0, 4.0, 1.0, 5.0], 500) + rng.standard_normal(2000)
996
+ Pelt(min_size=2, jump=5).fit(signal).predict(pen=20) # [500, 1000, 1500, 2000]
997
+ b = Binseg(min_size=2, jump=5).fit(signal)
998
+ b.predict(n_bkps=3) # [500, 1000, 1500, 2000]
999
+ b.cuts_ # [500, 1500, 1000]: the order made
1000
+ ```
1001
+
1002
+ `Pelt` returns the segmentation of least cost plus `pen` a change
1003
+ point (PELT, Killick, Fearnhead and Eckley 2012: the dynamic program
1004
+ over the segments' ends, without the starts that can no longer win).
1005
+ `Binseg` begins with one segment and each round makes the cut that
1006
+ lowers the cost most, until `n_bkps` cuts, a gain of `pen` or less, or
1007
+ a remaining cost of `epsilon` or less. Both return the ends of the
1008
+ segments in order, the last one n, as ruptures does; a (n, d) signal
1009
+ has d values a sample; a segment holds at least `min_size` samples and
1010
+ a cut sits at a multiple of `jump`. Parameters ruptures refuses
1011
+ (`BadSegmentationParameters`) raise `ValueError`.
1012
+
1013
+ `examples/kernel-only/changepoint.scm` writes both searches with each
1014
+ cost as it is defined, the sums over the segment as loops. Translated
1015
+ with `-I y,r`, every sum is the difference of two entries of a running
1016
+ sum formed once before the search: a cost is d + 1 subtractions
1017
+ whatever the segment's length, where ruptures' `Pelt` and `Binseg` go
1018
+ over the segment's samples for each (numpy's `var`, one call a
1019
+ candidate). ruptures' `KernelCPD` with the linear kernel is the same
1020
+ cost in C over running sums, `jump` fixed at 1.
1021
+
1022
+ Against ruptures 1.1.9 (`bench/changepoint-compare.py`; piecewise
1023
+ constant signals with noise, penalty 3 d log n; one thread, on a
1024
+ machine busy with other work). The segmentation of least penalised
1025
+ cost, ruptures' `Pelt` and `Pelt` at `min_size=2, jump=5`, `KernelCPD`
1026
+ and `Pelt` at `min_size=2, jump=1`:
1027
+
1028
+ | samples x values | change points | ruptures.Pelt | Pelt | ratio | same | KernelCPD | Pelt, jump 1 | ratio | same |
1029
+ |---|---|---|---|---|---|---|---|---|---|
1030
+ | 2,000 x 1 | 10 | 0.264 s | 0.000164 s | 1611x | yes | 0.00582 s | 0.00194 s | 3.0x | yes |
1031
+ | 5,000 x 1 | 25 | 0.909 s | 0.000374 s | 2429x | yes | 0.0194 s | 0.00628 s | 3.1x | yes |
1032
+ | 5,000 x 3 | 25 | 1.19 s | 0.00068 s | 1748x | yes | 0.018 s | 0.00696 s | 2.6x | yes |
1033
+ | 20,000 x 1 | 100 | 8.06 s | 0.00275 s | 2937x | yes | 0.153 s | 0.0513 s | 3.0x | yes |
1034
+ | 20,000 x 1 | 5 | 55.4 s | 0.0177 s | 3124x | yes | 1.16 s | 0.394 s | 2.9x | yes |
1035
+ | 20,000 x 3 | 100 | 8.06 s | 0.00396 s | 2037x | yes | 0.145 s | 0.0615 s | 2.4x | yes |
1036
+ | 100,000 x 1 | 500 | - | 0.0133 s | - | - | 0.728 s | 0.231 s | 3.1x | yes |
1037
+ | 100,000 x 3 | 500 | - | 0.0263 s | - | - | 0.762 s | 0.339 s | 2.2x | yes |
1038
+ | 100,000 x 1 | 5 | - | 0.397 s | - | - | 29.2 s | 10.2 s | 2.9x | yes |
1039
+
1040
+ Binary segmentation to the true number of change points,
1041
+ `min_size=2, jump=5`:
1042
+
1043
+ | samples x values | cuts | ruptures.Binseg | Binseg | ratio | same |
1044
+ |---|---|---|---|---|---|
1045
+ | 2,000 x 1 | 10 | 0.0953 s | 0.000188 s | 508x | yes |
1046
+ | 5,000 x 1 | 25 | 0.417 s | 0.000238 s | 1751x | yes |
1047
+ | 5,000 x 3 | 25 | 0.763 s | 0.000473 s | 1614x | yes |
1048
+ | 20,000 x 1 | 100 | 2.7 s | 0.000894 s | 3016x | yes |
1049
+ | 20,000 x 1 | 5 | 1.02 s | 0.000516 s | 1975x | yes |
1050
+ | 20,000 x 3 | 100 | 16 s | 0.00183 s | 8752x | yes |
1051
+ | 100,000 x 1 | 500 | 40.9 s | 0.00587 s | 6980x | yes |
1052
+ | 100,000 x 3 | 500 | 254 s | 0.0142 s | 17907x | yes |
1053
+ | 100,000 x 1 | 5 | 11.2 s | 0.00185 s | 6037x | yes |
1054
+
1055
+ "same": the two return the same change points. `Pelt` drops a start
1056
+ only from the first end at which a cut there has become possible, so
1057
+ its result is the optimum for every `min_size` and `jump` (equal to
1058
+ the dynamic program without pruning on 1,475 random signals);
1059
+ ruptures' `Pelt` drops it at once, and on 1,471 such signals returned
1060
+ a segmentation of higher objective in 17, by up to 1.5%. `Binseg`
1061
+ makes ruptures' cuts, equal gains included (1,373 of 1,373). The
1062
+ running sums are over the whole signal, so `fit` takes each column's
1063
+ mean off first; the cost does not see a level.
1064
+
911
1065
  ## Sparse X
912
1066
 
913
1067
  `GramLasso`, `GramElasticNet`, `GramRidge`, `GramLinearRegression` and
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "scm2cpp-learn"
7
- version = "0.23.1"
7
+ version = "0.24.0"
8
8
  description = "scikit-learn estimators (lasso, elastic net, ridge, RidgeCV, ARD, SVM, SVR, logistic regression, GLMs, Huber, Theil-Sen, F-score selection, RFE, NMF, Gaussian mixture, FastICA, PCA, KernelPCA, LDA, TruncatedSVD, PLS, CCA, graphical lasso, RANSAC, SGD, OMP, LARS) translated from Scheme to C++ by scm2cpp; GPU batch solver and CUDA kernels included"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.8"
@@ -330,8 +330,26 @@ else:
330
330
  extra_link_args=_omp,
331
331
  )
332
332
 
333
+ # The moments of a lag design (lag-gram.scm, derived): prefix tables and
334
+ # one lag table, plain loops, one thread.
335
+ lagops = Extension(
336
+ "scm2cpp_learn._lagops",
337
+ sources=[GEN_REL + "/lagops_capi.cpp"],
338
+ include_dirs=[GEN_REL],
339
+ extra_compile_args=_cls_flags,
340
+ )
341
+
342
+ # Change points under the least-squares cost (changepoint.scm with -I
343
+ # y,r): the searches over tables of running sums, plain loops, one thread.
344
+ changepoint = Extension(
345
+ "scm2cpp_learn._changepoint",
346
+ sources=[GEN_REL + "/changepoint_capi.cpp"],
347
+ include_dirs=[GEN_REL],
348
+ extra_compile_args=_cls_flags,
349
+ )
350
+
333
351
  _cuda = cuda_home()
334
- extensions = [kernel, derived, classifiers, nmf, gramops]
352
+ extensions = [kernel, derived, classifiers, nmf, gramops, lagops, changepoint]
335
353
  if _cuda is not None:
336
354
  extensions.append(Extension(
337
355
  "scm2cpp_learn._lasso_derived_cublas",