QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
discretizedasset.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb
5 Copyright (C) 2004, 2005, 2006 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
25#ifndef quantlib_discretized_asset_hpp
26#define quantlib_discretized_asset_hpp
27
28#include <ql/exercise.hpp>
29#include <ql/math/comparison.hpp>
30#include <ql/numericalmethod.hpp>
31#include <utility>
32
33namespace QuantLib {
34
37 public:
41 virtual ~DiscretizedAsset() = default;
42
44
45 Time time() const { return time_; }
46 Time& time() { return time_; }
47
48 const Array& values() const { return values_; }
49 Array& values() { return values_; }
50
51 const ext::shared_ptr<Lattice>& method() const {
52 return method_;
53 }
55
66 void initialize(const ext::shared_ptr<Lattice>&,
67 Time t);
68 void rollback(Time to);
69 void partialRollback(Time to);
72
89 virtual void reset(Size size) = 0;
90
99 void preAdjustValues();
100
110 void postAdjustValues();
111
116 }
117
124 virtual std::vector<Time> mandatoryTimes() const = 0;
126 protected:
128 enum class CouponAdjustment { pre, post };
129
132 bool isOnTime(Time t) const;
134 virtual void preAdjustValuesImpl() {}
136 virtual void postAdjustValuesImpl() {}
137
141 private:
142 ext::shared_ptr<Lattice> method_;
143 };
144
145
148 public:
150 void reset(Size size) override { values_ = Array(size, 1.0); }
151 std::vector<Time> mandatoryTimes() const override { return std::vector<Time>(); }
152 };
153
154
156
161 public:
162 DiscretizedOption(ext::shared_ptr<DiscretizedAsset> underlying,
163 Exercise::Type exerciseType,
164 std::vector<Time> exerciseTimes)
165 : underlying_(std::move(underlying)), exerciseType_(exerciseType),
166 exerciseTimes_(std::move(exerciseTimes)) {}
167 void reset(Size size) override;
168 std::vector<Time> mandatoryTimes() const override;
169
170 protected:
171 void postAdjustValuesImpl() override;
173 ext::shared_ptr<DiscretizedAsset> underlying_;
175 std::vector<Time> exerciseTimes_;
176 };
177
178
179
180 // inline definitions
181
183 const ext::shared_ptr<Lattice>& method,
184 Time t) {
185 method_ = method;
186 method_->initialize(*this, t);
187 }
188
190 method_->rollback(*this, to);
191 }
192
194 method_->partialRollback(*this, to);
195 }
196
198 return method_->presentValue(*this);
199 }
200
205 }
206 }
207
212 }
213 }
214
215 inline bool DiscretizedAsset::isOnTime(Time t) const {
216 const TimeGrid& grid = method()->timeGrid();
217 return close_enough(grid[grid.index(t)],time());
218 }
219
220
221 inline void DiscretizedOption::reset(Size size) {
222 QL_REQUIRE(method() == underlying_->method(),
223 "option and underlying were initialized on "
224 "different methods");
225 values_ = Array(size, 0.0);
226 adjustValues();
227 }
228
229 inline std::vector<Time> DiscretizedOption::mandatoryTimes() const {
230 std::vector<Time> times = underlying_->mandatoryTimes();
231 // discard negative times...
232 auto i = std::find_if(exerciseTimes_.begin(), exerciseTimes_.end(),
233 [](Time t){ return t >= 0.0; });
234 // and add the positive ones
235 times.insert(times.end(), i, exerciseTimes_.end());
236 return times;
237 }
238
240 for (Size i=0; i<values_.size(); i++)
241 values_[i] = std::max(underlying_->values()[i], values_[i]);
242 }
243
244
245}
246
247
248#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Discretized asset class used by numerical methods.
virtual std::vector< Time > mandatoryTimes() const =0
ext::shared_ptr< Lattice > method_
virtual ~DiscretizedAsset()=default
const Array & values() const
virtual void reset(Size size)=0
const ext::shared_ptr< Lattice > & method() const
void initialize(const ext::shared_ptr< Lattice > &, Time t)
Useful discretized discount bond asset.
std::vector< Time > mandatoryTimes() const override
Discretized option on a given asset.
DiscretizedOption(ext::shared_ptr< DiscretizedAsset > underlying, Exercise::Type exerciseType, std::vector< Time > exerciseTimes)
void postAdjustValuesImpl() override
std::vector< Time > mandatoryTimes() const override
std::vector< Time > exerciseTimes_
void reset(Size size) override
ext::shared_ptr< DiscretizedAsset > underlying_
time grid class
Definition: timegrid.hpp:43
Size index(Time t) const
returns the index i such that grid[i] = t
Definition: timegrid.cpp:43
#define QL_MAX_REAL
Definition: qldefines.hpp:176
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
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182
STL namespace.