QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
linearleastsquaresregression.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2009 Dirk Eddelbuettel
5 Copyright (C) 2006, 2009, 2010 Klaus Spanderen
6 Copyright (C) 2010 Kakhkhor Abdijalilov
7 Copyright (C) 2010 Slava Mazur
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
27#ifndef quantlib_linear_least_squares_regression_hpp
28#define quantlib_linear_least_squares_regression_hpp
29
30#include <ql/math/generallinearleastsquares.hpp>
31#include <ql/functional.hpp>
32#include <type_traits>
33
34namespace QuantLib {
35
36 namespace details {
37
38 template <class Container>
39 class LinearFct {
40 public:
44 QL_DEPRECATED
45 typedef Container argument_type;
46
50 QL_DEPRECATED
52 explicit LinearFct(Size i) : i_(i) {}
53
54 inline Real operator()(const Container& x) const {
55 return x[i_];
56 }
57
58 private:
59 const Size i_;
60 };
61
62 // 1d implementation (arithmetic types)
63 template <class xContainer, bool>
64 class LinearFcts {
65 public:
66 typedef typename xContainer::value_type ArgumentType;
67 LinearFcts (const xContainer &x, Real intercept) {
68 if (intercept != 0.0)
69 v.push_back([=](ArgumentType x){ return intercept; });
70 v.push_back([](ArgumentType x){ return x; });
71 }
72
73 const std::vector< ext::function<Real(ArgumentType)> > & fcts() {
74 return v;
75 }
76
77 private:
78 std::vector< ext::function<Real(ArgumentType)> > v;
79 };
80
81 // multi-dimensional implementation (container types)
82 template <class xContainer>
83 class LinearFcts<xContainer, false> {
84 public:
85 typedef typename xContainer::value_type ArgumentType;
86 LinearFcts (const xContainer &x, Real intercept) {
87 if (intercept != 0.0)
88 v.push_back([=](ArgumentType x){ return intercept; });
89 Size m = x.begin()->size();
90 for (Size i = 0; i < m; ++i)
91 v.push_back(LinearFct<ArgumentType>(i));
92 }
93
94 const std::vector< ext::function<Real(ArgumentType)> > & fcts() {
95 return v;
96 }
97 private:
98 std::vector< ext::function<Real(ArgumentType)> > v;
99 };
100 }
101
103 public:
105 template <class xContainer, class yContainer>
106 LinearRegression(const xContainer& x,
107 const yContainer& y, Real intercept = 1.0);
108
109 template <class xContainer, class yContainer, class vContainer>
110 LinearRegression(const xContainer& x,
111 const yContainer& y, const vContainer &v);
112 };
113
114
115 template <class xContainer, class yContainer> inline
117 const yContainer& y, Real intercept)
119 details::LinearFcts<xContainer,
120 std::is_arithmetic<typename xContainer::value_type>::value>
121 (x, intercept).fcts()) {
122 }
123
124 template <class xContainer, class yContainer, class vContainer> inline
126 const yContainer& y,
127 const vContainer &v)
128 : GeneralLinearLeastSquares(x, y, v) {
129 }
130
131 // general linear least squares regression
132 // this interface is support for backward compatibility only
133 // please use GeneralLinearLeastSquares directly
134 template <class ArgumentType = Real>
136 public:
138 const std::vector<ArgumentType> & x,
139 const std::vector<Real> & y,
140 const std::vector<ext::function<Real(ArgumentType)> > & v)
141 : GeneralLinearLeastSquares(x, y, v) {
142 }
143 };
144}
145#endif
general linear least squares regression
LinearLeastSquaresRegression(const std::vector< ArgumentType > &x, const std::vector< Real > &y, const std::vector< ext::function< Real(ArgumentType)> > &v)
LinearRegression(const xContainer &x, const yContainer &y, Real intercept=1.0)
linear regression y_i = a_0 + a_1*x_0 +..+a_n*x_{n-1} + eps
Real operator()(const Container &x) const
QL_DEPRECATED typedef Container argument_type
std::vector< ext::function< Real(ArgumentType)> > v
const std::vector< ext::function< Real(ArgumentType)> > & fcts()
LinearFcts(const xContainer &x, Real intercept)
std::vector< ext::function< Real(ArgumentType)> > v
const std::vector< ext::function< Real(ArgumentType)> > & fcts()
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.