QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
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 Real operator()(const Container& x) const {
44 return x[i_];
45 }
46
47 private:
48 const Size i_;
49 };
50
51 template <class xContainer>
52 class LinearFcts {
53 public:
54 typedef typename xContainer::value_type ArgumentType;
55 LinearFcts(const xContainer &x, Real intercept) {
56 if (intercept != 0.0)
57 v.push_back([=](const ArgumentType&){ return intercept; });
58 if constexpr (std::is_arithmetic_v<ArgumentType>) {
59 v.push_back([](ArgumentType x){ return x; });
60 } else {
61 Size m = x.begin()->size();
62 for (Size i = 0; i < m; ++i)
63 v.push_back(LinearFct<ArgumentType>(i));
64 }
65 }
66
67 const std::vector< std::function<Real(ArgumentType)> > & fcts() {
68 return v;
69 }
70 private:
71 std::vector< std::function<Real(ArgumentType)> > v;
72 };
73
74 }
75
77 public:
78 //! linear regression y_i = a_0 + a_1*x_0 +..+a_n*x_{n-1} + eps
79 template <class xContainer, class yContainer>
80 LinearRegression(const xContainer& x,
81 const yContainer& y, Real intercept = 1.0);
82
83 template <class xContainer, class yContainer, class vContainer>
84 LinearRegression(const xContainer& x,
85 const yContainer& y, const vContainer &v);
86 };
87
88
89 template <class xContainer, class yContainer> inline
91 const yContainer& y, Real intercept)
93 details::LinearFcts<xContainer>(x, intercept).fcts()) {
94 }
95
96 template <class xContainer, class yContainer, class vContainer> inline
98 const yContainer& y,
99 const vContainer &v)
101 }
102
103 // general linear least squares regression
104 // this interface is support for backward compatibility only
105 // please use GeneralLinearLeastSquares directly
106 template <class ArgumentType = Real>
108 public:
110 const std::vector<ArgumentType> & x,
111 const std::vector<Real> & y,
112 const std::vector<std::function<Real(ArgumentType)> > & v)
114 }
115 };
116}
117#endif
general linear least squares regression
LinearLeastSquaresRegression(const std::vector< ArgumentType > &x, const std::vector< Real > &y, const std::vector< std::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
LinearFcts(const xContainer &x, Real intercept)
const std::vector< std::function< Real(ArgumentType)> > & fcts()
std::vector< std::function< Real(ArgumentType)> > v
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:37
ext::shared_ptr< BlackVolTermStructure > v