QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
lognormalfwdrateballand.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) 2009 Sun Xiuxin
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
25
26namespace QuantLib {
27
29 const ext::shared_ptr<MarketModel>& marketModel,
30 const BrownianGeneratorFactory& factory,
31 const std::vector<Size>& numeraires,
32 Size initialStep)
33 : marketModel_(marketModel),
34 numeraires_(numeraires),
35 initialStep_(initialStep),
36 numberOfRates_(marketModel->numberOfRates()),
37 numberOfFactors_(marketModel->numberOfFactors()),
38 curveState_(marketModel->evolution().rateTimes()),
39 forwards_(marketModel->initialRates()),
40 displacements_(marketModel->displacements()),
41 logForwards_(numberOfRates_), initialLogForwards_(numberOfRates_),
42 drifts1_(numberOfRates_), drifts2_(numberOfRates_),
43 initialDrifts_(numberOfRates_), brownians_(numberOfFactors_),
44 correlatedBrownians_(numberOfRates_),
45 rateTaus_(marketModel->evolution().rateTaus()),
46 alive_(marketModel->evolution().firstAliveRate())
47 {
48 checkCompatibility(marketModel->evolution(), numeraires);
49
50 Size steps = marketModel->evolution().numberOfSteps();
51
53
55
56 calculators_.reserve(steps);
57 fixedDrifts_.reserve(steps);
58 for (Size j=0; j<steps; ++j) {
59 const Matrix& A = marketModel->pseudoRoot(j);
60 calculators_.emplace_back(A, displacements_, marketModel->evolution().rateTaus(),
61 numeraires[j], alive_[j]);
62 const Matrix& C = marketModel->covariance(j);
63 std::vector<Real> fixed(numberOfRates_);
64 for (Size k=0; k<numberOfRates_; ++k) {
65 Real variance = C[k][k];
66 fixed[k] = -0.5*variance;
67 }
68 fixedDrifts_.push_back(fixed);
69 }
70
71 setForwards(marketModel_->initialRates());
72 }
73
74 const std::vector<Size>& LogNormalFwdRateBalland::numeraires() const {
75 return numeraires_;
76 }
77
78 void LogNormalFwdRateBalland::setForwards(const std::vector<Real>& forwards)
79 {
80 QL_REQUIRE(forwards.size()==numberOfRates_,
81 "mismatch between forwards and rateTimes");
82 for (Size i=0; i<numberOfRates_; ++i)
83 initialLogForwards_[i] = std::log(forwards[i] +
85 calculators_[initialStep_].compute(forwards, initialDrifts_);
86 }
87
90 }
91
94 std::copy(initialLogForwards_.begin(), initialLogForwards_.end(),
95 logForwards_.begin());
96 return generator_->nextPath();
97 }
98
100 {
101 // we're going from T1 to T2:
102
103 // a) compute drifts D1 at T1;
106 } else {
107 std::copy(initialDrifts_.begin(), initialDrifts_.end(),
108 drifts1_.begin());
109 }
110
111 Real weight = generator_->nextStep(brownians_);
112 const Matrix& A = marketModel_->pseudoRoot(currentStep_);
113 const std::vector<Real>& fixedDrift = fixedDrifts_[currentStep_];
114
115 Integer alive = alive_[currentStep_];
116 Size i;
117 for ( i = alive; i < numberOfRates_ ; ++i )
118 {
119 logForwards_[i] += drifts1_[i] + fixedDrift[i];
120 logForwards_[i] += std::inner_product(A.row_begin(i), A.row_end(i),
121 brownians_.begin(), Real(0.0));
122 forwards_[i] = std::exp(logForwards_[i]) - displacements_[i];
123 }
124
125 for ( i = alive; i < numberOfRates_ ; ++i )
126 {
127 forwards_[i] = std::sqrt( forwards_[i]*(marketModel_->initialRates()[i]) );
128 }
129
131
132 for ( i = alive; i < numberOfRates_ ; ++i )
133 {
134 logForwards_[i] += drifts2_[i] - drifts1_[i];
135 forwards_[i] = std::exp(logForwards_[i]) - displacements_[i];
136 }
137
138 // update curve state
140
141 ++currentStep_;
142
143 return weight;
144 }
145
147 return currentStep_;
148 }
149
151 return curveState_;
152 }
153
154}
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)
const CurveState & currentState() const override
void setForwards(const std::vector< Real > &forwards)
std::vector< std::vector< Real > > fixedDrifts_
ext::shared_ptr< MarketModel > marketModel_
LogNormalFwdRateBalland(const ext::shared_ptr< MarketModel > &, const BrownianGeneratorFactory &, const std::vector< Size > &numeraires, Size initialStep=0)
const std::vector< Size > & numeraires() const override
std::vector< LMMDriftCalculator > calculators_
ext::shared_ptr< BrownianGenerator > generator_
void setInitialState(const CurveState &) override
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
#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
QL_INTEGER Integer
integer number
Definition: types.hpp:35
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)