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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2013 Peter Caspers
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
<http://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/models/smilesectionutils.hpp>
#include <ql/math/comparison.hpp>
namespace QuantLib {
SmileSectionUtils::SmileSectionUtils(const SmileSection §ion,
const std::vector<Real> &moneynessGrid,
const Real atm,
const bool deleteArbitragePoints) {
if (moneynessGrid.size() != 0) {
QL_REQUIRE(
moneynessGrid[0] >= 0.0,
"moneyness grid should only contain non negative values ("
<< moneynessGrid[0] << ")");
for (Size i = 0; i < moneynessGrid.size() - 1; i++) {
QL_REQUIRE(moneynessGrid[i] < moneynessGrid[i + 1],
"moneyness grid should contain strictly increasing "
"values ("
<< moneynessGrid[i] << ","
<< moneynessGrid[i + 1] << " at indices " << i
<< ", " << i + 1 << ")");
}
}
if (atm == Null<Real>()) {
f_ = section.atmLevel();
QL_REQUIRE(f_ != Null<Real>(),
"atm level must be provided by source section or given "
"in the constructor");
} else {
f_ = atm;
}
std::vector<Real> tmp;
static const Real defaultMoney[] = { 0.0, 0.01, 0.05, 0.10, 0.25, 0.40,
0.50, 0.60, 0.70, 0.80, 0.90, 1.0,
1.25, 1.5, 1.75, 2.0, 5.0, 7.5,
10.0, 15.0, 20.0 };
if (moneynessGrid.size() == 0)
tmp = std::vector<Real>(defaultMoney, defaultMoney + 21);
else
tmp = std::vector<Real>(moneynessGrid);
if (tmp[0] > QL_EPSILON) {
m_.push_back(0.0);
k_.push_back(0.0);
}
bool minStrikeAdded = false, maxStrikeAdded = false;
for (Size i = 0; i < tmp.size(); i++) {
Real k = tmp[i] * f_;
if (tmp[i] <= QL_EPSILON ||
(k >= section.minStrike() && k <= section.maxStrike())) {
if (!minStrikeAdded || !close(k, section.minStrike())) {
m_.push_back(tmp[i]);
k_.push_back(k);
}
if (close(k, section.maxStrike()))
maxStrikeAdded = true;
} else { // if the section provides a limited strike range
// we put the respective endpoint in our grid
// in order to not loose too much information
if (k < section.minStrike() && !minStrikeAdded) {
m_.push_back(section.minStrike() / f_);
k_.push_back(section.minStrike());
minStrikeAdded = true;
}
if (k > section.maxStrike() && !maxStrikeAdded) {
m_.push_back(section.maxStrike() / f_);
k_.push_back(section.maxStrike());
maxStrikeAdded = true;
}
}
}
c_.push_back(f_);
for (Size i = 1; i < k_.size(); i++) {
c_.push_back(section.optionPrice(k_[i], Option::Call, 1.0));
}
Size centralIndex =
std::upper_bound(m_.begin(), m_.end(), 1.0 - QL_EPSILON) -
m_.begin();
QL_REQUIRE(centralIndex < k_.size() - 1 && centralIndex > 1,
"Atm point in moneyness grid ("
<< centralIndex << ") too close to boundary.");
leftIndex_ = centralIndex;
rightIndex_ = centralIndex;
bool done = false;
while (!done) {
bool isAf = true;
done = true;
while (isAf && rightIndex_ < k_.size() - 1) {
rightIndex_++;
isAf = af(leftIndex_, rightIndex_, rightIndex_) &&
af(leftIndex_, rightIndex_ - 1, rightIndex_);
}
if (!isAf)
rightIndex_--;
isAf = true;
while (isAf && leftIndex_ > 1) {
leftIndex_--;
isAf = af(leftIndex_, leftIndex_, rightIndex_) &&
af(leftIndex_, leftIndex_ + 1, rightIndex_);
}
if (!isAf)
leftIndex_++;
if (rightIndex_ < leftIndex_)
rightIndex_ = leftIndex_;
if (deleteArbitragePoints && leftIndex_ > 1) {
m_.erase(m_.begin() + leftIndex_ - 1);
k_.erase(k_.begin() + leftIndex_ - 1);
c_.erase(c_.begin() + leftIndex_ - 1);
leftIndex_--;
rightIndex_--;
done = false;
}
if (deleteArbitragePoints && rightIndex_ < k_.size() - 1) {
m_.erase(m_.begin() + rightIndex_ + 1);
k_.erase(k_.begin() + rightIndex_ + 1);
c_.erase(c_.begin() + rightIndex_ + 1);
rightIndex_--;
done = false;
}
}
QL_REQUIRE(rightIndex_ > leftIndex_,
"arbitrage free region must at least contain two "
"points (only index is "
<< leftIndex_ << ")");
}
const std::pair<Real, Real> SmileSectionUtils::arbitragefreeRegion() const {
return std::pair<Real, Real>(k_[leftIndex_], k_[rightIndex_]);
}
const std::pair<Size, Size>
SmileSectionUtils::arbitragefreeIndices() const {
return std::pair<Size, Size>(leftIndex_, rightIndex_);
}
bool SmileSectionUtils::af(const Size i0, const Size i,
const Size i1) const {
if (i == 0)
return true;
Size im = i - 1 >= i0 ? i - 1 : 0;
Real q1 = (c_[i] - c_[im]) / (k_[i] - k_[im]);
if (q1 < -1.0 || q1 > 0.0)
return false;
if (i >= i1)
return true;
Real q2 = (c_[i + 1] - c_[i]) / (k_[i + 1] - k_[i]);
if (q1 <= q2)
return true;
return false;
}
}
|