File: issuer.cpp

package info (click to toggle)
quantlib 1.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 30,760 kB
  • sloc: cpp: 232,809; ansic: 21,483; sh: 11,108; makefile: 4,717; lisp: 86
file content (110 lines) | stat: -rw-r--r-- 4,407 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2008, 2009 StatPro Italia srl
 Copyright (C) 2009 Jose Aparicio

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <http://quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

#include <ql/experimental/credit/issuer.hpp>

namespace QuantLib {

    namespace {
        bool between(const boost::shared_ptr<DefaultEvent>& e,
                     const Date& start,
                     const Date& end,
                     bool includeRefDate = false) {
            return !e->hasOccurred(start, includeRefDate) &&
                e->hasOccurred(end, includeRefDate);
        }
    }

    Issuer::Issuer(const std::vector<std::pair<DefaultProbKey,
                    Handle<DefaultProbabilityTermStructure> > >&
                    probabilities,
                   const DefaultEventSet& events
        )
    : probabilities_(probabilities), events_(events) { }

    Issuer::Issuer(const std::vector<std::vector<
                       boost::shared_ptr<DefaultType> > >& eventTypes,
                   const std::vector<Currency>& currencies,
                   const std::vector<Seniority>& seniorities,
                   const std::vector<Handle<
                       DefaultProbabilityTermStructure> >& curves,
                   const DefaultEventSet& events)
    : events_(events) {
        QL_REQUIRE((eventTypes.size() == curves.size()) &&
            (curves.size()== currencies.size()) &&
            (currencies.size() == seniorities.size()),
            "Incompatible size of Issuer parameters.");

        for(Size i=0; i <eventTypes.size(); i++) {
            DefaultProbKey keytmp(eventTypes[i], currencies[i],
                seniorities[i]);
            probabilities_.push_back(std::make_pair(keytmp, curves[i]));
        }
    }

    const Handle<DefaultProbabilityTermStructure>&
        Issuer::defaultProbability(const DefaultProbKey& key) const {
        for(Size i=0; i<probabilities_.size(); i++)
            if(key == probabilities_[i].first)
                return probabilities_[i].second;
        QL_FAIL("Probability curve not available.");
    }

    boost::shared_ptr<DefaultEvent>
    Issuer::defaultedBetween(const Date& start,
                             const Date& end,
                             const DefaultProbKey& contractKey,
                             bool includeRefDate
                             ) const
    {
        // to do: the set is ordered, see how to use it to speed this up
        for(DefaultEventSet::const_iterator itev = events_.begin();
            // am i really speeding things up with the date comp?
            itev != events_.end(); // && (*itev)->date() > start;
            itev++) {
            if((*itev)->matchesDefaultKey(contractKey) &&
                between(*itev, start, end, includeRefDate))
                return *itev;
        }
        return boost::shared_ptr<DefaultEvent>();
    }


    std::vector<boost::shared_ptr<DefaultEvent> >
    Issuer::defaultsBetween(const Date& start,
                            const Date& end,
                            const DefaultProbKey& contractKey,
                            bool includeRefDate
                            ) const
    {
        std::vector<boost::shared_ptr<DefaultEvent> > defaults;
        // to do: the set is ordered, see how to use it to speed this up
        for(DefaultEventSet::const_iterator itev = events_.begin();
            // am i really speeding things up with the date comp?
            itev != events_.end(); // && (*itev)->date() > start;
            itev++) {
            if((*itev)->matchesDefaultKey(contractKey) &&
                between(*itev, start, end, includeRefDate))
                defaults.push_back(*itev);
        }
        return defaults;
    }

}