15 template <
typename It>
17 const auto n = std::distance(begin, end);
20 throw std::runtime_error(
21 "Empty range passed to simple linear regression.");
25 std::accumulate(begin, end, 0.0, [](
auto running_total,
auto i) {
26 return running_total + i.x;
29 std::accumulate(begin, end, 0.0, [](
auto running_total,
auto i) {
30 return running_total + i.y;
33 std::accumulate(begin, end, 0.0, [](
auto running_total,
auto i) {
34 return running_total + i.x * i.x;
37 std::accumulate(begin, end, 0.0, [](
auto running_total,
auto i) {
38 return running_total + i.x * i.y;
41 std::accumulate(begin, end, 0.0, [](
auto running_total,
auto i) {
42 return running_total + i.y * i.y;
45 const auto denominator = n * sxx - sx * sx;
46 if (denominator == 0.0) {
47 throw std::runtime_error(
48 "Linear regression estimated a denominator of 0");
51 const auto numerator = n * sxy - sx * sy;
52 const auto m = numerator / denominator;
53 const auto c = sy / n - m * sx / n;
55 numerator / std::sqrt((n * sxx - sx * sx) * (n * syy - sy * sy));
double c
gradient of regression line
Definition: linear_regression.h:11
Definition: capsule_base.h:9
Definition: linear_regression.h:9
double r
y intercept
Definition: linear_regression.h:12