/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* Copyright (C) 2006 Klaus Spanderen This file is part of QuantLib, a free-software/open-source library for financial quantitative analysts and developers - http://quantlib.org/ QuantLib is free software: you can redistribute it and/or modify it under the terms of the QuantLib license. You should have received a copy of the license along with this program; if not, please email . The license is also available online at . This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details. */ #include "linearleastsquaresregression.hpp" #include "utilities.hpp" #include #include #include using namespace QuantLib; using namespace boost::unit_test_framework; QL_BEGIN_TEST_LOCALS(LinearLeastSquaresRegressionTest) void teardown() { Settings::instance().evaluationDate() = Date(); } QL_END_TEST_LOCALS(LinearLeastSquaresRegressionTest) void LinearLeastSquaresRegressionTest::testRegression() { BOOST_MESSAGE("Testing linear least-squares regression..."); QL_TEST_BEGIN const Real tolerance = 0.025; const Size nr=100000; PseudoRandom::rng_type rng(MersenneTwisterUniformRng(1234u)); std::vector > v; v.push_back(constant(1.0)); v.push_back(identity()); v.push_back(square()); v.push_back(std::ptr_fun(std::sin)); std::vector > w(v); w.push_back(square()); for (Size k=0; k<3; ++k) { Size i; const Real a[] = {rng.next().value, rng.next().value, rng.next().value, rng.next().value}; std::vector x(nr), y(nr); for (i=0; i m(x, y, v); for (i=0; i tolerance) { BOOST_ERROR("Failed to reproduce linear regression coef." << "\n error: " << m.err()[i] << "\n tolerance: " << tolerance); } if (std::fabs(m.a()[i]-a[i]) > 3*m.err()[i]) { BOOST_ERROR("Failed to reproduce linear regression coef." << "\n calculated: " << m.a()[i] << "\n error: " << m.err()[i] << "\n expected: " << a[i]); } } m = LinearLeastSquaresRegression<>(x, y, w); const Real ma[] = {m.a()[0], m.a()[1], m.a()[2]+m.a()[4],m.a()[3]}; const Real err[] = {m.err()[0], m.err()[1], std::sqrt( m.err()[2]*m.err()[2] +m.err()[4]*m.err()[4]), m.err()[3]}; for (i=0; i 3*err[i]) { BOOST_ERROR("Failed to reproduce linear regression coef." << "\n calculated: " << ma[i] << "\n error: " << err[i] << "\n expected: " << a[i]); } } } QL_TEST_TEARDOWN } test_suite* LinearLeastSquaresRegressionTest::suite() { test_suite* suite = BOOST_TEST_SUITE("linear least squares regression tests"); suite->add(BOOST_TEST_CASE( &LinearLeastSquaresRegressionTest::testRegression)); return suite; }