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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2004 Ferdinando Ametrano
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/americanpayoffatexpiry.hpp>
#include <ql/math/distributions/normaldistribution.hpp>
namespace QuantLib {
AmericanPayoffAtExpiry::AmericanPayoffAtExpiry(
Real spot, DiscountFactor discount, DiscountFactor dividendDiscount,
Real variance, const ext::shared_ptr<StrikedTypePayoff>& payoff,
bool knock_in)
: spot_(spot), discount_(discount), dividendDiscount_(dividendDiscount),
variance_(variance), knock_in_(knock_in) {
QL_REQUIRE(spot_>0.0,
"positive spot value required");
QL_REQUIRE(discount_>0.0,
"positive discount required");
QL_REQUIRE(dividendDiscount_>0.0,
"positive dividend discount required");
QL_REQUIRE(variance_>=0.0,
"negative variance not allowed");
stdDev_ = std::sqrt(variance_);
Option::Type type = payoff->optionType();
strike_ = payoff->strike();
forward_ = spot_ * dividendDiscount_ / discount_;
mu_ = std::log(dividendDiscount_/discount_)/variance_ - 0.5;
// binary cash-or-nothing payoff?
ext::shared_ptr<CashOrNothingPayoff> coo =
ext::dynamic_pointer_cast<CashOrNothingPayoff>(payoff);
if (coo != nullptr) {
K_ = coo->cashPayoff();
}
// binary asset-or-nothing payoff?
ext::shared_ptr<AssetOrNothingPayoff> aoo =
ext::dynamic_pointer_cast<AssetOrNothingPayoff>(payoff);
if (aoo != nullptr) {
K_ = forward_;
mu_ += 1.0;
}
log_H_S_ = std::log(strike_/spot_);
Real log_S_H_ = std::log(spot_/strike_);
double eta;
double phi;
switch (type) {
case Option::Call:
if (knock_in_) {
// up-and-in cash-(at-expiry)-or-nothing option
// a.k.a. american call with cash-or-nothing payoff
eta = -1.0;
phi = 1.0;
} else {
// up-and-out cash-(at-expiry)-or-nothing option
eta = -1.0;
phi = -1.0;
}
break;
case Option::Put:
if (knock_in_) {
// down-and-in cash-(at-expiry)-or-nothing option
// a.k.a. american put with cash-or-nothing payoff
eta = 1.0;
phi = -1.0;
} else {
// down-and-out cash-(at-expiry)-or-nothing option
eta = 1.0;
phi = 1.0;
}
break;
default:
QL_FAIL("invalid option type");
}
if (variance_>=QL_EPSILON) {
D1_ = phi*(log_S_H_/stdDev_ + mu_*stdDev_);
D2_ = eta*(log_H_S_/stdDev_ + mu_*stdDev_);
CumulativeNormalDistribution f;
cum_d1_ = f(D1_);
cum_d2_ = f(D2_);
n_d1_ = f.derivative(D1_);
n_d2_ = f.derivative(D2_);
} else {
if (log_S_H_ * phi >0)
cum_d1_= 1.0;
else
cum_d1_= 0.0;
if (log_H_S_ * eta >0)
cum_d2_= 1.0;
else
cum_d2_= 0.0;
n_d1_ = 0.0;
n_d2_ = 0.0;
}
switch (type) {
case Option::Call:
if (strike_<=spot_) {
if (knock_in_) {
// up-and-in cash-(at-expiry)-or-nothing option
// a.k.a. american call with cash-or-nothing payoff
cum_d1_ = 0.5;
cum_d2_ = 0.5;
} else {
// up-and-out cash-(at-expiry)-or-nothing option
// already knocked out
cum_d1_ = 0.0;
cum_d2_ = 0.0;
}
n_d1_ = 0.0;
n_d2_ = 0.0;
}
break;
case Option::Put:
if (strike_>=spot_) {
if (knock_in_) {
// down-and-in cash-(at-expiry)-or-nothing option
// a.k.a. american put with cash-or-nothing payoff
cum_d1_ = 0.5;
cum_d2_ = 0.5;
} else {
// down-and-out cash-(at-expiry)-or-nothing option
// already knocked out
cum_d1_ = 0.0;
cum_d2_ = 0.0;
}
n_d1_ = 0.0;
n_d2_ = 0.0;
}
break;
default:
QL_FAIL("invalid option type");
}
inTheMoney_ = (type==Option::Call && strike_<spot_) ||
(type==Option::Put && strike_>spot_);
if (inTheMoney_) {
X_ = 1.0;
Y_ = 1.0;
} else {
X_ = 1.0;
if (cum_d2_ == 0.0)
Y_ = 0.0; // check needed on some extreme cases
else
Y_ = std::pow(Real(strike_/spot_), Real(2.0*mu_));
}
if (!knock_in_)
Y_ *= -1.0;
}
}
|