1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2006 Klaus Spanderen
5 Copyright (C) 2010 Slava Mazur
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include "linearleastsquaresregression.hpp"
22#include "utilities.hpp"
23#include <ql/math/randomnumbers/rngtraits.hpp>
24#include <ql/math/linearleastsquaresregression.hpp>
25#include <ql/functional.hpp>
26#include <boost/circular_buffer.hpp>
27
28using namespace QuantLib;
29using namespace boost::unit_test_framework;
30
31void LinearLeastSquaresRegressionTest::testRegression() {
32
33 BOOST_TEST_MESSAGE("Testing linear least-squares regression...");
34
35 const Real tolerance = 0.05;
36
37 const Size nr=100000;
38 PseudoRandom::rng_type rng(PseudoRandom::urng_type(1234U));
39
40 std::vector<ext::function<Real(Real)>> v = {
41 [](Real x) -> Real { return 1.0; },
42 [](Real x) -> Real { return x; },
43 [](Real x) -> Real { return x*x; },
44 [](Real x) -> Real { return std::sin(x: x); }
45 };
46
47 std::vector<ext::function<Real(Real)>> w(v);
48 w.emplace_back(args: [](Real x){ return x*x; });
49
50 for (Size k=0; k<3; ++k) {
51 Size i;
52 const Real a[] = {
53 rng.next().value,
54 rng.next().value,
55 rng.next().value,
56 rng.next().value
57 };
58
59 std::vector<Real> x(nr), y(nr);
60 for (i=0; i<nr; ++i) {
61 x[i] = rng.next().value;
62
63 // regression in y = a_1 + a_2*x + a_3*x^2 + a_4*sin(x) + eps
64 y[i] = a[0]*v[0](x[i]) + a[1]*v[1](x[i]) + a[2]*v[2](x[i])
65 + a[3]*v[3](x[i]) + rng.next().value;
66 }
67
68 LinearRegression m(x, y, v);
69
70 for (i=0; i<v.size(); ++i) {
71 if (m.standardErrors()[i] > tolerance) {
72 BOOST_ERROR("Failed to reproduce linear regression coef."
73 << "\n error: " << m.standardErrors()[i]
74 << "\n tolerance: " << tolerance);
75 }
76 if (std::fabs(x: m.coefficients()[i]-a[i]) > 3*m.standardErrors()[i]) {
77 BOOST_ERROR("Failed to reproduce linear regression coef."
78 << "\n calculated: " << m.coefficients()[i]
79 << "\n error: " << m.standardErrors()[i]
80 << "\n expected: " << a[i]);
81 }
82 }
83
84 m = LinearRegression(x, y, w);
85
86 const Real ma[] = {m.coefficients()[0], m.coefficients()[1],
87 m.coefficients()[2]+m.coefficients()[4],
88 m.coefficients()[3]};
89 const Real err[] = {m.standardErrors()[0], m.standardErrors()[1],
90 std::sqrt( x: m.standardErrors()[2]*m.standardErrors()[2]
91 +m.standardErrors()[4]*m.standardErrors()[4]),
92 m.standardErrors()[3]};
93 for (i=0; i<v.size(); ++i) {
94 if (std::fabs(x: ma[i] - a[i]) > 3*err[i]) {
95 BOOST_ERROR("Failed to reproduce linear regression coef."
96 << "\n calculated: " << ma[i]
97 << "\n error: " << err[i]
98 << "\n expected: " << a[i]);
99 }
100 }
101 }
102}
103
104namespace linear_least_square_regression_test {
105
106 struct get_item {
107 Size i;
108 explicit get_item(Size i) : i(i) {}
109 Real operator()(const Array& a) const {
110 return a[i];
111 }
112 };
113
114}
115
116void LinearLeastSquaresRegressionTest::testMultiDimRegression() {
117
118 BOOST_TEST_MESSAGE(
119 "Testing multi-dimensional linear least-squares regression...");
120
121 using namespace linear_least_square_regression_test;
122
123 const Size nr=100000;
124 const Size dims = 4;
125 const Real tolerance = 0.01;
126 PseudoRandom::rng_type rng(PseudoRandom::urng_type(1234U));
127
128 std::vector<ext::function<Real(Array)> > v;
129 v.emplace_back(args: [](const Array& x) { return 1.0; });
130 for (Size i=0; i < dims; ++i) {
131 v.emplace_back(args: get_item(i));
132 }
133
134 Array coeff(v.size());
135 for (Size i=0; i < v.size(); ++i) {
136 coeff[i] = rng.next().value;
137 }
138
139 std::vector<Real> y(nr, 0.0);
140 std::vector<Array> x(nr, Array(dims));
141 for (Size i=0; i < nr; ++i) {
142 for (Size j=0; j < dims; ++j) {
143 x[i][j] = rng.next().value;
144 }
145
146 for (Size j=0; j < v.size(); ++j) {
147 y[i] += coeff[j]*v[j](x[i]);
148 }
149 y[i] += rng.next().value;
150 }
151
152 LinearRegression m(x, y, v);
153
154 for (Size i=0; i < v.size(); ++i) {
155 if (m.standardErrors()[i] > tolerance) {
156 BOOST_ERROR("Failed to reproduce linear regression coef."
157 << "\n error: " << m.standardErrors()[i]
158 << "\n tolerance: " << tolerance);
159 }
160
161 if (std::fabs(x: m.coefficients()[i]-coeff[i]) > 3*tolerance) {
162 BOOST_ERROR("Failed to reproduce linear regression coef."
163 << "\n calculated: " << m.coefficients()[i]
164 << "\n error: " << m.standardErrors()[i]
165 << "\n expected: " << coeff[i]);
166 }
167 }
168
169 // much simpler
170 LinearRegression m1(x, y, Real(1.0));
171
172 for (Size i=0; i < m1.dim(); ++i) {
173 if (m1.standardErrors()[i] > tolerance) {
174 BOOST_ERROR("Failed to reproduce linear regression coef."
175 << "\n error: " << m1.standardErrors()[i]
176 << "\n tolerance: " << tolerance);
177 }
178
179 if (std::fabs(x: m1.coefficients()[i]-coeff[i]) > 3*tolerance) {
180 BOOST_ERROR("Failed to reproduce linear regression coef."
181 << "\n calculated: " << m1.coefficients()[i]
182 << "\n error: " << m1.standardErrors()[i]
183 << "\n expected: " << coeff[i]);
184 }
185 }
186}
187
188void LinearLeastSquaresRegressionTest::test1dLinearRegression() {
189
190 BOOST_TEST_MESSAGE("Testing 1D simple linear least-squares regression...");
191
192 /* Example taken from the QuantLib-User list, see posting
193 * Multiple linear regression/weighted regression, Boris Skorodumov */
194
195 std::vector<Real> x = {2.4, 1.8, 2.5, 3.0, 2.1, 1.2, 2.0, 2.7, 3.6};
196 std::vector<Real> y = {7.8, 5.5, 8.0, 9.0, 6.5, 4.0, 6.3, 8.4, 10.2};
197
198 std::vector<ext::function<Real(Real)>> v = {
199 [](Real x) { return 1.0; },
200 [](Real x) { return x; }
201 };
202
203 LinearRegression m(x, y);
204
205 const Real tol = 0.0002;
206 const Real coeffExpected[] = { 0.9448, 2.6853 };
207 const Real errorsExpected[] = { 0.3654, 0.1487 };
208
209 for (Size i=0; i < 2; ++i) {
210 if (std::fabs(x: m.standardErrors()[i]-errorsExpected[i]) > tol) {
211 BOOST_ERROR("Failed to reproduce linear regression standard errors"
212 << "\n calculated: " << m.standardErrors()[i]
213 << "\n expected: " << errorsExpected[i]
214 << "\n tolerance: " << tol);
215 }
216
217 if (std::fabs(x: m.coefficients()[i]-coeffExpected[i]) > tol) {
218 BOOST_ERROR("Failed to reproduce linear regression coef."
219 << "\n calculated: " << m.coefficients()[i]
220 << "\n expected: " << coeffExpected[i]
221 << "\n tolerance: " << tol);
222 }
223 }
224
225 // an alternative container type
226 boost::circular_buffer<Real> cx(x.begin(), x.end()), cy(y.begin(), y.end());
227 LinearRegression m1(cx, cy);
228
229 for (Size i=0; i < 2; ++i) {
230 if (std::fabs(x: m1.standardErrors()[i]-errorsExpected[i]) > tol) {
231 BOOST_ERROR("Failed to reproduce linear regression standard errors"
232 << "\n calculated: " << m1.standardErrors()[i]
233 << "\n expected: " << errorsExpected[i]
234 << "\n tolerance: " << tol);
235 }
236
237 if (std::fabs(x: m1.coefficients()[i]-coeffExpected[i]) > tol) {
238 BOOST_ERROR("Failed to reproduce linear regression coef."
239 << "\n calculated: " << m1.coefficients()[i]
240 << "\n expected: " << coeffExpected[i]
241 << "\n tolerance: " << tol);
242 }
243 }
244}
245
246
247test_suite* LinearLeastSquaresRegressionTest::suite() {
248 auto* suite = BOOST_TEST_SUITE("linear least squares regression tests");
249 suite->add(QUANTLIB_TEST_CASE(
250 &LinearLeastSquaresRegressionTest::testRegression));
251 suite->add(QUANTLIB_TEST_CASE(
252 &LinearLeastSquaresRegressionTest::testMultiDimRegression));
253 suite->add(QUANTLIB_TEST_CASE(
254 &LinearLeastSquaresRegressionTest::test1dLinearRegression));
255 return suite;
256}
257
258

source code of quantlib/test-suite/linearleastsquaresregression.cpp

Morty Proxy This is a proxified and sanitized view of the page, visit original site.