QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
bsmlattice.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) 2005 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_bsm_lattice_hpp
26#define quantlib_bsm_lattice_hpp
27
28#include <ql/methods/lattices/binomialtree.hpp>
29#include <ql/methods/lattices/lattice1d.hpp>
30
31namespace QuantLib {
32
34
35 template <class T>
36 class BlackScholesLattice : public TreeLattice1D<BlackScholesLattice<T> > {
37 public:
38 BlackScholesLattice(const ext::shared_ptr<T>& tree,
40 Time end,
41 Size steps);
42
43 Rate riskFreeRate() const { return riskFreeRate_; }
44 Time dt() const { return dt_; }
45 Size size(Size i) const { return tree_->size(i); }
47 Size) const { return discount_; }
48
49 void stepback(Size i, const Array& values, Array& newValues) const;
50
51 Real underlying(Size i, Size index) const {
52 return tree_->underlying(i, index);
53 }
54 Size descendant(Size i, Size index, Size branch) const {
55 return tree_->descendant(i, index, branch);
56 }
57 Real probability(Size i, Size index, Size branch) const {
58 return tree_->probability(i, index, branch);
59 }
60 protected:
61 ext::shared_ptr<T> tree_;
66 };
67
68
69 // template definitions
70
71 template <class T>
73 const ext::shared_ptr<T>& tree,
74 Rate riskFreeRate,
75 Time end,
76 Size steps)
77 : TreeLattice1D<BlackScholesLattice<T> >(TimeGrid(end, steps), 2),
78 tree_(tree), riskFreeRate_(riskFreeRate), dt_(end/steps),
79 discount_(std::exp(-riskFreeRate*(dt_))),
80 pd_(tree->probability(0, 0, 0)), pu_(tree->probability(0, 0, 1)) {}
81
82 template <class T>
84 Array& newValues) const {
85 for (Size j=0; j<size(i); j++)
86 newValues[j] = (pd_*values[j] + pu_*values[j+1])*discount_;
87 }
88
89}
90
91
92#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Simple binomial lattice approximating the Black-Scholes model.
Definition: bsmlattice.hpp:36
Size descendant(Size i, Size index, Size branch) const
Definition: bsmlattice.hpp:54
BlackScholesLattice(const ext::shared_ptr< T > &tree, Rate riskFreeRate, Time end, Size steps)
Definition: bsmlattice.hpp:72
Real probability(Size i, Size index, Size branch) const
Definition: bsmlattice.hpp:57
ext::shared_ptr< T > tree_
Definition: bsmlattice.hpp:61
Real underlying(Size i, Size index) const
Definition: bsmlattice.hpp:51
DiscountFactor discount(Size, Size) const
Definition: bsmlattice.hpp:46
void stepback(Size i, const Array &values, Array &newValues) const
Definition: bsmlattice.hpp:83
Size size(Size i) const
Definition: bsmlattice.hpp:45
time grid class
Definition: timegrid.hpp:43
One-dimensional tree-based lattice.
Definition: lattice1d.hpp:39
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.