QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
onefactorcopula.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Roland Lichters
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
24#ifndef quantlib_one_factor_copula_hpp
25#define quantlib_one_factor_copula_hpp
26
27#include <ql/experimental/credit/distribution.hpp>
28#include <ql/patterns/lazyobject.hpp>
29#include <ql/quote.hpp>
30#include <utility>
31
32namespace QuantLib {
33
35
103 public:
105 Real maximum = 5.0,
106 Size integrationSteps = 50,
107 Real minimum = -5.0)
108 : correlation_(std::move(correlation)), max_(maximum), steps_(integrationSteps),
109 min_(minimum) {
110 QL_REQUIRE(correlation_->value() >= -1
111 && correlation_->value() <= 1,
112 "correlation out of range [-1, +1]");
114 }
115
117
120 virtual Real density(Real m) const = 0;
122
125 virtual Real cumulativeZ(Real z) const = 0;
127
132 virtual Real cumulativeY(Real y) const;
134
139 virtual Real inverseCumulativeY(Real p) const;
140
142 Real correlation() const;
143
145
150 Real m) const;
151
153
158 std::vector<Real> conditionalProbability(const std::vector<Real>& prob,
159 Real m) const;
160
169 Real integral(Real p) const {
170 QL_REQUIRE(p >= 0 && p <= 1, "probability p=" << p
171 << " out of range [0,1]");
172 calculate();
173
174 Real avg = 0;
175 for (Size k = 0; k < steps(); k++) {
176 Real pp = conditionalProbability(p, m(k));
177 avg += pp * densitydm(k);
178 }
179 return avg;
180 }
181
193 template <class F>
194 Real integral(const F& f, std::vector<Real>& probabilities) const {
195 calculate();
196
197 Real avg = 0.0;
198 for (Size i = 0; i < steps_; i++) {
199 std::vector<Real> conditional
200 = conditionalProbability(probabilities, m(i));
201 Real prob = f(conditional);
202 avg += prob * densitydm(i);
203 }
204 return avg;
205 }
206
217 template <class F>
219 const std::vector<Real>& nominals,
220 const std::vector<Real>& probabilities) const {
221 calculate();
222
223 Distribution dist(f.buckets(), 0.0, f.maximum());
224 for (Size i = 0; i < steps(); i++) {
225 std::vector<Real> conditional
226 = conditionalProbability(probabilities, m(i));
227 Distribution d = f(nominals, conditional);
228 for (Size j = 0; j < dist.size(); j++)
229 dist.addDensity(j, d.density(j) * densitydm(i));
230 }
231 return dist;
232 }
233
239 int checkMoments(Real tolerance) const;
240
241 protected:
243 mutable Real max_;
244 mutable Size steps_;
245 mutable Real min_;
246
247 // Tabulated numerical solution of the cumulated distribution of Y
248 mutable std::vector<Real> y_;
249 mutable std::vector<Real> cumulativeY_;
250
251 //private:
252 // utilities for simple Euler integrations over the density of M
253 Size steps() const;
254
255 // i not used yet, might allow varying grid size
256 // for the copula integration in the future
257 Real dm(Size i) const;
258
259 Real m(Size i) const;
260 Real densitydm(Size i) const;
261 };
262
264 calculate();
265 return correlation_->value();
266 }
267
269 return steps_;
270 }
271
273 return (max_ - min_)/ steps_;
274 }
275
276 inline Real OneFactorCopula::m(Size i) const {
277 QL_REQUIRE(i < steps_, "index out of range");
278 return min_ + dm(i) * i + dm(i) / 2;
279 }
280
282 QL_REQUIRE(i < steps_, "index out of range");
283 return density(m(i)) * dm(i);
284 }
285
286}
287
288#endif
void addDensity(int bucket, Real value)
Shared handle to an observable.
Definition: handle.hpp:41
Framework for calculation on demand and result caching.
Definition: lazyobject.hpp:35
virtual void calculate() const
Definition: lazyobject.hpp:253
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Abstract base class for one-factor copula models.
Real conditionalProbability(Real prob, Real m) const
Conditional probability.
virtual Real density(Real m) const =0
Density function of M.
Real integral(Real p) const
Distribution integral(const F &f, const std::vector< Real > &nominals, const std::vector< Real > &probabilities) const
virtual Real cumulativeZ(Real z) const =0
Cumulative distribution of Z.
Real correlation() const
Single correlation parameter.
Real densitydm(Size i) const
std::vector< Real > cumulativeY_
virtual Real inverseCumulativeY(Real p) const
Inverse cumulative distribution of Y.
Real integral(const F &f, std::vector< Real > &probabilities) const
int checkMoments(Real tolerance) const
virtual Real cumulativeY(Real y) const
Cumulative distribution of Y.
OneFactorCopula(Handle< Quote > correlation, Real maximum=5.0, Size integrationSteps=50, Real minimum=-5.0)
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.