release 0.1.15
[liba.git] / src / regress_simple.c
blobf43de34597e82a804967becf1eb3a288a6a76b28
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_ols_(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_olsx(a_regress_simple *ctx, a_size n, a_float const *x, a_float const *y, a_float x_mean)
35 a_float const y_mean = a_float_mean(y, n);
36 a_regress_simple_ols_(ctx, n, x, y, x_mean, y_mean);
39 void a_regress_simple_olsy(a_regress_simple *ctx, a_size n, a_float const *x, a_float const *y, a_float y_mean)
41 a_float const x_mean = a_float_mean(x, n);
42 a_regress_simple_ols_(ctx, n, x, y, x_mean, y_mean);
45 void a_regress_simple_ols(a_regress_simple *ctx, a_size n, a_float const *x, a_float const *y)
47 a_float const x_mean = a_float_mean(x, n);
48 a_float const y_mean = a_float_mean(y, n);
49 a_regress_simple_ols_(ctx, n, x, y, x_mean, y_mean);
52 void a_regress_simple_zero(a_regress_simple *ctx)
54 ctx->coef = 0;
55 ctx->bias = 0;