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

Builder for a Lognormal EQ model component. More...

#include <ored/model/eqbsbuilder.hpp>

+ Inheritance diagram for EqBsBuilder:
+ Collaboration diagram for EqBsBuilder:

Public Member Functions

 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. More...
 
Real error () const
 Return calibration error. More...
 
Inspectors
std::string eqName ()
 
QuantLib::ext::shared_ptr< QuantExt::EqBsParametrizationparametrization () 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< EqBsDatadata_
 
const std::string referenceCalibrationGrid_
 
const QuantLib::Currency baseCcy_
 
Real error_
 
QuantLib::ext::shared_ptr< QuantExt::EqBsParametrizationparametrization_
 
std::vector< booloptionActive_
 
std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > optionBasket_
 
Array optionExpiries_
 
Handle< Quote > eqSpot_
 
Handle< Quote > fxSpot_
 
Handle< YieldTermStructure > ytsRate_
 
Handle< YieldTermStructure > ytsDiv_
 
Handle< BlackVolTermStructureeqVol_
 
std::vector< QuantLib::Real > eqVolCache_
 
bool forceCalibration_ = false
 
QuantLib::ext::shared_ptr< QuantExt::MarketObservermarketObserver_
 
void forceRecalculate () override
 
bool requiresRecalibration () const override
 
void setCalibrationDone () const
 
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 Lognormal EQ model component.

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

Definition at line 48 of file eqbsbuilder.hpp.

Constructor & Destructor Documentation

◆ EqBsBuilder()

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.

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

Definition at line 39 of file eqbsbuilder.cpp.

42 : market_(market), configuration_(configuration), data_(data), referenceCalibrationGrid_(referenceCalibrationGrid),
43 baseCcy_(baseCcy) {
44
45 optionActive_ = std::vector<bool>(data_->optionExpiries().size(), false);
46 marketObserver_ = QuantLib::ext::make_shared<MarketObserver>();
47 QuantLib::Currency ccy = ore::data::parseCurrency(data->currency());
48 string eqName = data->eqName();
49
50 LOG("Start building EqBs model for " << eqName);
51
52 // get market data
53 std::string fxCcyPair = ccy.code() + baseCcy_.code();
54 eqSpot_ = market_->equitySpot(eqName, configuration_);
55 fxSpot_ = market_->fxRate(fxCcyPair, configuration_);
56 // FIXME using the "discount curve" here instead of the equityReferenceRateCurve?
57 ytsRate_ = market_->discountCurve(ccy.code(), configuration_);
58 ytsDiv_ = market_->equityDividendCurve(eqName, configuration_);
59 eqVol_ = market_->equityVol(eqName, configuration_);
60
61 // register with market observables except vols
62 marketObserver_->registerWith(eqSpot_);
63 marketObserver_->registerWith(fxSpot_);
64 marketObserver_->registerWith(ytsRate_);
65 marketObserver_->registerWith(ytsDiv_);
66
67 // register the builder with the vol and the market observer
68 registerWith(eqVol_);
69 registerWith(marketObserver_);
70
71 // notify observers of all market data changes, not only when not calculated
72 alwaysForwardNotifications();
73
74 // build option basket and derive parametrization from it
75 if (data->calibrateSigma())
77
78 Array sigmaTimes, sigma;
79 if (data->sigmaParamType() == ParamType::Constant) {
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());
84 } else {
85 if (data->calibrateSigma()) { // override
86 QL_REQUIRE(optionExpiries_.size() > 0, "optionExpiries is empty");
87 sigmaTimes = Array(optionExpiries_.begin(), optionExpiries_.end() - 1);
88 sigma = Array(sigmaTimes.size() + 1, data->sigmaValues()[0]);
89 } else {
90 // use input time grid and input alpha array otherwise
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");
94 }
95 }
96
97 // Quotation needs to be consistent with FX spot quotation in the FX calibration basket
98 if (data->sigmaParamType() == ParamType::Piecewise)
99 parametrization_ = QuantLib::ext::make_shared<QuantExt::EqBsPiecewiseConstantParametrization>(
100 ccy, eqName, eqSpot_, fxSpot_, sigmaTimes, sigma, ytsRate_, ytsDiv_);
101 else if (data->sigmaParamType() == ParamType::Constant)
102 parametrization_ = QuantLib::ext::make_shared<QuantExt::EqBsConstantParametrization>(ccy, eqName, eqSpot_, fxSpot_,
103 sigma[0], ytsRate_, ytsDiv_);
104 else
105 QL_FAIL("interpolation type not supported for Equity");
106}
const std::string configuration_
Definition: eqbsbuilder.hpp:90
QuantLib::ext::shared_ptr< QuantExt::MarketObserver > marketObserver_
Handle< BlackVolTermStructure > eqVol_
Handle< Quote > eqSpot_
const QuantLib::Currency baseCcy_
Definition: eqbsbuilder.hpp:93
QuantLib::ext::shared_ptr< QuantExt::EqBsParametrization > parametrization_
Definition: eqbsbuilder.hpp:97
const QuantLib::ext::shared_ptr< EqBsData > data_
Definition: eqbsbuilder.hpp:91
Handle< YieldTermStructure > ytsDiv_
std::string eqName()
Definition: eqbsbuilder.hpp:67
const QuantLib::ext::shared_ptr< ore::data::Market > market_
Definition: eqbsbuilder.hpp:89
Handle< Quote > fxSpot_
const std::string referenceCalibrationGrid_
Definition: eqbsbuilder.hpp:92
Handle< YieldTermStructure > ytsRate_
void buildOptionBasket() const
std::vector< bool > optionActive_
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 108 of file eqbsbuilder.cpp.

108 {
109 calculate();
110 return error_;
111}

◆ eqName()

std::string eqName ( )

Definition at line 67 of file eqbsbuilder.hpp.

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

◆ parametrization()

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

Definition at line 113 of file eqbsbuilder.cpp.

113 {
114 calculate();
115 return parametrization_;
116}

◆ optionBasket()

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

Definition at line 117 of file eqbsbuilder.cpp.

117 {
118 calculate();
119 return optionBasket_;
120}
std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > optionBasket_

◆ forceRecalculate()

void forceRecalculate ( )
overridevirtual

Reimplemented from ModelBuilder.

Definition at line 234 of file eqbsbuilder.cpp.

234 {
235 forceCalibration_ = true;
237 forceCalibration_ = false;
238}
virtual void forceRecalculate()
+ Here is the call graph for this function:

◆ requiresRecalibration()

bool requiresRecalibration ( ) const
overridevirtual

Implements ModelBuilder.

Definition at line 122 of file eqbsbuilder.cpp.

122 {
123 return data_->calibrateSigma() &&
124 (volSurfaceChanged(false) || marketObserver_->hasUpdated(false) || forceCalibration_);
125}
bool volSurfaceChanged(const bool updateCache) const
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setCalibrationDone()

void setCalibrationDone ( ) const

Definition at line 134 of file eqbsbuilder.cpp.

134 {
135 // reset market observer updated flag
136 marketObserver_->hasUpdated(true);
137 // update vol cache
138 volSurfaceChanged(true);
139}
+ Here is the call graph for this function:

◆ performCalculations()

void performCalculations ( ) const
overrideprivate

Definition at line 127 of file eqbsbuilder.cpp.

127 {
128 if (requiresRecalibration()) {
129 // build option basket
131 }
132}
bool requiresRecalibration() const override
+ Here is the call graph for this function:

◆ optionStrike()

Real optionStrike ( const Size  j) const
private

Definition at line 141 of file eqbsbuilder.cpp.

141 {
142 ore::data::Strike strike = ore::data::parseStrike(data_->optionStrikes()[j]);
143 Real strikeValue;
144 // TODO: Extend strike type coverage
146 strikeValue = Null<Real>();
147 else if (strike.type == ore::data::Strike::Type::Absolute)
148 strikeValue = strike.value;
149 else
150 QL_FAIL("strike type ATMF or Absolute expected");
151 return strikeValue;
152}
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 154 of file eqbsbuilder.cpp.

154 {
155 Date today = Settings::instance().evaluationDate();
156 std::string expiryString = data_->optionExpiries()[j];
157 bool expiryDateBased;
158 Period expiryPb;
159 Date expiryDb;
160 parseDateOrPeriod(expiryString, expiryDb, expiryPb, expiryDateBased);
161 Date expiryDate = expiryDateBased ? expiryDb : today + expiryPb;
162 return expiryDate;
163}
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 187 of file eqbsbuilder.cpp.

187 {
188 QL_REQUIRE(data_->optionExpiries().size() == data_->optionStrikes().size(), "Eq option vector size mismatch");
189
190 optionActive_ = std::vector<bool>(data_->optionExpiries().size(), false);
191
192 DLOG("build reference date grid '" << referenceCalibrationGrid_ << "'");
193 Date lastRefCalDate = Date::minDate();
194 std::vector<Date> referenceCalibrationDates;
195 if (!referenceCalibrationGrid_.empty())
196 referenceCalibrationDates = DateGrid(referenceCalibrationGrid_).dates();
197
198 std::vector<Time> expiryTimes;
199 optionBasket_.clear();
200 for (Size j = 0; j < data_->optionExpiries().size(); j++) {
201 // may wish to calibrate against specific futures expiry dates...
202 Date expiryDate = optionExpiry(j);
203
204 // check if we want to keep the helper when a reference calibration grid is given
205 auto refCalDate =
206 std::lower_bound(referenceCalibrationDates.begin(), referenceCalibrationDates.end(), expiryDate);
207 if (refCalDate == referenceCalibrationDates.end() || *refCalDate > lastRefCalDate) {
208 optionActive_[j] = true;
209 Real strikeValue = optionStrike(j);
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>(
212 expiryDate, strikeValue, eqSpot_, volQuote, ytsRate_, ytsDiv_);
213 optionBasket_.push_back(helper);
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;
220 } else {
221 optionActive_[j] = false;
222 }
223 }
224
225 std::sort(expiryTimes.begin(), expiryTimes.end());
226 auto itExpiryTime = unique(expiryTimes.begin(), expiryTimes.end());
227 expiryTimes.resize(distance(expiryTimes.begin(), itExpiryTime));
228
229 optionExpiries_ = Array(expiryTimes.size());
230 for (Size j = 0; j < expiryTimes.size(); j++)
231 optionExpiries_[j] = expiryTimes[j];
232}
Real optionStrike(const Size j) const
Date optionExpiry(const Size j) const
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
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 165 of file eqbsbuilder.cpp.

165 {
166 bool hasUpdated = false;
167
168 // if cache doesn't exist resize vector
169 if (eqVolCache_.size() != optionBasket_.size())
170 eqVolCache_ = vector<Real>(optionBasket_.size(), Null<Real>());
171
172 Size optionCounter = 0;
173 for (Size j = 0; j < data_->optionExpiries().size(); j++) {
174 if (!optionActive_[j])
175 continue;
176 Real vol = eqVol_->blackVol(optionExpiry(j), optionStrike(j));
177 if (!close_enough(eqVolCache_[optionCounter], vol)) {
178 if (updateCache)
179 eqVolCache_[optionCounter] = vol;
180 hasUpdated = true;
181 }
182 optionCounter++;
183 }
184 return hasUpdated;
185}
std::vector< QuantLib::Real > eqVolCache_
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 eqbsbuilder.hpp.

◆ configuration_

const std::string configuration_
private

Definition at line 90 of file eqbsbuilder.hpp.

◆ data_

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

Definition at line 91 of file eqbsbuilder.hpp.

◆ referenceCalibrationGrid_

const std::string referenceCalibrationGrid_
private

Definition at line 92 of file eqbsbuilder.hpp.

◆ baseCcy_

const QuantLib::Currency baseCcy_
private

Definition at line 93 of file eqbsbuilder.hpp.

◆ error_

Real error_
private

Definition at line 96 of file eqbsbuilder.hpp.

◆ parametrization_

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

Definition at line 97 of file eqbsbuilder.hpp.

◆ optionActive_

std::vector<bool> optionActive_
mutableprivate

Definition at line 100 of file eqbsbuilder.hpp.

◆ optionBasket_

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

Definition at line 101 of file eqbsbuilder.hpp.

◆ optionExpiries_

Array optionExpiries_
mutableprivate

Definition at line 102 of file eqbsbuilder.hpp.

◆ eqSpot_

Handle<Quote> eqSpot_
private

Definition at line 105 of file eqbsbuilder.hpp.

◆ fxSpot_

Handle<Quote> fxSpot_
private

Definition at line 105 of file eqbsbuilder.hpp.

◆ ytsRate_

Handle<YieldTermStructure> ytsRate_
private

Definition at line 106 of file eqbsbuilder.hpp.

◆ ytsDiv_

Handle<YieldTermStructure> ytsDiv_
private

Definition at line 106 of file eqbsbuilder.hpp.

◆ eqVol_

Handle<BlackVolTermStructure> eqVol_
private

Definition at line 107 of file eqbsbuilder.hpp.

◆ eqVolCache_

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

Definition at line 110 of file eqbsbuilder.hpp.

◆ forceCalibration_

bool forceCalibration_ = false
private

Definition at line 113 of file eqbsbuilder.hpp.

◆ marketObserver_

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

Definition at line 116 of file eqbsbuilder.hpp.