create a_regress_simple_evar
[liba.git] / src / regress_simple.c
blob8286636b21d2d6eaac14e7f7e1267502254a9cb3
1 #include "a/regress_simple.h"
3 void a_regress_simple_init(a_regress_simple *ctx, a_float coef, a_float bias)
5 ctx->coef = coef;
6 ctx->bias = bias;
9 a_float a_regress_simple_eval(a_regress_simple const *ctx, a_float val)
11 return ctx->coef * val + ctx->bias;
14 a_float a_regress_simple_evar(a_regress_simple const *ctx, a_float val)
16 return (val - ctx->bias) / ctx->coef;
19 void a_regress_simple_olsm_(a_regress_simple *ctx, a_size n, a_float const *x, a_float const *y, a_float x_mean, a_float y_mean)
21 a_float num = 0, den = 0;
22 for (; n; --n)
24 a_float dy = *y++ - y_mean;
25 a_float dx = *x++ - x_mean;
26 num += dx * dy;
27 den += dx * dx;
29 ctx->coef = num / den;
30 ctx->bias = y_mean - ctx->coef * x_mean;
33 void a_regress_simple_olsm(a_regress_simple *ctx, a_size n, a_float const *x, a_float const *y)
35 a_float const x_mean = a_float_mean(x, n);
36 a_float const y_mean = a_float_mean(y, n);
37 a_regress_simple_olsm_(ctx, n, x, y, x_mean, y_mean);
40 void a_regress_simple_zero(a_regress_simple *ctx)
42 ctx->coef = 0;
43 ctx->bias = 0;