QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdmdividendhandler.cpp
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, 2009 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#include <ql/time/daycounter.hpp>
23#include <ql/math/interpolations/linearinterpolation.hpp>
24#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
25#include <ql/methods/finitedifferences/utilities/fdmdividendhandler.hpp>
26
27namespace QuantLib {
28
29
31 const DividendSchedule& schedule,
32 const ext::shared_ptr<FdmMesher>& mesher,
33 const Date& referenceDate,
34 const DayCounter& dayCounter,
35 Size equityDirection)
36 : x_(mesher->layout()->dim()[equityDirection]),
37 mesher_(mesher),
38 equityDirection_(equityDirection) {
39
40 dividends_.reserve(schedule.size());
41 dividendDates_.reserve(schedule.size());
42 dividendTimes_.reserve(schedule.size());
43 for (const auto& iter : schedule) {
44 dividends_.push_back(iter->amount());
45 dividendDates_.push_back(iter->date());
46 dividendTimes_.push_back(dayCounter.yearFraction(referenceDate, iter->date()));
47 }
48
49 Array tmp = mesher_->locations(equityDirection);
50 Size spacing = mesher_->layout()->spacing()[equityDirection];
51 for (Size i = 0; i < x_.size(); ++i) {
52 x_[i] = std::exp(tmp[i*spacing]);
53 }
54 }
55
56 const std::vector<Time>& FdmDividendHandler::dividendTimes() const {
57 return dividendTimes_;
58 }
59
60 const std::vector<Date>& FdmDividendHandler::dividendDates() const {
61 return dividendDates_;
62 }
63
64 const std::vector<Real>& FdmDividendHandler::dividends() const {
65 return dividends_;
66 }
67
69 Array aCopy(a);
70
71 auto iter = std::find(dividendTimes_.begin(), dividendTimes_.end(), t);
72
73 if (iter != dividendTimes_.end()) {
74 const Real dividend = dividends_[iter - dividendTimes_.begin()];
75
76 if (mesher_->layout()->dim().size() == 1) {
77 LinearInterpolation interp(x_.begin(), x_.end(), aCopy.begin());
78 for (Size k=0; k<x_.size(); ++k) {
79 a[k] = interp(std::max(x_[0], x_[k]-dividend), true);
80 }
81 }
82 else {
83 Array tmp(x_.size());
84 Size xSpacing = mesher_->layout()->spacing()[equityDirection_];
85
86 for (Size i=0; i<mesher_->layout()->dim().size(); ++i) {
87 if (i!=equityDirection_) {
88 Size ySpacing = mesher_->layout()->spacing()[i];
89 for (Size j=0; j<mesher_->layout()->dim()[i]; ++j) {
90 for (Size k=0; k<x_.size(); ++k) {
91 Size index = j*ySpacing + k*xSpacing;
92 tmp[k] = aCopy[index];
93 }
94 LinearInterpolation interp(x_.begin(), x_.end(),
95 tmp.begin());
96 for (Size k=0; k<x_.size(); ++k) {
97 Size index = j*ySpacing + k*xSpacing;
98 a[index] = interp(
99 std::max(x_[0], x_[k]-dividend), true);
100 }
101 }
102 }
103 }
104 }
105 }
106 }
107}
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:511
Size size() const
dimension of the array
Definition: array.hpp:495
const_iterator begin() const
Definition: array.hpp:503
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
FdmDividendHandler(const DividendSchedule &schedule, const ext::shared_ptr< FdmMesher > &mesher, const Date &referenceDate, const DayCounter &dayCounter, Size equityDirection)
std::vector< Date > dividendDates_
const std::vector< Time > & dividendTimes() const
std::vector< Time > dividendTimes_
const ext::shared_ptr< FdmMesher > mesher_
void applyTo(Array &a, Time t) const override
const std::vector< Date > & dividendDates() const
const std::vector< Real > & dividends() const
Linear interpolation between discrete points
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
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::vector< ext::shared_ptr< Dividend > > DividendSchedule