QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
lognormalcotswapratepc.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) 2006, 2007 Ferdinando Ametrano
5 Copyright (C) 2006, 2007 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
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 swapRates_(marketModel->initialRates()),
41 displacements_(marketModel->displacements()),
42 logSwapRates_(numberOfRates_), initialLogSwapRates_(numberOfRates_),
43 drifts1_(numberOfRates_), drifts2_(numberOfRates_),
44 initialDrifts_(numberOfRates_),
45 brownians_(numberOfFactors_), correlatedBrownians_(numberOfRates_),
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 std::vector<Real> fixed(numberOfRates_);
63 for (Size k=0; k<numberOfRates_; ++k) {
65 std::inner_product(A.row_begin(k), A.row_end(k),
66 A.row_begin(k), Real(0.0));
67 fixed[k] = -0.5*variance;
68 }
69 fixedDrifts_.push_back(fixed);
70 }
71
72 setCoterminalSwapRates(marketModel_->initialRates());
73 }
74
75 const std::vector<Size>& LogNormalCotSwapRatePc::numeraires() const {
76 return numeraires_;
77 }
78
79 void LogNormalCotSwapRatePc::setCoterminalSwapRates(const std::vector<Real>& swapRates)
80 {
81 QL_REQUIRE(swapRates.size()==numberOfRates_,
82 "mismatch between swapRates and rateTimes");
83 for (Size i=0; i<numberOfRates_; ++i)
84 initialLogSwapRates_[i] = std::log(swapRates[i] +
88 }
89
91 // why??
92 const auto* cotcs = dynamic_cast<const CoterminalSwapCurveState*>(&cs);
93 const std::vector<Real>& swapRates = cotcs->coterminalSwapRates();
94 setCoterminalSwapRates(swapRates);
95 }
96
99 std::copy(initialLogSwapRates_.begin(), initialLogSwapRates_.end(),
100 logSwapRates_.begin());
101 return generator_->nextPath();
102 }
103
105 {
106 //we're going from T1 to T2
107
108 //a) compute drifts D1 at T1;
111 else
112 std::copy(initialDrifts_.begin(), initialDrifts_.end(),
113 drifts1_.begin());
114
115 //b) evolve forwards up to T2 using D1;
116 Real weight = generator_->nextStep(brownians_);
117 const Matrix& A = marketModel_->pseudoRoot(currentStep_);
118 const std::vector<Real>& fixedDrift = fixedDrifts_[currentStep_];
119
120 Size i, alive = alive_[currentStep_];
121 for (i=alive; i<numberOfRates_; ++i) {
122 logSwapRates_[i] += drifts1_[i] + fixedDrift[i];
123 logSwapRates_[i] +=
124 std::inner_product(A.row_begin(i), A.row_end(i),
125 brownians_.begin(), Real(0.0));
126 swapRates_[i] = std::exp(logSwapRates_[i]) - displacements_[i];
127 }
128
129 // intermediate curve state update
131
132 //c) recompute drifts D2 using the predicted forwards;
134
135 //d) correct forwards using both drifts
136 for (i=alive; i<numberOfRates_; ++i) {
137 logSwapRates_[i] += (drifts2_[i]-drifts1_[i])/2.0;
138 swapRates_[i] = std::exp(logSwapRates_[i]) - displacements_[i];
139 }
140
141 //e) update curve state
143
144 ++currentStep_;
145
146 return weight;
147 }
148
150 return currentStep_;
151 }
152
154 return curveState_;
155 }
156
157}
virtual ext::shared_ptr< BrownianGenerator > create(Size factors, Size steps) const =0
Curve state for coterminal-swap market models
void setOnCoterminalSwapRates(const std::vector< Rate > &swapRates, Size firstValidIndex=0)
const std::vector< Rate > & coterminalSwapRates() const override
Curve state for market-model simulations
Definition: curvestate.hpp:41
LogNormalCotSwapRatePc(const ext::shared_ptr< MarketModel > &, const BrownianGeneratorFactory &, const std::vector< Size > &numeraires, Size initialStep=0)
void setCoterminalSwapRates(const std::vector< Real > &swapRates)
std::vector< SMMDriftCalculator > calculators_
const CurveState & currentState() const override
std::vector< std::vector< Real > > fixedDrifts_
ext::shared_ptr< MarketModel > marketModel_
const std::vector< Size > & numeraires() const override
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
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)
Drift computation for coterminal-swap market model.