QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
linearleastsquaresregression.hpp
Go to the documentation of this file.
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
23/*! \file linearleastsquaresregression.hpp
24 \brief general linear least square regression
25*/
26
27#ifndef quantlib_linear_least_squares_regression_hpp
28#define quantlib_linear_least_squares_regression_hpp
29
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:
41 explicit LinearFct(Size i) : i_(i) {}
42
43 inline Real operator()(const Container& x) const {
44 return x[i_];
45 }
46
47 private:
48 const Size i_;
49 };
50
51 // 1d implementation (arithmetic types)
52 template <class xContainer, bool>
53 class LinearFcts {
54 public:
55 typedef typename xContainer::value_type ArgumentType;
56 LinearFcts (const xContainer &x, Real intercept) {
57 if (intercept != 0.0)
58 v.push_back([=](ArgumentType x){ return intercept; });
59 v.push_back([](ArgumentType x){ return x; });
60 }
61
62 const std::vector< ext::function<Real(ArgumentType)> > & fcts() {
63 return v;
64 }
65
66 private:
67 std::vector< ext::function<Real(ArgumentType)> > v;
68 };
69
70 // multi-dimensional implementation (container types)
71 template <class xContainer>
72 class LinearFcts<xContainer, false> {
73 public:
74 typedef typename xContainer::value_type ArgumentType;
75 LinearFcts (const xContainer &x, Real intercept) {
76 if (intercept != 0.0)
77 v.push_back([=](ArgumentType x){ return intercept; });
78 Size m = x.begin()->size();
79 for (Size i = 0; i < m; ++i)
80 v.push_back(LinearFct<ArgumentType>(i));
81 }
82
83 const std::vector< ext::function<Real(ArgumentType)> > & fcts() {
84 return v;
85 }
86 private:
87 std::vector< ext::function<Real(ArgumentType)> > v;
88 };
89 }
90
92 public:
93 //! linear regression y_i = a_0 + a_1*x_0 +..+a_n*x_{n-1} + eps
94 template <class xContainer, class yContainer>
95 LinearRegression(const xContainer& x,
96 const yContainer& y, Real intercept = 1.0);
97
98 template <class xContainer, class yContainer, class vContainer>
99 LinearRegression(const xContainer& x,
100 const yContainer& y, const vContainer &v);
101 };
102
103
104 template <class xContainer, class yContainer> inline
106 const yContainer& y, Real intercept)
108 details::LinearFcts<xContainer,
109 std::is_arithmetic<typename xContainer::value_type>::value>
110 (x, intercept).fcts()) {
111 }
112
113 template <class xContainer, class yContainer, class vContainer> inline
115 const yContainer& y,
116 const vContainer &v)
118 }
119
120 // general linear least squares regression
121 // this interface is support for backward compatibility only
122 // please use GeneralLinearLeastSquares directly
123 template <class ArgumentType = Real>
125 public:
127 const std::vector<ArgumentType> & x,
128 const std::vector<Real> & y,
129 const std::vector<ext::function<Real(ArgumentType)> > & v)
131 }
132 };
133}
134#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
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()
Maps function, bind and cref to either the boost or std implementation.
general linear least square regression
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.
ext::shared_ptr< BlackVolTermStructure > v