Coverage Report

Created: 2026-08-14 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/credit/midpointcdoengine.cpp
Line
Count
Source
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) 2009, 2014 Jose Aparicio
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
#include <ql/experimental/credit/midpointcdoengine.hpp>
22
23
#ifndef QL_PATCH_SOLARIS
24
25
#include <ql/cashflows/fixedratecoupon.hpp>
26
#include <ql/termstructures/yieldtermstructure.hpp>
27
28
namespace QuantLib {
29
30
0
    void MidPointCDOEngine::calculate() const {
31
0
        Date today = Settings::instance().evaluationDate();
32
33
0
        results_.premiumValue = 0.0;
34
0
        results_.protectionValue = 0.0;
35
0
        results_.upfrontPremiumValue = 0.0;
36
0
        results_.error = 0;
37
0
        results_.expectedTrancheLoss.clear();
38
        // todo Should be remaining when considering realized loses
39
0
        results_.xMin = arguments_.basket->attachmentAmount();
40
0
        results_.xMax = arguments_.basket->detachmentAmount();
41
0
        results_.remainingNotional = results_.xMax - results_.xMin;
42
0
        const Real inceptionTrancheNotional = 
43
0
            arguments_.basket->trancheNotional();
44
45
        // compute expected loss at the beginning of first relevant period
46
0
        Real e1 = 0;
47
        // todo add includeSettlement date flows variable to engine.
48
0
        if (!arguments_.normalizedLeg[0]->hasOccurred(today))
49
            // Notice that since there might be a gap between the end of 
50
            // acrrual and payment dates and today be in between
51
            // the tranche loss on that date might not be contingent but 
52
            // realized:
53
0
            e1 = arguments_.basket->expectedTrancheLoss(
54
0
                coupon_cast(
55
0
                    arguments_.normalizedLeg[0])->accrualStartDate());
56
0
        results_.expectedTrancheLoss.push_back(e1);
57
        //'e1'  should contain the existing loses.....? use remaining amounts?
58
0
        for (auto& i : arguments_.normalizedLeg) {
59
0
            if (i->hasOccurred(today)) {
60
0
                results_.expectedTrancheLoss.push_back(0.);
61
0
                continue;
62
0
            }
63
0
            ext::shared_ptr<Coupon> coupon = coupon_cast(i);
64
0
            Date paymentDate = coupon->date();
65
0
            Date startDate = std::max(coupon->accrualStartDate(),
66
0
                                      discountCurve_->referenceDate());
67
0
            Date endDate = coupon->accrualEndDate();
68
            // we assume the loss within the period took place on this date:
69
0
            Date defaultDate = startDate + (endDate-startDate)/2;
70
71
0
            Real e2 = arguments_.basket->expectedTrancheLoss(endDate);
72
0
            results_.expectedTrancheLoss.push_back(e2);
73
0
            results_.premiumValue += 
74
0
                ((inceptionTrancheNotional - e2) / inceptionTrancheNotional)
75
0
                * coupon->amount()
76
0
                * discountCurve_->discount(paymentDate);
77
            // default flows:
78
0
            const Real discount = discountCurve_->discount(defaultDate);
79
80
            /* Accrual removed till the argument flag is implemented
81
            // pays accrued on defaults' date
82
            results_.premiumValue += coupon->accruedAmount(defaultDate)
83
                * discount * (e2 - e1) / inceptionTrancheNotional;
84
            */
85
0
            results_.protectionValue += discount * (e2 - e1);
86
            /* use it in a future version for coherence with the integral engine
87
            * arguments_.leverageFactor;
88
            */
89
0
            e1 = e2;
90
0
        }
91
92
        //\todo treat upfron tnow as in the new CDS (see March 2014)
93
        // add includeSettlement date flows variable to engine ?
94
0
        if (!arguments_.normalizedLeg[0]->hasOccurred(today))
95
0
            results_.upfrontPremiumValue 
96
0
                = inceptionTrancheNotional * arguments_.upfrontRate 
97
0
                    * discountCurve_->discount(
98
0
                        coupon_cast(
99
0
                            arguments_.normalizedLeg[0])->accrualStartDate());
100
            /* use it in a future version for coherence with the integral engine
101
                arguments_.leverageFactor * ;
102
            */
103
0
        if (arguments_.side == Protection::Buyer) {
104
0
            results_.protectionValue *= -1;
105
0
            results_.premiumValue *= -1;
106
0
            results_.upfrontPremiumValue *= -1;
107
0
        }
108
0
        results_.value = results_.premiumValue - results_.protectionValue
109
0
            + results_.upfrontPremiumValue;
110
0
        results_.errorEstimate = Null<Real>();
111
        // Fair spread GIVEN the upfront
112
0
        Real fairSpread = 0.;
113
0
        if (results_.premiumValue != 0.0) {
114
0
            fairSpread =
115
0
                -(results_.protectionValue + results_.upfrontPremiumValue)
116
0
                  *arguments_.runningRate/results_.premiumValue;
117
0
        }
118
119
0
        results_.additionalResults["fairPremium"] = fairSpread;
120
0
        results_.additionalResults["premiumLegNPV"] = 
121
0
            Real(results_.premiumValue + results_.upfrontPremiumValue);
122
0
        results_.additionalResults["protectionLegNPV"] = 
123
0
            results_.protectionValue;
124
0
    }
125
126
}
127
128
#endif