QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdmstepconditioncomposite.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) 2008 Andreas Gaida
5 Copyright (C) 2008 Ralph Schreyer
6 Copyright (C) 2008 Klaus Spanderen
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22
23#include <ql/exercise.hpp>
31#include <set>
32#include <utility>
33
34namespace QuantLib {
35
37 const std::list<std::vector<Time> >& stoppingTimes, Conditions conditions)
38 : conditions_(std::move(conditions)) {
39
40 std::set<Real> allStoppingTimes;
41 for (const auto& stoppingTime : stoppingTimes) {
42 allStoppingTimes.insert(stoppingTime.begin(), stoppingTime.end());
43 }
44 stoppingTimes_ = std::vector<Time>(allStoppingTimes.begin(),
45 allStoppingTimes.end());
46 }
47
50 return conditions_;
51 }
52
53 const std::vector<Time>& FdmStepConditionComposite::stoppingTimes() const {
54 return stoppingTimes_;
55 }
56
58 for (const auto& condition : conditions_) {
59 condition->applyTo(a, t);
60 }
61 }
62
63 ext::shared_ptr<FdmStepConditionComposite>
65 const ext::shared_ptr<FdmSnapshotCondition>& c1,
66 const ext::shared_ptr<FdmStepConditionComposite>& c2) {
67
68 std::list<std::vector<Time> > stoppingTimes;
69 stoppingTimes.push_back(c2->stoppingTimes());
70 stoppingTimes.emplace_back(1, c1->getTime());
71
73 conditions.push_back(c2);
74 conditions.push_back(c1);
75
76 return ext::make_shared<FdmStepConditionComposite>(
78 }
79
80 ext::shared_ptr<FdmStepConditionComposite>
82 const DividendSchedule& cashFlow,
83 const ext::shared_ptr<Exercise>& exercise,
84 const ext::shared_ptr<FdmMesher>& mesher,
85 const ext::shared_ptr<FdmInnerValueCalculator>& calculator,
86 const Date& refDate,
87 const DayCounter& dayCounter) {
88
89 std::list<std::vector<Time> > stoppingTimes;
90 std::list<ext::shared_ptr<StepCondition<Array> > > stepConditions;
91
92 if (!cashFlow.empty()) {
93 auto dividendCondition =
94 ext::make_shared<FdmDividendHandler>(cashFlow, mesher,
95 refDate, dayCounter, 0);
96 stepConditions.push_back(dividendCondition);
97
98 std::vector<Time> dividendTimes = dividendCondition->dividendTimes();
99 const Time maturityTime = dayCounter.yearFraction(refDate, exercise->lastDate());
100
101 // this effectively excludes times after maturity
102 for (auto& t: dividendTimes)
103 t = std::min(maturityTime, t);
104 stoppingTimes.push_back(dividendTimes);
105
106 // smoother convergence behavior with number of time steps
107 for (auto& t: dividendTimes)
108 t = std::min(maturityTime, t+1e-5);
109 stoppingTimes.push_back(dividendTimes);
110 }
111
112 QL_REQUIRE( exercise->type() == Exercise::American
113 || exercise->type() == Exercise::European
114 || exercise->type() == Exercise::Bermudan,
115 "exercise type is not supported");
116 if (exercise->type() == Exercise::American) {
117 stepConditions.push_back(ext::shared_ptr<StepCondition<Array> >(
118 new FdmAmericanStepCondition(mesher,calculator)));
119 }
120 else if (exercise->type() == Exercise::Bermudan) {
121 ext::shared_ptr<FdmBermudanStepCondition> bermudanCondition(
122 new FdmBermudanStepCondition(exercise->dates(),
123 refDate, dayCounter,
124 mesher, calculator));
125 stepConditions.push_back(bermudanCondition);
126 stoppingTimes.push_back(bermudanCondition->exerciseTimes());
127 }
128
129 return ext::make_shared<FdmStepConditionComposite>(
130 stoppingTimes, stepConditions);
131
132 }
133
134}
1-D array used in linear algebra.
Definition: array.hpp:52
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
const std::vector< Time > & stoppingTimes() const
static ext::shared_ptr< FdmStepConditionComposite > joinConditions(const ext::shared_ptr< FdmSnapshotCondition > &c1, const ext::shared_ptr< FdmStepConditionComposite > &c2)
std::list< ext::shared_ptr< StepCondition< Array > > > Conditions
void applyTo(Array &a, Time t) const override
FdmStepConditionComposite(const std::list< std::vector< Time > > &stoppingTimes, Conditions conditions)
static ext::shared_ptr< FdmStepConditionComposite > vanillaComposite(const DividendSchedule &schedule, const ext::shared_ptr< Exercise > &exercise, const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< FdmInnerValueCalculator > &calculator, const Date &refDate, const DayCounter &dayCounter)
condition to be applied at every time step
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Option exercise classes and payoff function.
american step condition for multi dimensional problems
bermudan step condition for multi dimensional problems
dividend handler for fdm method for one equity direction
layer of abstraction to calculate the inner value
mesher for a fdm grid
step condition for value inspection
composite of fdm step conditions
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
STL namespace.