| 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 | <http://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 | bool includeRefDate = false) { |
| 31 | return !e->hasOccurred(refDate: start, includeRefDate) && |
| 32 | e->hasOccurred(refDate: end, includeRefDate); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | Issuer::Issuer(std::vector<std::pair<DefaultProbKey, Handle<DefaultProbabilityTermStructure> > > |
| 37 | probabilities, |
| 38 | DefaultEventSet events) |
| 39 | : 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 | : events_(std::move(events)) { |
| 47 | QL_REQUIRE((eventTypes.size() == curves.size()) && |
| 48 | (curves.size()== currencies.size()) && |
| 49 | (currencies.size() == seniorities.size()), |
| 50 | "Incompatible size of Issuer parameters." ); |
| 51 | |
| 52 | for(Size i=0; i <eventTypes.size(); i++) { |
| 53 | DefaultProbKey keytmp(eventTypes[i], currencies[i], |
| 54 | seniorities[i]); |
| 55 | probabilities_.emplace_back(args&: keytmp, args: curves[i]); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | const Handle<DefaultProbabilityTermStructure>& |
| 60 | Issuer::defaultProbability(const DefaultProbKey& key) const { |
| 61 | for (const auto& probabilitie : probabilities_) |
| 62 | if (key == probabilitie.first) |
| 63 | return probabilitie.second; |
| 64 | QL_FAIL("Probability curve not available." ); |
| 65 | } |
| 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 | { |
| 74 | // to do: the set is ordered, see how to use it to speed this up |
| 75 | for (const auto& event : events_) { |
| 76 | if (event->matchesDefaultKey(contractKey) && between(e: event, start, end, includeRefDate)) |
| 77 | return event; |
| 78 | } |
| 79 | return ext::shared_ptr<DefaultEvent>(); |
| 80 | } |
| 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 | { |
| 90 | std::vector<ext::shared_ptr<DefaultEvent> > defaults; |
| 91 | // to do: the set is ordered, see how to use it to speed this up |
| 92 | for (const auto& event : events_) { |
| 93 | if (event->matchesDefaultKey(contractKey) && between(e: event, start, end, includeRefDate)) |
| 94 | defaults.push_back(x: event); |
| 95 | } |
| 96 | return defaults; |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |