Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
nadarayawatson.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19#ifndef quantext_nadaraya_watson_regression_hpp
20#define quantext_nadaraya_watson_regression_hpp
21
22#include <ql/math/comparison.hpp>
23
24#include <boost/make_shared.hpp>
25
26/*! \file qle/math/nadarayawatson.hpp
27 \brief Nadaraya-Watson regression
28 \ingroup math
29*/
30
31namespace QuantExt {
32using QuantLib::Real;
33using QuantLib::Size;
34namespace detail {
35
36//! Regression impl
37/*! \ingroup math
38 */
40public:
41 virtual ~RegressionImpl() {}
42 virtual void update() = 0;
43 virtual Real value(Real x) const = 0;
44 virtual Real standardDeviation(Real x) const = 0;
45};
46
47//! Nadaraya Watson impl
48/*! \ingroup math
49 */
50template <class I1, class I2, class Kernel> class NadarayaWatsonImpl : public RegressionImpl {
51public:
52 /*! \pre the \f$ x \f$ values must be sorted.
53 \pre kernel needs a Real operator()(Real x) implementation
54 */
55 NadarayaWatsonImpl(const I1& xBegin, const I1& xEnd, const I2& yBegin, const Kernel& kernel)
56 : xBegin_(xBegin), xEnd_(xEnd), yBegin_(yBegin), kernel_(kernel) {}
57
58 void update() override {}
59
60 Real value(Real x) const override {
61
62 Real tmp1 = 0.0, tmp2 = 0.0;
63
64 for (Size i = 0; i < static_cast<Size>(xEnd_ - xBegin_); ++i) {
65 Real tmp = kernel_(x - xBegin_[i]);
66 tmp1 += yBegin_[i] * tmp;
67 tmp2 += tmp;
68 }
69
70 return QuantLib::close_enough(tmp2, 0.0) ? 0.0 : tmp1 / tmp2;
71 }
72
73 Real standardDeviation(Real x) const override {
74
75 Real tmp1 = 0.0, tmp1b = 0.0, tmp2 = 0.0;
76
77 for (Size i = 0; i < static_cast<Size>(xEnd_ - xBegin_); ++i) {
78 Real tmp = kernel_(x - xBegin_[i]);
79 tmp1 += yBegin_[i] * tmp;
80 tmp1b += yBegin_[i] * yBegin_[i] * tmp;
81 tmp2 += tmp;
82 }
83
84 return QuantLib::close_enough(tmp2, 0.0) ? 0.0 : std::sqrt(tmp1b / tmp2 - (tmp1 * tmp1) / (tmp2 * tmp2));
85 }
86
87private:
90 Kernel kernel_;
91};
92} // namespace detail
93
94//! Nadaraya Watson regression
95/*! This implements the estimator
96
97 \f[
98 m(x) = \frac{\sum_i y_i K(x-x_i)}{\sum_i K(x-x_i)}
99 \f]
100
101 \ingroup math
102*/
104public:
105 /*! \pre the \f$ x \f$ values must be sorted.
106 \pre kernel needs a Real operator()(Real x) implementation
107 */
108 template <class I1, class I2, class Kernel>
109 NadarayaWatson(const I1& xBegin, const I1& xEnd, const I2& yBegin, const Kernel& kernel) {
110 impl_ = QuantLib::ext::make_shared<detail::NadarayaWatsonImpl<I1, I2, Kernel> >(xBegin, xEnd, yBegin, kernel);
111 }
112
113 Real operator()(Real x) const { return impl_->value(x); }
114
115 Real standardDeviation(Real x) const { return impl_->standardDeviation(x); }
116
117private:
118 QuantLib::ext::shared_ptr<detail::RegressionImpl> impl_;
119};
120
121} // namespace QuantExt
122
123#endif
Nadaraya Watson regression.
NadarayaWatson(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const Kernel &kernel)
Real operator()(Real x) const
Real standardDeviation(Real x) const
QuantLib::ext::shared_ptr< detail::RegressionImpl > impl_
Real value(Real x) const override
Real standardDeviation(Real x) const override
NadarayaWatsonImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const Kernel &kernel)
virtual Real standardDeviation(Real x) const =0
virtual Real value(Real x) const =0