3 @brief simple linear regression
4 @details https://en.wikipedia.org/wiki/Simple_linear_regression
7 #ifndef LIBA_REGRESS_SIMPLE_H
8 #define LIBA_REGRESS_SIMPLE_H
14 @addtogroup a_regress_simple simple linear regression
18 typedef struct a_regress_simple a_regress_simple
;
20 #if defined(__cplusplus)
22 #endif /* __cplusplus */
25 @brief initialize for simple linear regression
26 @param[in,out] ctx points to an instance of simple linear regression
27 @param[in] coef regression coefficient
28 @param[in] bias intercept
30 A_EXTERN
void a_regress_simple_init(a_regress_simple
*ctx
, a_float coef
, a_float bias
);
33 @brief calculate predicted value for simple linear regression
34 @param[in] ctx points to an instance of simple linear regression
35 @param[in] val independent variable
36 @return predicted value
38 A_EXTERN a_float
a_regress_simple_eval(a_regress_simple
const *ctx
, a_float val
);
41 @brief calculate predicted value for simple linear regression
42 @param[in] ctx points to an instance of simple linear regression
43 @param[in] val dependent variable
44 @return predicted value
46 A_EXTERN a_float
a_regress_simple_evar(a_regress_simple
const *ctx
, a_float val
);
49 @brief ordinary least squares for simple linear regression
50 @param[in,out] ctx points to an instance of simple linear regression
51 @param[in] n number of samples, x[n], y[n]
52 @param[in] x predictor data, specified as a numeric vector
53 @param[in] y response data, specified as a numeric vector
54 @param[in] x_mean mean of predictor data
55 @param[in] y_mean mean of response data
57 A_EXTERN
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
);
60 @brief ordinary least squares for simple linear regression
61 @param[in,out] ctx points to an instance of simple linear regression
62 @param[in] n number of samples, x[n], y[n]
63 @param[in] x predictor data, specified as a numeric vector
64 @param[in] y response data, specified as a numeric vector
65 @param[in] x_mean mean of predictor data
67 A_EXTERN
void a_regress_simple_olsx(a_regress_simple
*ctx
, a_size n
, a_float
const *x
, a_float
const *y
, a_float x_mean
);
70 @brief ordinary least squares for simple linear regression
71 @param[in,out] ctx points to an instance of simple linear regression
72 @param[in] n number of samples, x[n], y[n]
73 @param[in] x predictor data, specified as a numeric vector
74 @param[in] y response data, specified as a numeric vector
75 @param[in] y_mean mean of response data
77 A_EXTERN
void a_regress_simple_olsy(a_regress_simple
*ctx
, a_size n
, a_float
const *x
, a_float
const *y
, a_float y_mean
);
80 @brief ordinary least squares for simple linear regression
81 @param[in,out] ctx points to an instance of simple linear regression
82 @param[in] n number of samples, x[n], y[n]
83 @param[in] x predictor data, specified as a numeric vector
84 @param[in] y response data, specified as a numeric vector
86 A_EXTERN
void a_regress_simple_ols(a_regress_simple
*ctx
, a_size n
, a_float
const *x
, a_float
const *y
);
89 @brief zeroing for simple linear regression
90 @param[in,out] ctx points to an instance of simple linear regression
92 A_EXTERN
void a_regress_simple_zero(a_regress_simple
*ctx
);
94 #if defined(__cplusplus)
98 typedef struct a_regress_simple regress_simple
;
100 #endif /* __cplusplus */
103 @brief instance structure for simple linear regression
105 struct a_regress_simple
107 a_float coef
; /*!< regression coefficient */
108 a_float bias
; /*!< intercept */
109 #if defined(__cplusplus)
110 A_INLINE
void init(a_float a
= 1, a_float b
= 0)
112 a_regress_simple_init(this, a
, b
);
114 A_INLINE a_float
eval(a_float val
) const
116 return a_regress_simple_eval(this, val
);
118 A_INLINE a_float
evar(a_float val
) const
120 return a_regress_simple_evar(this, val
);
122 A_INLINE
void ols(a_size n
, a_float
const *x
, a_float
const *y
, a_float x_mean
, a_float y_mean
)
124 a_regress_simple_ols_(this, n
, x
, y
, x_mean
, y_mean
);
126 A_INLINE
void olsx(a_size n
, a_float
const *x
, a_float
const *y
, a_float x_mean
)
128 a_regress_simple_olsx(this, n
, x
, y
, x_mean
);
130 A_INLINE
void olsy(a_size n
, a_float
const *x
, a_float
const *y
, a_float y_mean
)
132 a_regress_simple_olsy(this, n
, x
, y
, y_mean
);
134 A_INLINE
void ols(a_size n
, a_float
const *x
, a_float
const *y
)
136 a_regress_simple_ols(this, n
, x
, y
);
138 A_INLINE
void zero() { a_regress_simple_zero(this); }
139 #endif /* __cplusplus */
142 /*! @} a_regress_simple */
144 #endif /* a/regress_simple.h */