QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
lognormalcmswapratepc.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 Size spanningForwards,
31 const ext::shared_ptr<MarketModel>& marketModel,
32 const BrownianGeneratorFactory& factory,
33 const std::vector<Size>& numeraires,
34 Size initialStep)
35 : spanningForwards_(spanningForwards),
36 marketModel_(marketModel),
37 numeraires_(numeraires),
38 initialStep_(initialStep),
39 numberOfRates_(marketModel->numberOfRates()),
40 numberOfFactors_(marketModel_->numberOfFactors()),
41 curveState_(marketModel->evolution().rateTimes(), spanningForwards),
42 swapRates_(marketModel->initialRates()),
43 displacements_(marketModel->displacements()),
44 logSwapRates_(numberOfRates_), initialLogSwapRates_(numberOfRates_),
45 drifts1_(numberOfRates_), drifts2_(numberOfRates_),
46 initialDrifts_(numberOfRates_), brownians_(numberOfFactors_),
47 correlatedBrownians_(numberOfRates_),
48 alive_(marketModel->evolution().firstAliveRate())
49 {
50 checkCompatibility(marketModel->evolution(), numeraires);
51
52 Size steps = marketModel->evolution().numberOfSteps();
53
55
57
58 calculators_.reserve(steps);
59 fixedDrifts_.reserve(steps);
60 for (Size j=0; j<steps; ++j) {
61 const Matrix& A = marketModel_->pseudoRoot(j);
62 calculators_.emplace_back(A, displacements_, marketModel->evolution().rateTaus(),
63 numeraires[j], alive_[j], spanningForwards);
64 std::vector<Real> fixed(numberOfRates_);
65 for (Size k=0; k<numberOfRates_; ++k) {
67 std::inner_product(A.row_begin(k), A.row_end(k),
68 A.row_begin(k), Real(0.0));
69 fixed[k] = -0.5*variance;
70 }
71 fixedDrifts_.push_back(fixed);
72 }
73
74 setCMSwapRates(marketModel_->initialRates());
75 }
76
77 const std::vector<Size>& LogNormalCmSwapRatePc::numeraires() const {
78 return numeraires_;
79 }
80
81 void LogNormalCmSwapRatePc::setCMSwapRates(const std::vector<Real>& swapRates)
82 {
83 QL_REQUIRE(swapRates.size()==numberOfRates_,
84 "mismatch between swapRates and rateTimes");
85 for (Size i=0; i<numberOfRates_; ++i)
86 initialLogSwapRates_[i] = std::log(swapRates[i] +
90 }
91
93 const auto* cotcs = dynamic_cast<const CMSwapCurveState*>(&cs);
94 const std::vector<Real>& swapRates = cotcs->cmSwapRates(spanningForwards_);
95 setCMSwapRates(swapRates);
96 }
97
100 std::copy(initialLogSwapRates_.begin(), initialLogSwapRates_.end(),
101 logSwapRates_.begin());
102 return generator_->nextPath();
103 }
104
106 {
107 // we're going from T1 to T2
108
109 // a) compute drifts D1 at T1;
112 else
113 std::copy(initialDrifts_.begin(), initialDrifts_.end(),
114 drifts1_.begin());
115
116 // b) evolve forwards up to T2 using D1;
117 Real weight = generator_->nextStep(brownians_);
118 const Matrix& A = marketModel_->pseudoRoot(currentStep_);
119 const std::vector<Real>& fixedDrift = fixedDrifts_[currentStep_];
120
121 Size i, alive = alive_[currentStep_];
122 for (i=alive; i<numberOfRates_; ++i) {
123 logSwapRates_[i] += drifts1_[i] + fixedDrift[i];
124 logSwapRates_[i] +=
125 std::inner_product(A.row_begin(i), A.row_end(i),
126 brownians_.begin(), Real(0.0));
127 swapRates_[i] = std::exp(logSwapRates_[i]) - displacements_[i];
128 }
129
130 // intermediate curve state update
132
133 // c) recompute drifts D2 using the predicted forwards;
135
136 // d) correct forwards using both drifts
137 for (i=alive; i<numberOfRates_; ++i) {
138 logSwapRates_[i] += (drifts2_[i]-drifts1_[i])/2.0;
139 swapRates_[i] = std::exp(logSwapRates_[i]) - displacements_[i];
140 }
141
142 // e) update curve state
143 //curveState_.setOnCMSwapRates(swapRates_, alive);
145
146 ++currentStep_;
147
148 return weight;
149 }
150
152 return currentStep_;
153 }
154
156 return curveState_;
157 }
158
159}
virtual ext::shared_ptr< BrownianGenerator > create(Size factors, Size steps) const =0
Curve state for constant-maturity-swap market models
const std::vector< Rate > & cmSwapRates(Size spanningForwards) const override
void setOnCMSwapRates(const std::vector< Rate > &cmSwapRates, Size firstValidIndex=0)
Curve state for market-model simulations
Definition: curvestate.hpp:41
LogNormalCmSwapRatePc(Size spanningForwards, const ext::shared_ptr< MarketModel > &, const BrownianGeneratorFactory &, const std::vector< Size > &numeraires, Size initialStep=0)
void setCMSwapRates(const std::vector< Real > &swapRates)
const CurveState & currentState() const override
std::vector< std::vector< Real > > fixedDrifts_
ext::shared_ptr< MarketModel > marketModel_
std::vector< CMSMMDriftCalculator > calculators_
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
Drift computation for CMS market model.
#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)