QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdmaffinemodelswapinnervalue.hpp
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) 2011 Klaus Spanderen
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
20/*! \file fdmaffinemodelswapinnervalue.hpp
21*/
22
23#ifndef quantlib_fdm_affine_model_swap_inner_value_hpp
24#define quantlib_fdm_affine_model_swap_inner_value_hpp
25
34#include <map>
35#include <utility>
36
37namespace QuantLib {
38
39 template <class ModelType>
41 public:
42 FdmAffineModelSwapInnerValue(ext::shared_ptr<ModelType> disModel,
43 ext::shared_ptr<ModelType> fwdModel,
44 const ext::shared_ptr<FixedVsFloatingSwap>& swap,
45 std::map<Time, Date> exerciseDates,
46 ext::shared_ptr<FdmMesher> mesher,
47 Size direction);
48
49 Real innerValue(const FdmLinearOpIterator& iter, Time t) override;
50 Real avgInnerValue(const FdmLinearOpIterator& iter, Time t) override;
51
52 private:
53 Array getState(const ext::shared_ptr<ModelType>& model,
54 Time t,
55 const FdmLinearOpIterator& iter) const;
56
58 const ext::shared_ptr<ModelType> disModel_, fwdModel_;
59
60 const ext::shared_ptr<IborIndex> index_;
61 const ext::shared_ptr<FixedVsFloatingSwap> swap_;
62 const std::map<Time, Date> exerciseDates_;
63 const ext::shared_ptr<FdmMesher> mesher_;
65 };
66
67 template <class ModelType>
69 ext::shared_ptr<ModelType> disModel,
70 ext::shared_ptr<ModelType> fwdModel,
71 const ext::shared_ptr<FixedVsFloatingSwap>& swap,
72 std::map<Time, Date> exerciseDates,
73 ext::shared_ptr<FdmMesher> mesher,
74 Size direction)
75 : disModel_(std::move(disModel)), fwdModel_(std::move(fwdModel)), index_(swap->iborIndex()),
76 swap_(ext::make_shared<VanillaSwap>(swap->type(),
77 swap->nominal(),
78 swap->fixedSchedule(),
79 swap->fixedRate(),
80 swap->fixedDayCount(),
81 swap->floatingSchedule(),
82 swap->iborIndex()->clone(fwdTs_),
83 swap->spread(),
84 swap->floatingDayCount(),
85 swap->paymentConvention())),
86 exerciseDates_(std::move(exerciseDates)), mesher_(std::move(mesher)), direction_(direction) {}
87
88 template <class ModelType> inline
90 const FdmLinearOpIterator& iter, Time t) {
91
92 const Date& iterExerciseDate = exerciseDates_.find(t)->second;
93
94 const Array disRate(getState(disModel_, t, iter));
95 const Array fwdRate(getState(fwdModel_, t, iter));
96
97 if (disTs_.empty() || iterExerciseDate != disTs_->referenceDate()) {
98
99 const Handle<YieldTermStructure> discount
100 = disModel_->termStructure();
101
102 disTs_.linkTo(ext::shared_ptr<YieldTermStructure>(
103 new FdmAffineModelTermStructure(disRate,
104 discount->calendar(), discount->dayCounter(),
105 iterExerciseDate, discount->referenceDate(),
106 disModel_)));
107
108 const Handle<YieldTermStructure> fwd = fwdModel_->termStructure();
109
110 fwdTs_.linkTo(ext::shared_ptr<YieldTermStructure>(
111 new FdmAffineModelTermStructure(fwdRate,
112 fwd->calendar(), fwd->dayCounter(),
113 iterExerciseDate, fwd->referenceDate(),
114 fwdModel_)));
115
116 }
117 else {
118 ext::dynamic_pointer_cast<FdmAffineModelTermStructure>(
119 disTs_.currentLink())->setVariable(disRate);
120 ext::dynamic_pointer_cast<FdmAffineModelTermStructure>(
121 fwdTs_.currentLink())->setVariable(fwdRate);
122 }
123
124 Real npv = 0.0;
125 for (Size j = 0; j < 2; j++) {
126 for (const auto& i : swap_->leg(j)) {
127 npv +=
128 ext::dynamic_pointer_cast<Coupon>(i)->accrualStartDate() >= iterExerciseDate ?
129 Real(i->amount() * disTs_->discount(i->date())) :
130 0.0;
131 }
132 if (j == 0)
133 npv *= -1.0;
134 }
135 if (swap_->type() == Swap::Receiver)
136 npv *= -1.0;
137
138 return std::max(0.0, npv);
139 }
140
141 template <class ModelType> inline
143 const FdmLinearOpIterator& iter, Time t) {
144 return innerValue(iter, t);
145 }
146
147}
148#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Concrete date class.
Definition: date.hpp:125
Real innerValue(const FdmLinearOpIterator &iter, Time t) override
const ext::shared_ptr< ModelType > disModel_
Array getState(const ext::shared_ptr< ModelType > &model, Time t, const FdmLinearOpIterator &iter) const
Real avgInnerValue(const FdmLinearOpIterator &iter, Time t) override
RelinkableHandle< YieldTermStructure > fwdTs_
const ext::shared_ptr< FixedVsFloatingSwap > swap_
const ext::shared_ptr< ModelType > fwdModel_
RelinkableHandle< YieldTermStructure > disTs_
FdmAffineModelSwapInnerValue(ext::shared_ptr< ModelType > disModel, ext::shared_ptr< ModelType > fwdModel, const ext::shared_ptr< FixedVsFloatingSwap > &swap, std::map< Time, Date > exerciseDates, ext::shared_ptr< FdmMesher > mesher, Size direction)
Shared handle to an observable.
Definition: handle.hpp:41
Relinkable handle to an observable.
Definition: handle.hpp:117
Plain-vanilla swap: fix vs ibor leg.
Definition: vanillaswap.hpp:65
Coupon accruing over a fixed period.
const DefaultType & t
discounting swap engine
layer of abstraction to calculate the inner value
mesher for a fdm grid
const ext::shared_ptr< FdmMesher > mesher_
const Size direction_
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
Hull & White (HW) model.
base class for Inter-Bank-Offered-Rate indexes
Definition: any.hpp:35
void swap(Array &v, Array &w) noexcept
Definition: array.hpp:903
STL namespace.
Simple fixed-rate vs Libor swap.