QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
normalclvmodel.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2016 Klaus Spanderen
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
23#include <ql/exercise.hpp>
24#include <ql/experimental/models/normalclvmodel.hpp>
25#include <ql/instruments/vanillaoption.hpp>
26#include <ql/math/distributions/normaldistribution.hpp>
27#include <ql/math/integrals/gaussianquadratures.hpp>
28#include <ql/math/interpolations/linearinterpolation.hpp>
29#include <ql/math/solvers1d/brent.hpp>
30#include <ql/methods/finitedifferences/utilities/gbsmrndcalculator.hpp>
31#include <ql/pricingengines/vanilla/analyticeuropeanengine.hpp>
32#include <ql/processes/blackscholesprocess.hpp>
33#include <ql/processes/ornsteinuhlenbeckprocess.hpp>
34#include <utility>
35
36
37namespace QuantLib {
38
39 NormalCLVModel::NormalCLVModel(const ext::shared_ptr<GeneralizedBlackScholesProcess>& bsProcess,
40 ext::shared_ptr<OrnsteinUhlenbeckProcess> ouProcess,
41 const std::vector<Date>& maturityDates,
42 Size lagrangeOrder,
43 Real pMax,
44 Real pMin)
45 : x_(M_SQRT2 * GaussHermiteIntegration(lagrangeOrder).x()),
46 sigma_((pMax != Null<Real>()) ?
47 x_.back() / InverseCumulativeNormal()(pMax) :
48 (pMin != Null<Real>()) ? x_.front() / InverseCumulativeNormal()(pMin) : Real(1.0)),
49 bsProcess_(bsProcess), ouProcess_(std::move(ouProcess)), maturityDates_(maturityDates),
50 rndCalculator_(ext::make_shared<GBSMRNDCalculator>(bsProcess)),
51 maturityTimes_(maturityDates.size()) {
52
55
56 for (Size i=0; i < maturityTimes_.size(); ++i) {
57 maturityTimes_[i] = bsProcess_->time(maturityDates[i]);
58 QL_REQUIRE(i==0 || maturityTimes_[i-1] < maturityTimes_[i],
59 "dates must be sorted");
60 }
61 }
62
63 Real NormalCLVModel::cdf(const Date& d, Real k) const {
64 return rndCalculator_->cdf(k, bsProcess_->time(d));
65 }
66
67
68 Real NormalCLVModel::invCDF(const Date& d, Real q) const {
69 return rndCalculator_->invcdf(q, bsProcess_->time(d));
70 }
71
73 const Time t = bsProcess_->time(d);
74
75 const Real expectation
76 = ouProcess_->expectation(0.0, ouProcess_->x0(), t);
77 const Real stdDeviation
78 = ouProcess_->stdDeviation(0.0, ouProcess_->x0(), t);
79
80 return expectation + stdDeviation*x_;
81 }
82
84 Array s(x_.size());
85
87 for (Size i=0, n=s.size(); i < n; ++i) {
88 s[i] = invCDF(d, N(x_[i]/sigma_));
89 }
90
91 return s;
92 }
93
94
95 ext::function<Real(Time, Real)> NormalCLVModel::g() const {
96 calculate();
97 return g_;
98 }
99
101 const NormalCLVModel& model)
102 : y_(model.x_.size()),
103 sigma_(model.sigma_),
104 ouProcess_(model.ouProcess_),
105 data_(ext::make_shared<InterpolationData>(model)) {
106
107 for (Size i=0; i < data_->s_.columns(); ++i) {
108 const Array y = model.collocationPointsY(model.maturityDates_[i]);
109 std::copy(y.begin(), y.end(), data_->s_.column_begin(i));
110 }
111
112 for (Size i=0; i < data_->s_.rows(); ++i) {
113 data_->interpl_.emplace_back(data_->t_.begin(), data_->t_.end(),
114 data_->s_.row_begin(i));
115 }
116 }
117
118
120 for (Size i=0; i < y_.size(); ++i) {
121 y_[i] = data_->interpl_[i](t, true);
122 }
123
124 const Real expectation
125 = ouProcess_->expectation(0.0, ouProcess_->x0(), t);
126 const Real stdDeviation
127 = ouProcess_->stdDeviation(0.0, ouProcess_->x0(), t);
128
129 const Real r = sigma_*(x-expectation)/stdDeviation;
130
131 return data_->lagrangeInterpl_.value(y_, r);
132 }
133
135 g_ = ext::function<Real(Time, Real)>(MappingFunction(*this));
136 }
137}
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Cumulative normal distribution function.
Concrete date class.
Definition: date.hpp:125
generalized Gauss-Hermite integration
Inverse cumulative normal distribution function.
virtual void calculate() const
Definition: lazyobject.hpp:253
const ext::shared_ptr< InterpolationData > data_
MappingFunction(const NormalCLVModel &model)
const std::vector< Date > maturityDates_
void performCalculations() const override
std::vector< Time > maturityTimes_
const ext::shared_ptr< GeneralizedBlackScholesProcess > bsProcess_
NormalCLVModel(const ext::shared_ptr< GeneralizedBlackScholesProcess > &bsProcess, ext::shared_ptr< OrnsteinUhlenbeckProcess > ouProcess, const std::vector< Date > &maturityDates, Size lagrangeOrder, Real pMax=Null< Real >(), Real pMin=Null< Real >())
Real invCDF(const Date &d, Real q) const
Real cdf(const Date &d, Real x) const
ext::function< Real(Time, Real)> g_
const ext::shared_ptr< OrnsteinUhlenbeckProcess > ouProcess_
const ext::shared_ptr< GBSMRNDCalculator > rndCalculator_
Array collocationPointsX(const Date &d) const
Array collocationPointsY(const Date &d) const
ext::function< Real(Time, Real)> g() const
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
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.