1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*!
@file regress_simple.h
@brief simple linear regression
@details https://en.wikipedia.org/wiki/Simple_linear_regression
*/
#ifndef LIBA_REGRESS_SIMPLE_H
#define LIBA_REGRESS_SIMPLE_H
#include "a.h"
/*!
@ingroup a_regress
@addtogroup a_regress_simple simple linear regression
@{
*/
typedef struct a_regress_simple a_regress_simple;
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
/*!
@brief initialize for simple linear regression
@param[in,out] ctx points to an instance of simple linear regression
@param[in] coef regression coefficient
@param[in] bias intercept
*/
A_EXTERN void a_regress_simple_init(a_regress_simple *ctx, a_float coef, a_float bias);
/*!
@brief calculate predicted value for simple linear regression
@param[in] ctx points to an instance of simple linear regression
@param[in] val independent variable
@return predicted value
*/
A_EXTERN a_float a_regress_simple_eval(a_regress_simple const *ctx, a_float val);
/*!
@brief calculate predicted value for simple linear regression
@param[in] ctx points to an instance of simple linear regression
@param[in] val dependent variable
@return predicted value
*/
A_EXTERN a_float a_regress_simple_evar(a_regress_simple const *ctx, a_float val);
/*!
@brief ordinary least squares for simple linear regression
@param[in,out] ctx points to an instance of simple linear regression
@param[in] n number of samples, x[n], y[n]
@param[in] x predictor data, specified as a numeric vector
@param[in] y response data, specified as a numeric vector
@param[in] x_mean mean of predictor data
@param[in] y_mean mean of response data
*/
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);
/*!
@brief ordinary least squares for simple linear regression
@param[in,out] ctx points to an instance of simple linear regression
@param[in] n number of samples, x[n], y[n]
@param[in] x predictor data, specified as a numeric vector
@param[in] y response data, specified as a numeric vector
@param[in] x_mean mean of predictor data
*/
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);
/*!
@brief ordinary least squares for simple linear regression
@param[in,out] ctx points to an instance of simple linear regression
@param[in] n number of samples, x[n], y[n]
@param[in] x predictor data, specified as a numeric vector
@param[in] y response data, specified as a numeric vector
@param[in] y_mean mean of response data
*/
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);
/*!
@brief ordinary least squares for simple linear regression
@param[in,out] ctx points to an instance of simple linear regression
@param[in] n number of samples, x[n], y[n]
@param[in] x predictor data, specified as a numeric vector
@param[in] y response data, specified as a numeric vector
*/
A_EXTERN void a_regress_simple_ols(a_regress_simple *ctx, a_size n, a_float const *x, a_float const *y);
/*!
@brief zeroing for simple linear regression
@param[in,out] ctx points to an instance of simple linear regression
*/
A_EXTERN void a_regress_simple_zero(a_regress_simple *ctx);
#if defined(__cplusplus)
} /* extern "C" */
namespace a
{
typedef struct a_regress_simple regress_simple;
} /* namespace a */
#endif /* __cplusplus */
/*!
@brief instance structure for simple linear regression
*/
struct a_regress_simple
{
a_float coef; // regression coefficient
a_float bias; // intercept
#if defined(__cplusplus)
A_INLINE void init(a_float a = 1, a_float b = 0)
{
a_regress_simple_init(this, a, b);
}
A_INLINE a_float eval(a_float val) const
{
return a_regress_simple_eval(this, val);
}
A_INLINE a_float evar(a_float val) const
{
return a_regress_simple_evar(this, val);
}
A_INLINE void ols(a_size n, a_float const *x, a_float const *y, a_float x_mean, a_float y_mean)
{
a_regress_simple_ols_(this, n, x, y, x_mean, y_mean);
}
A_INLINE void olsx(a_size n, a_float const *x, a_float const *y, a_float x_mean)
{
a_regress_simple_olsx(this, n, x, y, x_mean);
}
A_INLINE void olsy(a_size n, a_float const *x, a_float const *y, a_float y_mean)
{
a_regress_simple_olsy(this, n, x, y, y_mean);
}
A_INLINE void ols(a_size n, a_float const *x, a_float const *y)
{
a_regress_simple_ols(this, n, x, y);
}
A_INLINE void zero() { a_regress_simple_zero(this); }
#endif /* __cplusplus */
};
/*! @} a_regress_simple */
#endif /* a/regress_simple.h */