QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
normalclvmodel.cpp
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) 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
20/*! \file normalclvmodel.cpp
21*/
22
23#include <ql/exercise.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]);
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
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}
Analytic European engine.
Black-Scholes processes.
Brent 1-D solver.
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
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
Option exercise classes and payoff function.
Integral of a 1-dimensional function using the Gauss quadratures.
risk neutral terminal density calculator for the Black-Scholes-Merton model with skew dependent volat...
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
linear interpolation between discrete points
#define M_SQRT2
Definition: any.hpp:35
STL namespace.
CLV model with a normally distributed kernel process.
normal, cumulative and inverse cumulative distributions
Ornstein-Uhlenbeck process.
ext::shared_ptr< YieldTermStructure > q
ext::shared_ptr< YieldTermStructure > r
Vanilla option on a single asset.
std::uint64_t x_