19#include <ql/math/optimization/levenbergmarquardt.hpp>
20#include <ql/quotes/simplequote.hpp>
39EqBsBuilder::EqBsBuilder(
const QuantLib::ext::shared_ptr<ore::data::Market>& market,
const QuantLib::ext::shared_ptr<EqBsData>& data,
40 const QuantLib::Currency& baseCcy,
const std::string& configuration,
41 const std::string& referenceCalibrationGrid)
42 : market_(market), configuration_(configuration), data_(
data), referenceCalibrationGrid_(referenceCalibrationGrid),
50 LOG(
"Start building EqBs model for " <<
eqName);
53 std::string fxCcyPair = ccy.code() +
baseCcy_.code();
72 alwaysForwardNotifications();
75 if (
data->calibrateSigma())
78 Array sigmaTimes, sigma;
80 QL_REQUIRE(
data->sigmaTimes().size() == 0,
"empty sigma time grid expected");
81 QL_REQUIRE(
data->sigmaValues().size() == 1,
"initial sigma grid size 1 expected");
82 sigmaTimes = Array(0);
83 sigma = Array(
data_->sigmaValues().begin(),
data_->sigmaValues().end());
85 if (
data->calibrateSigma()) {
88 sigma = Array(sigmaTimes.size() + 1,
data->sigmaValues()[0]);
91 sigmaTimes = Array(
data_->sigmaTimes().begin(),
data_->sigmaTimes().end());
92 sigma = Array(
data_->sigmaValues().begin(),
data_->sigmaValues().end());
93 QL_REQUIRE(sigma.size() == sigmaTimes.size() + 1,
"sigma grids do not match");
99 parametrization_ = QuantLib::ext::make_shared<QuantExt::EqBsPiecewiseConstantParametrization>(
105 QL_FAIL(
"interpolation type not supported for Equity");
123 return data_->calibrateSigma() &&
146 strikeValue = Null<Real>();
148 strikeValue = strike.
value;
150 QL_FAIL(
"strike type ATMF or Absolute expected");
155 Date today = Settings::instance().evaluationDate();
156 std::string expiryString =
data_->optionExpiries()[j];
157 bool expiryDateBased;
161 Date expiryDate = expiryDateBased ? expiryDb : today + expiryPb;
166 bool hasUpdated =
false;
172 Size optionCounter = 0;
173 for (Size j = 0; j <
data_->optionExpiries().
size(); j++) {
188 QL_REQUIRE(
data_->optionExpiries().size() ==
data_->optionStrikes().size(),
"Eq option vector size mismatch");
193 Date lastRefCalDate = Date::minDate();
194 std::vector<Date> referenceCalibrationDates;
198 std::vector<Time> expiryTimes;
200 for (Size j = 0; j <
data_->optionExpiries().
size(); j++) {
206 std::lower_bound(referenceCalibrationDates.begin(), referenceCalibrationDates.end(), expiryDate);
207 if (refCalDate == referenceCalibrationDates.end() || *refCalDate > lastRefCalDate) {
210 Handle<Quote> volQuote(QuantLib::ext::make_shared<SimpleQuote>(
eqVol_->blackVol(expiryDate, strikeValue)));
211 QuantLib::ext::shared_ptr<QuantExt::FxEqOptionHelper>
helper = QuantLib::ext::make_shared<QuantExt::FxEqOptionHelper>(
214 helper->performCalculations();
215 expiryTimes.push_back(
ytsRate_->timeFromReference(
helper->option()->exercise()->date(0)));
216 DLOG(
"Added EquityOptionHelper " <<
data_->eqName() <<
" " << QuantLib::io::iso_date(expiryDate) <<
" "
217 << volQuote->value());
218 if (refCalDate != referenceCalibrationDates.end())
219 lastRefCalDate = *refCalDate;
225 std::sort(expiryTimes.begin(), expiryTimes.end());
226 auto itExpiryTime = unique(expiryTimes.begin(), expiryTimes.end());
227 expiryTimes.resize(distance(expiryTimes.begin(), itExpiryTime));
230 for (Size j = 0; j < expiryTimes.size(); j++)
virtual void forceRecalculate()
const std::vector< QuantLib::Date > & dates() const
const std::string configuration_
void forceRecalculate() override
void performCalculations() const override
bool volSurfaceChanged(const bool updateCache) const
QuantLib::ext::shared_ptr< QuantExt::MarketObserver > marketObserver_
Handle< BlackVolTermStructure > eqVol_
const QuantLib::Currency baseCcy_
std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > optionBasket() const
QuantLib::ext::shared_ptr< QuantExt::EqBsParametrization > parametrization_
Real optionStrike(const Size j) const
std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > optionBasket_
void setCalibrationDone() const
const QuantLib::ext::shared_ptr< EqBsData > data_
Handle< YieldTermStructure > ytsDiv_
Date optionExpiry(const Size j) const
bool requiresRecalibration() const override
const QuantLib::ext::shared_ptr< ore::data::Market > market_
EqBsBuilder(const QuantLib::ext::shared_ptr< ore::data::Market > &market, const QuantLib::ext::shared_ptr< EqBsData > &data, const QuantLib::Currency &baseCcy, const std::string &configuration=Market::defaultConfiguration, const std::string &referenceCalibrationGrid="")
Constructor.
const std::string referenceCalibrationGrid_
Handle< YieldTermStructure > ytsRate_
QuantLib::ext::shared_ptr< QuantExt::EqBsParametrization > parametrization() const
std::vector< QuantLib::Real > eqVolCache_
Real error() const
Return calibration error.
void buildOptionBasket() const
std::vector< bool > optionActive_
Builder for a Lognormal EQ model component.
Strike parseStrike(const std::string &s)
Convert text to Strike.
boost::variant< QuantLib::Date, QuantLib::Period > parseDateOrPeriod(const string &s)
Convert text to QuantLib::Period or QuantLib::Date.
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
Classes and functions for log message handling.
#define LOG(text)
Logging Macro (Level = Notice)
#define DLOG(text)
Logging Macro (Level = Debug)
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
Size size(const ValueType &v)
Serializable Credit Default Swap.
Map text representations to QuantLib/QuantExt types.
QuantLib::BootstrapHelper< QuantLib::OptionletVolatilityStructure > helper