diff options
| author | Ethan Werner <mail@ethanwerner.com> | 2026-07-01 09:42:39 +0400 |
|---|---|---|
| committer | Ethan Werner <mail@ethanwerner.com> | 2026-07-01 09:42:39 +0400 |
| commit | 07e18043145f3d91c8ea7ec3ad41b3c905585277 (patch) | |
| tree | 2bdeae46973f4bdfce263679a4008ac768011f28 /lib/regression_linear_least_squares.c | |
initial commit: ts time series analysis library v0.1.0
- 354 financial and mathematical time-series indicators
- Uniform C11 API with consistent function signatures
- 349 passing tests with -Werror
- Static and shared library builds
- pkg-config integration
- Python bindings
- Fuzzing and sanitizer support
- Security hardening flags
Diffstat (limited to 'lib/regression_linear_least_squares.c')
| -rwxr-xr-x | lib/regression_linear_least_squares.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/lib/regression_linear_least_squares.c b/lib/regression_linear_least_squares.c new file mode 100755 index 0000000..49123bb --- /dev/null +++ b/lib/regression_linear_least_squares.c @@ -0,0 +1,92 @@ +// Simple Least Squares Moving Linear Regression +// +// regression_linear_simple_least_squares - slrls +// +// ((k * sum_{i=0}^{n}[x_i * y_i]) - (sum_{i=0}^{n}[x_i] * sum_{i=0}^{n}[y_i])) +// β = ──────────────────────────────────────────────────────────────────────── +// ((k * sum_{i=0}^{n}[x_i^2]) - (sum_{i=0}^{n}[x_i^2])^2) +// +// α = E(y) - β * E(x) +// = μ_y - β * μ_x +// +// ( E( xy ) - E( μ_x ) * E( μ_y ) ) +// r_xy = ──────────────────────────────────────────────────────────────────────── +// ((E(x^2) - E(x)^2) - (E(y^2) - E(y)^2))^(1 / 2) +// +// y = α + βx +// +// Inputs: 2 +// Outputs: 3 +// Constants: 0 + + +#include <math.h> +#include "ts.h" + + +typedef struct { + uint_t k; +} ts_regression_linear_least_squares_params_t; + +ts_parameter_t const ts_regression_linear_least_squares_p = { .n_inputs = 2, .n_outputs = 3, .n_constants = 1, .params_size = sizeof(ts_regression_linear_least_squares_params_t) }; + + +uint_t ts_regression_linear_least_squares_offset( ts_constant_t const *c ) +{ + ( void )c; + return ( c ? ((ts_regression_linear_least_squares_params_t *)c)->k : 0 ); +} + +int ts_regression_linear_least_squares( + fp_t * const *y, uint_t y_n, + fp_t const * const *x, uint_t x_n, + uint_t n, ts_constant_t const *c +) +{ + TS_PROLOGUE( ts_regression_linear_least_squares_p ); + if ( !c ) return -1; + uint_t k = ((ts_regression_linear_least_squares_params_t *)c)->k; + if (k == 0 || n < k) return -1; + + uint_t xi = k; + uint_t yi = k; + fp_t var_x, var_y, cov_xy; + + for( ; xi < n; ++xi, ++yi ) + { + // Two-pass variance/covariance for numerical stability + fp_t mean_x = 0, mean_y = 0; + for (uint_t j = xi - k + 1; j <= xi; j++) { + mean_x += x[0][j]; + mean_y += x[1][j]; + } + mean_x /= (fp_t)k; + mean_y /= (fp_t)k; + var_x = 0; var_y = 0; cov_xy = 0; + for (uint_t j = xi - k + 1; j <= xi; j++) { + fp_t dx = x[0][j] - mean_x; + fp_t dy = x[1][j] - mean_y; + var_x += dx * dx; + var_y += dy * dy; + cov_xy += dx * dy; + } + var_x /= (fp_t)k; + var_y /= (fp_t)k; + cov_xy /= (fp_t)k; + + if ( var_x != 0 ) + { + y[0][yi] = cov_xy / var_x; + y[1][yi] = mean_y - y[0][yi] * mean_x; + } + else + { + y[0][yi] = 0; + y[1][yi] = mean_y; + } + fp_t prod = var_x * var_y; + y[2][yi] = prod > 0 ? cov_xy / sqrt( prod ) : 0; + } + return 0; +} + |