Skip to main content

Module linear_regression

Module linear_regression 

Source
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

OutputHabit
slopeDirection & steepness per bar (price units / bar)
angle_degreesatan(slope) in degrees — comparable trend “angle” when scale is fixed
r_squaredHow linear the window is (1 = perfect line)
interceptFitted value at the oldest bar of the window

§Engineering

LayerAPI
ParamsLinRegParams
Batchlinear_regression
LiveLinRegState::push / push_bars / from_history
Teachinglinear_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§

LinRegBar
One fitted window.
LinRegParams
Rolling regression window length.
LinRegSolution
LinRegState
Incremental rolling regression on a caller-chosen series.
ValidatedLinReg
Validated pack.

Functions§

linear_regression
Batch rolling OLS. series is your choice of bar field (close, high, …).
linear_regression_solution