1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Roland Lichters
5 Copyright (C) 2008 Chris Kenyon
6 Copyright (C) 2008 StatPro Italia srl
7 Copyright (C) 2009 Ferdinando Ametrano
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
23#include <ql/termstructures/defaulttermstructure.hpp>
24#include <ql/utilities/dataformatters.hpp>
25#include <utility>
26
27namespace QuantLib {
28
29 DefaultProbabilityTermStructure::DefaultProbabilityTermStructure(
30 const DayCounter& dc, std::vector<Handle<Quote> > jumps, const std::vector<Date>& jumpDates)
31 : TermStructure(dc), jumps_(std::move(jumps)), jumpDates_(jumpDates),
32 jumpTimes_(jumpDates.size()), nJumps_(jumps_.size()) {
33 setJumps();
34 for (Size i=0; i<nJumps_; ++i)
35 registerWith(h: jumps_[i]);
36 }
37
38 DefaultProbabilityTermStructure::DefaultProbabilityTermStructure(
39 const Date& referenceDate,
40 const Calendar& cal,
41 const DayCounter& dc,
42 std::vector<Handle<Quote> > jumps,
43 const std::vector<Date>& jumpDates)
44 : TermStructure(referenceDate, cal, dc), jumps_(std::move(jumps)), jumpDates_(jumpDates),
45 jumpTimes_(jumpDates.size()), nJumps_(jumps_.size()) {
46 setJumps();
47 for (Size i=0; i<nJumps_; ++i)
48 registerWith(h: jumps_[i]);
49 }
50
51 DefaultProbabilityTermStructure::DefaultProbabilityTermStructure(
52 Natural settlementDays,
53 const Calendar& cal,
54 const DayCounter& dc,
55 std::vector<Handle<Quote> > jumps,
56 const std::vector<Date>& jumpDates)
57 : TermStructure(settlementDays, cal, dc), jumps_(std::move(jumps)), jumpDates_(jumpDates),
58 jumpTimes_(jumpDates.size()), nJumps_(jumps_.size()) {
59 setJumps();
60 for (Size i=0; i<nJumps_; ++i)
61 registerWith(h: jumps_[i]);
62 }
63
64 void DefaultProbabilityTermStructure::setJumps() {
65 if (jumpDates_.empty() && !jumps_.empty()) { // turn of year dates
66 jumpDates_.resize(new_size: nJumps_);
67 jumpTimes_.resize(new_size: nJumps_);
68 Year y = referenceDate().year();
69 for (Size i=0; i<nJumps_; ++i)
70 jumpDates_[i] = Date(31, December, y+i);
71 } else { // fixed dats
72 QL_REQUIRE(jumpDates_.size()==nJumps_,
73 "mismatch between number of jumps (" << nJumps_ <<
74 ") and jump dates (" << jumpDates_.size() << ")");
75 }
76 for (Size i=0; i<nJumps_; ++i)
77 jumpTimes_[i] = timeFromReference(d: jumpDates_[i]);
78 latestReference_ = referenceDate();
79 }
80
81 Probability DefaultProbabilityTermStructure::survivalProbability(
82 Time t,
83 bool extrapolate) const {
84 checkRange(t, extrapolate);
85
86 if (!jumps_.empty()) {
87 Probability jumpEffect = 1.0;
88 for (Size i=0; i<nJumps_ && jumpTimes_[i]<t; ++i) {
89 QL_REQUIRE(jumps_[i]->isValid(),
90 "invalid " << io::ordinal(i+1) << " jump quote");
91 DiscountFactor thisJump = jumps_[i]->value();
92 QL_REQUIRE(thisJump > 0.0 && thisJump <= 1.0,
93 "invalid " << io::ordinal(i+1) << " jump value: " <<
94 thisJump);
95 jumpEffect *= thisJump;
96 }
97 return jumpEffect * survivalProbabilityImpl(t);
98 }
99
100 return survivalProbabilityImpl(t);
101 }
102
103 Probability DefaultProbabilityTermStructure::defaultProbability(
104 const Date& d1,
105 const Date& d2,
106 bool extrapolate) const {
107 QL_REQUIRE(d1 <= d2,
108 "initial date (" << d1 << ") "
109 "later than final date (" << d2 << ")");
110 Probability p1 = d1 < referenceDate() ? 0.0 :
111 defaultProbability(d: d1,extrapolate),
112 p2 = defaultProbability(d: d2,extrapolate);
113 return p2 - p1;
114 }
115
116 Probability DefaultProbabilityTermStructure::defaultProbability(
117 Time t1,
118 Time t2,
119 bool extrapolate) const {
120 QL_REQUIRE(t1 <= t2,
121 "initial time (" << t1 << ") "
122 "later than final time (" << t2 << ")");
123 Probability p1 = t1 < 0.0 ? 0.0 : defaultProbability(t: t1,extrapolate),
124 p2 = defaultProbability(t: t2,extrapolate);
125 return p2 - p1;
126 }
127
128}
129

source code of quantlib/ql/termstructures/defaulttermstructure.cpp