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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/*
* Copyright Nick Thompson, 2019
* Copyright Matt Borland, 2021
* 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 <utility>
#include <tuple>
#include <algorithm>
#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];
}
std::pair<Real, Real> temp = simple_ordinary_least_squares(x, y);
Real computed_c0 = std::get<0>(temp);
Real computed_c1 = std::get<1>(temp);
CHECK_ULP_CLOSE(expected_c0, computed_c0, 0);
CHECK_ULP_CLOSE(expected_c1, computed_c1, 0);
std::tuple<Real, Real, Real> temp_with_R = simple_ordinary_least_squares_with_R_squared(x, y);
Real computed_c0_R = std::get<0>(temp_with_R);
Real computed_c1_R = std::get<1>(temp_with_R);
Real Rsquared = std::get<2>(temp_with_R);
Real expected_Rsquared = 1;
CHECK_ULP_CLOSE(expected_c0, computed_c0_R, 0);
CHECK_ULP_CLOSE(expected_c1, computed_c1_R, 0);
CHECK_ULP_CLOSE(expected_Rsquared, Rsquared, 0);
}
template<typename Z>
void test_integer_line()
{
std::vector<Z> x(128);
std::vector<Z> y(128);
double expected_c0 = 7;
double expected_c1 = 12;
for (size_t i = 0; i < x.size(); ++i) {
x[i] = i;
y[i] = expected_c0 + expected_c1*x[i];
}
std::pair<double, double> temp = simple_ordinary_least_squares(x, y);
double computed_c0 = std::get<0>(temp);
double computed_c1 = std::get<1>(temp);
CHECK_ULP_CLOSE(expected_c0, computed_c0, 0);
CHECK_ULP_CLOSE(expected_c1, computed_c1, 0);
std::tuple<double, double, double> temp_with_R = simple_ordinary_least_squares_with_R_squared(x, y);
double computed_c0_R = std::get<0>(temp_with_R);
double computed_c1_R = std::get<1>(temp_with_R);
double Rsquared = std::get<2>(temp_with_R);
double expected_Rsquared = 1;
CHECK_ULP_CLOSE(expected_c0, computed_c0_R, 0);
CHECK_ULP_CLOSE(expected_c1, computed_c1_R, 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];
}
std::pair<Real, Real> temp = simple_ordinary_least_squares(x, y);
Real computed_c0 = std::get<0>(temp);
Real computed_c1 = std::get<1>(temp);
CHECK_ULP_CLOSE(expected_c0, computed_c0, 0);
CHECK_ULP_CLOSE(expected_c1, computed_c1, 0);
std::tuple<Real, Real, Real> temp_with_R = simple_ordinary_least_squares_with_R_squared(x, y);
Real computed_c0_R = std::get<0>(temp_with_R);
Real computed_c1_R = std::get<1>(temp_with_R);
Real Rsquared = std::get<2>(temp_with_R);
Real expected_Rsquared = 1;
CHECK_ULP_CLOSE(expected_c0, computed_c0_R, 0);
CHECK_ULP_CLOSE(expected_c1, computed_c1_R, 0);
CHECK_ULP_CLOSE(expected_Rsquared, Rsquared, 0);
}
template<typename Z>
void test_integer_constant()
{
std::vector<Z> x(128);
std::vector<Z> y(128);
double expected_c0 = 7;
double expected_c1 = 0;
for (size_t i = 0; i < x.size(); ++i) {
x[i] = i;
y[i] = expected_c0 + expected_c1*x[i];
}
std::pair<double, double> temp = simple_ordinary_least_squares(x, y);
double computed_c0 = std::get<0>(temp);
double computed_c1 = std::get<1>(temp);
CHECK_ULP_CLOSE(expected_c0, computed_c0, 0);
CHECK_ULP_CLOSE(expected_c1, computed_c1, 0);
std::tuple<double, double, double> temp_with_R = simple_ordinary_least_squares_with_R_squared(x, y);
double computed_c0_R = std::get<0>(temp_with_R);
double computed_c1_R = std::get<1>(temp_with_R);
double Rsquared = std::get<2>(temp_with_R);
double expected_Rsquared = 1;
CHECK_ULP_CLOSE(expected_c0, computed_c0_R, 0);
CHECK_ULP_CLOSE(expected_c1, computed_c1_R, 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);
}
std::tuple<Real, Real, Real> temp = simple_ordinary_least_squares_with_R_squared(x, y);
Real c0 = std::get<0>(temp);
Real c1 = std::get<1>(temp);
Real Rsquared = std::get<2>(temp);
CHECK_MOLLIFIED_CLOSE(expected_c0, c0, 0.003);
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);
std::tuple<Real, Real, Real> temp_ = simple_ordinary_least_squares_with_R_squared(x, y);
Real c0_ = std::get<0>(temp_);
Real c1_ = std::get<1>(temp_);
Real Rsquared_ = std::get<2>(temp_);
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);
}
std::tuple<Real, Real, Real> temp = simple_ordinary_least_squares_with_R_squared(x, y);
Real c0 = std::get<0>(temp);
Real c1 = std::get<1>(temp);
Real Rsquared = std::get<2>(temp);
CHECK_MOLLIFIED_CLOSE(expected_c0, c0, 0.006);
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;
}
temp = simple_ordinary_least_squares_with_R_squared(x, y);
Real c0_lambda = std::get<0>(temp);
Real c1_lambda = std::get<1>(temp);
Real Rsquared_lambda = std::get<2>(temp);
CHECK_ULP_CLOSE(lambda*c0, c0_lambda, 70);
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;
}
temp = simple_ordinary_least_squares_with_R_squared(x, y);
Real c0_ = std::get<0>(temp);
Real c1_ = std::get<1>(temp);
Real Rsquared_ = std::get<2>(temp);
CHECK_ULP_CLOSE(c0, c0_, 100);
CHECK_ULP_CLOSE(c1, c1_*lambda, 50);
CHECK_ULP_CLOSE(Rsquared, Rsquared_, 50);
}
int main()
{
test_line<float>();
test_line<double>();
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
test_line<long double>();
#endif
test_integer_line<int>();
test_integer_line<int32_t>();
test_integer_line<int64_t>();
test_integer_line<uint32_t>();
test_constant<float>();
test_constant<double>();
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
test_constant<long double>();
#endif
test_integer_constant<int>();
test_integer_constant<int32_t>();
test_integer_constant<int64_t>();
test_integer_constant<uint32_t>();
test_permutation_invariance<float>();
test_permutation_invariance<double>();
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
test_permutation_invariance<long double>();
#endif
test_scaling_relations<float>();
test_scaling_relations<double>();
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
test_scaling_relations<long double>();
#endif
return boost::math::test::report_errors();
}
|