Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | List of all members
CommoditySchwartzModelBuilder Class Reference

Builder for a COM model component. More...

#include <ored/model/commodityschwartzmodelbuilder.hpp>

+ Inheritance diagram for CommoditySchwartzModelBuilder:
+ Collaboration diagram for CommoditySchwartzModelBuilder:

Public Member Functions

 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. More...
 
Real error () const
 Return calibration error. More...
 
Inspectors
std::string name ()
 
QuantLib::ext::shared_ptr< QuantExt::CommoditySchwartzParametrizationparametrization () const
 
QuantLib::ext::shared_ptr< QuantExt::CommoditySchwartzModelmodel () const
 
std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > optionBasket () const
 
- Public Member Functions inherited from ModelBuilder
void recalibrate () const
 
virtual void forceRecalculate ()
 
virtual bool requiresRecalibration () const=0
 

ModelBuilder interface

const QuantLib::ext::shared_ptr< ore::data::Marketmarket_
 
const std::string configuration_
 
const QuantLib::ext::shared_ptr< CommoditySchwartzDatadata_
 
const std::string referenceCalibrationGrid_
 
const QuantLib::Currency baseCcy_
 
Real error_
 
QuantLib::ext::shared_ptr< QuantExt::CommoditySchwartzParametrizationparametrization_
 
QuantLib::ext::shared_ptr< QuantExt::CommoditySchwartzModelmodel_
 
std::vector< booloptionActive_
 
std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > optionBasket_
 
Array optionExpiries_
 
Handle< Quote > fxSpot_
 
Handle< QuantExt::PriceTermStructurecurve_
 
Handle< BlackVolTermStructurevol_
 
std::vector< QuantLib::Real > volCache_
 
bool forceCalibration_ = false
 
QuantLib::ext::shared_ptr< QuantExt::MarketObservermarketObserver_
 
std::vector< Real > calibrationErrors_
 
Array params_
 
void forceRecalculate () override
 
bool requiresRecalibration () const override
 
void performCalculations () const override
 
Real optionStrike (const Size j) const
 
Date optionExpiry (const Size j) const
 
void buildOptionBasket () const
 
bool volSurfaceChanged (const bool updateCache) const
 

Detailed Description

Builder for a COM model component.

This class is a utility to turn a COM model component's description into a COM model parametrization which can be used to ultimately instantiate a CrossAssetModel. h

Definition at line 49 of file commodityschwartzmodelbuilder.hpp.

Constructor & Destructor Documentation

◆ CommoditySchwartzModelBuilder()

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.

Parameters
marketMarket object
dataEQ model parameters/description
baseCcybase currency for calibration
configurationMarket configuration to use
referenceCalibrationGridthe reference calibration grid

Definition at line 40 of file commodityschwartzmodelbuilder.cpp.

44 : market_(market), configuration_(configuration), data_(data), referenceCalibrationGrid_(referenceCalibrationGrid),
45 baseCcy_(baseCcy) {
46
47 optionActive_ = std::vector<bool>(data_->optionExpiries().size(), false);
48 marketObserver_ = QuantLib::ext::make_shared<MarketObserver>();
49 QuantLib::Currency ccy = ore::data::parseCurrency(data->currency());
50 string name = data->name();
51
52 LOG("Start building CommoditySchwartz model for " << name);
53
54 // get market data
55 std::string fxCcyPair = ccy.code() + baseCcy_.code();
56 fxSpot_ = market_->fxRate(fxCcyPair, configuration_);
57 curve_ = market_->commodityPriceCurve(name, configuration_);
58 vol_ = market_->commodityVolatility(name, configuration_);
59
60 // register with market observables except vols
61 marketObserver_->registerWith(fxSpot_);
62 marketObserver_->registerWith(curve_);
63
64 // register the builder with the vol and the market observer
65 registerWith(vol_);
66 registerWith(marketObserver_);
67
68 // notify observers of all market data changes, not only when not calculated
69 alwaysForwardNotifications();
70
71 // build option basket and derive parametrization from it
72 if (data->calibrateSigma() || data->calibrateKappa())
74
75 parametrization_ = QuantLib::ext::make_shared<QuantExt::CommoditySchwartzParametrization>(ccy, name, curve_, fxSpot_,
76 data->sigmaValue(), data->kappaValue(),
77 data->driftFreeState());
78 model_ = QuantLib::ext::make_shared<QuantExt::CommoditySchwartzModel>(parametrization_);
79 params_ = model_->params();
80}
QuantLib::ext::shared_ptr< QuantExt::MarketObserver > marketObserver_
QuantLib::ext::shared_ptr< QuantExt::CommoditySchwartzModel > model_
const QuantLib::ext::shared_ptr< ore::data::Market > market_
const QuantLib::ext::shared_ptr< CommoditySchwartzData > data_
Handle< QuantExt::PriceTermStructure > curve_
QuantLib::ext::shared_ptr< QuantExt::CommoditySchwartzParametrization > parametrization_
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
Definition: parsers.cpp:290
@ data
Definition: log.hpp:77
#define LOG(text)
Logging Macro (Level = Notice)
Definition: log.hpp:552
+ Here is the call graph for this function:

Member Function Documentation

◆ error()

Real error ( ) const

Return calibration error.

Definition at line 87 of file commodityschwartzmodelbuilder.cpp.

87 {
88 calculate();
89 return error_;
90}

◆ name()

std::string name ( )

Definition at line 68 of file commodityschwartzmodelbuilder.hpp.

68{ return data_->name(); }
+ Here is the caller graph for this function:

◆ parametrization()

QuantLib::ext::shared_ptr< QuantExt::CommoditySchwartzParametrization > parametrization ( ) const

Definition at line 92 of file commodityschwartzmodelbuilder.cpp.

92 {
93 calculate();
94 return parametrization_;
95}

◆ model()

QuantLib::ext::shared_ptr< QuantExt::CommoditySchwartzModel > model ( ) const

Definition at line 82 of file commodityschwartzmodelbuilder.cpp.

82 {
83 calculate();
84 return model_;
85}

◆ optionBasket()

std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > optionBasket ( ) const

Definition at line 96 of file commodityschwartzmodelbuilder.cpp.

96 {
97 calculate();
98 return optionBasket_;
99}
std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > optionBasket_

◆ forceRecalculate()

void forceRecalculate ( )
overridevirtual

Reimplemented from ModelBuilder.

Definition at line 269 of file commodityschwartzmodelbuilder.cpp.

+ Here is the call graph for this function:

◆ requiresRecalibration()

bool requiresRecalibration ( ) const
overridevirtual

Implements ModelBuilder.

Definition at line 101 of file commodityschwartzmodelbuilder.cpp.

101 {
102 return (data_->calibrateSigma() || data_->calibrateKappa()) &&
103 (volSurfaceChanged(false) || marketObserver_->hasUpdated(false) || forceCalibration_);
104}
bool volSurfaceChanged(const bool updateCache) const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ performCalculations()

void performCalculations ( ) const
overrideprivate

Definition at line 106 of file commodityschwartzmodelbuilder.cpp.

106 {
107 if (requiresRecalibration()) {
108 DLOG("COM model requires recalibration");
109
110 // reset market observer updated flag
111 marketObserver_->hasUpdated(true);
112 // build option basket
114 // update vol cache
115 volSurfaceChanged(true);
116
117 // attach pricing engine to helpers
118 QuantLib::ext::shared_ptr<QuantExt::CommoditySchwartzFutureOptionEngine> engine =
119 QuantLib::ext::make_shared<QuantExt::CommoditySchwartzFutureOptionEngine>(model_);
120 for (Size j = 0; j < optionBasket_.size(); j++)
121 optionBasket_[j]->setPricingEngine(engine);
122
123 QL_REQUIRE(data_->calibrationType() != CalibrationType::Bootstrap, "Bootstrap COM calibration not supported yet");
124
125 if (data_->calibrationType() == CalibrationType::None ||
126 (data_->calibrateSigma() == false && data_->calibrateKappa() == false)) {
127 LOG("COM calibration is deactivated in the CommoditySchwartzModelData for name " << data_->name());
128 return;
129 }
130
131 // check which parameters are kept fixed
132 std::vector<bool> fix(model_->parametrization()->numberOfParameters(), true);
133 std::vector<Real> weights;
134 Size freeParams = 0;
135 if (data_->calibrateSigma()) {
136 fix[0] = false;
137 freeParams++;
138 LOG("CommoditySchwartzModel: calibrate sigma for name " << data_->name());
139 }
140 if (data_->calibrateKappa()) {
141 fix[1] = false;
142 freeParams++;
143 LOG("CommoditySchwartzModel: calibrate kappa for name " << data_->name());
144 }
145 if (freeParams == 0) {
146 WLOG("CommoditySchwartzModel: skip calibration for name " << data_->name() << ", no free parameters");
147 error_ = 0.0;
148 return;
149 }
150
151 // use identical start values for each calibration to ensure identical results for identical baskets
152 model_->setParams(params_);
153
154 LOG("CommoditySchwartzModel for name " << data_->name() << " before calibration:"
155 << " sigma=" << parametrization_->sigmaParameter()
156 << " kappa=" << parametrization_->kappaParameter());
157
158 model_->calibrate(optionBasket_, *data_->optimizationMethod(), data_->endCriteria(), data_->constraint(), weights, fix);
159
160 LOG("CommoditySchwartzModel for name " << data_->name() << " after calibration:"
161 << " sigma=" << parametrization_->sigmaParameter()
162 << " kappa=" << parametrization_->kappaParameter());
163
165 LOG("CommoditySchwartzModel calibration rmse error " << error_ << " for name " << data_->name());
166 try {
168 DLOG(d);
169 } catch (const std::exception& e) {
170 WLOG("An error occurred: " << e.what());
171 }
172 }
173}
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
#define WLOG(text)
Logging Macro (Level = Warning)
Definition: log.hpp:550
Real getCalibrationError(const std::vector< QuantLib::ext::shared_ptr< Helper > > &basket)
Definition: utilities.hpp:47
std::string getCalibrationDetails(LgmCalibrationInfo &info, const std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > &basket, const QuantLib::ext::shared_ptr< IrLgm1fParametrization > &parametrization)
Definition: utilities.cpp:125
+ Here is the call graph for this function:

◆ optionStrike()

Real optionStrike ( const Size  j) const
private

Definition at line 175 of file commodityschwartzmodelbuilder.cpp.

175 {
176 ore::data::Strike strike = ore::data::parseStrike(data_->optionStrikes()[j]);
177 Real strikeValue;
178 // TODO: Extend strike type coverage
180 strikeValue = Null<Real>();
181 else if (strike.type == ore::data::Strike::Type::Absolute)
182 strikeValue = strike.value;
183 else
184 QL_FAIL("strike type ATMF or Absolute expected");
185 return strikeValue;
186}
Strike parseStrike(const std::string &s)
Convert text to Strike.
Definition: strike.cpp:30
QuantLib::Real value
Definition: strike.hpp:47
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ optionExpiry()

Date optionExpiry ( const Size  j) const
private

Definition at line 188 of file commodityschwartzmodelbuilder.cpp.

188 {
189 Date today = Settings::instance().evaluationDate();
190 std::string expiryString = data_->optionExpiries()[j];
191 bool expiryDateBased;
192 Period expiryPb;
193 Date expiryDb;
194 parseDateOrPeriod(expiryString, expiryDb, expiryPb, expiryDateBased);
195 Date expiryDate = expiryDateBased ? expiryDb : today + expiryPb;
196 return expiryDate;
197}
boost::variant< QuantLib::Date, QuantLib::Period > parseDateOrPeriod(const string &s)
Convert text to QuantLib::Period or QuantLib::Date.
Definition: parsers.cpp:493
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buildOptionBasket()

void buildOptionBasket ( ) const
private

Definition at line 221 of file commodityschwartzmodelbuilder.cpp.

221 {
222
223 QL_REQUIRE(data_->optionExpiries().size() == data_->optionStrikes().size(), "Commodity option vector size mismatch");
224
225 optionActive_ = std::vector<bool>(data_->optionExpiries().size(), false);
226
227 DLOG("build reference date grid '" << referenceCalibrationGrid_ << "'");
228 Date lastRefCalDate = Date::minDate();
229 std::vector<Date> referenceCalibrationDates;
230 if (!referenceCalibrationGrid_.empty())
231 referenceCalibrationDates = DateGrid(referenceCalibrationGrid_).dates();
232
233 std::vector<Time> expiryTimes;
234 optionBasket_.clear();
235 for (Size j = 0; j < data_->optionExpiries().size(); j++) {
236 // may wish to calibrate against specific futures expiry dates...
237 Date expiryDate = optionExpiry(j);
238
239 // check if we want to keep the helper when a reference calibration grid is given
240 auto refCalDate =
241 std::lower_bound(referenceCalibrationDates.begin(), referenceCalibrationDates.end(), expiryDate);
242 if (refCalDate == referenceCalibrationDates.end() || *refCalDate > lastRefCalDate) {
243 optionActive_[j] = true;
244 Real strikeValue = optionStrike(j);
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());
248 optionBasket_.push_back(helper);
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;
255 } else {
256 optionActive_[j] = false;
257 }
258 }
259
260 std::sort(expiryTimes.begin(), expiryTimes.end());
261 auto itExpiryTime = unique(expiryTimes.begin(), expiryTimes.end());
262 expiryTimes.resize(distance(expiryTimes.begin(), itExpiryTime));
263
264 optionExpiries_ = Array(expiryTimes.size());
265 for (Size j = 0; j < expiryTimes.size(); j++)
266 optionExpiries_[j] = expiryTimes[j];
267}
Size size(const ValueType &v)
Definition: value.cpp:145
QuantLib::BootstrapHelper< QuantLib::OptionletVolatilityStructure > helper
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ volSurfaceChanged()

bool volSurfaceChanged ( const bool  updateCache) const
private

Definition at line 199 of file commodityschwartzmodelbuilder.cpp.

199 {
200 bool hasUpdated = false;
201
202 // if cache doesn't exist resize vector
203 if (volCache_.size() != optionBasket_.size())
204 volCache_ = vector<Real>(optionBasket_.size(), Null<Real>());
205
206 Size optionCounter = 0;
207 for (Size j = 0; j < data_->optionExpiries().size(); j++) {
208 if (!optionActive_[j])
209 continue;
210 Real vol = vol_->blackVol(optionExpiry(j), optionStrike(j));
211 if (!close_enough(volCache_[optionCounter], vol)) {
212 if (updateCache)
213 volCache_[optionCounter] = vol;
214 hasUpdated = true;
215 }
216 optionCounter++;
217 }
218 return hasUpdated;
219}
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ market_

const QuantLib::ext::shared_ptr<ore::data::Market> market_
private

Definition at line 89 of file commodityschwartzmodelbuilder.hpp.

◆ configuration_

const std::string configuration_
private

Definition at line 90 of file commodityschwartzmodelbuilder.hpp.

◆ data_

const QuantLib::ext::shared_ptr<CommoditySchwartzData> data_
private

Definition at line 91 of file commodityschwartzmodelbuilder.hpp.

◆ referenceCalibrationGrid_

const std::string referenceCalibrationGrid_
private

Definition at line 92 of file commodityschwartzmodelbuilder.hpp.

◆ baseCcy_

const QuantLib::Currency baseCcy_
private

Definition at line 93 of file commodityschwartzmodelbuilder.hpp.

◆ error_

Real error_
mutableprivate

Definition at line 96 of file commodityschwartzmodelbuilder.hpp.

◆ parametrization_

QuantLib::ext::shared_ptr<QuantExt::CommoditySchwartzParametrization> parametrization_
mutableprivate

Definition at line 97 of file commodityschwartzmodelbuilder.hpp.

◆ model_

QuantLib::ext::shared_ptr<QuantExt::CommoditySchwartzModel> model_
mutableprivate

Definition at line 98 of file commodityschwartzmodelbuilder.hpp.

◆ optionActive_

std::vector<bool> optionActive_
mutableprivate

Definition at line 101 of file commodityschwartzmodelbuilder.hpp.

◆ optionBasket_

std::vector<QuantLib::ext::shared_ptr<BlackCalibrationHelper> > optionBasket_
mutableprivate

Definition at line 102 of file commodityschwartzmodelbuilder.hpp.

◆ optionExpiries_

Array optionExpiries_
mutableprivate

Definition at line 103 of file commodityschwartzmodelbuilder.hpp.

◆ fxSpot_

Handle<Quote> fxSpot_
private

Definition at line 106 of file commodityschwartzmodelbuilder.hpp.

◆ curve_

Handle<QuantExt::PriceTermStructure> curve_
private

Definition at line 107 of file commodityschwartzmodelbuilder.hpp.

◆ vol_

Handle<BlackVolTermStructure> vol_
private

Definition at line 108 of file commodityschwartzmodelbuilder.hpp.

◆ volCache_

std::vector<QuantLib::Real> volCache_
mutableprivate

Definition at line 111 of file commodityschwartzmodelbuilder.hpp.

◆ forceCalibration_

bool forceCalibration_ = false
private

Definition at line 114 of file commodityschwartzmodelbuilder.hpp.

◆ marketObserver_

QuantLib::ext::shared_ptr<QuantExt::MarketObserver> marketObserver_
private

Definition at line 117 of file commodityschwartzmodelbuilder.hpp.

◆ calibrationErrors_

std::vector<Real> calibrationErrors_
mutableprivate

Definition at line 119 of file commodityschwartzmodelbuilder.hpp.

◆ params_

Array params_
mutableprivate

Definition at line 121 of file commodityschwartzmodelbuilder.hpp.