1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2015 Thema Consulting SA
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<https://www.quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <ql/experimental/barrieroption/discretizeddoublebarrieroption.hpp>
#include <vector>
namespace QuantLib {
DiscretizedDoubleBarrierOption::DiscretizedDoubleBarrierOption(
const DoubleBarrierOption::arguments& args,
const StochasticProcess& process,
const TimeGrid& grid)
: arguments_(args), vanilla_(arguments_, process, grid) {
QL_REQUIRE(!args.exercise->dates().empty(), "specify at least one stopping date");
stoppingTimes_.resize(args.exercise->dates().size());
for (Size i=0; i<stoppingTimes_.size(); ++i) {
stoppingTimes_[i] =
process.time(args.exercise->date(i));
if (!grid.empty()) {
// adjust to the given grid
stoppingTimes_[i] = grid.closestTime(stoppingTimes_[i]);
}
}
}
void DiscretizedDoubleBarrierOption::reset(Size size) {
vanilla_.initialize(method(), time());
values_ = Array(size, 0.0);
adjustValues();
}
void DiscretizedDoubleBarrierOption::postAdjustValuesImpl() {
if (arguments_.barrierType!=DoubleBarrier::KnockOut) {
vanilla_.rollback(time());
}
Array grid = method()->grid(time());
checkBarrier(values_, grid);
}
void DiscretizedDoubleBarrierOption::checkBarrier(Array &optvalues, const Array &grid) const {
Time now = time();
bool endTime = isOnTime(stoppingTimes_.back());
bool stoppingTime = false;
switch (arguments_.exercise->type()) {
case Exercise::American:
if (now <= stoppingTimes_[1] &&
now >= stoppingTimes_[0])
stoppingTime = true;
break;
case Exercise::European:
if (isOnTime(stoppingTimes_[0]))
stoppingTime = true;
break;
case Exercise::Bermudan:
for (Real i : stoppingTimes_) {
if (isOnTime(i)) {
stoppingTime = true;
break;
}
}
break;
default:
QL_FAIL("invalid option type");
}
for (Size j=0; j<optvalues.size(); j++) {
switch (arguments_.barrierType) {
case DoubleBarrier::KnockIn:
if (grid[j] <= arguments_.barrier_lo) {
// knocked in dn
if (stoppingTime) {
optvalues[j] = std::max(vanilla()[j],
(*arguments_.payoff)(grid[j]));
}
else
optvalues[j] = vanilla()[j];
}
else if (grid[j] >= arguments_.barrier_hi) {
// knocked in up
if (stoppingTime) {
optvalues[j] = std::max(vanilla()[j],
(*arguments_.payoff)(grid[j]));
}
else
optvalues[j] = vanilla()[j];
}
else if (endTime)
optvalues[j] = arguments_.rebate;
break;
case DoubleBarrier::KnockOut:
if (grid[j] <= arguments_.barrier_lo)
optvalues[j] = arguments_.rebate; // knocked out lo
else if (grid[j] >= arguments_.barrier_hi)
optvalues[j] = arguments_.rebate; // knocked out hi
else if (stoppingTime)
optvalues[j] = std::max(optvalues[j],
(*arguments_.payoff)(grid[j]));
break;
case DoubleBarrier::KIKO:
// low barrier is KI, high is KO
if (grid[j] <= arguments_.barrier_lo) {
// knocked in dn
if (stoppingTime) {
optvalues[j] = std::max(vanilla()[j],
(*arguments_.payoff)(grid[j]));
}
else
optvalues[j] = vanilla()[j];
}
else if (grid[j] >= arguments_.barrier_hi)
optvalues[j] = arguments_.rebate; // knocked out hi
else if (endTime)
optvalues[j] = arguments_.rebate;
break;
case DoubleBarrier::KOKI:
// low barrier is KO, high is KI
if (grid[j] <= arguments_.barrier_lo)
optvalues[j] = arguments_.rebate; // knocked out lo
else if (grid[j] >= arguments_.barrier_hi) {
// knocked in up
if (stoppingTime) {
optvalues[j] = std::max(vanilla()[j],
(*arguments_.payoff)(grid[j]));
}
else
optvalues[j] = vanilla()[j];
}
else if (endTime)
optvalues[j] = arguments_.rebate;
break;
default:
QL_FAIL("invalid barrier type");
}
}
}
DiscretizedDermanKaniDoubleBarrierOption::DiscretizedDermanKaniDoubleBarrierOption(
const DoubleBarrierOption::arguments& args,
const StochasticProcess& process,
const TimeGrid& grid)
: unenhanced_(args, process, grid) {
}
void DiscretizedDermanKaniDoubleBarrierOption::reset(Size size) {
unenhanced_.initialize(method(), time());
values_ = Array(size, 0.0);
adjustValues();
}
void DiscretizedDermanKaniDoubleBarrierOption::postAdjustValuesImpl() {
unenhanced_.rollback(time());
Array grid = method()->grid(time());
unenhanced_.checkBarrier(values_, grid); // compute payoffs
adjustBarrier(values_, grid);
}
void DiscretizedDermanKaniDoubleBarrierOption::adjustBarrier(Array &optvalues, const Array &grid) {
Real barrier_lo = unenhanced_.arguments().barrier_lo;
Real barrier_hi = unenhanced_.arguments().barrier_hi;
Real rebate = unenhanced_.arguments().rebate;
switch (unenhanced_.arguments().barrierType) {
case DoubleBarrier::KnockIn:
for (Size j=0; j<optvalues.size()-1; ++j) {
if (grid[j]<=barrier_lo && grid[j+1] > barrier_lo) {
// grid[j+1] above barrier_lo, grid[j] under (in),
// interpolate optvalues[j+1]
Real ltob = (barrier_lo-grid[j]);
Real htob = (grid[j+1]-barrier_lo);
Real htol = (grid[j+1]-grid[j]);
Real u1 = unenhanced_.values()[j+1];
Real t1 = unenhanced_.vanilla()[j+1];
optvalues[j+1] = std::max(0.0, (ltob*t1+htob*u1)/htol); // derman std
}
else if (grid[j] < barrier_hi && grid[j+1] >= barrier_hi) {
// grid[j+1] above barrier_hi (in), grid[j] under,
// interpolate optvalues[j]
Real ltob = (barrier_hi-grid[j]);
Real htob = (grid[j+1]-barrier_hi);
Real htol = (grid[j+1]-grid[j]);
Real u = unenhanced_.values()[j];
Real t = unenhanced_.vanilla()[j];
optvalues[j] = std::max(0.0, (ltob*u+htob*t)/htol); // derman std
}
}
break;
case DoubleBarrier::KnockOut:
for (Size j=0; j<optvalues.size()-1; ++j) {
if (grid[j]<=barrier_lo && grid[j+1] > barrier_lo) {
// grid[j+1] above barrier_lo, grid[j] under (out),
// interpolate optvalues[j+1]
Real a = (barrier_lo-grid[j])*rebate;
Real b = (grid[j+1]-barrier_lo)*unenhanced_.values()[j+1];
Real c = (grid[j+1]-grid[j]);
optvalues[j+1] = std::max(0.0, (a+b)/c);
}
else if (grid[j] < barrier_hi && grid[j+1] >= barrier_hi) {
// grid[j+1] above barrier_hi (out), grid[j] under,
// interpolate optvalues[j]
Real a = (barrier_hi-grid[j])*unenhanced_.values()[j];
Real b = (grid[j+1]-barrier_hi)*rebate;
Real c = (grid[j+1]-grid[j]);
optvalues[j] = std::max(0.0, (a+b)/c);
}
}
break;
default:
QL_FAIL("unsupported barrier type");
break;
}
}
}
|