QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
discretizedswaption.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
5 Copyright (C) 2004, 2007 StatPro Italia srl
6 Copyright (C) 2021, 2022 Ralf Konrad Eckel
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
25
26namespace QuantLib {
27
28 namespace {
29
30 bool withinPreviousWeek(const Date& d1, const Date& d2) { return d2 >= d1 - 7 && d2 <= d1; }
31
32 bool withinNextWeek(const Date& d1, const Date& d2) { return d2 >= d1 && d2 <= d1 + 7; }
33
34 bool withinOneWeek(const Date& d1, const Date& d2) {
35 return withinPreviousWeek(d1, d2) || withinNextWeek(d1, d2);
36 }
37 }
38
40 const Date& referenceDate,
41 const DayCounter& dayCounter)
43 ext::shared_ptr<DiscretizedAsset>(), args.exercise->type(), std::vector<Time>()),
44 arguments_(args) {
45
46 // Date adjustments can get time vectors out of synch.
47 // Here, we try and collapse similar dates which could cause
48 // a mispricing.
49 Swaption::arguments snappedArgs;
50 std::vector<CouponAdjustment> fixedCouponAdjustments;
51 std::vector<CouponAdjustment> floatingCouponAdjustments;
52
53 prepareSwaptionWithSnappedDates(arguments_, referenceDate, dayCounter, snappedArgs,
54 fixedCouponAdjustments, floatingCouponAdjustments);
55
56 exerciseTimes_.resize(snappedArgs.exercise->dates().size());
57 for (Size i = 0; i < exerciseTimes_.size(); ++i)
59 dayCounter.yearFraction(referenceDate, snappedArgs.exercise->date(i));
60
61 Time lastFixedPayment =
62 dayCounter.yearFraction(referenceDate, snappedArgs.fixedPayDates.back());
63 Time lastFloatingPayment =
64 dayCounter.yearFraction(referenceDate, snappedArgs.floatingPayDates.back());
65 lastPayment_ = std::max(lastFixedPayment, lastFloatingPayment);
66
68 ext::make_shared<DiscretizedSwap>(snappedArgs, referenceDate, dayCounter,
69 fixedCouponAdjustments, floatingCouponAdjustments);
70 }
71
73 underlying_->initialize(method(), lastPayment_);
75 }
76
78 const Swaption::arguments& args,
79 const Date& referenceDate,
80 const DayCounter& dayCounter,
81 PricingEngine::arguments& snappedArgs,
82 std::vector<CouponAdjustment>& fixedCouponAdjustments,
83 std::vector<CouponAdjustment>& floatingCouponAdjustments) {
84
85 std::vector<Date> fixedDates = args.swap->fixedSchedule().dates();
86 std::vector<Date> floatDates = args.swap->floatingSchedule().dates();
87
88 fixedCouponAdjustments.resize(args.swap->fixedLeg().size(),
90 floatingCouponAdjustments.resize(args.swap->floatingLeg().size(),
92
93 for (const auto& exerciseDate : args.exercise->dates()) {
94 for (Size j = 0; j < fixedDates.size() - 1; j++) {
95 auto unadjustedDate = fixedDates[j];
96 if (exerciseDate != unadjustedDate && withinOneWeek(exerciseDate, unadjustedDate)) {
97 fixedDates[j] = exerciseDate;
98 if (withinPreviousWeek(exerciseDate, unadjustedDate))
99 fixedCouponAdjustments[j] = CouponAdjustment::post;
100 }
101 }
102
103 for (Size j = 0; j < floatDates.size() - 1; j++) {
104 auto unadjustedDate = floatDates[j];
105 if (exerciseDate != unadjustedDate && withinOneWeek(exerciseDate, unadjustedDate)) {
106 floatDates[j] = exerciseDate;
107 if (withinPreviousWeek(exerciseDate, unadjustedDate))
108 floatingCouponAdjustments[j] = CouponAdjustment::post;
109 }
110 }
111 }
112
113 Schedule snappedFixedSchedule(fixedDates);
114 Schedule snappedFloatSchedule(floatDates);
115
116 auto snappedSwap = ext::make_shared<VanillaSwap>(
117 args.swap->type(), args.swap->nominal(), snappedFixedSchedule, args.swap->fixedRate(),
118 args.swap->fixedDayCount(), snappedFloatSchedule, args.swap->iborIndex(),
119 args.swap->spread(), args.swap->floatingDayCount(), args.swap->paymentConvention());
120
121 Swaption snappedSwaption(snappedSwap, args.exercise, args.settlementType,
122 args.settlementMethod);
123
124 snappedSwaption.setupArguments(&snappedArgs);
125 }
126}
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
Discretized asset class used by numerical methods.
const ext::shared_ptr< Lattice > & method() const
Discretized option on a given asset.
std::vector< Time > exerciseTimes_
void reset(Size size) override
ext::shared_ptr< DiscretizedAsset > underlying_
DiscretizedSwaption(const Swaption::arguments &, const Date &referenceDate, const DayCounter &dayCounter)
static void prepareSwaptionWithSnappedDates(const Swaption::arguments &args, const Date &referenceDate, const DayCounter &dayCounter, PricingEngine::arguments &snappedArgs, std::vector< CouponAdjustment > &fixedCouponAdjustments, std::vector< CouponAdjustment > &floatingCouponAdjustments)
void reset(Size size) override
ext::shared_ptr< Exercise > exercise
Definition: option.hpp:65
Payment schedule.
Definition: schedule.hpp:40
Arguments for swaption calculation
Definition: swaption.hpp:147
ext::shared_ptr< FixedVsFloatingSwap > swap
Definition: swaption.hpp:150
Settlement::Method settlementMethod
Definition: swaption.hpp:152
Settlement::Type settlementType
Definition: swaption.hpp:151
Swaption class
Definition: swaption.hpp:88
void setupArguments(PricingEngine::arguments *) const override
Definition: swaption.cpp:162
Discretized swap class.
Discretized swaption class.
Coupon paying a fixed annual rate.
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.