| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 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/defaultprobabilitykey.hpp> |
| 22 | #if defined(QL_PATCH_MSVC) |
| 23 | #pragma warning(push) |
| 24 | #pragma warning(disable:4181) |
| 25 | #endif |
| 26 | #include <algorithm> |
| 27 | #include <set> |
| 28 | #include <utility> |
| 29 | |
| 30 | namespace QuantLib { |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | struct points_to { |
| 35 | explicit points_to(const DefaultType& t) : t(t) {} |
| 36 | bool operator()(const ext::shared_ptr<DefaultType>& p) const { |
| 37 | return *p == t; |
| 38 | } |
| 39 | const DefaultType& t; |
| 40 | }; |
| 41 | |
| 42 | } |
| 43 | |
| 44 | bool operator==(const DefaultProbKey& lhs, const DefaultProbKey& rhs) { |
| 45 | if(lhs.seniority() != rhs.seniority()) return false; |
| 46 | if(lhs.currency() != rhs.currency()) return false; |
| 47 | |
| 48 | Size mySize = rhs.eventTypes().size(); |
| 49 | if(mySize != lhs.eventTypes().size()) return false; |
| 50 | // the all types must be equal in the weak sense. |
| 51 | for(Size i=0; i<mySize; i++) { |
| 52 | if(std::find_if(first: lhs.eventTypes().begin(), last: lhs.eventTypes().end(), |
| 53 | pred: points_to(*rhs.eventTypes()[i])) == lhs.eventTypes().end()) |
| 54 | return false; |
| 55 | }// naah, I bet this can be done with a double lambda |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | DefaultProbKey::DefaultProbKey() = default; |
| 60 | |
| 61 | DefaultProbKey::DefaultProbKey(std::vector<ext::shared_ptr<DefaultType> > eventTypes, |
| 62 | Currency cur, |
| 63 | Seniority sen) |
| 64 | : eventTypes_(std::move(eventTypes)), obligationCurrency_(std::move(cur)), seniority_(sen) { |
| 65 | std::set<AtomicDefault::Type> buffer; |
| 66 | Size numEvents = eventTypes_.size(); |
| 67 | for(Size i=0; i< numEvents; i++) |
| 68 | buffer.insert(x: eventTypes_[i]->defaultType()); |
| 69 | QL_REQUIRE(buffer.size() == numEvents, |
| 70 | "Duplicated event type in contract definition" ); |
| 71 | } |
| 72 | |
| 73 | NorthAmericaCorpDefaultKey::NorthAmericaCorpDefaultKey( |
| 74 | const Currency& currency, |
| 75 | Seniority sen, |
| 76 | Period graceFailureToPay, |
| 77 | Real amountFailure, |
| 78 | Restructuring::Type resType) |
| 79 | : DefaultProbKey(std::vector<ext::shared_ptr<DefaultType> >(), |
| 80 | currency, sen) { |
| 81 | eventTypes_.push_back( x: ext::shared_ptr<DefaultType>( |
| 82 | new FailureToPay(graceFailureToPay, |
| 83 | amountFailure))); |
| 84 | // no specifics for Bankruptcy |
| 85 | eventTypes_.push_back( x: ext::make_shared<DefaultType>( |
| 86 | args: AtomicDefault::Bankruptcy, |
| 87 | args: Restructuring::XR)); |
| 88 | if(resType != Restructuring::NoRestructuring) |
| 89 | eventTypes_.push_back( x: ext::make_shared<DefaultType>( |
| 90 | args: AtomicDefault::Restructuring, args&: resType)); |
| 91 | } |
| 92 | |
| 93 | } |
| 94 | |
| 95 | #if defined(QL_PATCH_MSVC) |
| 96 | #pragma warning(pop) |
| 97 | #endif |
| 98 | |