QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
gaussiancopulapolicy.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) 2014 Jose Aparicio
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#ifndef quantlib_gaussian_copula_policy_hpp
21#define quantlib_gaussian_copula_policy_hpp
22
24#include <vector>
25#include <numeric>
26#include <algorithm>
27
28namespace QuantLib {
29
30 /*! Gaussian Latent Model's copula policy. Its simplicity is a result of
31 the convolution stability of the Gaussian distribution.
32 */
33 /* This is the only case that would have allowed the policy to be static,
34 but other copulas will need parameters and initialization.*/
36
37 typedef int initTraits;
38
40 const std::vector<std::vector<Real> >& factorWeights =
41 std::vector<std::vector<Real> >(),
42 const initTraits& dummy = int())
43 : numFactors_(factorWeights.size() + factorWeights[0].size())
44 {
45 /* check factors in LM are normalized. */
46 for (const auto& factorWeight : factorWeights) {
47 Real factorsNorm = std::inner_product(factorWeight.begin(), factorWeight.end(),
48 factorWeight.begin(), Real(0.));
49 QL_REQUIRE(factorsNorm < 1.,
50 "Non normal random factor combination.");
51 }
52 /* check factor matrix is squared .......... */
53 }
54
55 /*! Number of independent random factors.
56 This is the only methos that ould stop the class from being static, it
57 is needed for the MC generator construction.
58 */
59 Size numFactors() const {
60 return numFactors_;
61 }
62
63 //! returns a copy of the initialization arguments
65 return initTraits();
66 }
67
68 /*! Cumulative probability of a given latent variable
69 The iVariable parameter is the index of the requested variable.
70 */
71 Probability cumulativeY(Real val, Size iVariable) const {
72 return cumulative_(val);
73 }
74 //! Cumulative probability of the idiosyncratic factors (all the same)
76 return cumulative_(z);
77 }
78 /*! Probability density of a given realization of values of the systemic
79 factors (remember they are independent). In the normal case, since
80 they all follow the same law it is just a trivial product of the same
81 density.
82 Intended to be used in numerical integration of an arbitrary function
83 depending on those values.
84 */
85 Probability density(const std::vector<Real>& m) const {
86 return std::accumulate(m.begin(), m.end(), Real(1.),
87 [&](Real x, Real y) -> Real { return x*density_(y); });
88 }
89 /*! Returns the inverse of the cumulative distribution of the (modelled)
90 latent variable (as indexed by iVariable). The normal stability avoids
91 the convolution of the factors' distributions
92 */
95 }
96 /*! Returns the inverse of the cumulative distribution of the
97 idiosyncratic factor (identically distributed for all latent variables)
98 */
101 }
102 /*! Returns the inverse of the cumulative distribution of the
103 systemic factor iFactor.
104 */
107 }
108 //!
109 //to use this (by default) version, the generator must be a uniform one.
110 std::vector<Real> allFactorCumulInverter(const std::vector<Real>& probs) const {
111 std::vector<Real> result;
112 result.resize(probs.size());
113 std::transform(probs.begin(), probs.end(), result.begin(),
114 [&](Real p){ return InverseCumulativeNormal::standard_value(p); });
115 return result;
116 }
117 private:
119 // no op =
122 };
123
124}
125
126#endif
Cumulative normal distribution function.
Normal distribution function.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
QL_REAL Real
real number
Definition: types.hpp:50
Real Probability
probability
Definition: types.hpp:82
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
normal, cumulative and inverse cumulative distributions
static const NormalDistribution density_
GaussianCopulaPolicy(const std::vector< std::vector< Real > > &factorWeights=std::vector< std::vector< Real > >(), const initTraits &dummy=int())
static const CumulativeNormalDistribution cumulative_
Real inverseCumulativeY(Probability p, Size iVariable) const
Real inverseCumulativeDensity(Probability p, Size iFactor) const
Probability density(const std::vector< Real > &m) const
initTraits getInitTraits() const
returns a copy of the initialization arguments
Probability cumulativeY(Real val, Size iVariable) const
std::vector< Real > allFactorCumulInverter(const std::vector< Real > &probs) const
Probability cumulativeZ(Real z) const
Cumulative probability of the idiosyncratic factors (all the same)
Real inverseCumulativeZ(Probability p) const