/usr/include/ql/math/linearleastsquaresregression.hpp is in libquantlib0-dev 1.1-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2009 Dirk Eddelbuettel
Copyright (C) 2006, 2009 Klaus Spanderen
Copyright (C) 2010 Kakhkhor Abdijalilov
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
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
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.
*/
/*! \file linearleastsquaresregression.hpp
\brief general linear least square regression
*/
#ifndef quantlib_linear_least_squares_regression_hpp
#define quantlib_linear_least_squares_regression_hpp
#include <ql/qldefines.hpp>
#include <ql/math/matrixutilities/svd.hpp>
#include <ql/math/array.hpp>
#include <ql/math/functional.hpp>
#include <boost/function.hpp>
#include <vector>
namespace QuantLib {
//! general linear least squares regression
/*! References:
"Numerical Recipes in C", 2nd edition,
Press, Teukolsky, Vetterling, Flannery,
\test the correctness of the returned values is tested by
checking their properties.
*/
template <class ArgumentType = Real>
class LinearLeastSquaresRegression {
public:
LinearLeastSquaresRegression(
const std::vector<ArgumentType> & x,
const std::vector<Real> & y,
const std::vector<boost::function1<Real, ArgumentType> > & v);
const Array& coefficients() const { return a_; }
const Array& residuals() const { return residuals_; }
//! standard parameter errors as given by Excel, R etc.
const Array& standardErrors() const { return standardErrors_; }
//! modeling uncertainty as definied in Numerical Recipes
const Array& error() const { return err_;}
#ifndef QL_DISABLE_DEPRECATED
const Array& a() const { return a_; }
#endif
private:
Array a_, err_, residuals_, standardErrors_;
};
//! linear regression y_i = a_0 + a_1*x_0 +..+a_n*x_{n-1} + eps
class LinearRegression {
public:
//! one dimensional linear regression
LinearRegression(const std::vector<Real>& x,
const std::vector<Real>& y);
//! multi dimensional linear regression
LinearRegression(const std::vector<std::vector<Real> >& x,
const std::vector<Real>& y);
//! returns paramters {a_0, a_1, ..., a_n}
const Array& coefficients() const { return reg_.coefficients(); }
const Array& residuals() const { return reg_.residuals(); }
const Array& standardErrors() const { return reg_.standardErrors(); }
private:
LinearLeastSquaresRegression<std::vector<Real> > reg_;
};
template <class ArgumentType> inline
LinearLeastSquaresRegression<ArgumentType>::LinearLeastSquaresRegression(
const std::vector<ArgumentType> & x,
const std::vector<Real> & y,
const std::vector<boost::function1<Real, ArgumentType> > & v)
: a_ (v.size(), 0.0),
err_ (v.size(), 0.0),
residuals_ (x.size()),
standardErrors_(v.size()) {
QL_REQUIRE(x.size() == y.size(),
"sample set need to be of the same size");
QL_REQUIRE(x.size() >= v.size(), "sample set is too small");
Size i;
const Size n = x.size();
const Size m = v.size();
Matrix A(n, m);
for (i=0; i<m; ++i)
std::transform(x.begin(), x.end(), A.column_begin(i), v[i]);
const SVD svd(A);
const Matrix& V = svd.V();
const Matrix& U = svd.U();
const Array& w = svd.singularValues();
for (i=0; i<svd.rank(); ++i) {
const Real u = std::inner_product(U.column_begin(i),
U.column_end(i),
y.begin(), 0.0)/w[i];
for (Size j=0; j<m; ++j) {
a_[j]+=u*V[j][i];
err_[j]+=V[j][i]*V[j][i]/(w[i]*w[i]);
}
}
err_ = Sqrt(err_);
residuals_= A*a_-Array(y.begin(), y.end());
const Real chiSq
= std::inner_product(residuals_.begin(), residuals_.end(),
residuals_.begin(), 0.0);
std::transform(err_.begin(), err_.end(), standardErrors_.begin(),
std::bind1st(std::multiplies<Real>(),
std::sqrt(chiSq/(n-2))));
}
namespace details {
class LinearFct : public std::unary_function<Real, std::vector<Real> >{
public:
LinearFct(Size i) : i_(i) {}
inline Real operator()(const std::vector<Real>& x) const {
return x[i_];
}
private:
const Size i_;
};
inline std::vector<boost::function1<Real, std::vector<Real> > >
linearFcts(Size dims) {
std::vector<boost::function1<Real, std::vector<Real> > > retVal;
retVal.push_back(constant<std::vector<Real>, Real>(1.0));
for (Size i=0; i < dims; ++i) {
retVal.push_back(LinearFct(i));
}
return retVal;
}
inline std::vector<std::vector<Real> > argumentWrapper(
const std::vector<Real>& x) {
std::vector<std::vector<Real> > retVal;
for (std::vector<Real>::const_iterator iter = x.begin();
iter != x.end(); ++iter) {
retVal.push_back(std::vector<Real>(1, *iter));
}
return retVal;
}
}
inline LinearRegression::LinearRegression(
const std::vector<std::vector<Real> >& x,
const std::vector<Real>& y)
: reg_(x, y, details::linearFcts(x[0].size())) {
#ifdef QL_EXTRA_SAFETY_CHECKS
for (Size i=1; i < x.size(); ++i) {
QL_REQUIRE(x[i-1].size() == x[i].size(),
"inconsistent sample size");
}
#endif
}
inline LinearRegression::LinearRegression(
const std::vector<Real>& x,
const std::vector<Real>& y)
: reg_(details::argumentWrapper(x), y, details::linearFcts(1)) { }
}
#endif
|