Coverage Report

Created: 2025-10-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/math/tcopulapolicy.cpp
Line
Count
Source
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
 <https://www.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
0
    {
30
0
        for (int tOrder : vals.tOrders) {
31
            // require no T is of order 2 (finite variance)
32
0
            QL_REQUIRE(tOrder > 2, "Non finite variance T in latent model.");
33
34
0
            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
0
            varianceFactors_.push_back(std::sqrt((tOrder - 2.) / tOrder));
40
0
        }
41
42
0
        for (const auto& factorWeight : factorWeights) {
43
            // This ensures the latent model is 'canonical'
44
0
            QL_REQUIRE(vals.tOrders.size() == factorWeight.size() + 1,
45
                       // num factors plus one
46
0
                       "Incompatible number of T functions and number of factors.");
47
48
0
            Real factorsNorm = std::inner_product(factorWeight.begin(), factorWeight.end(),
49
0
                                                  factorWeight.begin(), Real(0.));
50
0
            QL_REQUIRE(factorsNorm < 1., 
51
0
                "Non normal random factor combination.");
52
0
            Real idiosyncFctr = std::sqrt(1.-factorsNorm);
53
54
            // linear comb factors ajusted for the variance renormalization:
55
0
            std::vector<Real> normFactorWeights;
56
0
            normFactorWeights.reserve(factorWeight.size());
57
0
            for (Size iFactor = 0; iFactor < factorWeight.size(); iFactor++)
58
0
                normFactorWeights.push_back(factorWeight[iFactor] * varianceFactors_[iFactor]);
59
            // idiosincratic term, all Z factors are assumed identical.
60
0
            normFactorWeights.push_back(idiosyncFctr * varianceFactors_.back());
61
0
            latentVarsCumul_.emplace_back(vals.tOrders, normFactorWeights);
62
0
            latentVarsInverters_.emplace_back(vals.tOrders, normFactorWeights);
63
0
        }
64
0
    }
65
66
    std::vector<Real> TCopulaPolicy::allFactorCumulInverter(
67
        const std::vector<Real>& probs) const 
68
0
    {
69
    #if defined(QL_EXTRA_SAFETY_CHECKS)
70
        QL_REQUIRE(probs.size()-latentVarsCumul_.size() 
71
            == distributions_.size()-1, 
72
            "Incompatible sample and latent model sizes");
73
    #endif
74
75
0
        std::vector<Real> result(probs.size());
76
0
        Size indexSystemic = 0;
77
0
        std::transform(probs.begin(), probs.begin() + varianceFactors_.size()-1,
78
0
                       result.begin(),
79
0
                       [&](Probability p) { return inverseCumulativeDensity(p, indexSystemic++); });
80
0
        std::transform(probs.begin() + varianceFactors_.size()-1, probs.end(),
81
0
                       result.begin()+ varianceFactors_.size()-1,
82
0
                       [&](Probability p) { return inverseCumulativeZ(p); });
83
0
        return result;
84
0
    }
85
86
}