QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
callspecifiedmultiproduct.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
23#include <utility>
24
25namespace QuantLib {
26
28 const Clone<MarketModelMultiProduct>& underlying,
29 const Clone<ExerciseStrategy<CurveState> >& strategy,
31 : underlying_(underlying), strategy_(strategy), rebate_(std::move(rebate)) {
32 Size products = underlying_->numberOfProducts();
34 const std::vector<Time>& rateTimes1 = d1.rateTimes();
35 const std::vector<Time>& evolutionTimes1 = d1.evolutionTimes();
36 const std::vector<Time>& exerciseTimes = strategy->exerciseTimes();
37
38 if (!rebate_.empty())
39 {
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(),
44 rateTimes2.begin()),
45 "incompatible rate times");
46 }
47 else
48 {
49 EvolutionDescription description(rateTimes1, exerciseTimes);
50 Matrix amounts(products, exerciseTimes.size(), 0.0);
51
52 rebate_ = MarketModelCashRebate(description, exerciseTimes,
53 amounts, products);
54 }
55
56 std::vector<Time> mergedEvolutionTimes;
57 std::vector<std::vector<Time> > allEvolutionTimes(4);
58 allEvolutionTimes[0] = evolutionTimes1;
59 allEvolutionTimes[1] = exerciseTimes;
60 allEvolutionTimes[2] = rebate_->evolution().evolutionTimes();
61 allEvolutionTimes[3] = strategy->relevantTimes();
62
63 mergeTimes(allEvolutionTimes,
64 mergedEvolutionTimes,
66
67 // TODO: add relevant rates
68 evolution_ = EvolutionDescription(rateTimes1, mergedEvolutionTimes);
69
70 cashFlowTimes_ = underlying_->possibleCashFlowTimes();
72 const std::vector<Time> rebateTimes = rebate_->possibleCashFlowTimes();
73 cashFlowTimes_.insert(cashFlowTimes_.end(),
74 rebateTimes.begin(), rebateTimes.end());
75
76 dummyCashFlowsThisStep_ = std::vector<Size>(products, 0);
77 Size n = rebate_->maxNumberOfCashFlowsPerProductPerStep();
79 std::vector<std::vector<CashFlow> >(products,
80 std::vector<CashFlow>(n));
81 }
82
83 std::vector<Size>
85 {
86 return underlying_->suggestedNumeraires();
87 }
88
90 {
91 return evolution_;
92 }
93
94 std::vector<Time>
96 {
97 return cashFlowTimes_;
98 }
99
101 {
102 return underlying_->numberOfProducts();
103 }
104
105 Size
107 {
108 return std::max(underlying_->maxNumberOfCashFlowsPerProductPerStep(),
109 rebate_->maxNumberOfCashFlowsPerProductPerStep());
110 }
111
113 {
114 underlying_->reset();
115 rebate_->reset();
116 strategy_->reset();
117 currentIndex_ = 0;
118 wasCalled_ = false;
119 }
120
121
123 const CurveState& currentState,
124 std::vector<Size>& numberCashFlowsThisStep,
125 std::vector<std::vector<CashFlow> >& cashFlowsGenerated)
126 {
127
128 bool isUnderlyingTime = isPresent_[0][currentIndex_];
129 bool isExerciseTime = isPresent_[1][currentIndex_];
130 bool isRebateTime = isPresent_[2][currentIndex_];
131 bool isStrategyRelevantTime = isPresent_[3][currentIndex_];
132
133 bool done = false;
134
135 if (!wasCalled_ && isStrategyRelevantTime)
136 strategy_->nextStep(currentState);
137
138
139 if (!wasCalled_ && isExerciseTime && callable_)
140 wasCalled_ = strategy_->exercise(currentState);
141
142 if (wasCalled_)
143 {
144 if (isRebateTime)
145 {
146 done = rebate_->nextTimeStep(currentState,
147 numberCashFlowsThisStep,
148 cashFlowsGenerated);
149 for (Size i=0; i<numberCashFlowsThisStep.size(); ++i)
150 for (Size j=0; j<numberCashFlowsThisStep[i]; ++j)
151 cashFlowsGenerated[i][j].timeIndex += rebateOffset_;
152 }
153 }
154 else
155 {
156 if (isRebateTime)
157 rebate_->nextTimeStep(currentState,
160 if (isUnderlyingTime)
161 done = underlying_->nextTimeStep(currentState,
162 numberCashFlowsThisStep,
163 cashFlowsGenerated);
164 }
165
167 return done || currentIndex_ == evolution_.evolutionTimes().size();
168 }
169
170 std::unique_ptr<MarketModelMultiProduct>
172 {
173 return std::unique_ptr<MarketModelMultiProduct>(new CallSpecifiedMultiProduct(*this));
174 }
175
178 {
179 return *underlying_;
180 }
181
184 {
185 return *strategy_;
186 }
187
190 {
191 return *rebate_;
192 }
193
195 {
196 callable_ = true;
197 }
198
200 {
201 callable_ = false;
202 }
203
204}
205
const ExerciseStrategy< CurveState > & strategy() const
std::vector< Size > suggestedNumeraires() const override
std::vector< std::vector< CashFlow > > dummyCashFlowsGenerated_
std::unique_ptr< MarketModelMultiProduct > clone() const override
returns a newly-allocated copy of itself
const MarketModelMultiProduct & underlying() const
bool nextTimeStep(const CurveState &currentState, std::vector< Size > &numberCashFlowsThisStep, std::vector< std::vector< CashFlow > > &cashFlowsGenerated) override
return value indicates whether path is finished, TRUE means done
CallSpecifiedMultiProduct(const Clone< MarketModelMultiProduct > &underlying, const Clone< ExerciseStrategy< CurveState > > &, Clone< MarketModelMultiProduct > rebate=Clone< MarketModelMultiProduct >())
std::vector< Time > possibleCashFlowTimes() const override
const EvolutionDescription & evolution() const override
Clone< ExerciseStrategy< CurveState > > strategy_
Clone< MarketModelMultiProduct > rebate_
Clone< MarketModelMultiProduct > underlying_
const MarketModelMultiProduct & rebate() const
std::vector< std::valarray< bool > > isPresent_
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
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
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.