Expand description
§Rolling least-squares linear regression
Fit (y = \text{intercept} + \text{slope}\cdot x) over the last N samples, where (x = 0,1,\ldots,N-1) (oldest → newest in the window).
Versatile: pass any f64 series — closes, highs, lows, typical price,
volume, custom transforms. The crate does not force OHLCV; your engine picks
the slice (e.g. highs for resistance slope, closes for trend slope).
§Trading perspective
| Output | Habit |
|---|---|
| slope | Direction & steepness per bar (price units / bar) |
| angle_degrees | atan(slope) in degrees — comparable trend “angle” when scale is fixed |
| r_squared | How linear the window is (1 = perfect line) |
| intercept | Fitted value at the oldest bar of the window |
§Engineering
| Layer | API |
|---|---|
| Params | LinRegParams |
| Batch | linear_regression |
| Live | LinRegState::push / push_bars / from_history |
| Teaching | linear_regression_solution |
Each push when warm is O(period) (recompute OLS on the ring). Fine for typical windows (20–200); not nanosecond-critical path.
§Word problem
Closes 1,2,3,4,5 over five bars. Slope of the 5-bar regression?
Expect: slope 1.0 (perfect line).
use finance_solution::stocks::ta::{linear_regression, LinRegParams};
let y = [1.0, 2.0, 3.0, 4.0, 5.0];
let s = linear_regression(&y, LinRegParams::new(5)).unwrap();
assert!((s[4].unwrap().slope - 1.0).abs() < 1e-12);
assert!((s[4].unwrap().r_squared - 1.0).abs() < 1e-12);Structs§
- LinReg
Bar - One fitted window.
- LinReg
Params - Rolling regression window length.
- LinReg
Solution - LinReg
State - Incremental rolling regression on a caller-chosen series.
- Validated
LinReg - Validated pack.
Functions§
- linear_
regression - Batch rolling OLS.
seriesis your choice of bar field (close, high, …). - linear_
regression_ solution