Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
discountingswapenginedeltagamma.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 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
19/*! \file qle/pricingengines/discountingswapenginedeltagamma.hpp
20 \brief Swap engine providing analytical deltas and gammas for vanilla swaps
21
22 \ingroup engines
23*/
24
25#ifndef quantext_discounting_swap_engine_deltagamma_hpp
26#define quantext_discounting_swap_engine_deltagamma_hpp
27
28#include <ql/cashflows/cashflows.hpp>
29#include <ql/cashflows/fixedratecoupon.hpp>
30#include <ql/cashflows/iborcoupon.hpp>
31#include <ql/cashflows/simplecashflow.hpp>
32#include <ql/handle.hpp>
33#include <ql/instruments/swap.hpp>
34#include <ql/math/matrix.hpp>
35#include <ql/patterns/visitor.hpp>
36#include <ql/termstructures/yieldtermstructure.hpp>
38
40
41namespace QuantExt {
42using namespace QuantLib;
43
44//! Discounting swap engine providing analytical deltas and gammas
45/*! The provided deltas and gammas are (continuously compounded) zero yield deltas assuming linear in zero or
46 loglinear in discount factor interpolation on the discounting and forwarding curves with flat extrapolation
47 of the zero rate before the first and last bucket time. The deltas are provided as additional results
48
49 deltaDiscount (vector<Real> ): Delta on discount curve, rebucketed on time grid
50 deltaForward (vector<Real> ): Delta on forward curve, rebucketed on time grid
51 deltaBPS (vector<vector<Real>> ): Delta of BPS (on discount curve, per leg)
52
53 The gammas, likewise,
54
55 gamma (Matrix ): Gamma matrix with blocks | dsc-dsc dsc-fwd |
56 | dsc-fwd fwd-fwd |
57 gammaBps (vector<Matrix> ): Gamma of BPS (on dsc, per leg)
58
59 bucketTimes (vector<Real> ): Bucketing grid for deltas and gammas
60
61 \warning Deltas and gammas are produced only for fixed and Ibor coupons without caps or floors,
62 for Ibor coupons they are ignoring convexity adjustments (like in arrears adjustments). It is possible
63 to have different Ibor coupons (with different forward curves) on a leg, but the computed deltas would
64 be aggregated over all underlying curves then.
65
66 \warning Derivatives are not w.r.t. basispoints, but w.r.t. the usual unit
67 \warning BPS is the value of one unit (not one basispoint actually), it has to be divided by 10000.0 to get QL's BPS
68*/
69
71public:
72 DiscountingSwapEngineDeltaGamma(const Handle<YieldTermStructure>& discountCurve = Handle<YieldTermStructure>(),
73 const std::vector<Time>& bucketTimes = std::vector<Time>(),
74 const bool computeDelta = false, const bool computeGamma = false,
75 const bool computeBPS = false, const bool linearInZero = true);
76 void calculate() const override;
77 Handle<YieldTermStructure> discountCurve() const { return discountCurve_; }
78
79private:
80 Handle<YieldTermStructure> discountCurve_;
81 const std::vector<Real> bucketTimes_;
83};
84
85namespace detail {
86
87class NpvDeltaGammaCalculator : public AcyclicVisitor,
88 public Visitor<CashFlow>,
89 public Visitor<SimpleCashFlow>,
90 public Visitor<FixedRateCoupon>,
91 public Visitor<IborCoupon>,
92 public Visitor<QuantExt::OvernightIndexedCoupon>,
93 public Visitor<FloatingRateFXLinkedNotionalCoupon>,
94 public Visitor<FXLinkedCashFlow> {
95public:
96 // if excludeSimpleCashFlowsFromSensis = true, SimpleCashFlow's are excluded from all results, and their npv
97 // is collected in simpleCashFlowNpv instead
98 NpvDeltaGammaCalculator(Handle<YieldTermStructure> discountCurve, const Real payer, Real& npv, Real& bps,
99 const bool computeDelta, const bool computeGamma, const bool computeBPS,
100 std::map<Date, Real>& deltaDiscount, std::map<Date, Real>& deltaForward,
101 std::map<Date, Real>& deltaBPS, std::map<Date, Real>& gammaDiscount,
102 std::map<std::pair<Date, Date>, Real>& gammaForward,
103 std::map<std::pair<Date, Date>, Real>& gammaDscFwd, std::map<Date, Real>& gammaBPS,
104 Real& fxLinkedForeignNpv, const bool excludeSimpleCashFlowsFromSensis,
105 Real& simpleCashFlowNpv);
106 void visit(CashFlow& c) override;
107 void visit(SimpleCashFlow& c) override;
108 void visit(FixedRateCoupon& c) override;
109 void visit(IborCoupon& c) override;
111 void visit(FXLinkedCashFlow& c) override;
112 void visit(QuantExt::OvernightIndexedCoupon& c) override;
113
114private:
116
117 Handle<YieldTermStructure> discountCurve_;
118 const Real payer_;
119 Real &npv_, &bps_;
122 std::map<std::pair<Date, Date>, Real>&gammaForward_, &gammaDscFwd_;
123 std::map<Date, Real>& gammaBPS_;
127};
128
129std::vector<Real> rebucketDeltas(const std::vector<Time>& deltaTimes, const std::map<Date, Real>& deltaRaw,
130 const Date& referenceDate, const DayCounter& dc, const bool linearInZero);
131
132Matrix rebucketGammas(const std::vector<Time>& gammaTimes, const std::map<Date, Real>& gammaDscRaw,
133 std::map<std::pair<Date, Date>, Real>& gammaForward,
134 std::map<std::pair<Date, Date>, Real>& gammaDscFwd, const bool forceFullMatrix,
135 const Date& referenceDate, const DayCounter& dc, const bool linearInZero);
136
137} // namespace detail
138
139} // namespace QuantExt
140
141#endif
Discounting swap engine providing analytical deltas and gammas.
std::map< std::pair< Date, Date >, Real > & gammaDscFwd_
std::map< std::pair< Date, Date >, Real > & gammaForward_
Coupon paying a Libor-type index but with an FX linked notional.
Matrix rebucketGammas(const std::vector< Time > &gammaTimes, const std::map< Date, Real > &gammaDscRaw, std::map< std::pair< Date, Date >, Real > &gammaForward, std::map< std::pair< Date, Date >, Real > &gammaDscFwd, const bool forceFullMatrix, const Date &referenceDate, const DayCounter &dc, const bool linearInZero)
std::vector< Real > rebucketDeltas(const std::vector< Time > &deltaTimes, const std::map< Date, Real > &deltaRaw, const Date &referenceDate, const DayCounter &dc, const bool linearInZero)
coupon paying the compounded daily overnight rate, copy of QL class, added includeSpread flag