QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
lsstrategy.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 <utility>
26
27namespace QuantLib {
28
29 typedef MarketModelMultiProduct::CashFlow CashFlow;
30
33 std::vector<std::vector<Real> > basisCoefficients,
34 const EvolutionDescription& evolution,
35 const std::vector<Size>& numeraires,
38 : basisSystem_(std::move(basisSystem)), basisCoefficients_(std::move(basisCoefficients)),
39 exercise_(std::move(exercise)), control_(std::move(control)), numeraires_(numeraires) {
40
41 checkCompatibility(evolution, numeraires);
42 relevantTimes_ = evolution.evolutionTimes();
43
44 isBasisTime_.resize(relevantTimes_.size());
46 basisSystem_->evolution().evolutionTimes());
47 isRebateTime_.resize(relevantTimes_.size());
49 exercise_->evolution().evolutionTimes());
50 isControlTime_.resize(relevantTimes_.size());
52 control_->evolution().evolutionTimes());
53
54 exerciseIndex_ = std::vector<Size>(relevantTimes_.size());
55 isExerciseTime_.resize(relevantTimes_.size(), false);
56 std::valarray<bool> v = exercise_->isExerciseTime();
57 Size exercises = 0, idx = 0;
58 Size i;
59 for (i=0; i<relevantTimes_.size(); ++i) {
60 exerciseIndex_[i] = exercises;
61 if (isRebateTime_[i]) {
62 isExerciseTime_[i] = v[idx++];
63 if (isExerciseTime_[i]) {
64 exerciseTimes_.push_back(relevantTimes_[i]);
65 ++exercises;
66 }
67 }
68 }
69
70 const std::vector<Time>& rateTimes = evolution.rateTimes();
71 std::vector<Time> rebateTimes = exercise_->possibleCashFlowTimes();
72 rebateDiscounters_.reserve(rebateTimes.size());
73 for (i=0; i<rebateTimes.size(); ++i)
74 rebateDiscounters_.emplace_back(rebateTimes[i], rateTimes);
75
76 std::vector<Time> controlTimes = control_->possibleCashFlowTimes();
77 controlDiscounters_.reserve(controlTimes.size());
78 for (i=0; i<controlTimes.size(); ++i)
79 controlDiscounters_.emplace_back(controlTimes[i], rateTimes);
80
81 std::vector<Size> basisSizes = basisSystem_->numberOfFunctions();
82 basisValues_.resize(basisSystem_->numberOfExercises());
83 for (i=0; i<basisValues_.size(); ++i)
84 basisValues_[i].resize(basisSizes[i]);
85 }
86
87 std::vector<Time>
89 return exerciseTimes_;
90 }
91
92 std::vector<Time>
94 return relevantTimes_;
95 }
96
98 exercise_->reset();
99 control_->reset();
100 basisSystem_->reset();
101 currentIndex_ = 0;
103 }
104
106 const CurveState& currentState) const {
107
108 Size exerciseIndex = exerciseIndex_[currentIndex_-1];
109
110 CashFlow exerciseCF = exercise_->value(currentState);
111 Real exerciseValue = exerciseCF.amount *
112 rebateDiscounters_[exerciseCF.timeIndex]
113 .numeraireBonds(currentState,
116
117 CashFlow controlCF = control_->value(currentState);
118 Real controlValue = controlCF.amount *
119 controlDiscounters_[controlCF.timeIndex]
120 .numeraireBonds(currentState,
123
124 basisSystem_->values(currentState,
125 basisValues_[exerciseIndex]);
126
127 const std::vector<Real>& alphas = basisCoefficients_[exerciseIndex];
128 Real continuationValue =
129 std::inner_product(alphas.begin(), alphas.end(),
130 basisValues_[exerciseIndex].begin(),
131 controlValue);
132
133 return exerciseValue >= continuationValue;
134 }
135
137 const CurveState& currentState) {
139
141 exercise_->nextStep(currentState);
143 control_->nextStep(currentState);
145 basisSystem_->nextStep(currentState);
146
147 if (currentIndex_ < numeraires_.size()-1) {
148 Size numeraire = numeraires_[currentIndex_];
149 Size nextNumeraire = numeraires_[currentIndex_+1];
151 currentState.discountRatio(numeraire, nextNumeraire);
152 }
153
155 }
156
157 std::unique_ptr<ExerciseStrategy<CurveState>>
159 return std::unique_ptr<ExerciseStrategy<CurveState>>(new LongstaffSchwartzExerciseStrategy(*this));
160 }
161
162}
163
Base class for cash flows.
Definition: cashflow.hpp:40
virtual Real amount() const =0
returns the amount of the cash flow
cloning proxy to an underlying object
Definition: clone.hpp:40
Curve state for market-model simulations
Definition: curvestate.hpp:41
virtual Real discountRatio(Size i, Size j) const =0
Market-model evolution description.
const std::vector< Time > & rateTimes() const
const std::vector< Time > & evolutionTimes() const
std::vector< std::vector< Real > > basisCoefficients_
Definition: lsstrategy.hpp:49
std::vector< Time > relevantTimes() const override
Definition: lsstrategy.cpp:93
LongstaffSchwartzExerciseStrategy(Clone< MarketModelBasisSystem > basisSystem, std::vector< std::vector< Real > > basisCoefficients, const EvolutionDescription &evolution, const std::vector< Size > &numeraires, Clone< MarketModelExerciseValue > exercise, Clone< MarketModelExerciseValue > control)
Definition: lsstrategy.cpp:31
Clone< MarketModelBasisSystem > basisSystem_
Definition: lsstrategy.hpp:48
std::vector< std::vector< Real > > basisValues_
Definition: lsstrategy.hpp:61
std::vector< MarketModelDiscounter > controlDiscounters_
Definition: lsstrategy.hpp:60
Clone< MarketModelExerciseValue > exercise_
Definition: lsstrategy.hpp:50
Clone< MarketModelExerciseValue > control_
Definition: lsstrategy.hpp:51
bool exercise(const CurveState &currentState) const override
Definition: lsstrategy.cpp:105
std::unique_ptr< ExerciseStrategy< CurveState > > clone() const override
Definition: lsstrategy.cpp:158
std::vector< MarketModelDiscounter > rebateDiscounters_
Definition: lsstrategy.hpp:59
void nextStep(const CurveState &currentState) override
Definition: lsstrategy.cpp:136
std::vector< Time > exerciseTimes() const override
Definition: lsstrategy.cpp:88
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
std::valarray< bool > isInSubset(const std::vector< Time > &set, const std::vector< Time > &subset)
Definition: utilities.cpp:56
void checkCompatibility(const EvolutionDescription &evolution, const std::vector< Size > &numeraires)
MarketModelMultiProduct::CashFlow CashFlow
STL namespace.
const ParametricExercise & exercise_
ext::shared_ptr< BlackVolTermStructure > v