QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
defaulttermstructure.cpp
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
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)
36 }
37
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)
49 }
50
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)
62 }
63
65 if (jumpDates_.empty() && !jumps_.empty()) { // turn of year dates
66 jumpDates_.resize(nJumps_);
67 jumpTimes_.resize(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)
79 }
80
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
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(d1,extrapolate),
112 p2 = defaultProbability(d2,extrapolate);
113 return p2 - p1;
114 }
115
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(t1,extrapolate),
124 p2 = defaultProbability(t2,extrapolate);
125 return p2 - p1;
126 }
127
128}
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
Year year() const
Definition: date.cpp:93
day counter class
Definition: daycounter.hpp:44
Probability survivalProbability(const Date &d, bool extrapolate=false) const
virtual Probability survivalProbabilityImpl(Time) const =0
survival probability calculation
Probability defaultProbability(const Date &d, bool extrapolate=false) const
DefaultProbabilityTermStructure(const DayCounter &dc=DayCounter(), std::vector< Handle< Quote > > jumps={}, const std::vector< Date > &jumpDates={})
Shared handle to an observable.
Definition: handle.hpp:41
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Basic term-structure functionality.
virtual const Date & referenceDate() const
the date at which discount = 1.0 and/or variance = 0.0
Time timeFromReference(const Date &date) const
date/time conversion
void checkRange(const Date &d, bool extrapolate) const
date-range check
Integer Year
Year number.
Definition: date.hpp:87
@ December
Definition: date.hpp:68
detail::ordinal_holder ordinal(Size)
outputs naturals as 1st, 2nd, 3rd...
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Real Probability
probability
Definition: types.hpp:82
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.