QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
pathwiseaccountingengine.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4
5 Copyright (C) 2008 Mark Joshi
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 <http://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
22#ifndef quantlib_pathwise_accounting_engine_hpp
23#define quantlib_pathwise_accounting_engine_hpp
24
25#include <ql/models/marketmodels/pathwisemultiproduct.hpp>
26#include <ql/models/marketmodels/pathwisediscounter.hpp>
27#include <ql/math/statistics/sequencestatistics.hpp>
28#include <ql/models/marketmodels/pathwisegreeks/ratepseudorootjacobian.hpp>
29
30#include <ql/utilities/clone.hpp>
31#include <ql/types.hpp>
32#include <vector>
33
34namespace QuantLib {
35
36 class LogNormalFwdRateEuler;
37 class MarketModel;
38
39
41 // using Giles--Glasserman smoking adjoints method
42 // note only works with displaced LMM, and requires knowledge of pseudo-roots and displacements
43 // This is tested in MarketModelTest::testPathwiseGreeks
45 {
46 public:
48 ext::shared_ptr<LogNormalFwdRateEuler> evolver, // method relies heavily on LMM Euler
50 ext::shared_ptr<MarketModel>
51 pseudoRootStructure, // we need pseudo-roots and displacements
52 Real initialNumeraireValue);
53
55 Size numberOfPaths);
56 private:
57 Real singlePathValues(std::vector<Real>& values);
58
59 ext::shared_ptr<LogNormalFwdRateEuler> evolver_;
61 ext::shared_ptr<MarketModel> pseudoRootStructure_;
62
68
69 std::vector<Real> currentForwards_, lastForwards_;
70
72
73
74 // workspace
75 std::vector<Real> numerairesHeld_;
76 std::vector<Size> numberCashFlowsThisStep_;
77 std::vector<std::vector<MarketModelPathwiseMultiProduct::CashFlow> >
79 std::vector<MarketModelPathwiseDiscounter> discounters_;
80
81 std::vector<Matrix> V_; // one V for each product, with components for each time step and rate
82
83 // std::vector<std::vector<std::vector<Real> > > V_; // one V for each product, with components for each time step and rate
84
85 Matrix LIBORRatios_; // dimensions are step and rate number
86 Matrix Discounts_; // dimensions are step and rate number, goes from 0 to n. P(t_0, t_j)
87
88 Matrix StepsDiscountsSquared_; // dimensions are step and rate number
89
90 Matrix LIBORRates_; // dimensions are step and rate number
91 Matrix partials_; // dimensions are factor and rate
92
93 std::vector<Real> deflatorAndDerivatives_;
94
95 std::vector<std::vector<Size> > numberCashFlowsThisIndex_;
96 std::vector<Matrix> totalCashFlowsThisIndex_; // need product cross times cross which sensitivity
97
98 std::vector<std::vector<Size> > cashFlowIndicesThisStep_;
99
100 };
101
102
104 // using Giles--Glasserman smoking adjoints method
105 // note only works with displaced LMM,
106 //
107 // The method is intimately connected with log-normal Euler evolution
108 //
109 // We must work with discretely compounding MM account
110 // To compute a vega means changing the pseudo-square root at each time step
111 // So for each vega, we have a vector of matrices. So we need a vector of vectors of matrices to compute all the vegas.
112 // We do the outermost vector by time step and inner one by which vega.
113 // This is tested in MarketModelTest::testPathwiseVegas
114
116 {
117 public:
119 ext::shared_ptr<LogNormalFwdRateEuler> evolver, // method relies heavily on LMM Euler
121 ext::shared_ptr<MarketModel>
122 pseudoRootStructure, // we need pseudo-roots and displacements
123 const std::vector<std::vector<Matrix> >& VegaBumps,
124 Real initialNumeraireValue);
125
126 void multiplePathValues(std::vector<Real>& means,
127 std::vector<Real>& errors,
128 Size numberOfPaths);
129 private:
130 Real singlePathValues(std::vector<Real>& values);
131
132 ext::shared_ptr<LogNormalFwdRateEuler> evolver_;
134 ext::shared_ptr<MarketModel> pseudoRootStructure_;
135 std::vector<Size> numeraires_;
136
143
144 std::vector<RatePseudoRootJacobian> jacobianComputers_;
145
146
148
149
150 // workspace
151 std::vector<Real> currentForwards_, lastForwards_;
152 std::vector<Real> numerairesHeld_;
153 std::vector<Size> numberCashFlowsThisStep_;
154 std::vector<std::vector<MarketModelPathwiseMultiProduct::CashFlow> >
156 std::vector<MarketModelPathwiseDiscounter> discounters_;
157
158 std::vector<Matrix> V_; // one V for each product, with components for each time step and rate
159
160 Matrix LIBORRatios_; // dimensions are step and rate number
161 Matrix Discounts_; // dimensions are step and rate number, goes from 0 to n. P(t_0, t_j)
162
163 Matrix StepsDiscountsSquared_; // dimensions are step and rate number
164 std::vector<Real> stepsDiscounts_;
165
166 Matrix LIBORRates_; // dimensions are step and rate number
167 Matrix partials_; // dimensions are factor and rate
168
169 Matrix vegasThisPath_; // dimensions are product and which vega
170 std::vector<Matrix> jacobiansThisPaths_; // dimensions are step, rate and factor
171
172 std::vector<Real> deflatorAndDerivatives_;
173 std::vector<Real> fullDerivatives_;
174
175 std::vector<std::vector<Size> > numberCashFlowsThisIndex_;
176 std::vector<Matrix> totalCashFlowsThisIndex_; // need product cross times cross which sensitivity
177
178 std::vector<std::vector<Size> > cashFlowIndicesThisStep_;
179
180 };
181
183 // using Giles--Glasserman smoking adjoints method
184 // note only works with displaced LMM,
185 //
186 // The method is intimately connected with log-normal Euler evolution
187 //
188 // We must work with discretely compounding MM account
189 // To compute a vega means changing the pseudo-square root at each time step
190 // So for each vega, we have a vector of matrices. So we need a vector of vectors of matrices to compute all the vegas.
191 // We do the outermost vector by time step and inner one by which vega.
192 // This implementation is different in that all the linear combinations by the bumps are done as late as possible,
193 // whereas PathwiseVegasAccountingEngine does them as early as possible.
194 // This is tested in MarketModelTest::testPathwiseVegas
195
197 {
198 public:
200 ext::shared_ptr<LogNormalFwdRateEuler> evolver, // method relies heavily on LMM Euler
202 ext::shared_ptr<MarketModel>
203 pseudoRootStructure, // we need pseudo-roots and displacements
204 const std::vector<std::vector<Matrix> >& VegaBumps,
205 Real initialNumeraireValue);
206
208 void multiplePathValues(std::vector<Real>& means,
209 std::vector<Real>& errors,
210 Size numberOfPaths);
211
213 void multiplePathValuesElementary(std::vector<Real>& means,
214 std::vector<Real>& errors,
215 Size numberOfPaths);
216
217 private:
218 Real singlePathValues(std::vector<Real>& values);
219
220 ext::shared_ptr<LogNormalFwdRateEuler> evolver_;
222 ext::shared_ptr<MarketModel> pseudoRootStructure_;
223 std::vector<std::vector<Matrix> > vegaBumps_;
224 std::vector<Size> numeraires_;
225
234
235 std::vector<RatePseudoRootJacobianAllElements> jacobianComputers_;
236
237
239
240
241 // workspace
242 std::vector<Real> currentForwards_, lastForwards_;
243 std::vector<Real> numerairesHeld_;
244 std::vector<Size> numberCashFlowsThisStep_;
245 std::vector<std::vector<MarketModelPathwiseMultiProduct::CashFlow> >
247 std::vector<MarketModelPathwiseDiscounter> discounters_;
248
249 std::vector<Matrix> V_; // one V for each product, with components for each time step and rate
250
251 Matrix LIBORRatios_; // dimensions are step and rate number
252 Matrix Discounts_; // dimensions are step and rate number, goes from 0 to n. P(t_0, t_j)
253
254 Matrix StepsDiscountsSquared_; // dimensions are step and rate number
255 std::vector<Real> stepsDiscounts_;
256
257 Matrix LIBORRates_; // dimensions are step and rate number
258 Matrix partials_; // dimensions are factor and rate
259
260 std::vector<std::vector<Matrix> > elementary_vegas_ThisPath_; // dimensions are product, step, rate and factor
261 std::vector<std::vector<Matrix> > jacobiansThisPaths_; // dimensions are step, rate, rate and factor
262
263 std::vector<Real> deflatorAndDerivatives_;
264 std::vector<Real> fullDerivatives_;
265
266 std::vector<std::vector<Size> > numberCashFlowsThisIndex_;
267 std::vector<Matrix> totalCashFlowsThisIndex_; // need product cross times cross which sensitivity
268
269 std::vector<std::vector<Size> > cashFlowIndicesThisStep_;
270/*
271 // experimental
272
273 std::vector<std::vector<Real> > gaussians_;
274 int distinguishedFactor_;
275 int distinguishedRate_;
276 int distinguishedStep_;
277
278*/
279 };
280
281}
282
283#endif
cloning proxy to an underlying object
Definition: clone.hpp:40
Statistics analysis of N-dimensional (sequence) data.
Matrix used in linear algebra.
Definition: matrix.hpp:41
Engine collecting cash flows along a market-model simulation for doing pathwise computation of Deltas...
ext::shared_ptr< LogNormalFwdRateEuler > evolver_
std::vector< std::vector< MarketModelPathwiseMultiProduct::CashFlow > > cashFlowsGenerated_
std::vector< std::vector< Size > > numberCashFlowsThisIndex_
void multiplePathValues(SequenceStatisticsInc &stats, Size numberOfPaths)
ext::shared_ptr< MarketModel > pseudoRootStructure_
std::vector< std::vector< Size > > cashFlowIndicesThisStep_
Real singlePathValues(std::vector< Real > &values)
Clone< MarketModelPathwiseMultiProduct > product_
std::vector< MarketModelPathwiseDiscounter > discounters_
Engine collecting cash flows along a market-model simulation for doing pathwise computation of Deltas...
void multiplePathValues(std::vector< Real > &means, std::vector< Real > &errors, Size numberOfPaths)
ext::shared_ptr< LogNormalFwdRateEuler > evolver_
std::vector< std::vector< MarketModelPathwiseMultiProduct::CashFlow > > cashFlowsGenerated_
std::vector< std::vector< Size > > numberCashFlowsThisIndex_
std::vector< RatePseudoRootJacobian > jacobianComputers_
ext::shared_ptr< MarketModel > pseudoRootStructure_
std::vector< std::vector< Size > > cashFlowIndicesThisStep_
Real singlePathValues(std::vector< Real > &values)
Clone< MarketModelPathwiseMultiProduct > product_
std::vector< MarketModelPathwiseDiscounter > discounters_
Engine collecting cash flows along a market-model simulation for doing pathwise computation of Deltas...
void multiplePathValues(std::vector< Real > &means, std::vector< Real > &errors, Size numberOfPaths)
Use to get vegas with respect to VegaBumps.
ext::shared_ptr< LogNormalFwdRateEuler > evolver_
void multiplePathValuesElementary(std::vector< Real > &means, std::vector< Real > &errors, Size numberOfPaths)
Use to get vegas with respect to pseudo-root-elements.
std::vector< std::vector< MarketModelPathwiseMultiProduct::CashFlow > > cashFlowsGenerated_
std::vector< std::vector< Size > > numberCashFlowsThisIndex_
std::vector< std::vector< Matrix > > vegaBumps_
std::vector< std::vector< Matrix > > jacobiansThisPaths_
std::vector< std::vector< Matrix > > elementary_vegas_ThisPath_
std::vector< RatePseudoRootJacobianAllElements > jacobianComputers_
std::vector< std::vector< Size > > cashFlowIndicesThisStep_
Clone< MarketModelPathwiseMultiProduct > product_
std::vector< MarketModelPathwiseDiscounter > discounters_
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