QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
pathwiseproductcallspecified.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4Copyright (C) 2009 Mark Joshi
5
6This file is part of QuantLib, a free-software/open-source library
7for financial quantitative analysts and developers - http://quantlib.org/
8
9QuantLib is free software: you can redistribute it and/or modify it
10under the terms of the QuantLib license. You should have received a
11copy 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
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20#include <ql/math/matrix.hpp>
21#include <ql/models/marketmodels/products/pathwise/pathwiseproductcallspecified.hpp>
22#include <ql/models/marketmodels/products/pathwise/pathwiseproductcashrebate.hpp>
23#include <ql/models/marketmodels/utilities.hpp>
24#include <utility>
25
26namespace QuantLib {
27
30 const Clone<ExerciseStrategy<CurveState> >& strategy,
32 : underlying_(underlying), strategy_(strategy), rebate_(std::move(rebate)) {
33 Size products = underlying_->numberOfProducts();
35 const std::vector<Time>& rateTimes1 = d1.rateTimes();
36 const std::vector<Time>& evolutionTimes1 = d1.evolutionTimes();
37 const std::vector<Time>& exerciseTimes = strategy->exerciseTimes();
38
39 if (!rebate_.empty()) {
40 EvolutionDescription d2 = rebate_->evolution();
41 const std::vector<Time>& rateTimes2 = d2.rateTimes();
42 QL_REQUIRE(rateTimes1.size() == rateTimes2.size() &&
43 std::equal(rateTimes1.begin(), rateTimes1.end(), rateTimes2.begin()),
44 "incompatible rate times");
45
46 QL_REQUIRE(underlying_->alreadyDeflated() == rebate_->alreadyDeflated(),
47 "incompatible deflations");
48 } else {
49 EvolutionDescription description(rateTimes1, exerciseTimes);
50 Matrix amounts(products, exerciseTimes.size(), 0.0);
51
52 rebate_ = MarketModelPathwiseCashRebate(description, exerciseTimes, amounts, products);
53 }
54
55 std::vector<Time> mergedEvolutionTimes;
56 std::vector<std::vector<Time> > allEvolutionTimes(4);
57 allEvolutionTimes[0] = evolutionTimes1;
58 allEvolutionTimes[1] = exerciseTimes;
59 allEvolutionTimes[2] = rebate_->evolution().evolutionTimes();
60 allEvolutionTimes[3] = strategy->relevantTimes();
61
62 mergeTimes(allEvolutionTimes, mergedEvolutionTimes, isPresent_);
63
64 // TODO: add relevant rates
65 evolution_ = EvolutionDescription(rateTimes1, mergedEvolutionTimes);
66
67 cashFlowTimes_ = underlying_->possibleCashFlowTimes();
69 const std::vector<Time> rebateTimes = rebate_->possibleCashFlowTimes();
70 cashFlowTimes_.insert(cashFlowTimes_.end(), rebateTimes.begin(), rebateTimes.end());
71
72 dummyCashFlowsThisStep_ = std::vector<Size>(products, 0);
73 Size n = rebate_->maxNumberOfCashFlowsPerProductPerStep();
74
75 CashFlow modelCashFlow;
76 modelCashFlow.amount.resize(d1.numberOfRates() + 1);
77
79 std::vector<std::vector<CashFlow> >(products, std::vector<CashFlow>(n, modelCashFlow));
80 }
81
83 return underlying_->alreadyDeflated();
84 }
85
87 return underlying_->suggestedNumeraires();
88 }
89
91 return evolution_;
92 }
93
95 return cashFlowTimes_;
96 }
97
99 return underlying_->numberOfProducts();
100 }
101
103 return std::max(underlying_->maxNumberOfCashFlowsPerProductPerStep(),
104 rebate_->maxNumberOfCashFlowsPerProductPerStep());
105 }
106
108 underlying_->reset();
109 rebate_->reset();
110 strategy_->reset();
111 currentIndex_ = 0;
112 wasCalled_ = false;
113 }
114
115
117 const CurveState& currentState,
118 std::vector<Size>& numberCashFlowsThisStep,
119 std::vector<std::vector<CashFlow> >& cashFlowsGenerated) {
120
121 bool isUnderlyingTime = isPresent_[0][currentIndex_];
122 bool isExerciseTime = isPresent_[1][currentIndex_];
123 bool isRebateTime = isPresent_[2][currentIndex_];
124 bool isStrategyRelevantTime = isPresent_[3][currentIndex_];
125
126 bool done = false;
127
128 if (!wasCalled_ && isStrategyRelevantTime)
129 strategy_->nextStep(currentState);
130
131
132 if (!wasCalled_ && isExerciseTime && callable_)
133 wasCalled_ = strategy_->exercise(currentState);
134
135 if (wasCalled_) {
136 if (isRebateTime) {
137 done = rebate_->nextTimeStep(currentState, numberCashFlowsThisStep,
138 cashFlowsGenerated);
139 for (Size i = 0; i < numberCashFlowsThisStep.size(); ++i)
140 for (Size j = 0; j < numberCashFlowsThisStep[i]; ++j)
141 cashFlowsGenerated[i][j].timeIndex += rebateOffset_;
142 }
143 } else {
144 if (isRebateTime)
145 rebate_->nextTimeStep(currentState, dummyCashFlowsThisStep_,
147 if (isUnderlyingTime)
148 done = underlying_->nextTimeStep(currentState, numberCashFlowsThisStep,
149 cashFlowsGenerated);
150 }
151
153 return done || currentIndex_ == evolution_.evolutionTimes().size();
154 }
155
156 std::unique_ptr<MarketModelPathwiseMultiProduct>
158 return std::unique_ptr<MarketModelPathwiseMultiProduct>(new CallSpecifiedPathwiseMultiProduct(*this));
159 }
160
162 return *underlying_;
163 }
164
166 return *strategy_;
167 }
168
170 return *rebate_;
171 }
172
174
176
177 }
178
179
180
181
182
const ExerciseStrategy< CurveState > & strategy() const
std::vector< Size > suggestedNumeraires() const override
Clone< MarketModelPathwiseMultiProduct > underlying_
Clone< MarketModelPathwiseMultiProduct > rebate_
std::vector< std::vector< CashFlow > > dummyCashFlowsGenerated_
bool nextTimeStep(const CurveState &currentState, std::vector< Size > &numberCashFlowsThisStep, std::vector< std::vector< MarketModelPathwiseMultiProduct::CashFlow > > &cashFlowsGenerated) override
return value indicates whether path is finished, TRUE means done
std::unique_ptr< MarketModelPathwiseMultiProduct > clone() const override
returns a newly-allocated copy of itself
std::vector< Time > possibleCashFlowTimes() const override
const MarketModelPathwiseMultiProduct & underlying() const
CallSpecifiedPathwiseMultiProduct(const Clone< MarketModelPathwiseMultiProduct > &underlying, const Clone< ExerciseStrategy< CurveState > > &, Clone< MarketModelPathwiseMultiProduct > rebate=Clone< MarketModelPathwiseMultiProduct >())
const EvolutionDescription & evolution() const override
Clone< ExerciseStrategy< CurveState > > strategy_
const MarketModelPathwiseMultiProduct & rebate() const
void reset() override
during simulation put product at start of path
cloning proxy to an underlying object
Definition: clone.hpp:40
Curve state for market-model simulations
Definition: curvestate.hpp:41
Market-model evolution description.
const std::vector< Time > & rateTimes() const
const std::vector< Time > & evolutionTimes() const
virtual std::vector< Time > exerciseTimes() const =0
virtual std::vector< Time > relevantTimes() const =0
virtual const EvolutionDescription & evolution() const =0
Matrix used in linear algebra.
Definition: matrix.hpp:41
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
void mergeTimes(const std::vector< std::vector< Time > > &times, std::vector< Time > &mergedTimes, std::vector< std::valarray< bool > > &isPresent)
Definition: utilities.cpp:29
STL namespace.