3 @brief linear regression
4 @details https://en.wikipedia.org/wiki/Linear_regression
7 #ifndef LIBA_REGRESS_LINEAR_H
8 #define LIBA_REGRESS_LINEAR_H
14 @addtogroup a_regress_linear linear regression
18 typedef struct a_regress_linear a_regress_linear
;
20 #if defined(__cplusplus)
22 #endif /* __cplusplus */
25 @brief initialize for linear regression
26 @param[in,out] ctx points to an instance of linear regression
27 @param[in] coef_p points to regression coefficients
28 @param[in] coef_n number of regression coefficients
29 @param[in] bias intercept
31 A_EXTERN
void a_regress_linear_init(a_regress_linear
*ctx
, a_float
*coef_p
, a_size coef_n
, a_float bias
);
34 @brief calculate predicted value for linear regression
35 @param[in] ctx points to an instance of linear regression
36 @param[in] val independent variables
37 @return predicted value
39 A_EXTERN a_float
a_regress_linear_eval(a_regress_linear
const *ctx
, a_float
const *val
);
42 @brief calculate residuals for linear regression
43 @param[in] ctx points to an instance of linear regression
44 @param[in] n number of samples, x[n*coef_n], y[n], err[n]
45 @param[in] x predictor data, specified as a numeric matrix
46 @param[in] y response data, specified as a numeric vector
47 @param[in,out] err residuals, specified as a numeric vector
49 A_EXTERN
void a_regress_linear_err(a_regress_linear
const *ctx
, a_size n
, a_float
const *x
, a_float
const *y
, a_float
*err
);
52 @brief calculate prediction deviation from mean for linear regression
53 @param[in] ctx points to an instance of linear regression
54 @param[in] n number of samples, x[n*coef_n], pdm[n]
55 @param[in] x predictor data, specified as a numeric matrix
56 @param[in,out] pdm deviation, specified as a numeric vector
57 @param[in] y_mean mean of response data
59 A_EXTERN
void a_regress_linear_pdm(a_regress_linear
const *ctx
, a_size n
, a_float
const *x
, a_float
*pdm
, a_float y_mean
);
62 @brief gradient descent for linear regression
63 @param[in,out] ctx points to an instance of linear regression
64 @param[in] input predictor data, specified as a numeric vector
65 @param[in] error residual, specified as a numeric scalar
66 @param[in] alpha learning rate for gradient descent
68 A_EXTERN
void a_regress_linear_gd(a_regress_linear
*ctx
, a_float
const *input
, a_float error
, a_float alpha
);
71 @brief stochastic gradient descent for linear regression
72 @param[in,out] ctx points to an instance of linear regression
73 @param[in] n number of samples, x[n*coef_n], y[n]
74 @param[in] x predictor data, specified as a numeric matrix
75 @param[in] y response data, specified as a numeric vector
76 @param[in] alpha learning rate for gradient descent
78 A_EXTERN
void a_regress_linear_sgd(a_regress_linear
*ctx
, a_size n
, a_float
const *x
, a_float
const *y
, a_float alpha
);
81 @brief batch gradient descent for linear regression
82 @param[in,out] ctx points to an instance of linear regression
83 @param[in] n number of samples, x[n*coef_n], err[n]
84 @param[in] x predictor data, specified as a numeric matrix
85 @param[in] err residuals, specified as a numeric vector
86 @param[in] alpha learning rate for gradient descent
88 A_EXTERN
void a_regress_linear_bgd(a_regress_linear
*ctx
, a_size n
, a_float
const *x
, a_float
const *err
, a_float alpha
);
91 @brief mini-batch gradient descent for linear regression
92 @param[in,out] ctx points to an instance of linear regression
93 @param[in] n number of samples, x[n*coef_n], y[n], err[n]
94 @param[in] x predictor data, specified as a numeric matrix
95 @param[in] y response data, specified as a numeric vector
96 @param[in,out] err residuals, specified as a numeric vector
97 @param[in] delta threshold for gradient descent value
98 @param[in] lrmax maximum learning rate of iterations
99 @param[in] lrmin minimum learning rate of iterations
100 @param[in] lrtim total number of learning rate steps
101 @param[in] epoch maximum number of epochs
102 @param[in] batch batch size of data
103 @return change in loss function
105 A_EXTERN a_float
a_regress_linear_mgd(a_regress_linear
*ctx
, a_size n
, a_float
const *x
, a_float
const *y
, a_float
*err
,
106 a_float delta
, a_float lrmax
, a_float lrmin
, a_size lrtim
, a_size epoch
, a_size batch
);
109 @brief zeroing for linear regression
110 @param[in,out] ctx points to an instance of linear regression
112 A_EXTERN
void a_regress_linear_zero(a_regress_linear
*ctx
);
114 #if defined(__cplusplus)
118 typedef struct a_regress_linear regress_linear
;
120 #endif /* __cplusplus */
123 @brief instance structure for linear regression
125 struct a_regress_linear
127 a_float
*coef_p
; /*!< points to regression coefficients */
128 a_size coef_n
; /*!< number of regression coefficients */
129 a_float bias
; /*!< intercept */
130 #if defined(__cplusplus)
131 A_INLINE
void init(a_float
*p
, a_size n
, a_float b
= 0)
133 a_regress_linear_init(this, p
, n
, b
);
135 A_INLINE a_float
eval(a_float
const *val
) const
137 return a_regress_linear_eval(this, val
);
139 A_INLINE
void err(a_size n
, a_float
const *x
, a_float
const *y
, a_float
*err
) const
141 a_regress_linear_err(this, n
, x
, y
, err
);
143 A_INLINE
void pdm(a_size n
, a_float
const *x
, a_float
*pdm
, a_float y_mean
) const
145 a_regress_linear_pdm(this, n
, x
, pdm
, y_mean
);
147 A_INLINE
void gd(a_float
const *input
, a_float error
, a_float alpha
)
149 a_regress_linear_gd(this, input
, error
, alpha
);
151 A_INLINE
void sgd(a_size n
, a_float
const *x
, a_float
const *y
, a_float alpha
)
153 a_regress_linear_sgd(this, n
, x
, y
, alpha
);
155 A_INLINE
void bgd(a_size n
, a_float
const *x
, a_float
const *err
, a_float alpha
)
157 a_regress_linear_bgd(this, n
, x
, err
, alpha
);
159 A_INLINE a_float
mgd(a_size n
, a_float
const *x
, a_float
const *y
, a_float
*err
, a_float delta
,
160 a_float lrmax
, a_float lrmin
, a_size lrtim
, a_size epoch
, a_size batch
)
162 return a_regress_linear_mgd(this, n
, x
, y
, err
, delta
, lrmax
, lrmin
, lrtim
, epoch
, batch
);
164 A_INLINE
void zero() { a_regress_linear_zero(this); }
165 #endif /* __cplusplus */
168 /*! @} a_regress_linear */
170 #endif /* a/regress_linear.h */