QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
pathwiseproductswaption.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 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/models/marketmodels/products/pathwise/pathwiseproductswaption.hpp>
21#include <ql/models/marketmodels/curvestate.hpp>
22#include <ql/models/marketmodels/utilities.hpp>
23
24namespace QuantLib {
25
26
28 {
29 return false;
30 }
31
33 const std::vector<Rate>& strikes)
34 : rateTimes_(rateTimes),
35 strikes_(strikes) ,
36 numberRates_(rateTimes.size()-1)
37 {
38 checkIncreasingTimes(rateTimes);
39 std::vector<Time> evolTimes(rateTimes_);
40 evolTimes.pop_back();
41
42 QL_REQUIRE(evolTimes.size()==numberRates_,
43 "rateTimes.size()<> numberOfRates+1");
44
45 QL_REQUIRE(strikes.size()==numberRates_,
46 "strikes.size()<> numberOfRates");
47
48
49 evolution_ = EvolutionDescription(rateTimes,evolTimes);
50
51 }
52
54 const CurveState& currentState,
55 std::vector<Size>& numberCashFlowsThisStep,
56 std::vector<std::vector<MarketModelPathwiseMultiProduct::CashFlow> >& cashFlowsGenerated)
57 {
58 Rate swapRate = currentState.coterminalSwapRate(currentIndex_);
59 cashFlowsGenerated[currentIndex_][0].timeIndex = currentIndex_;
60
62 cashFlowsGenerated[currentIndex_][0].amount[0] =
63 (swapRate-strikes_[currentIndex_])*annuity;
64
65 std::fill(numberCashFlowsThisStep.begin(),
66 numberCashFlowsThisStep.end(),0);
67
68 if ( cashFlowsGenerated[currentIndex_][0].amount[0] >0)
69 {
70 numberCashFlowsThisStep[currentIndex_] = 1;
71 for (Size i=1; i <= numberRates_; ++i)
72 cashFlowsGenerated[currentIndex_][0].amount[i] =0;
73
74 for (Size k=currentIndex_; k < numberRates_; ++k)
75 {
76 cashFlowsGenerated[currentIndex_][0].amount[k+1] = (rateTimes_[k+1]-rateTimes_[k])*currentState.discountRatio(k+1,currentIndex_);
77
78 Real multiplier = - (rateTimes_[k+1]-rateTimes_[k])*currentState.discountRatio(k+1,k);
79
80 for (Size l=k; l < numberRates_; ++l)
81 cashFlowsGenerated[currentIndex_][0].amount[k+1] +=(currentState.forwardRate(l)-strikes_[currentIndex_])*(rateTimes_[l+1]-rateTimes_[l])
82 * multiplier*currentState.discountRatio(l+1,currentIndex_);
83 }
84 }
86 return (currentIndex_ == strikes_.size());
87 }
88
89 std::unique_ptr<MarketModelPathwiseMultiProduct>
91 {
92 return std::unique_ptr<MarketModelPathwiseMultiProduct>(new MarketModelPathwiseCoterminalSwaptionsDeflated(*this));
93 }
94
96 {
97 std::vector<Size> numeraires(numberRates_);
98 for (Size i=0; i < numberRates_; ++i)
99 numeraires[i] = i;
100
101 return numeraires;
102 }
103
105 {
106 return evolution_;
107 }
108
110 {
111 return rateTimes_;
112 }
113
115 {
116 return numberRates_;
117 }
118
120 {
121 return 1;
122
123 }
124
126 {
128 }
129
130
132
133
135 {
136 return false;
137 }
138
140 const std::vector<Rate>& strikes,
141 Real bumpSize)
142 : rateTimes_(rateTimes),
143 strikes_(strikes) ,
144 numberRates_(rateTimes.size()-1), bumpSize_(bumpSize), up_(rateTimes), down_(rateTimes), forwards_(numberRates_)
145 {
146 checkIncreasingTimes(rateTimes);
147 std::vector<Time> evolTimes(rateTimes_);
148 evolTimes.pop_back();
149
150 QL_REQUIRE(evolTimes.size()==numberRates_,
151 "rateTimes.size()<> numberOfRates+1");
152
153 QL_REQUIRE(strikes.size()==numberRates_,
154 "strikes.size()<> numberOfRates");
155
156
157 evolution_ = EvolutionDescription(rateTimes,evolTimes);
158
159 }
160
162 const CurveState& currentState,
163 std::vector<Size>& numberCashFlowsThisStep,
164 std::vector<std::vector<MarketModelPathwiseMultiProduct::CashFlow> >& cashFlowsGenerated)
165 {
166 Rate swapRate = currentState.coterminalSwapRate(currentIndex_);
167 cashFlowsGenerated[currentIndex_][0].timeIndex = currentIndex_;
168
170 cashFlowsGenerated[currentIndex_][0].amount[0] =
171 (swapRate-strikes_[currentIndex_])*annuity;
172
173 std::fill(numberCashFlowsThisStep.begin(),
174 numberCashFlowsThisStep.end(),0);
175
176 if ( cashFlowsGenerated[currentIndex_][0].amount[0] >0)
177 {
178 numberCashFlowsThisStep[currentIndex_] = 1;
179 for (Size i=1; i <= numberRates_; ++i)
180 cashFlowsGenerated[currentIndex_][0].amount[i] =0;
181
182 for (Size k=currentIndex_; k < numberRates_; ++k)
183 {
184 forwards_ = currentState.forwardRates();
185 forwards_[k] += bumpSize_;
187
188 forwards_[k] -= bumpSize_;
189 forwards_[k] -= bumpSize_;
191
194 Real upValue = (upSR - strikes_[currentIndex_])* upAnnuity;
195
198 Real downValue = (downSR - strikes_[currentIndex_])* downAnnuity;
199
200 Real deriv = (upValue - downValue)/(2.0*bumpSize_);
201
202 cashFlowsGenerated[currentIndex_][0].amount[k+1] = deriv;
203
204 }
205 }
207 return (currentIndex_ == strikes_.size());
208 }
209
210 std::unique_ptr<MarketModelPathwiseMultiProduct>
212 {
213 return std::unique_ptr<MarketModelPathwiseMultiProduct>(
215 }
216
218 {
219 std::vector<Size> numeraires(numberRates_);
220 for (Size i=0; i < numberRates_; ++i)
221 numeraires[i] = i;
222
223 return numeraires;
224 }
225
227 {
228 return evolution_;
229 }
230
232 {
233 return rateTimes_;
234 }
235
237 {
238 return numberRates_;
239 }
240
242 {
243 return 1;
244
245 }
246
248 {
250 }
251
252
253}
254
Curve state for market-model simulations
Definition: curvestate.hpp:41
virtual Rate coterminalSwapAnnuity(Size numeraire, Size i) const =0
virtual Rate forwardRate(Size i) const =0
virtual const std::vector< Rate > & forwardRates() const =0
virtual Rate coterminalSwapRate(Size i) const =0
virtual Real discountRatio(Size i, Size j) const =0
Market-model evolution description.
void setOnForwardRates(const std::vector< Rate > &fwdRates, Size firstValidIndex=0)
Rate coterminalSwapRate(Size i) const override
Rate coterminalSwapAnnuity(Size numeraire, Size i) const override
MarketModelPathwiseCoterminalSwaptionsDeflated(const std::vector< Time > &rateTimes, const std::vector< Rate > &strikes)
bool nextTimeStep(const CurveState &currentState, std::vector< Size > &numberCashFlowsThisStep, std::vector< std::vector< MarketModelPathwiseMultiProduct::CashFlow > > &cashFlowsGenerated) override
return value indicates whether path is finished, TRUE means done
std::unique_ptr< MarketModelPathwiseMultiProduct > clone() const override
returns a newly-allocated copy of itself
const EvolutionDescription & evolution() const override
void reset() override
during simulation put product at start of path
MarketModelPathwiseCoterminalSwaptionsNumericalDeflated(const std::vector< Time > &rateTimes, const std::vector< Rate > &strikes, Real bumpSize_)
bool nextTimeStep(const CurveState &currentState, std::vector< Size > &numberCashFlowsThisStep, std::vector< std::vector< MarketModelPathwiseMultiProduct::CashFlow > > &cashFlowsGenerated) override
return value indicates whether path is finished, TRUE means done
std::unique_ptr< MarketModelPathwiseMultiProduct > clone() const override
returns a newly-allocated copy of itself
void reset() override
during simulation put product at start of path
QL_REAL Real
real number
Definition: types.hpp:50
Real Rate
interest rates
Definition: types.hpp:70
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