Coverage Report

Created: 2026-03-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/pricingengines/credit/isdacdsengine.hpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2014 Jose Aparicio
5
 Copyright (C) 2014 Peter Caspers
6
7
 This file is part of QuantLib, a free-software/open-source library
8
 for financial quantitative analysts and developers - http://quantlib.org/
9
10
 QuantLib is free software: you can redistribute it and/or modify it
11
 under the terms of the QuantLib license.  You should have received a
12
 copy of the license along with this program; if not, please email
13
 <quantlib-dev@lists.sf.net>. The license is also available online at
14
 <https://www.quantlib.org/license.shtml>.
15
16
 This program is distributed in the hope that it will be useful, but WITHOUT
17
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
 FOR A PARTICULAR PURPOSE.  See the license for more details.
19
*/
20
21
/*! \file isdacdsengine.hpp
22
    \brief ISDA engine for credit default swaps
23
*/
24
25
#ifndef quantlib_isda_cds_engine_hpp
26
#define quantlib_isda_cds_engine_hpp
27
28
#include <ql/instruments/creditdefaultswap.hpp>
29
#include <ql/termstructures/yieldtermstructure.hpp>
30
#include <ql/termstructures/defaulttermstructure.hpp>
31
#include <ql/optional.hpp>
32
33
namespace QuantLib {
34
35
    /*! References:
36
37
        [1] The Pricing and Risk Management of Credit Default Swaps, with a
38
            Focus on the ISDA Model,
39
            OpenGamma Quantitative Research, Version as of 15-Oct-2013
40
41
        [2] ISDA CDS Standard Model Proposed Numerical Fix \ Thursday,
42
            November 15, 2012, Markit
43
44
        [3] Markit Interest Rate Curve XML Specifications,
45
            Version 1.16, Tuesday, 15 October 2013
46
47
    */
48
49
    class IsdaCdsEngine : public CreditDefaultSwap::engine {
50
51
      public:
52
        /*! According to [1] the settings for the flags
53
            AccrualBias / ForwardsInCouponPeriod corresponding
54
            to the standard model implementation C code are
55
56
            prior 1.8.2    HalfDayBias / Flat
57
            1.8.2          NoBias / Flat
58
59
            The theoretical correct setting would be NoBias / Piecewise
60
61
            Todo: Clarify in which version of the standard model
62
            implementation C code the numerical problem of zero denominators
63
            is solved and how exactly.
64
        */
65
66
        enum NumericalFix {
67
            None,  // as in [1] footnote 26 (i.e. 10^{-50} is added to
68
                   // denominators $f_i+h_i$$)
69
            Taylor // as in [2] i.e. for $f_i+h_i < 10^{-4}$ a Taylor expansion
70
                   // is used to avoid zero denominators
71
        };
72
73
        enum AccrualBias {
74
            HalfDayBias, // as in [1] formula (50), second (error) term is
75
                         // included
76
            NoBias // as in [1], but second term in formula (50) is not included
77
        };
78
79
        enum ForwardsInCouponPeriod {
80
            Flat, // as in [1], formula (52), second (error) term is included
81
            Piecewise // as in [1], but second term in formula (52) is not
82
                      // included
83
        };
84
85
        /*! Constructor where the client code is responsible for providing a
86
            default curve and an interest rate curve compliant with the ISDA
87
            specifications.
88
89
            To be precisely consistent with the ISDA specification
90
                bool IborCoupon::Settings::usingAtParCoupons();
91
            must be true. This is not checked in order not to
92
            kill the engine completely in this case.
93
94
            Furthermore, the ibor index in the swap rate helpers should not
95
            provide the evaluation date's fixing.
96
        */
97
98
        IsdaCdsEngine(Handle<DefaultProbabilityTermStructure> probability,
99
                      Real recoveryRate,
100
                      Handle<YieldTermStructure> discountCurve,
101
                      const ext::optional<bool>& includeSettlementDateFlows = ext::nullopt,
102
                      NumericalFix numericalFix = Taylor,
103
                      AccrualBias accrualBias = HalfDayBias,
104
                      ForwardsInCouponPeriod forwardsInCouponPeriod = Piecewise);
105
106
0
        Handle<YieldTermStructure> isdaRateCurve() const { return discountCurve_; }
107
0
        Handle<DefaultProbabilityTermStructure> isdaCreditCurve() const { return probability_; }
108
109
        void calculate() const override;
110
111
      private:
112
        Handle<DefaultProbabilityTermStructure> probability_;
113
        const Real recoveryRate_;
114
        Handle<YieldTermStructure> discountCurve_;
115
        const ext::optional<bool> includeSettlementDateFlows_;
116
        const NumericalFix numericalFix_;
117
        const AccrualBias accrualBias_;
118
        const ForwardsInCouponPeriod forwardsInCouponPeriod_;
119
    };
120
}
121
122
#endif