20#include <qle/pricingengines/numericlgmflexiswapengine.hpp>
26#include <ql/pricingengines/swap/discountingswapengine.hpp>
32 const std::vector<Date>& expiries,
34 const std::vector<Real>& strikes) {
38 DLOG(
"Get model data");
41 std::string referenceCalibrationGrid =
modelParameter(
"ReferenceCalibrationGrid", {},
false,
"");
45 QL_REQUIRE(sigma.size() == sigmaTimes.size() + 1,
"there must be n+1 volatilities (" << sigma.size()
46 <<
") for n volatility times ("
47 << sigmaTimes.size() <<
")");
51 bool continueOnCalibrationError =
globalParameters_.count(
"ContinueOnCalibrationError") > 0 &&
54 auto data = QuantLib::ext::make_shared<IrLgmData>();
57 std::vector<std::pair<CalibrationType, CalibrationStrategy>> validCalPairs = {
64 QL_REQUIRE(std::find(validCalPairs.begin(), validCalPairs.end(),
65 std::make_pair(calibration, calibrationStrategy)) != validCalPairs.end(),
66 "Calibration (" << calibration <<
") and CalibrationStrategy (" << calibrationStrategy
67 <<
") are not allowed in this combination");
71 Date today = Settings::instance().evaluationDate();
72 shiftHorizon = ActualActual(ActualActual::ISDA).yearFraction(today,
maturity) * shiftHorizon;
76 data->qualifier() = key;
77 data->calibrateH() =
false;
79 data->hValues() = {lambda};
80 data->reversionType() = reversionType;
81 data->calibrateA() =
false;
83 data->aValues() = sigma;
84 data->aTimes() = sigmaTimes;
86 data->calibrationType() = calibration;
87 data->shiftHorizon() = shiftHorizon;
91 DLOG(
"Build LgmData for co-terminal specification");
92 vector<string> expiryDates, termDates;
93 for (Size i = 0; i < expiries.size(); ++i) {
94 expiryDates.push_back(
to_string(expiries[i]));
97 data->optionExpiries() = expiryDates;
98 data->optionTerms() = termDates;
99 data->optionStrikes().resize(expiryDates.size(),
"ATM");
101 for (Size i = 0; i < expiryDates.size(); ++i) {
102 if (
strikes[i] != Null<Real>())
103 data->optionStrikes()[i] = std::to_string(
strikes[i]);
107 DLOG(
"Calibrate piecewise alpha");
109 data->calibrateH() =
false;
111 data->hValues() = {lambda};
112 data->calibrateA() =
true;
114 data->aValues() = {sigma};
116 DLOG(
"Calibrate constant sigma");
118 data->calibrateH() =
false;
120 data->hValues() = {lambda};
121 data->calibrateA() =
true;
123 data->aValues() = {sigma};
125 QL_FAIL(
"choice of calibration type invalid");
128 bool generateAdditionalResults =
false;
131 generateAdditionalResults =
parseBool(p->second);
135 DLOG(
"Build LGM model");
136 QuantLib::ext::shared_ptr<LgmBuilder> calib = QuantLib::ext::make_shared<LgmBuilder>(
138 referenceCalibrationGrid, generateAdditionalResults,
id);
141 QuantLib::ext::shared_ptr<QuantExt::LGM>
model;
144 model = calib->model();
146 DLOG(
"Skip calibration of model based on global parameters");
148 model = calib->model();
158 const std::vector<Date>& expiries,
160 const std::vector<Real>& strikes) {
161 DLOG(
"Building LGM Grid Flexi Swap engine for trade " <<
id);
165 DLOG(
"Get engine data");
170 QuantExt::NumericLgmFlexiSwapEngine::Method method;
172 method = QuantExt::NumericLgmFlexiSwapEngine::Method::SingleSwaptions;
174 method = QuantExt::NumericLgmFlexiSwapEngine::Method::SwaptionArray;
176 method = QuantExt::NumericLgmFlexiSwapEngine::Method::Automatic;
178 QL_FAIL(
"FlexiSwap engine parameter method (" <<
engineParameter(
"method") <<
") not recognised");
184 QuantLib::ext::shared_ptr<IborIndex> index;
187 return QuantLib::ext::make_shared<QuantExt::NumericLgmFlexiSwapEngine>(lgm, sy, ny, sx, nx, dscCurve, method,
188 singleSwaptionThreshold);
191QuantLib::ext::shared_ptr<PricingEngine>
193 const std::vector<Date>& expiries,
const Date&
maturity,
194 const std::vector<Real>& strikes) {
195 DLOG(
"Building Discounting Flexi Swap engine for trade " <<
id);
197 QuantLib::ext::shared_ptr<IborIndex> index;
200 return QuantLib::ext::make_shared<QuantLib::DiscountingSwapEngine>(dscCurve);
QuantLib::ext::shared_ptr< Market > market_
const string & model() const
Return the model name.
std::string modelParameter(const std::string &p, const std::vector< std::string > &qualifiers={}, const bool mandatory=true, const std::string &defaultValue="") const
std::string engineParameter(const std::string &p, const std::vector< std::string > &qualifiers={}, const bool mandatory=true, const std::string &defaultValue="") const
const string & configuration(const MarketContext &key)
Return a configuration (or the default one if key not found)
set< std::pair< string, QuantLib::ext::shared_ptr< QuantExt::ModelBuilder > > > modelBuilders_
std::map< std::string, std::string > globalParameters_
virtual QuantLib::ext::shared_ptr< QuantLib::PricingEngine > engineImpl(const std::string &id, const std::string &id2, const std::string &key, const std::vector< QuantLib::Date > &dates, const QuantLib::Date &maturity, const std::vector< QuantLib::Real > &strikes) override
virtual QuantLib::ext::shared_ptr< QuantLib::PricingEngine > engineImpl(const std::string &id, const std::string &id2, const std::string &key, const std::vector< QuantLib::Date > &dates, const QuantLib::Date &maturity, const std::vector< QuantLib::Real > &strikes) override
bool tryParseIborIndex(const string &s, QuantLib::ext::shared_ptr< IborIndex > &index)
Try to convert std::string to QuantLib::IborIndex.
bool parseBool(const string &s)
Convert text to bool.
Real parseReal(const string &s)
Convert text to Real.
Integer parseInteger(const string &s)
Convert text to QuantLib::Integer.
#define DLOG(text)
Logging Macro (Level = Debug)
CalibrationType parseCalibrationType(const string &s)
Convert calibration type string into enumerated class value.
CalibrationStrategy parseCalibrationStrategy(const string &s)
Convert calibration strategy string into enumerated class value.
VolatilityType volatilityType(CapFloorVolatilityCurveConfig::VolatilityType type)
Imply QuantLib::VolatilityType from CapFloorVolatilityCurveConfig::VolatilityType.
std::string to_string(const LocationInfo &l)
LgmData::ReversionType parseReversionType(const string &s)
Enum parsers.
LgmData::VolatilityType parseVolatilityType(const string &s)
Serializable Credit Default Swap.
Map text representations to QuantLib/QuantExt types.
string conversion utilities