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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2014 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/pricingengines/barrier/discretizedbarrieroption.hpp>
#include <vector>
namespace QuantLib {
DiscretizedBarrierOption::DiscretizedBarrierOption(
const BarrierOption::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 DiscretizedBarrierOption::reset(Size size) {
vanilla_.initialize(method(), time());
values_ = Array(size, 0.0);
adjustValues();
}
void DiscretizedBarrierOption::postAdjustValuesImpl() {
if (arguments_.barrierType==Barrier::DownIn ||
arguments_.barrierType==Barrier::UpIn) {
vanilla_.rollback(time());
}
Array grid = method()->grid(time());
checkBarrier(values_, grid);
}
void DiscretizedBarrierOption::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 Barrier::DownIn:
if (grid[j] <= arguments_.barrier) {
// knocked in
if (stoppingTime) {
optvalues[j] = std::max(vanilla_.values()[j],
(*arguments_.payoff)(grid[j]));
}
else
optvalues[j] = vanilla_.values()[j];
}
else if (endTime)
optvalues[j] = arguments_.rebate;
break;
case Barrier::DownOut:
if (grid[j] <= arguments_.barrier)
optvalues[j] = arguments_.rebate; // knocked out
else if (stoppingTime) {
optvalues[j] = std::max(optvalues[j],
(*arguments_.payoff)(grid[j]));
}
break;
case Barrier::UpIn:
if (grid[j] >= arguments_.barrier) {
// knocked in
if (stoppingTime) {
optvalues[j] = std::max(vanilla_.values()[j],
(*arguments_.payoff)(grid[j]));
}
else
optvalues[j] = vanilla_.values()[j];
}
else if (endTime)
optvalues[j] = arguments_.rebate;
break;
case Barrier::UpOut:
if (grid[j] >= arguments_.barrier)
optvalues[j] = arguments_.rebate; // knocked out
else if (stoppingTime)
optvalues[j] = std::max(optvalues[j],
(*arguments_.payoff)(grid[j]));
break;
default:
QL_FAIL("invalid barrier type");
}
}
}
DiscretizedDermanKaniBarrierOption::DiscretizedDermanKaniBarrierOption(
const BarrierOption::arguments& args,
const StochasticProcess& process,
const TimeGrid& grid)
: unenhanced_(args, process, grid) {
}
void DiscretizedDermanKaniBarrierOption::reset(Size size) {
unenhanced_.initialize(method(), time());
values_ = Array(size, 0.0);
adjustValues();
}
void DiscretizedDermanKaniBarrierOption::postAdjustValuesImpl() {
unenhanced_.rollback(time());
Array grid = method()->grid(time());
adjustBarrier(values_, grid);
unenhanced_.checkBarrier(values_, grid); // compute payoffs
}
void DiscretizedDermanKaniBarrierOption::adjustBarrier(Array &optvalues, const Array &grid) {
Real barrier = unenhanced_.arguments().barrier;
Real rebate = unenhanced_.arguments().rebate;
switch (unenhanced_.arguments().barrierType) {
case Barrier::DownIn:
for (Size j=0; j<optvalues.size()-1; ++j) {
if (grid[j]<=barrier && grid[j+1] > barrier) {
// grid[j+1] above barrier, grid[j] under (in),
// interpolate optvalues[j+1]
Real ltob = (barrier-grid[j]);
Real htob = (grid[j+1]-barrier);
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);
}
}
break;
case Barrier::DownOut:
for (Size j=0; j<optvalues.size()-1; ++j) {
if (grid[j]<=barrier && grid[j+1] > barrier) {
// grid[j+1] above barrier, grid[j] under (out),
// interpolate optvalues[j+1]
Real a = (barrier-grid[j])*rebate;
Real b = (grid[j+1]-barrier)*unenhanced_.values()[j+1];
Real c = (grid[j+1]-grid[j]);
optvalues[j+1] = std::max(0.0, (a+b)/c);
}
}
break;
case Barrier::UpIn:
for (Size j=0; j<optvalues.size()-1; ++j) {
if (grid[j] < barrier && grid[j+1] >= barrier) {
// grid[j+1] above barrier (in), grid[j] under,
// interpolate optvalues[j]
Real ltob = (barrier-grid[j]);
Real htob = (grid[j+1]-barrier);
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 Barrier::UpOut:
for (Size j=0; j<optvalues.size()-1; ++j) {
if (grid[j] < barrier && grid[j+1] >= barrier) {
// grid[j+1] above barrier (out), grid[j] under,
// interpolate optvalues[j]
Real a = (barrier-grid[j])*unenhanced_.values()[j];
Real b = (grid[j+1]-barrier)*rebate;
Real c = (grid[j+1]-grid[j]);
optvalues[j] = std::max(0.0, (a+b)/c);
}
}
break;
}
}
}
|