/src/quantlib/ql/experimental/credit/defaultprobabilitykey.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 0 | explicit points_to(const DefaultType& t) : t(t) {} |
36 | 0 | bool operator()(const ext::shared_ptr<DefaultType>& p) const { |
37 | 0 | return *p == t; |
38 | 0 | } |
39 | | const DefaultType& t; |
40 | | }; |
41 | | |
42 | | } |
43 | | |
44 | 0 | bool operator==(const DefaultProbKey& lhs, const DefaultProbKey& rhs) { |
45 | 0 | if(lhs.seniority() != rhs.seniority()) return false; |
46 | 0 | if(lhs.currency() != rhs.currency()) return false; |
47 | | |
48 | 0 | Size mySize = rhs.eventTypes().size(); |
49 | 0 | if(mySize != lhs.eventTypes().size()) return false; |
50 | | // the all types must be equal in the weak sense. |
51 | 0 | for(Size i=0; i<mySize; i++) { |
52 | 0 | if(std::find_if(lhs.eventTypes().begin(), lhs.eventTypes().end(), |
53 | 0 | points_to(*rhs.eventTypes()[i])) == lhs.eventTypes().end()) |
54 | 0 | return false; |
55 | 0 | }// naah, I bet this can be done with a double lambda |
56 | 0 | return true; |
57 | 0 | } |
58 | | |
59 | 0 | DefaultProbKey::DefaultProbKey() = default; |
60 | | |
61 | | DefaultProbKey::DefaultProbKey(std::vector<ext::shared_ptr<DefaultType> > eventTypes, |
62 | | Currency cur, |
63 | | Seniority sen) |
64 | 0 | : eventTypes_(std::move(eventTypes)), obligationCurrency_(std::move(cur)), seniority_(sen) { |
65 | 0 | std::set<AtomicDefault::Type> buffer; |
66 | 0 | Size numEvents = eventTypes_.size(); |
67 | 0 | for(Size i=0; i< numEvents; i++) |
68 | 0 | buffer.insert(eventTypes_[i]->defaultType()); |
69 | 0 | QL_REQUIRE(buffer.size() == numEvents, |
70 | 0 | "Duplicated event type in contract definition"); |
71 | 0 | } |
72 | | |
73 | | NorthAmericaCorpDefaultKey::NorthAmericaCorpDefaultKey( |
74 | | const Currency& currency, |
75 | | Seniority sen, |
76 | | Period graceFailureToPay, |
77 | | Real amountFailure, |
78 | | Restructuring::Type resType) |
79 | 0 | : DefaultProbKey(std::vector<ext::shared_ptr<DefaultType> >(), |
80 | 0 | currency, sen) { |
81 | 0 | eventTypes_.push_back( ext::shared_ptr<DefaultType>( |
82 | 0 | new FailureToPay(graceFailureToPay, |
83 | 0 | amountFailure))); |
84 | | // no specifics for Bankruptcy |
85 | 0 | eventTypes_.push_back( ext::make_shared<DefaultType>( |
86 | 0 | AtomicDefault::Bankruptcy, |
87 | 0 | Restructuring::XR)); |
88 | 0 | if(resType != Restructuring::NoRestructuring) |
89 | 0 | eventTypes_.push_back( ext::make_shared<DefaultType>( |
90 | 0 | AtomicDefault::Restructuring, resType)); |
91 | 0 | } |
92 | | |
93 | | } |
94 | | |
95 | | #if defined(QL_PATCH_MSVC) |
96 | | #pragma warning(pop) |
97 | | #endif |