Coverage Report

Created: 2025-10-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/credit/distribution.hpp
Line
Count
Source
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
 <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
/*! \file distribution.hpp
21
    \brief Discretized probability density and cumulative probability
22
*/
23
24
#ifndef quantlib_probability_distribution_hpp
25
#define quantlib_probability_distribution_hpp
26
27
#include <ql/types.hpp>
28
#include <vector>
29
30
namespace QuantLib {
31
32
    //! Discretized probability density and cumulative probability
33
    /*! Discretized probability density and cumulative probability
34
      \ingroup probability
35
    */
36
    class ManipulateDistribution;
37
    class Distribution {
38
    public:
39
        friend class ManipulateDistribution;
40
        Distribution (int nBuckets, Real xmin, Real xmax);
41
        Distribution() = default;
42
        ;
43
44
        void add (Real value);
45
        void addDensity (int bucket, Real value);
46
        void addAverage (int bucket, Real value);
47
        void normalize ();
48
49
0
        Size size () const { return size_; }
50
0
        Real x (Size k) { return x_.at(k); }
51
0
        std::vector<Real>& x () { return x_; }
52
0
        Real dx (Size k) { return dx_.at(k); }
53
0
        std::vector<Real>& dx () { return dx_; }
54
        Real dx (Real x);
55
56
0
        Real density (Size k) {
57
0
            normalize();
58
0
            return density_.at(k);
59
0
        }
60
0
        Real cumulative (Size k) {
61
0
            normalize();
62
0
            return cumulativeDensity_.at(k);
63
0
        }
64
0
        Real excess (Size k) {
65
0
            normalize();
66
0
            return excessProbability_.at(k);
67
0
        }
68
0
        Real cumulativeExcess (Size k) {
69
0
            normalize();
70
0
            return cumulativeExcessProbability_.at(k);
71
0
        }
72
0
        Real average (Size k) { return average_.at(k); }
73
74
        Real confidenceLevel (Real quantil);
75
        Real cumulativeDensity (Real x);
76
        Real cumulativeExcessProbability (Real a, Real b);
77
        Real expectedValue ();
78
        Real trancheExpectedValue (Real a, Real d);
79
80
        template <class F>
81
        Real expectedValue (F& f) {
82
            normalize();
83
            Real expected = 0;
84
            for (int i = 0; i < size_; i++) {
85
                Real x = x_[i] + dx_[i]/2;
86
                expected += f (x) * dx_[i] * density_[i];
87
            }
88
            return expected;
89
        }
90
91
        /*!
92
          Transform the loss distribution into the tranche loss distribution
93
          for losses L_T = min(L,D) - min(L,A).
94
          The effects are:
95
          1) shift the distribution to the left by A, then
96
          2) cut off at D-A, Pr(L_T > D-A) = 0
97
          3) ensure Pr(L_T >= 0) = 1, i.e. a density spike at L_T = 0
98
         */
99
        void tranche (Real attachmentPoint, Real detachmentPoint);
100
101
        /*
102
          index of the grid point to the left of x
103
        */
104
        int locate (Real x);
105
106
        /* Returns the average value conditional on values above
107
        the passed percentile probability */
108
        Real expectedShortfall (Real percValue);
109
    private:
110
        int size_;
111
        Real xmin_, xmax_;
112
        std::vector<int> count_;
113
        // x: coordinate of left hand cell bundary
114
        // dx: cell width
115
        std::vector<Real> x_, dx_;
116
        // density: probability density, densitx*dx = prob. of loss in cell i
117
        // cumulatedDensity: cumulated (integrated) from x = 0
118
        // excessProbability: cumulated from x_i to infinity
119
        // cumulativeExcessProbability: integrated excessProbability from x = 0
120
        std::vector<Real> density_, cumulativeDensity_;
121
        std::vector<Real> excessProbability_, cumulativeExcessProbability_;
122
        // average loss in cell i
123
        std::vector<Real> average_;
124
125
        int overFlow_, underFlow_;
126
        bool isNormalized_;
127
    };
128
129
    class ManipulateDistribution {
130
    public:
131
        static Distribution convolve (const Distribution& d1,
132
                                      const Distribution& d2);
133
    };
134
135
}
136
137
#endif