Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/experimental/catbonds/catbond.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) 2012, 2013 Grzegorz Andruszkiewicz
5
6
 This file is part of QuantLib, a free-software/open-source library
7
 for financial quantitative analysts and developers - http://quantlib.org/
8
9
 QuantLib is free software: you can redistribute it and/or modify it
10
 under the terms of the QuantLib license.  You should have received a
11
 copy of the license along with this program; if not, please email
12
 <quantlib-dev@lists.sf.net>. The license is also available online at
13
 <https://www.quantlib.org/license.shtml>.
14
15
 This program is distributed in the hope that it will be useful, but WITHOUT
16
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
 FOR A PARTICULAR PURPOSE.  See the license for more details.
18
*/
19
20
#include <ql/experimental/catbonds/catbond.hpp>
21
#include <ql/settings.hpp>
22
#include <ql/experimental/credit/loss.hpp>
23
#include <ql/time/daycounters/actualactual.hpp>
24
#include <ql/cashflows/cashflowvectors.hpp>
25
#include <ql/cashflows/iborcoupon.hpp>
26
#include <ql/cashflows/couponpricer.hpp>
27
#include <ql/cashflows/simplecashflow.hpp>
28
29
using namespace std;
30
31
namespace QuantLib {
32
33
0
    void CatBond::arguments::validate() const {
34
0
        Bond::arguments::validate();
35
0
        QL_REQUIRE(notionalRisk, "null notionalRisk");
36
0
    }
37
38
0
    void CatBond::setupArguments(PricingEngine::arguments* args) const {
39
40
0
        auto* arguments = dynamic_cast<CatBond::arguments*>(args);
41
0
        QL_REQUIRE(arguments != nullptr, "wrong arguments type");
42
43
0
        Bond::setupArguments(args);
44
45
0
        arguments->notionalRisk = notionalRisk_;
46
0
        arguments->startDate = issueDate();
47
0
    }
48
49
0
    void CatBond::fetchResults(const PricingEngine::results* r) const {
50
0
        Bond::fetchResults(r);
51
52
0
        const auto* results = dynamic_cast<const CatBond::results*>(r);
53
0
        QL_ENSURE(results != nullptr, "wrong result type");
54
55
0
        lossProbability_ = results->lossProbability;
56
0
        expectedLoss_ = results->expectedLoss;
57
0
        exhaustionProbability_ = results->exhaustionProbability;
58
0
    }
59
60
    FloatingCatBond::FloatingCatBond(Natural settlementDays,
61
                                     Real faceAmount,
62
                                     Schedule schedule,
63
                                     const ext::shared_ptr<IborIndex>& iborIndex,
64
                                     const DayCounter& paymentDayCounter,
65
                                     const ext::shared_ptr<NotionalRisk>& notionalRisk,
66
                                     BusinessDayConvention paymentConvention,
67
                                     Natural fixingDays,
68
                                     const std::vector<Real>& gearings,
69
                                     const std::vector<Spread>& spreads,
70
                                     const std::vector<Rate>& caps,
71
                                     const std::vector<Rate>& floors,
72
                                     bool inArrears,
73
                                     Real redemption,
74
                                     const Date& issueDate)
75
0
    : CatBond(settlementDays, schedule.calendar(), issueDate, notionalRisk) {
76
77
0
        maturityDate_ = schedule.endDate();
78
79
0
        cashflows_ = IborLeg(std::move(schedule), iborIndex)
80
0
            .withNotionals(faceAmount)
81
0
            .withPaymentDayCounter(paymentDayCounter)
82
0
            .withPaymentAdjustment(paymentConvention)
83
0
            .withFixingDays(fixingDays)
84
0
            .withGearings(gearings)
85
0
            .withSpreads(spreads)
86
0
            .withCaps(caps)
87
0
            .withFloors(floors)
88
0
            .inArrears(inArrears);
89
90
0
        addRedemptionsToCashflows(std::vector<Real>(1, redemption));
91
92
0
        QL_ENSURE(!cashflows().empty(), "bond with no cashflows!");
93
0
        QL_ENSURE(redemptions_.size() == 1, "multiple redemptions created");
94
95
0
        registerWith(iborIndex);
96
0
    }
Unexecuted instantiation: QuantLib::FloatingCatBond::FloatingCatBond(unsigned int, double, QuantLib::Schedule, boost::shared_ptr<QuantLib::IborIndex> const&, QuantLib::DayCounter const&, boost::shared_ptr<QuantLib::NotionalRisk> const&, QuantLib::BusinessDayConvention, unsigned int, std::__1::vector<double, std::__1::allocator<double> > const&, std::__1::vector<double, std::__1::allocator<double> > const&, std::__1::vector<double, std::__1::allocator<double> > const&, std::__1::vector<double, std::__1::allocator<double> > const&, bool, double, QuantLib::Date const&)
Unexecuted instantiation: QuantLib::FloatingCatBond::FloatingCatBond(unsigned int, double, QuantLib::Schedule, boost::shared_ptr<QuantLib::IborIndex> const&, QuantLib::DayCounter const&, boost::shared_ptr<QuantLib::NotionalRisk> const&, QuantLib::BusinessDayConvention, unsigned int, std::__1::vector<double, std::__1::allocator<double> > const&, std::__1::vector<double, std::__1::allocator<double> > const&, std::__1::vector<double, std::__1::allocator<double> > const&, std::__1::vector<double, std::__1::allocator<double> > const&, bool, double, QuantLib::Date const&)
97
98
    FloatingCatBond::FloatingCatBond(Natural settlementDays,
99
                                     Real faceAmount,
100
                                     const Date& startDate,
101
                                     const Date& maturityDate,
102
                                     Frequency couponFrequency,
103
                                     const Calendar& calendar,
104
                                     const ext::shared_ptr<IborIndex>& iborIndex,
105
                                     const DayCounter& accrualDayCounter,
106
                                     const ext::shared_ptr<NotionalRisk>& notionalRisk,
107
                                     BusinessDayConvention accrualConvention,
108
                                     BusinessDayConvention paymentConvention,
109
                                     Natural fixingDays,
110
                                     const std::vector<Real>& gearings,
111
                                     const std::vector<Spread>& spreads,
112
                                     const std::vector<Rate>& caps,
113
                                     const std::vector<Rate>& floors,
114
                                     bool inArrears,
115
                                     Real redemption,
116
                                     const Date& issueDate,
117
                                     const Date& stubDate,
118
                                     DateGeneration::Rule rule,
119
                                     bool endOfMonth)
120
0
    : CatBond(settlementDays, calendar, issueDate, notionalRisk) {
121
122
0
        maturityDate_ = maturityDate;
123
124
0
        Date firstDate, nextToLastDate;
125
0
        switch (rule) {
126
0
          case DateGeneration::Backward:
127
0
            firstDate = Date();
128
0
            nextToLastDate = stubDate;
129
0
            break;
130
0
          case DateGeneration::Forward:
131
0
            firstDate = stubDate;
132
0
            nextToLastDate = Date();
133
0
            break;
134
0
          case DateGeneration::Zero:
135
0
          case DateGeneration::ThirdWednesday:
136
0
          case DateGeneration::Twentieth:
137
0
          case DateGeneration::TwentiethIMM:
138
0
            QL_FAIL("stub date (" << stubDate << ") not allowed with " <<
139
0
                    rule << " DateGeneration::Rule");
140
0
          default:
141
0
            QL_FAIL("unknown DateGeneration::Rule (" << Integer(rule) << ")");
142
0
        }
143
144
0
        Schedule schedule(startDate, maturityDate_, Period(couponFrequency),
145
0
                          calendar_, accrualConvention, accrualConvention,
146
0
                          rule, endOfMonth,
147
0
                          firstDate, nextToLastDate);
148
149
0
        cashflows_ = IborLeg(schedule, iborIndex)
150
0
            .withNotionals(faceAmount)
151
0
            .withPaymentDayCounter(accrualDayCounter)
152
0
            .withPaymentAdjustment(paymentConvention)
153
0
            .withFixingDays(fixingDays)
154
0
            .withGearings(gearings)
155
0
            .withSpreads(spreads)
156
0
            .withCaps(caps)
157
0
            .withFloors(floors)
158
0
            .inArrears(inArrears);
159
160
0
        addRedemptionsToCashflows(std::vector<Real>(1, redemption));
161
162
0
        QL_ENSURE(!cashflows().empty(), "bond with no cashflows!");
163
0
        QL_ENSURE(redemptions_.size() == 1, "multiple redemptions created");
164
165
0
        registerWith(iborIndex);
166
0
    }
Unexecuted instantiation: QuantLib::FloatingCatBond::FloatingCatBond(unsigned int, double, QuantLib::Date const&, QuantLib::Date const&, QuantLib::Frequency, QuantLib::Calendar const&, boost::shared_ptr<QuantLib::IborIndex> const&, QuantLib::DayCounter const&, boost::shared_ptr<QuantLib::NotionalRisk> const&, QuantLib::BusinessDayConvention, QuantLib::BusinessDayConvention, unsigned int, std::__1::vector<double, std::__1::allocator<double> > const&, std::__1::vector<double, std::__1::allocator<double> > const&, std::__1::vector<double, std::__1::allocator<double> > const&, std::__1::vector<double, std::__1::allocator<double> > const&, bool, double, QuantLib::Date const&, QuantLib::Date const&, QuantLib::DateGeneration::Rule, bool)
Unexecuted instantiation: QuantLib::FloatingCatBond::FloatingCatBond(unsigned int, double, QuantLib::Date const&, QuantLib::Date const&, QuantLib::Frequency, QuantLib::Calendar const&, boost::shared_ptr<QuantLib::IborIndex> const&, QuantLib::DayCounter const&, boost::shared_ptr<QuantLib::NotionalRisk> const&, QuantLib::BusinessDayConvention, QuantLib::BusinessDayConvention, unsigned int, std::__1::vector<double, std::__1::allocator<double> > const&, std::__1::vector<double, std::__1::allocator<double> > const&, std::__1::vector<double, std::__1::allocator<double> > const&, std::__1::vector<double, std::__1::allocator<double> > const&, bool, double, QuantLib::Date const&, QuantLib::Date const&, QuantLib::DateGeneration::Rule, bool)
167
168
}