| 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 | #include <ql/experimental/math/tcopulapolicy.hpp> |
| 21 | #include <numeric> |
| 22 | #include <algorithm> |
| 23 | |
| 24 | namespace QuantLib { |
| 25 | |
| 26 | TCopulaPolicy::TCopulaPolicy( |
| 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(args&: 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(x: std::sqrt(x: (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(first1: factorWeight.begin(), last1: factorWeight.end(), |
| 49 | first2: factorWeight.begin(), init: Real(0.)); |
| 50 | QL_REQUIRE(factorsNorm < 1., |
| 51 | "Non normal random factor combination." ); |
| 52 | Real idiosyncFctr = std::sqrt(x: 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(x: factorWeight[iFactor] * varianceFactors_[iFactor]); |
| 58 | // idiosincratic term, all Z factors are assumed identical. |
| 59 | normFactorWeights.push_back(x: idiosyncFctr * varianceFactors_.back()); |
| 60 | latentVarsCumul_.emplace_back(args: vals.tOrders, args&: normFactorWeights); |
| 61 | latentVarsInverters_.emplace_back(args: vals.tOrders, args&: normFactorWeights); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | std::vector<Real> TCopulaPolicy::allFactorCumulInverter( |
| 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(first: probs.begin(), last: probs.begin() + varianceFactors_.size()-1, |
| 77 | result: result.begin(), |
| 78 | unary_op: [&](Probability p) { return inverseCumulativeDensity(p, iFactor: indexSystemic++); }); |
| 79 | std::transform(first: probs.begin() + varianceFactors_.size()-1, last: probs.end(), |
| 80 | result: result.begin()+ varianceFactors_.size()-1, |
| 81 | unary_op: [&](Probability p) { return inverseCumulativeZ(p); }); |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |