/src/quantlib/ql/experimental/credit/issuer.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2008, 2009 StatPro Italia srl |
5 | | Copyright (C) 2009 Jose Aparicio |
6 | | |
7 | | This file is part of QuantLib, a free-software/open-source library |
8 | | for financial quantitative analysts and developers - http://quantlib.org/ |
9 | | |
10 | | QuantLib is free software: you can redistribute it and/or modify it |
11 | | under the terms of the QuantLib license. You should have received a |
12 | | copy of the license along with this program; if not, please email |
13 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
14 | | <https://www.quantlib.org/license.shtml>. |
15 | | |
16 | | This program is distributed in the hope that it will be useful, but WITHOUT |
17 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
18 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
19 | | */ |
20 | | |
21 | | #include <ql/experimental/credit/issuer.hpp> |
22 | | #include <utility> |
23 | | |
24 | | namespace QuantLib { |
25 | | |
26 | | namespace { |
27 | | bool between(const ext::shared_ptr<DefaultEvent>& e, |
28 | | const Date& start, |
29 | | const Date& end, |
30 | 0 | bool includeRefDate = false) { |
31 | 0 | return !e->hasOccurred(start, includeRefDate) && |
32 | 0 | e->hasOccurred(end, includeRefDate); |
33 | 0 | } |
34 | | } |
35 | | |
36 | | Issuer::Issuer(std::vector<std::pair<DefaultProbKey, Handle<DefaultProbabilityTermStructure> > > |
37 | | probabilities, |
38 | | DefaultEventSet events) |
39 | 0 | : probabilities_(std::move(probabilities)), events_(std::move(events)) {} |
40 | | |
41 | | Issuer::Issuer(const std::vector<std::vector<ext::shared_ptr<DefaultType> > >& eventTypes, |
42 | | const std::vector<Currency>& currencies, |
43 | | const std::vector<Seniority>& seniorities, |
44 | | const std::vector<Handle<DefaultProbabilityTermStructure> >& curves, |
45 | | DefaultEventSet events) |
46 | 0 | : events_(std::move(events)) { |
47 | 0 | QL_REQUIRE((eventTypes.size() == curves.size()) && |
48 | 0 | (curves.size()== currencies.size()) && |
49 | 0 | (currencies.size() == seniorities.size()), |
50 | 0 | "Incompatible size of Issuer parameters."); |
51 | | |
52 | 0 | for(Size i=0; i <eventTypes.size(); i++) { |
53 | 0 | DefaultProbKey keytmp(eventTypes[i], currencies[i], |
54 | 0 | seniorities[i]); |
55 | 0 | probabilities_.emplace_back(keytmp, curves[i]); |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | | const Handle<DefaultProbabilityTermStructure>& |
60 | 0 | Issuer::defaultProbability(const DefaultProbKey& key) const { |
61 | 0 | for (const auto& probabilitie : probabilities_) |
62 | 0 | if (key == probabilitie.first) |
63 | 0 | return probabilitie.second; |
64 | 0 | QL_FAIL("Probability curve not available."); |
65 | 0 | } |
66 | | |
67 | | ext::shared_ptr<DefaultEvent> |
68 | | Issuer::defaultedBetween(const Date& start, |
69 | | const Date& end, |
70 | | const DefaultProbKey& contractKey, |
71 | | bool includeRefDate |
72 | | ) const |
73 | 0 | { |
74 | | // to do: the set is ordered, see how to use it to speed this up |
75 | 0 | for (const auto& event : events_) { |
76 | 0 | if (event->matchesDefaultKey(contractKey) && between(event, start, end, includeRefDate)) |
77 | 0 | return event; |
78 | 0 | } |
79 | 0 | return ext::shared_ptr<DefaultEvent>(); |
80 | 0 | } |
81 | | |
82 | | |
83 | | std::vector<ext::shared_ptr<DefaultEvent> > |
84 | | Issuer::defaultsBetween(const Date& start, |
85 | | const Date& end, |
86 | | const DefaultProbKey& contractKey, |
87 | | bool includeRefDate |
88 | | ) const |
89 | 0 | { |
90 | 0 | std::vector<ext::shared_ptr<DefaultEvent> > defaults; |
91 | | // to do: the set is ordered, see how to use it to speed this up |
92 | 0 | for (const auto& event : events_) { |
93 | 0 | if (event->matchesDefaultKey(contractKey) && between(event, start, end, includeRefDate)) |
94 | 0 | defaults.push_back(event); |
95 | 0 | } |
96 | 0 | return defaults; |
97 | 0 | } |
98 | | |
99 | | } |