QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
discretizedbarrieroption.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2014 Thema Consulting SA
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#include <ql/pricingengines/barrier/discretizedbarrieroption.hpp>
21#include <vector>
22
23namespace QuantLib {
24
26 const BarrierOption::arguments& args,
27 const StochasticProcess& process,
28 const TimeGrid& grid)
29 : arguments_(args), vanilla_(arguments_, process, grid) {
30 QL_REQUIRE(!args.exercise->dates().empty(), "specify at least one stopping date");
31
32 stoppingTimes_.resize(args.exercise->dates().size());
33 for (Size i=0; i<stoppingTimes_.size(); ++i) {
35 process.time(args.exercise->date(i));
36 if (!grid.empty()) {
37 // adjust to the given grid
39 }
40 }
41 }
42
45 values_ = Array(size, 0.0);
47 }
48
53 }
54 Array grid = method()->grid(time());
55 checkBarrier(values_, grid);
56 }
57
58 void DiscretizedBarrierOption::checkBarrier(Array &optvalues, const Array &grid) const {
59
60 Time now = time();
61 bool endTime = isOnTime(stoppingTimes_.back());
62 bool stoppingTime = false;
63 switch (arguments_.exercise->type()) {
65 if (now <= stoppingTimes_[1] &&
66 now >= stoppingTimes_[0])
67 stoppingTime = true;
68 break;
71 stoppingTime = true;
72 break;
74 for (Real i : stoppingTimes_) {
75 if (isOnTime(i)) {
76 stoppingTime = true;
77 break;
78 }
79 }
80 break;
81 default:
82 QL_FAIL("invalid option type");
83 }
84 for (Size j=0; j<optvalues.size(); j++) {
85 switch (arguments_.barrierType) {
86 case Barrier::DownIn:
87 if (grid[j] <= arguments_.barrier) {
88 // knocked in
89 if (stoppingTime) {
90 optvalues[j] = std::max(vanilla_.values()[j],
91 (*arguments_.payoff)(grid[j]));
92 }
93 else
94 optvalues[j] = vanilla_.values()[j];
95 }
96 else if (endTime)
97 optvalues[j] = arguments_.rebate;
98 break;
100 if (grid[j] <= arguments_.barrier)
101 optvalues[j] = arguments_.rebate; // knocked out
102 else if (stoppingTime) {
103 optvalues[j] = std::max(optvalues[j],
104 (*arguments_.payoff)(grid[j]));
105 }
106 break;
107 case Barrier::UpIn:
108 if (grid[j] >= arguments_.barrier) {
109 // knocked in
110 if (stoppingTime) {
111 optvalues[j] = std::max(vanilla_.values()[j],
112 (*arguments_.payoff)(grid[j]));
113 }
114 else
115 optvalues[j] = vanilla_.values()[j];
116 }
117 else if (endTime)
118 optvalues[j] = arguments_.rebate;
119 break;
120 case Barrier::UpOut:
121 if (grid[j] >= arguments_.barrier)
122 optvalues[j] = arguments_.rebate; // knocked out
123 else if (stoppingTime)
124 optvalues[j] = std::max(optvalues[j],
125 (*arguments_.payoff)(grid[j]));
126 break;
127 default:
128 QL_FAIL("invalid barrier type");
129 }
130 }
131 }
132
133
134
136 const BarrierOption::arguments& args,
137 const StochasticProcess& process,
138 const TimeGrid& grid)
139 : unenhanced_(args, process, grid) {
140 }
141
144 values_ = Array(size, 0.0);
145 adjustValues();
146 }
147
150
151 Array grid = method()->grid(time());
152 adjustBarrier(values_, grid);
153 unenhanced_.checkBarrier(values_, grid); // compute payoffs
154 }
155
157 Real barrier = unenhanced_.arguments().barrier;
158 Real rebate = unenhanced_.arguments().rebate;
160 case Barrier::DownIn:
161 for (Size j=0; j<optvalues.size()-1; ++j) {
162 if (grid[j]<=barrier && grid[j+1] > barrier) {
163 // grid[j+1] above barrier, grid[j] under (in),
164 // interpolate optvalues[j+1]
165 Real ltob = (barrier-grid[j]);
166 Real htob = (grid[j+1]-barrier);
167 Real htol = (grid[j+1]-grid[j]);
168 Real u1 = unenhanced_.values()[j+1];
169 Real t1 = unenhanced_.vanilla()[j+1];
170 optvalues[j+1] = std::max(0.0, (ltob*t1+htob*u1)/htol);
171 }
172 }
173 break;
174 case Barrier::DownOut:
175 for (Size j=0; j<optvalues.size()-1; ++j) {
176 if (grid[j]<=barrier && grid[j+1] > barrier) {
177 // grid[j+1] above barrier, grid[j] under (out),
178 // interpolate optvalues[j+1]
179 Real a = (barrier-grid[j])*rebate;
180 Real b = (grid[j+1]-barrier)*unenhanced_.values()[j+1];
181 Real c = (grid[j+1]-grid[j]);
182 optvalues[j+1] = std::max(0.0, (a+b)/c);
183 }
184 }
185 break;
186 case Barrier::UpIn:
187 for (Size j=0; j<optvalues.size()-1; ++j) {
188 if (grid[j] < barrier && grid[j+1] >= barrier) {
189 // grid[j+1] above barrier (in), grid[j] under,
190 // interpolate optvalues[j]
191 Real ltob = (barrier-grid[j]);
192 Real htob = (grid[j+1]-barrier);
193 Real htol = (grid[j+1]-grid[j]);
194 Real u = unenhanced_.values()[j];
195 Real t = unenhanced_.vanilla()[j];
196 optvalues[j] = std::max(0.0, (ltob*u+htob*t)/htol); // derman std
197 }
198 }
199 break;
200 case Barrier::UpOut:
201 for (Size j=0; j<optvalues.size()-1; ++j) {
202 if (grid[j] < barrier && grid[j+1] >= barrier) {
203 // grid[j+1] above barrier (out), grid[j] under,
204 // interpolate optvalues[j]
205 Real a = (barrier-grid[j])*unenhanced_.values()[j];
206 Real b = (grid[j+1]-barrier)*rebate;
207 Real c = (grid[j+1]-grid[j]);
208 optvalues[j] = std::max(0.0, (a+b)/c);
209 }
210 }
211 break;
212 }
213 }
214
215}
216
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Arguments for barrier option calculation
const Array & values() const
const ext::shared_ptr< Lattice > & method() const
void initialize(const ext::shared_ptr< Lattice > &, Time t)
void checkBarrier(Array &optvalues, const Array &grid) const
const BarrierOption::arguments & arguments() const
DiscretizedBarrierOption(const BarrierOption::arguments &, const StochasticProcess &process, const TimeGrid &grid=TimeGrid())
DiscretizedDermanKaniBarrierOption(const BarrierOption::arguments &, const StochasticProcess &process, const TimeGrid &grid=TimeGrid())
void adjustBarrier(Array &optvalues, const Array &grid)
multi-dimensional stochastic process class.
virtual Time time(const Date &) const
time grid class
Definition: timegrid.hpp:43
bool empty() const
Definition: timegrid.hpp:165
Time closestTime(Time t) const
returns the time on the grid closest to the given t
Definition: timegrid.hpp:148
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