QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
backwardflatinterpolation.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) 2005, 2008 StatPro Italia srl
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file backwardflatinterpolation.hpp
21 \brief backward-flat interpolation between discrete points
22*/
23
24#ifndef quantlib_backward_flat_interpolation_hpp
25#define quantlib_backward_flat_interpolation_hpp
26
28#include <vector>
29
30namespace QuantLib {
31
32 namespace detail {
33 template<class I1, class I2> class BackwardFlatInterpolationImpl;
34 }
35
36 //! Backward-flat interpolation between discrete points
37 /*! \ingroup interpolations
38 \warning See the Interpolation class for information about the
39 required lifetime of the underlying data.
40 */
42 public:
43 /*! \pre the \f$ x \f$ values must be sorted. */
44 template <class I1, class I2>
45 BackwardFlatInterpolation(const I1& xBegin, const I1& xEnd,
46 const I2& yBegin) {
47 impl_ = ext::shared_ptr<Interpolation::Impl>(new
49 yBegin));
50 impl_->update();
51 }
52 };
53
54 //! Backward-flat interpolation factory and traits
55 /*! \ingroup interpolations */
57 public:
58 template <class I1, class I2>
59 Interpolation interpolate(const I1& xBegin, const I1& xEnd,
60 const I2& yBegin) const {
61 return BackwardFlatInterpolation(xBegin, xEnd, yBegin);
62 }
63 static const bool global = false;
64 static const Size requiredPoints = 1;
65 };
66
67 namespace detail {
68
69 template <class I1, class I2>
71 : public Interpolation::templateImpl<I1,I2> {
72 public:
73 BackwardFlatInterpolationImpl(const I1& xBegin, const I1& xEnd,
74 const I2& yBegin)
75 : Interpolation::templateImpl<I1,I2>(xBegin,xEnd,yBegin,
76 BackwardFlat::requiredPoints),
77 primitive_(xEnd-xBegin) {}
78 void update() override {
79 Size n = this->xEnd_-this->xBegin_;
80 primitive_[0] = 0.0;
81 for (Size i=1; i<n; i++) {
82 Real dx = this->xBegin_[i]-this->xBegin_[i-1];
83 primitive_[i] = primitive_[i-1] + dx*this->yBegin_[i];
84 }
85 }
86 Real value(Real x) const override {
87 if (x <= this->xBegin_[0]
88 || std::distance(this->xBegin_, this->xEnd_) == 1)
89 return this->yBegin_[0];
90
91 Size i = this->locate(x);
92 if (x == this->xBegin_[i])
93 return this->yBegin_[i];
94 else
95 return this->yBegin_[i+1];
96 }
97 Real primitive(Real x) const override {
98 if (std::distance(this->xBegin_, this->xEnd_) == 1)
99 return (x - this->xBegin_[0]) * this->yBegin_[0];
100
101 Size i = this->locate(x);
102 Real dx = x-this->xBegin_[i];
103 return primitive_[i] + dx*this->yBegin_[i+1];
104 }
105 Real derivative(Real) const override { return 0.0; }
106 Real secondDerivative(Real) const override { return 0.0; }
107
108 private:
109 std::vector<Real> primitive_;
110 };
111
112 }
113
114}
115
116#endif
Backward-flat interpolation factory and traits.
Interpolation interpolate(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin) const
Backward-flat interpolation between discrete points.
BackwardFlatInterpolation(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin)
basic template implementation
templateImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin, const int requiredPoints=2)
base class for 1-D interpolations.
ext::shared_ptr< Impl > impl_
BackwardFlatInterpolationImpl(const I1 &xBegin, const I1 &xEnd, const I2 &yBegin)
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
base class for 1-D interpolations
Definition: any.hpp:35