QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
normalfwdratepc.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007 Giorgio Facchinetti
5 Copyright (C) 2007 Chiara Fornarola
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#include <ql/models/marketmodels/evolvers/normalfwdratepc.hpp>
22#include <ql/models/marketmodels/marketmodel.hpp>
23#include <ql/models/marketmodels/evolutiondescription.hpp>
24#include <ql/models/marketmodels/browniangenerator.hpp>
25#include <ql/models/marketmodels/driftcomputation/lmmnormaldriftcalculator.hpp>
26
27namespace QuantLib {
28
30 const ext::shared_ptr<MarketModel>& marketModel,
31 const BrownianGeneratorFactory& factory,
32 const std::vector<Size>& numeraires,
33 Size initialStep)
34 : marketModel_(marketModel),
35 numeraires_(numeraires),
36 initialStep_(initialStep),
37 numberOfRates_(marketModel->numberOfRates()),
38 numberOfFactors_(marketModel_->numberOfFactors()),
39 curveState_(marketModel->evolution().rateTimes()),
40 forwards_(marketModel->initialRates()),
41 initialForwards_(marketModel->initialRates()),
42 drifts1_(numberOfRates_), drifts2_(numberOfRates_),
43 initialDrifts_(numberOfRates_), brownians_(numberOfFactors_),
44 correlatedBrownians_(numberOfRates_),
45 alive_(marketModel->evolution().firstAliveRate())
46 {
47 checkCompatibility(marketModel->evolution(), numeraires);
48
49 Size steps = marketModel->evolution().numberOfSteps();
50
52
54
55 calculators_.reserve(steps);
56 for (Size j=0; j<steps; ++j) {
57 const Matrix& A = marketModel_->pseudoRoot(j);
58 calculators_.emplace_back(A, marketModel->evolution().rateTaus(), numeraires[j],
59 alive_[j]);
60 /*
61 for (Size k=0; k<numberOfRates_; ++k) {
62 Real variance =
63 std::inner_product(A.row_begin(k), A.row_end(k),
64 A.row_begin(k), 0.0);
65 }
66 */
67 }
68
69 setForwards(marketModel_->initialRates());
70 }
71
72 const std::vector<Size>& NormalFwdRatePc::numeraires() const {
73 return numeraires_;
74 }
75
76 void NormalFwdRatePc::setForwards(const std::vector<Real>& forwards)
77 {
78 QL_REQUIRE(forwards.size()==numberOfRates_,
79 "mismatch between forwards and rateTimes");
80 for (Size i=0; i<numberOfRates_; ++i)
81 calculators_[initialStep_].compute(forwards, initialDrifts_);
82 }
83
86 }
87
90 std::copy(initialForwards_.begin(), initialForwards_.end(),
91 forwards_.begin());
92 return generator_->nextPath();
93 }
94
96 {
97 // we're going from T1 to T2
98
99 // a) compute drifts D1 at T1;
102 } else {
103 std::copy(initialDrifts_.begin(), initialDrifts_.end(),
104 drifts1_.begin());
105 }
106
107 // b) evolve forwards up to T2 using D1;
108 Real weight = generator_->nextStep(brownians_);
109 const Matrix& A = marketModel_->pseudoRoot(currentStep_);
110
111 Size i, alive = alive_[currentStep_];
112 for (i=alive; i<numberOfRates_; ++i) {
113 forwards_[i] += drifts1_[i] ;
114 forwards_[i] +=
115 std::inner_product(A.row_begin(i), A.row_end(i),
116 brownians_.begin(), Real(0.0));
117 }
118
119 // c) recompute drifts D2 using the predicted forwards;
121
122 // d) correct forwards using both drifts
123 for (i=alive; i<numberOfRates_; ++i) {
124 forwards_[i] += (drifts2_[i]-drifts1_[i])/2.0;
125 }
126
127 // e) update curve state
129
130 ++currentStep_;
131
132 return weight;
133 }
134
136 return currentStep_;
137 }
138
140 return curveState_;
141 }
142
143}
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< Rate > forwards_
const CurveState & currentState() const override
void setForwards(const std::vector< Real > &forwards)
std::vector< Real > brownians_
NormalFwdRatePc(const ext::shared_ptr< MarketModel > &, const BrownianGeneratorFactory &, const std::vector< Size > &numeraires, Size initialStep=0)
Size currentStep() const override
std::vector< Real > drifts2_
std::vector< LMMNormalDriftCalculator > calculators_
std::vector< Size > alive_
ext::shared_ptr< MarketModel > marketModel_
std::vector< Real > initialDrifts_
std::vector< Size > numeraires_
const std::vector< Size > & numeraires() const override
ext::shared_ptr< BrownianGenerator > generator_
std::vector< Rate > initialForwards_
void setInitialState(const CurveState &) override
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 checkCompatibility(const EvolutionDescription &evolution, const std::vector< Size > &numeraires)