QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
tcopulapolicy.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) 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
21#include <numeric>
22#include <algorithm>
23
24namespace QuantLib {
25
27 const std::vector<std::vector<Real> >& factorWeights,
28 const initTraits& vals)
29 {
30 for (int tOrder : vals.tOrders) {
31 // require no T is of order 2 (finite variance)
32 QL_REQUIRE(tOrder > 2, "Non finite variance T in latent model.");
33
34 distributions_.emplace_back(tOrder);
35 // inverses T variaces used in normalization of the random factors
36 // For low values of the T order this number is very close to zero
37 // and it enters the expressions dividing them, which introduces
38 // numerical errors.
39 varianceFactors_.push_back(std::sqrt((tOrder - 2.) / tOrder));
40 }
41
42 for (const auto& factorWeight : factorWeights) {
43 // This ensures the latent model is 'canonical'
44 QL_REQUIRE(vals.tOrders.size() == factorWeight.size() + 1,
45 // num factors plus one
46 "Incompatible number of T functions and number of factors.");
47
48 Real factorsNorm = std::inner_product(factorWeight.begin(), factorWeight.end(),
49 factorWeight.begin(), Real(0.));
50 QL_REQUIRE(factorsNorm < 1.,
51 "Non normal random factor combination.");
52 Real idiosyncFctr = std::sqrt(1.-factorsNorm);
53
54 // linear comb factors ajusted for the variance renormalization:
55 std::vector<Real> normFactorWeights;
56 for (Size iFactor = 0; iFactor < factorWeight.size(); iFactor++)
57 normFactorWeights.push_back(factorWeight[iFactor] * varianceFactors_[iFactor]);
58 // idiosincratic term, all Z factors are assumed identical.
59 normFactorWeights.push_back(idiosyncFctr * varianceFactors_.back());
60 latentVarsCumul_.emplace_back(vals.tOrders, normFactorWeights);
61 latentVarsInverters_.emplace_back(vals.tOrders, normFactorWeights);
62 }
63 }
64
66 const std::vector<Real>& probs) const
67 {
68 #if defined(QL_EXTRA_SAFETY_CHECKS)
69 QL_REQUIRE(probs.size()-latentVarsCumul_.size()
70 == distributions_.size()-1,
71 "Incompatible sample and latent model sizes");
72 #endif
73
74 std::vector<Real> result(probs.size());
75 Size indexSystemic = 0;
76 std::transform(probs.begin(), probs.begin() + varianceFactors_.size()-1,
77 result.begin(),
78 [&](Probability p) { return inverseCumulativeDensity(p, indexSystemic++); });
79 std::transform(probs.begin() + varianceFactors_.size()-1, probs.end(),
80 result.begin()+ varianceFactors_.size()-1,
81 [&](Probability p) { return inverseCumulativeZ(p); });
82 return result;
83 }
84
85}
TCopulaPolicy(const std::vector< std::vector< Real > > &factorWeights=std::vector< std::vector< Real > >(), const initTraits &vals=initTraits())
std::vector< boost::math::students_t_distribution< Real > > distributions_
std::vector< Real > allFactorCumulInverter(const std::vector< Real > &probs) const
std::vector< Real > varianceFactors_
std::vector< CumulativeBehrensFisher > latentVarsCumul_
std::vector< InverseCumulativeBehrensFisher > latentVarsInverters_
#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