forked from lballabio/QuantLib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerallinearleastsquares.hpp
170 lines (137 loc) · 6.28 KB
/
generallinearleastsquares.hpp
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
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2009 Dirk Eddelbuettel
Copyright (C) 2006, 2009, 2010 Klaus Spanderen
Copyright (C) 2010 Kakhkhor Abdijalilov
Copyright (C) 2010 Slava Mazur
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_general_linear_least_squares_hpp
#define quantlib_general_linear_least_squares_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 <boost/type_traits.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.
*/
class GeneralLinearLeastSquares {
public:
template <class xContainer, class yContainer, class vContainer>
GeneralLinearLeastSquares(const xContainer & x,
const yContainer & y, const vContainer & v);
template<class xIterator, class yIterator, class vIterator>
GeneralLinearLeastSquares(xIterator xBegin, xIterator xEnd,
yIterator yBegin, yIterator yEnd,
vIterator vBegin, vIterator vEnd);
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_;}
Size size() const { return residuals_.size(); }
Size dim() const { return a_.size(); }
protected:
Array a_, err_, residuals_, standardErrors_;
template <class xIterator, class yIterator, class vIterator>
void calculate(
xIterator xBegin, xIterator xEnd,
yIterator yBegin, yIterator yEnd,
vIterator vBegin);
template <class xIterator, class yIterator, class vIterator>
QL_DEPRECATED
void calculate(
xIterator xBegin, xIterator xEnd,
yIterator yBegin, yIterator yEnd,
vIterator vBegin, vIterator vEnd);
};
template <class xContainer, class yContainer, class vContainer> inline
GeneralLinearLeastSquares::GeneralLinearLeastSquares(const xContainer & x,
const yContainer & y,
const vContainer & v)
: a_(v.size(), 0.0),
err_(v.size(), 0.0),
residuals_(y.size()),
standardErrors_(v.size()) {
calculate(x.begin(), x.end(), y.begin(), y.end(), v.begin());
}
template<class xIterator, class yIterator, class vIterator> inline
GeneralLinearLeastSquares::GeneralLinearLeastSquares(
xIterator xBegin, xIterator xEnd,
yIterator yBegin, yIterator yEnd,
vIterator vBegin, vIterator vEnd)
: a_(std::distance(vBegin, vEnd), 0.0),
err_(a_.size(), 0.0),
residuals_(std::distance(yBegin, yEnd)),
standardErrors_(a_.size()) {
calculate(xBegin, xEnd, yBegin, yEnd, vBegin);
}
template <class xIterator, class yIterator, class vIterator>
void GeneralLinearLeastSquares::calculate(xIterator xBegin, xIterator xEnd,
yIterator yBegin, yIterator yEnd,
vIterator vBegin) {
const Size n = residuals_.size();
const Size m = err_.size();
QL_REQUIRE( n == Size(std::distance(yBegin, yEnd)),
"sample set need to be of the same size");
QL_REQUIRE(n >= m, "sample set is too small");
Size i;
Matrix A(n, m);
for (i=0; i<m; ++i)
std::transform(xBegin, xEnd, A.column_begin(i), *vBegin++);
const SVD svd(A);
const Matrix& V = svd.V();
const Matrix& U = svd.U();
const Array& w = svd.singularValues();
const Real threshold = n * QL_EPSILON * svd.singularValues()[0];
for (i=0; i<m; ++i) {
if (w[i] > threshold) {
const Real u = std::inner_product(U.column_begin(i),
U.column_end(i),
yBegin, 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_);
Array tmp = A*a_;
std::transform(tmp.begin(), tmp.end(),
yBegin, residuals_.begin(), std::minus<Real>());
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))));
}
template <class xIterator, class yIterator, class vIterator>
void GeneralLinearLeastSquares::calculate(xIterator xBegin, xIterator xEnd,
yIterator yBegin, yIterator yEnd,
vIterator vBegin, vIterator) {
calculate(xBegin, xEnd, yBegin, yEnd, vBegin);
}
}
#endif