1 #ifndef GENESIS_UTILS_MATH_REGRESSION_SLR_H_
2 #define GENESIS_UTILS_MATH_REGRESSION_SLR_H_
59 double y(
double x )
const
73 template <
class ForwardIteratorA,
class ForwardIteratorB>
75 ForwardIteratorA first_x, ForwardIteratorA last_x,
76 ForwardIteratorB first_y, ForwardIteratorB last_y
88 return { std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN() };
91 mean_x /=
static_cast<double>( count );
92 mean_y /=
static_cast<double>( count );
93 assert( std::isfinite( mean_x ));
94 assert( std::isfinite( mean_y ));
97 double covariance = 0.0;
98 double variance_x = 0.0;
100 double const dx = val_x - mean_x;
101 double const dy = val_y - mean_y;
102 covariance += dx * dy;
103 variance_x += dx * dx;
105 assert( std::isfinite( covariance ));
106 assert( std::isfinite( variance_x ));
110 result.
slope = covariance / variance_x;
124 template <
class ForwardIteratorA,
class ForwardIteratorB>
126 ForwardIteratorA first_x, ForwardIteratorA last_x,
127 ForwardIteratorB first_y, ForwardIteratorB last_y,
134 double const e = val_y - lin_fct.
y( val_x );
140 assert( error == 0.0 );
143 return error /
static_cast<double>( count );
154 template <
class ForwardIteratorA,
class ForwardIteratorB>
156 ForwardIteratorA first_x, ForwardIteratorA last_x,
157 ForwardIteratorB first_y, ForwardIteratorB last_y,
169 y_mean /=
static_cast<double>( count );
183 double const y_hat = lin_fct.
y( x_val );
185 double const d_err = y_val - y_hat;
186 double const d_tot = y_val - y_mean;
189 ss_err += d_err * d_err;
190 ss_tot += d_tot * d_tot;
194 auto const fvu = ( ss_err / ss_tot );
196 assert( 0.0 <= fvu && fvu <= 1.0 );
203 #endif // include guard