19#include <ql/math/optimization/levenbergmarquardt.hpp>
20#include <ql/quotes/simplequote.hpp>
41 const QuantLib::ext::shared_ptr<ore::data::Market>& market,
const QuantLib::ext::shared_ptr<CommoditySchwartzData>& data,
42 const QuantLib::Currency& baseCcy,
const std::string& configuration,
43 const std::string& referenceCalibrationGrid)
44 : market_(market), configuration_(configuration), data_(
data), referenceCalibrationGrid_(referenceCalibrationGrid),
52 LOG(
"Start building CommoditySchwartz model for " <<
name);
55 std::string fxCcyPair = ccy.code() +
baseCcy_.code();
69 alwaysForwardNotifications();
72 if (
data->calibrateSigma() ||
data->calibrateKappa())
76 data->sigmaValue(),
data->kappaValue(),
77 data->driftFreeState());
102 return (
data_->calibrateSigma() ||
data_->calibrateKappa()) &&
108 DLOG(
"COM model requires recalibration");
118 QuantLib::ext::shared_ptr<QuantExt::CommoditySchwartzFutureOptionEngine> engine =
119 QuantLib::ext::make_shared<QuantExt::CommoditySchwartzFutureOptionEngine>(
model_);
126 (
data_->calibrateSigma() ==
false &&
data_->calibrateKappa() ==
false)) {
127 LOG(
"COM calibration is deactivated in the CommoditySchwartzModelData for name " <<
data_->name());
132 std::vector<bool> fix(
model_->parametrization()->numberOfParameters(),
true);
133 std::vector<Real> weights;
135 if (
data_->calibrateSigma()) {
138 LOG(
"CommoditySchwartzModel: calibrate sigma for name " <<
data_->name());
140 if (
data_->calibrateKappa()) {
143 LOG(
"CommoditySchwartzModel: calibrate kappa for name " <<
data_->name());
145 if (freeParams == 0) {
146 WLOG(
"CommoditySchwartzModel: skip calibration for name " <<
data_->name() <<
", no free parameters");
154 LOG(
"CommoditySchwartzModel for name " <<
data_->name() <<
" before calibration:"
160 LOG(
"CommoditySchwartzModel for name " <<
data_->name() <<
" after calibration:"
165 LOG(
"CommoditySchwartzModel calibration rmse error " <<
error_ <<
" for name " <<
data_->name());
169 }
catch (
const std::exception& e) {
170 WLOG(
"An error occurred: " << e.what());
180 strikeValue = Null<Real>();
182 strikeValue = strike.
value;
184 QL_FAIL(
"strike type ATMF or Absolute expected");
189 Date today = Settings::instance().evaluationDate();
190 std::string expiryString =
data_->optionExpiries()[j];
191 bool expiryDateBased;
195 Date expiryDate = expiryDateBased ? expiryDb : today + expiryPb;
200 bool hasUpdated =
false;
206 Size optionCounter = 0;
207 for (Size j = 0; j <
data_->optionExpiries().
size(); j++) {
223 QL_REQUIRE(
data_->optionExpiries().size() ==
data_->optionStrikes().size(),
"Commodity option vector size mismatch");
228 Date lastRefCalDate = Date::minDate();
229 std::vector<Date> referenceCalibrationDates;
233 std::vector<Time> expiryTimes;
235 for (Size j = 0; j <
data_->optionExpiries().
size(); j++) {
241 std::lower_bound(referenceCalibrationDates.begin(), referenceCalibrationDates.end(), expiryDate);
242 if (refCalDate == referenceCalibrationDates.end() || *refCalDate > lastRefCalDate) {
245 Handle<Quote> volQuote(QuantLib::ext::make_shared<SimpleQuote>(
vol_->blackVol(expiryDate, strikeValue)));
246 QuantLib::ext::shared_ptr<QuantExt::FutureOptionHelper>
helper = QuantLib::ext::make_shared<QuantExt::FutureOptionHelper>(
247 expiryDate, strikeValue,
curve_, volQuote,
data_->calibrationErrorType());
249 helper->performCalculations();
250 expiryTimes.push_back(
curve_->timeFromReference(
helper->option()->exercise()->date(0)));
251 DLOG(
"Added FutureOptionHelper " <<
data_->name() <<
" " << QuantLib::io::iso_date(expiryDate) <<
" "
252 << volQuote->value());
253 if (refCalDate != referenceCalibrationDates.end())
254 lastRefCalDate = *refCalDate;
260 std::sort(expiryTimes.begin(), expiryTimes.end());
261 auto itExpiryTime = unique(expiryTimes.begin(), expiryTimes.end());
262 expiryTimes.resize(distance(expiryTimes.begin(), itExpiryTime));
265 for (Size j = 0; j < expiryTimes.size(); j++)
virtual void forceRecalculate()
const std::string configuration_
void forceRecalculate() override
void performCalculations() const override
bool volSurfaceChanged(const bool updateCache) const
QuantLib::ext::shared_ptr< QuantExt::MarketObserver > marketObserver_
QuantLib::ext::shared_ptr< QuantExt::CommoditySchwartzModel > model_
const QuantLib::Currency baseCcy_
std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > optionBasket() const
Real optionStrike(const Size j) const
std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > optionBasket_
Handle< BlackVolTermStructure > vol_
CommoditySchwartzModelBuilder(const QuantLib::ext::shared_ptr< ore::data::Market > &market, const QuantLib::ext::shared_ptr< CommoditySchwartzData > &data, const QuantLib::Currency &baseCcy, const std::string &configuration=Market::defaultConfiguration, const std::string &referenceCalibrationGrid="")
Constructor.
std::vector< QuantLib::Real > volCache_
Date optionExpiry(const Size j) const
bool requiresRecalibration() const override
const QuantLib::ext::shared_ptr< ore::data::Market > market_
const QuantLib::ext::shared_ptr< CommoditySchwartzData > data_
Handle< QuantExt::PriceTermStructure > curve_
const std::string referenceCalibrationGrid_
QuantLib::ext::shared_ptr< QuantExt::CommoditySchwartzParametrization > parametrization() const
Real error() const
Return calibration error.
void buildOptionBasket() const
std::vector< bool > optionActive_
QuantLib::ext::shared_ptr< QuantExt::CommoditySchwartzParametrization > parametrization_
QuantLib::ext::shared_ptr< QuantExt::CommoditySchwartzModel > model() const
const std::vector< QuantLib::Date > & dates() const
Builder for a Lognormal COM 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)
#define WLOG(text)
Logging Macro (Level = Warning)
Shared utilities for model building and calibration.
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
Real getCalibrationError(const std::vector< QuantLib::ext::shared_ptr< Helper > > &basket)
Size size(const ValueType &v)
std::string getCalibrationDetails(LgmCalibrationInfo &info, const std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > &basket, const QuantLib::ext::shared_ptr< IrLgm1fParametrization > ¶metrization)
Serializable Credit Default Swap.
Map text representations to QuantLib/QuantExt types.
QuantLib::BootstrapHelper< QuantLib::OptionletVolatilityStructure > helper