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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/*
* Copyright Nick Thompson, 2019
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#include "math_unit_test.hpp"
#include <vector>
#include <random>
#include <boost/math/statistics/linear_regression.hpp>
using boost::math::statistics::simple_ordinary_least_squares;
using boost::math::statistics::simple_ordinary_least_squares_with_R_squared;
template<typename Real>
void test_line()
{
std::vector<Real> x(128);
std::vector<Real> y(128);
Real expected_c0 = 7;
Real expected_c1 = 12;
for (size_t i = 0; i < x.size(); ++i) {
x[i] = i;
y[i] = expected_c0 + expected_c1*x[i];
}
auto [computed_c0, computed_c1] = simple_ordinary_least_squares(x, y);
CHECK_ULP_CLOSE(expected_c0, computed_c0, 0);
CHECK_ULP_CLOSE(expected_c1, computed_c1, 0);
auto [computed_c0_R, computed_c1_R, Rsquared] = simple_ordinary_least_squares_with_R_squared(x, y);
Real expected_Rsquared = 1;
CHECK_ULP_CLOSE(expected_c0, computed_c0, 0);
CHECK_ULP_CLOSE(expected_c1, computed_c1, 0);
CHECK_ULP_CLOSE(expected_Rsquared, Rsquared, 0);
}
template<typename Real>
void test_constant()
{
std::vector<Real> x(128);
std::vector<Real> y(128);
Real expected_c0 = 7;
Real expected_c1 = 0;
for (size_t i = 0; i < x.size(); ++i) {
x[i] = i;
y[i] = expected_c0 + expected_c1*x[i];
}
auto [computed_c0, computed_c1] = simple_ordinary_least_squares(x, y);
CHECK_ULP_CLOSE(expected_c0, computed_c0, 0);
CHECK_ULP_CLOSE(expected_c1, computed_c1, 0);
auto [computed_c0_R, computed_c1_R, Rsquared] = simple_ordinary_least_squares_with_R_squared(x, y);
Real expected_Rsquared = 1;
CHECK_ULP_CLOSE(expected_c0, computed_c0, 0);
CHECK_ULP_CLOSE(expected_c1, computed_c1, 0);
CHECK_ULP_CLOSE(expected_Rsquared, Rsquared, 0);
}
template<typename Real>
void test_permutation_invariance()
{
std::vector<Real> x(256);
std::vector<Real> y(256);
std::mt19937_64 gen{123456};
std::normal_distribution<Real> dis(0, 0.1);
Real expected_c0 = -7.2;
Real expected_c1 = -13.5;
x[0] = 0;
y[0] = expected_c0 + dis(gen);
for(size_t i = 1; i < x.size(); ++i) {
Real t = dis(gen);
x[i] = x[i-1] + t*t;
y[i] = expected_c0 + expected_c1*x[i] + dis(gen);
}
auto [c0, c1, Rsquared] = simple_ordinary_least_squares_with_R_squared(x, y);
CHECK_MOLLIFIED_CLOSE(expected_c0, c0, 0.002);
CHECK_MOLLIFIED_CLOSE(expected_c1, c1, 0.002);
int j = 0;
std::mt19937_64 gen1{12345};
std::mt19937_64 gen2{12345};
while(j++ < 10) {
std::shuffle(x.begin(), x.end(), gen1);
std::shuffle(y.begin(), y.end(), gen2);
auto [c0_, c1_, Rsquared_] = simple_ordinary_least_squares_with_R_squared(x, y);
CHECK_ULP_CLOSE(c0, c0_, 100);
CHECK_ULP_CLOSE(c1, c1_, 100);
CHECK_ULP_CLOSE(Rsquared, Rsquared_, 65);
}
}
template<typename Real>
void test_scaling_relations()
{
std::vector<Real> x(256);
std::vector<Real> y(256);
std::mt19937_64 gen{123456};
std::normal_distribution<Real> dis(0, 0.1);
Real expected_c0 = 3.2;
Real expected_c1 = -13.5;
x[0] = 0;
y[0] = expected_c0 + dis(gen);
for(size_t i = 1; i < x.size(); ++i) {
Real t = dis(gen);
x[i] = x[i-1] + t*t;
y[i] = expected_c0 + expected_c1*x[i] + dis(gen);
}
auto [c0, c1, Rsquared] = simple_ordinary_least_squares_with_R_squared(x, y);
CHECK_MOLLIFIED_CLOSE(expected_c0, c0, 0.005);
CHECK_MOLLIFIED_CLOSE(expected_c1, c1, 0.005);
// If y -> lambda y, then c0 -> lambda c0 and c1 -> lambda c1.
Real lambda = 6;
for (auto& s : y) {
s *= lambda;
}
auto [c0_lambda, c1_lambda, Rsquared_lambda] = simple_ordinary_least_squares_with_R_squared(x, y);
CHECK_ULP_CLOSE(lambda*c0, c0_lambda, 30);
CHECK_ULP_CLOSE(lambda*c1, c1_lambda, 30);
CHECK_ULP_CLOSE(Rsquared, Rsquared_lambda, 3);
// If x -> lambda x, then c0 -> c0 and c1 -> c1/lambda
for (auto& s : x) {
s *= lambda;
}
// Put y back into it's original state:
for (auto& s : y) {
s /= lambda;
}
auto [c0_, c1_, Rsquared_] = simple_ordinary_least_squares_with_R_squared(x, y);
CHECK_ULP_CLOSE(c0, c0_, 50);
CHECK_ULP_CLOSE(c1, c1_*lambda, 50);
CHECK_ULP_CLOSE(Rsquared, Rsquared_, 50);
}
int main()
{
test_line<float>();
test_line<double>();
test_line<long double>();
test_constant<float>();
test_constant<double>();
test_constant<long double>();
test_permutation_invariance<float>();
test_permutation_invariance<double>();
test_permutation_invariance<long double>();
test_scaling_relations<float>();
test_scaling_relations<double>();
test_scaling_relations<long double>();
return boost::math::test::report_errors();
}
|