Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
spreadedsurvivalprobabilitytermstructure.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21namespace QuantExt {
22
24 const Handle<DefaultProbabilityTermStructure>& referenceCurve, const std::vector<Time>& times,
25 const std::vector<Handle<Quote>>& spreads, const Extrapolation extrapolation)
26 : SurvivalProbabilityStructure(referenceCurve->dayCounter()), referenceCurve_(referenceCurve), times_(times),
27 spreads_(spreads), data_(times.size(), 1.0), extrapolation_(extrapolation) {
28 QL_REQUIRE(times_.size() > 1, "at least two times required");
29 QL_REQUIRE(times_.size() == spreads_.size(), "size of time and quote vectors do not match");
30 QL_REQUIRE(times_[0] == 0.0, "First time must be 0, got " << times_[0]);
31 for (Size i = 0; i < spreads_.size(); ++i) {
32 registerWith(spreads_[i]);
33 }
34 interpolation_ = QuantLib::ext::make_shared<LogLinearInterpolation>(times_.begin(), times_.end(), data_.begin());
35 interpolation_->enableExtrapolation();
36 registerWith(Settings::instance().evaluationDate());
37 registerWith(referenceCurve_);
38}
39
41 LazyObject::update();
42 TermStructure::update();
43}
44
46 for (Size i = 0; i < times_.size(); ++i) {
47 QL_REQUIRE(!spreads_[i].empty(),
48 "SpreadedSurvivalProbabilityTermStructure: quote at index " << i << " is empty");
49 data_[i] = spreads_[i]->value();
50 QL_REQUIRE(data_[i] > 0,
51 "SpreadedSurvivalProbabilityTermStructure: invalid value " << data_[i] << " at index " << i);
52 }
53 interpolation_->update();
54}
55
57 calculate();
58 if (t <= this->times_.back())
59 return referenceCurve_->survivalProbability(t) * (*interpolation_)(t, true);
60 // flat fwd extrapolation
61 Real tMax = this->times_.back();
62 Real dMax = this->data_.back();
64 Real instFwdMax = -(*interpolation_).derivative(tMax) / dMax;
65 return referenceCurve_->survivalProbability(t) * dMax * std::exp(-instFwdMax * (t - tMax));
66 } else {
67 return referenceCurve_->survivalProbability(t) * std::pow(dMax, t / tMax);
68 }
69}
70
72
74
76
77const Date& SpreadedSurvivalProbabilityTermStructure::referenceDate() const { return referenceCurve_->referenceDate(); }
78
80
82
84
85Handle<DefaultProbabilityTermStructure> SpreadedSurvivalProbabilityTermStructure::referenceCurve() const {
86 return referenceCurve_;
87}
88} // namespace QuantExt
Handle< DefaultProbabilityTermStructure > referenceCurve() const
SpreadedSurvivalProbabilityTermStructure(const Handle< DefaultProbabilityTermStructure > &referenceCurve, const std::vector< Time > &times, const std::vector< Handle< Quote > > &spreads, const Extrapolation extrapolation=Extrapolation::flatFwd)
times should be consistent with reference ts day counter
spreaded default term structure