QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
multistepperiodcapletswaptions.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007 Mark Joshi
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 <http://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/instruments/payoffs.hpp>
21#include <ql/models/marketmodels/curvestate.hpp>
22#include <ql/models/marketmodels/products/multistep/multistepperiodcapletswaptions.hpp>
23#include <ql/models/marketmodels/utilities.hpp>
24#include <utility>
25
26namespace QuantLib {
27
29 const std::vector<Time>& rateTimes,
30 const std::vector<Time>& forwardOptionPaymentTimes,
31 const std::vector<Time>& swaptionPaymentTimes,
32 std::vector<ext::shared_ptr<StrikedTypePayoff> > forwardPayOffs,
33 std::vector<ext::shared_ptr<StrikedTypePayoff> > swapPayOffs,
34 Size period,
35 Size offset)
36 : MultiProductMultiStep(rateTimes), paymentTimes_(forwardOptionPaymentTimes),
37 forwardOptionPaymentTimes_(forwardOptionPaymentTimes),
38 swaptionPaymentTimes_(swaptionPaymentTimes), forwardPayOffs_(std::move(forwardPayOffs)),
39 swapPayOffs_(std::move(swapPayOffs)), period_(period), offset_(offset) {
40 QL_REQUIRE(rateTimes.size() >=2,
41 "we need at least two rate times in MultiStepPeriodCapletSwaptions ");
42
43 checkIncreasingTimes(forwardOptionPaymentTimes);
44 checkIncreasingTimes(swaptionPaymentTimes);
45 for (Real& swaptionPaymentTime : swaptionPaymentTimes_)
46 paymentTimes_.push_back(swaptionPaymentTime);
47 lastIndex_ = rateTimes.size()-1;
48 numberFRAs_ = rateTimes.size()-1;
50
51
52 QL_REQUIRE(offset_< period_,
53 "the offset must be less then the period in MultiStepPeriodCapletSwaptions ");
54 QL_REQUIRE(numberBigFRAs_ > 0,
55 "we must have at least one FRA after the periodizing in MultiStepPeriodCapletSwaptions ");
56
57 QL_REQUIRE(forwardOptionPaymentTimes_.size() == numberBigFRAs_,
58 "we must have precisely one payment time for each forward option MultiStepPeriodCapletSwaptions ");
59
60 QL_REQUIRE(forwardPayOffs_.size() == numberBigFRAs_,
61 "we must have precisely one payoff for each forward option MultiStepPeriodCapletSwaptions ");
62
63 QL_REQUIRE(swaptionPaymentTimes_.size() == numberBigFRAs_,
64 "we must have precisely one payment time for each swaption in MultiStepPeriodCapletSwaptions ");
65
66 QL_REQUIRE(swapPayOffs_.size() == numberBigFRAs_,
67 "we must have precisely one payoff for each swaption in MultiStepPeriodCapletSwaptions ");
68 }
69
71 const CurveState& currentState,
72 std::vector<Size>& numberCashFlowsThisStep,
73 std::vector<std::vector<MarketModelMultiProduct::CashFlow> >&
74 genCashFlows)
75 {
76
77 for (Size & i : numberCashFlowsThisStep)
78 i = 0UL;
79
80 if (currentIndex_ >= offset_ && (currentIndex_ - offset_) % period_ == 0) {
81 // caplet first
84 Real forward = (1.0 / df - 1.0) / tau;
85 Real value = (*forwardPayOffs_[productIndex_])(forward);
86 value *= tau * currentState.discountRatio(currentIndex_ + period_, currentIndex_);
87
88 if (value > 0) {
89 numberCashFlowsThisStep[productIndex_] = 1UL;
90 genCashFlows[productIndex_][0].amount = value;
91 genCashFlows[productIndex_][0].timeIndex = productIndex_;
92 }
93
94 // now swaption
95
96 unsigned long numberPeriods = numberBigFRAs_ - productIndex_;
97 Real B = 0.0;
98 double P0 = 1.0; // i.e currentState.discountRatio(currentIndex_,currentIndex_);
99 Real Pn =
100 currentState.discountRatio(currentIndex_ + numberPeriods * period_, currentIndex_);
101 for (unsigned long i = 0; i < numberPeriods; ++i) {
102 Time tau = rateTimes_[currentIndex_ + (i + 1) * period_] -
104 B += tau *
105 currentState.discountRatio(currentIndex_ + (i + 1) * period_, currentIndex_);
106 }
107
108
109 Real swapRate = (P0 - Pn) / B;
110
111 Real swaptionValue = (*swapPayOffs_[productIndex_])(swapRate);
112 swaptionValue *= B;
113
114 if (swaptionValue > 0) {
115 numberCashFlowsThisStep[productIndex_ + numberBigFRAs_] = 1UL;
116 genCashFlows[productIndex_ + numberBigFRAs_][0].amount = swaptionValue;
117 genCashFlows[productIndex_ + numberBigFRAs_][0].timeIndex =
119 }
120
122
123 }
124
126
127 bool terminate =(productIndex_ >= numberBigFRAs_);
128
129
130 return terminate;
131 }
132
133 std::unique_ptr<MarketModelMultiProduct>
135 return std::unique_ptr<MarketModelMultiProduct>(new MultiStepPeriodCapletSwaptions(*this));
136 }
137
138}
Curve state for market-model simulations
Definition: curvestate.hpp:41
virtual Real discountRatio(Size i, Size j) const =0
Multiple-step market-model product.
std::vector< ext::shared_ptr< StrikedTypePayoff > > forwardPayOffs_
std::unique_ptr< MarketModelMultiProduct > clone() const override
returns a newly-allocated copy of itself
bool nextTimeStep(const CurveState &currentState, std::vector< Size > &numberCashFlowsThisStep, std::vector< std::vector< CashFlow > > &cashFlowsGenerated) override
return value indicates whether path is finished, TRUE means done
std::vector< ext::shared_ptr< StrikedTypePayoff > > swapPayOffs_
MultiStepPeriodCapletSwaptions(const std::vector< Time > &rateTimes, const std::vector< Time > &forwardOptionPaymentTimes, const std::vector< Time > &swaptionPaymentTimes, std::vector< ext::shared_ptr< StrikedTypePayoff > > forwardPayOffs, std::vector< ext::shared_ptr< StrikedTypePayoff > > swapPayOffs, Size period, Size offset)
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
void checkIncreasingTimes(const std::vector< Time > &times)
check for strictly increasing times, first time greater than zero
Definition: utilities.cpp:92
STL namespace.