QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
distribution.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) 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 <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/*! \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
30namespace QuantLib {
31
32 //! Discretized probability density and cumulative probability
33 /*! Discretized probability density and cumulative probability
34 \ingroup probability
35 */
36 class ManipulateDistribution;
38 public:
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 Size size () const { return size_; }
50 Real x (Size k) { return x_.at(k); }
51 std::vector<Real>& x () { return x_; }
52 Real dx (Size k) { return dx_.at(k); }
53 std::vector<Real>& dx () { return dx_; }
54 Real dx (Real x);
55
57 normalize();
58 return density_.at(k);
59 }
61 normalize();
62 return cumulativeDensity_.at(k);
63 }
65 normalize();
66 return excessProbability_.at(k);
67 }
69 normalize();
71 }
72 Real average (Size k) { return average_.at(k); }
73
74 Real confidenceLevel (Real quantil);
79
80 template <class 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_;
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_;
122 // average loss in cell i
123 std::vector<Real> average_;
124
127 };
128
130 public:
131 static Distribution convolve (const Distribution& d1,
132 const Distribution& d2);
133 };
134
135}
136
137#endif
void tranche(Real attachmentPoint, Real detachmentPoint)
std::vector< Real > density_
std::vector< Real > excessProbability_
Real cumulativeDensity(Real x)
void add(Real value)
Real cumulativeExcessProbability(Real a, Real b)
Real confidenceLevel(Real quantil)
std::vector< int > count_
std::vector< Real > cumulativeDensity_
Real cumulativeExcess(Size k)
std::vector< Real > cumulativeExcessProbability_
Real trancheExpectedValue(Real a, Real d)
std::vector< Real > & x()
std::vector< Real > & dx()
void addDensity(int bucket, Real value)
std::vector< Real > x_
Real expectedShortfall(Real percValue)
std::vector< Real > dx_
std::vector< Real > average_
void addAverage(int bucket, Real value)
static Distribution convolve(const Distribution &d1, const Distribution &d2)
Date d
ext::function< Real(Real)> b
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Real F
Definition: sabr.cpp:200
Custom types.