QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
svddfwdratepc.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) 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
26
27namespace QuantLib {
28
29 SVDDFwdRatePc::SVDDFwdRatePc(const ext::shared_ptr<MarketModel>& marketModel,
30 const BrownianGeneratorFactory& factory,
31 const ext::shared_ptr<MarketModelVolProcess>& volProcess,
32 Size firstVolatilityFactor,
33 Size volatilityFactorStep,
34 const std::vector<Size>& numeraires,
35 Size initialStep )
36 : marketModel_(marketModel),
37 volProcess_(volProcess),
38 firstVolatilityFactor_(firstVolatilityFactor),
39 volFactorsPerStep_(volProcess->variatesPerStep()),
40 numeraires_(numeraires),
41 initialStep_(initialStep),
42 isVolVariate_(false,volProcess->variatesPerStep()+marketModel_->numberOfFactors()),
43 numberOfRates_(marketModel->numberOfRates()),
44 numberOfFactors_(marketModel_->numberOfFactors()),
45 curveState_(marketModel->evolution().rateTimes()),
46 forwards_(marketModel->initialRates()),
47 displacements_(marketModel->displacements()),
48 logForwards_(numberOfRates_), initialLogForwards_(numberOfRates_),
49 drifts1_(numberOfRates_), drifts2_(numberOfRates_),
50 initialDrifts_(numberOfRates_), allBrownians_(volProcess->variatesPerStep()+marketModel_->numberOfFactors()),
51 brownians_(numberOfFactors_),
52 volBrownians_(volProcess->variatesPerStep()),
53 correlatedBrownians_(numberOfRates_),
54 alive_(marketModel->evolution().firstAliveRate())
55 {
56 QL_REQUIRE(initialStep ==0, "initial step zero only supported currently. ");
57 checkCompatibility(marketModel->evolution(), numeraires);
58
59 Size steps = marketModel->evolution().numberOfSteps();
60
62
64
65 calculators_.reserve(steps);
66 fixedDrifts_.reserve(steps);
67 for (Size j=0; j<steps; ++j)
68 {
69 const Matrix& A = marketModel_->pseudoRoot(j);
70 calculators_.emplace_back(A, displacements_, marketModel->evolution().rateTaus(),
71 numeraires[j], alive_[j]);
72 std::vector<Real> fixed(numberOfRates_);
73 for (Size k=0; k<numberOfRates_; ++k)
74 {
76 std::inner_product(A.row_begin(k), A.row_end(k),
77 A.row_begin(k), Real(0.0));
78 fixed[k] = -0.5*variance;
79 }
80 fixedDrifts_.push_back(fixed);
81 }
82
83 setForwards(marketModel_->initialRates());
84
86
88
89 Size volIncrement = (variatesPerStep - firstVolatilityFactor_)/volFactorsPerStep_;
90
91 for (Size i=0; i < volFactorsPerStep_; ++i)
92 isVolVariate_[firstVolatilityFactor_+i*volIncrement] = true;
93 }
94
95 const std::vector<Size>& SVDDFwdRatePc::numeraires() const {
96 return numeraires_;
97 }
98
99 void SVDDFwdRatePc::setForwards(const std::vector<Real>& forwards)
100 {
101 QL_REQUIRE(forwards.size()==numberOfRates_,
102 "mismatch between forwards and rateTimes");
103 for (Size i=0; i<numberOfRates_; ++i)
104 initialLogForwards_[i] = std::log(forwards[i] +
105 displacements_[i]);
106 calculators_[initialStep_].compute(forwards, initialDrifts_);
107 }
108
110 {
112 }
113
115 {
117 std::copy(initialLogForwards_.begin(), initialLogForwards_.end(),
118 logForwards_.begin());
119 volProcess_->nextPath();
120 return generator_->nextPath();
121 }
122
124 {
125 // we're going from T1 to T2
126
127 // a) compute drifts D1 at T1;
129 {
131 }
132 else
133 {
134 std::copy(initialDrifts_.begin(), initialDrifts_.end(),
135 drifts1_.begin());
136 }
137
138 // b) evolve forwards up to T2 using D1;
139 Real weight = generator_->nextStep(allBrownians_);
140
141 // divide Brownians between vol process and forward process
142
143 for (Size i=0, j=0, k=0; i < allBrownians_.size(); ++i)
144 if ( isVolVariate_[i])
145 {
147 ++j;
148 }
149 else
150 {
152 ++k;
153 }
154
155
156 // get sd for step
157
158 Real weight2 = volProcess_->nextstep(volBrownians_);
159 Real sdMultiplier = volProcess_->stepSd();
160 Real varianceMultiplier = sdMultiplier*sdMultiplier;
161
162 const Matrix& A = marketModel_->pseudoRoot(currentStep_);
163 const std::vector<Real>& fixedDrift = fixedDrifts_[currentStep_];
164
165 Size alive = alive_[currentStep_];
166 for (Size i=alive; i<numberOfRates_; ++i) {
167 logForwards_[i] += varianceMultiplier*(drifts1_[i] + fixedDrift[i]);
168 logForwards_[i] += sdMultiplier*
169 std::inner_product(A.row_begin(i), A.row_end(i),
170 brownians_.begin(), Real(0.0));
171 forwards_[i] = std::exp(logForwards_[i]) - displacements_[i];
172 }
173
174 // c) recompute drifts D2 using the predicted forwards;
176
177 // d) correct forwards using both drifts
178 for (Size i=alive; i<numberOfRates_; ++i) {
179 logForwards_[i] += varianceMultiplier*(drifts2_[i]-drifts1_[i])/2.0;
180 forwards_[i] = std::exp(logForwards_[i]) - displacements_[i];
181 }
182
183 // e) update curve state
185
186 ++currentStep_;
187
188 return weight*weight2;
189 }
190
192 return currentStep_;
193 }
194
196 return curveState_;
197 }
198
199}
virtual ext::shared_ptr< BrownianGenerator > create(Size factors, Size steps) const =0
Curve state for market-model simulations
Definition: curvestate.hpp:41
virtual const std::vector< Rate > & forwardRates() const =0
void setOnForwardRates(const std::vector< Rate > &fwdRates, Size firstValidIndex=0)
Matrix used in linear algebra.
Definition: matrix.hpp:41
const_row_iterator row_begin(Size i) const
Definition: matrix.hpp:360
const_row_iterator row_end(Size i) const
Definition: matrix.hpp:378
std::vector< Real > drifts1_
std::vector< Real > allBrownians_
std::vector< Rate > displacements_
std::vector< Real > volBrownians_
Real advanceStep() override
Real startNewPath() override
std::vector< Rate > forwards_
const CurveState & currentState() const override
void setForwards(const std::vector< Real > &forwards)
std::vector< Real > brownians_
std::valarray< bool > isVolVariate_
std::vector< Rate > logForwards_
Size currentStep() const override
std::vector< std::vector< Real > > fixedDrifts_
std::vector< Real > drifts2_
std::vector< Size > alive_
std::vector< Rate > initialLogForwards_
ext::shared_ptr< MarketModel > marketModel_
std::vector< Real > initialDrifts_
SVDDFwdRatePc(const ext::shared_ptr< MarketModel > &, const BrownianGeneratorFactory &, const ext::shared_ptr< MarketModelVolProcess > &volProcess, Size firstVolatilityFactor, Size volatilityFactorStep, const std::vector< Size > &numeraires, Size initialStep=0)
std::vector< Size > numeraires_
ext::shared_ptr< MarketModelVolProcess > volProcess_
const std::vector< Size > & numeraires() const override
std::vector< LMMDriftCalculator > calculators_
ext::shared_ptr< BrownianGenerator > generator_
void setInitialState(const CurveState &) override
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
LinearInterpolation variance
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Drift computation for Libor market model.
Definition: any.hpp:35
void checkCompatibility(const EvolutionDescription &evolution, const std::vector< Size > &numeraires)