QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
proxygreekengine.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 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
25#include <algorithm>
26#include <utility>
27
28namespace QuantLib {
29
31 ext::shared_ptr<MarketModelEvolver> evolver,
32 std::vector<std::vector<ext::shared_ptr<ConstrainedEvolver> > > constrainedEvolvers,
33 std::vector<std::vector<std::vector<Real> > > diffWeights,
34 std::vector<Size> startIndexOfConstraint,
35 std::vector<Size> endIndexOfConstraint,
36 const Clone<MarketModelMultiProduct>& product,
37 Real initialNumeraireValue)
38 : originalEvolver_(std::move(evolver)), constrainedEvolvers_(std::move(constrainedEvolvers)),
39 diffWeights_(std::move(diffWeights)),
40 startIndexOfConstraint_(std::move(startIndexOfConstraint)),
41 endIndexOfConstraint_(std::move(endIndexOfConstraint)), product_(product),
42 initialNumeraireValue_(initialNumeraireValue), numberProducts_(product->numberOfProducts()),
43 numerairesHeld_(product->numberOfProducts()),
44 numberCashFlowsThisStep_(product->numberOfProducts()),
45 cashFlowsGenerated_(product->numberOfProducts()) {
46 for (Size i=0; i<numberProducts_; ++i)
47 cashFlowsGenerated_[i].resize(
48 product_->maxNumberOfCashFlowsPerProductPerStep());
49
50 const std::vector<Time>& cashFlowTimes =
51 product_->possibleCashFlowTimes();
52 const std::vector<Rate>& rateTimes = product_->evolution().rateTimes();
53 Size n = cashFlowTimes.size();
54 discounters_.reserve(n);
55 for (Size j=0; j<n; ++j)
56 discounters_.emplace_back(cashFlowTimes[j], rateTimes);
57 const std::vector<Rate>& evolutionTimes =
58 product_->evolution().evolutionTimes();
59 constraints_.resize(evolutionTimes.size());
60 constraintsActive_.resize(evolutionTimes.size());
61 }
62
64 std::vector<Real>& values,
65 std::vector<std::vector<std::vector<Real> > >& modifiedValues) {
67 for (Size i=0; i<constrainedEvolvers_.size(); ++i) {
68 for (Size j=0; j<constrainedEvolvers_[i].size(); ++j) {
69 constrainedEvolvers_[i][j]->setThisConstraint(
72 modifiedValues[i][j]);
73 }
74 }
75 }
76
79 std::vector<std::vector<SequenceStatisticsInc> >& modifiedStats,
80 Size numberOfPaths) {
81 Size N = product_->numberOfProducts();
82
83 std::vector<Real> values(N);
84 std::vector<std::vector<std::vector<Real> > > modifiedValues;
85 modifiedValues.resize(constrainedEvolvers_.size());
86 for (Size i=0; i<modifiedValues.size(); ++i) {
87 modifiedValues[i].resize(constrainedEvolvers_[i].size());
88 for (auto& j : modifiedValues[i])
89 j.resize(N);
90 }
91
92 std::vector<Real> results(N);
93
94 for (Size i=0; i<numberOfPaths; ++i) {
95 singlePathValues(values, modifiedValues);
96 stats.add(values);
97
98 for (Size j=0; j<diffWeights_.size(); ++j) {
99 for (Size k=0; k<diffWeights_[j].size(); ++k) {
100 const std::vector<Real>& weights = diffWeights_[j][k];
101 for (Size l=0; l<N; ++l) {
102 results[l] = weights[0]*values[l];
103 for (Size n=1; n<weights.size(); ++n)
104 results[l] += weights[n]*modifiedValues[j][n-1][l];
105 }
106 modifiedStats[j][k].add(results);
107 }
108 }
109 }
110 }
111
113 std::vector<Real>& values,
114 bool storeRates) {
115
116 std::fill(numerairesHeld_.begin(), numerairesHeld_.end(), 0.0);
117 Real weight = evolver.startNewPath();
118 product_->reset();
119 Real principalInNumerairePortfolio = 1.0;
120
121 if (storeRates)
122 constraintsActive_ =false;
123 // std::fill(constraintsActive_.begin(),
124 // constraintsActive_.end(),
125 // false);
126 // }
127
128 bool done = false;
129 do {
130 Size thisStep = evolver.currentStep();
131 weight *= evolver.advanceStep();
132 done = product_->nextTimeStep(evolver.currentState(),
135 if (storeRates) {
136 constraints_[thisStep] = evolver.currentState().swapRate(
137 startIndexOfConstraint_[thisStep],
138 endIndexOfConstraint_[thisStep]);
139 constraintsActive_[thisStep] = true;
140 }
141
142 Size numeraire =
143 evolver.numeraires()[thisStep];
144
145 // for each product...
146 for (Size i=0; i<numberProducts_; ++i) {
147 // ...and each cash flow...
148 const std::vector<MarketModelMultiProduct::CashFlow>& cashflows =
150 for (Size j=0; j<numberCashFlowsThisStep_[i]; ++j) {
151 // ...convert the cash flow to numeraires.
152 // This is done by calculating the number of
153 // numeraire bonds corresponding to such cash flow...
154 const MarketModelDiscounter& discounter =
155 discounters_[cashflows[j].timeIndex];
156
157 Real bonds = cashflows[j].amount *
158 discounter.numeraireBonds(evolver.currentState(),
159 numeraire);
160
161 // ...and adding the newly bought bonds to the number
162 // of numeraires held.
163 numerairesHeld_[i] +=
164 weight*bonds/principalInNumerairePortfolio;
165 }
166 }
167
168 if (!done) {
169
170 // The numeraire might change between steps. This implies
171 // that we might have to convert the numeraire bonds for
172 // this step into a corresponding amount of numeraire
173 // bonds for the next step. This can be done by changing
174 // the principal of the numeraire and updating the number
175 // of bonds in the numeraire portfolio accordingly.
176
177 Size nextNumeraire = evolver.numeraires()[thisStep+1];
178
179 principalInNumerairePortfolio *=
180 evolver.currentState().discountRatio(numeraire,
181 nextNumeraire);
182 }
183
184 } while (!done);
185
186 for (Size i=0; i<numerairesHeld_.size(); ++i)
188
189 }
190
191}
cloning proxy to an underlying object
Definition: clone.hpp:40
Rate swapRate(Size begin, Size end) const
Definition: curvestate.cpp:32
virtual Real discountRatio(Size i, Size j) const =0
Statistics analysis of N-dimensional (sequence) data.
void add(const Sequence &sample, Real weight=1.0)
Real numeraireBonds(const CurveState &, Size numeraire) const
Definition: discounter.cpp:43
Market-model evolver.
Definition: evolver.hpp:35
virtual const std::vector< Size > & numeraires() const =0
virtual Real advanceStep()=0
virtual const CurveState & currentState() const =0
virtual Size currentStep() const =0
virtual Real startNewPath()=0
std::vector< Size > startIndexOfConstraint_
std::vector< std::vector< ext::shared_ptr< ConstrainedEvolver > > > constrainedEvolvers_
std::vector< Size > endIndexOfConstraint_
void singleEvolverValues(MarketModelEvolver &evolver, std::vector< Real > &values, bool storeRates=false)
std::valarray< bool > constraintsActive_
std::vector< MarketModelDiscounter > discounters_
void multiplePathValues(SequenceStatisticsInc &stats, std::vector< std::vector< SequenceStatisticsInc > > &modifiedStats, Size numberOfPaths)
std::vector< std::vector< MarketModelMultiProduct::CashFlow > > cashFlowsGenerated_
std::vector< Size > numberCashFlowsThisStep_
ext::shared_ptr< MarketModelEvolver > originalEvolver_
std::vector< Real > numerairesHeld_
ProxyGreekEngine(ext::shared_ptr< MarketModelEvolver > evolver, std::vector< std::vector< ext::shared_ptr< ConstrainedEvolver > > > constrainedEvolvers, std::vector< std::vector< std::vector< Real > > > diffWeights, std::vector< Size > startIndexOfConstraint, std::vector< Size > endIndexOfConstraint, const Clone< MarketModelMultiProduct > &product, Real initialNumeraireValue)
void singlePathValues(std::vector< Real > &values, std::vector< std::vector< std::vector< Real > > > &modifiedValues)
std::vector< std::vector< std::vector< Real > > > diffWeights_
std::vector< Rate > constraints_
Clone< MarketModelMultiProduct > product_
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
STL namespace.
std::vector< Size > numberCashFlowsThisStep_
std::vector< std::vector< CashFlow > > cashFlowsGenerated_