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

Builder for a Hull White model or a HW component for the CAM. More...

#include <ored/model/hwbuilder.hpp>

+ Inheritance diagram for HwBuilder:
+ Collaboration diagram for HwBuilder:

Public Member Functions

 HwBuilder (const QuantLib::ext::shared_ptr< ore::data::Market > &market, const QuantLib::ext::shared_ptr< HwModelData > &data, const IrModel::Measure measure=IrModel::Measure::BA, const HwModel::Discretization discretization=HwModel::Discretization::Euler, const bool evaluateBankAccount=true, const std::string &configuration=Market::defaultConfiguration, Real bootstrapTolerance=0.001, const bool continueOnError=false, const std::string &referenceCalibrationGrid="", const bool setCalibrationInfo=false)
 
Real error () const
 Return calibration error. More...
 
Inspectors
std::string qualifier ()
 
std::string ccy ()
 
QuantLib::ext::shared_ptr< QuantExt::HwModelmodel () const
 
QuantLib::ext::shared_ptr< QuantExt::IrHwParametrizationparametrization () const
 
RelinkableHandle< YieldTermStructure > discountCurve ()
 
std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > swaptionBasket () const
 
- Public Member Functions inherited from ModelBuilder
void recalibrate () const
 
virtual void forceRecalculate ()
 
virtual bool requiresRecalibration () const=0
 

ModelBuilder interface

QuantLib::ext::shared_ptr< ore::data::Marketmarket_
 
const std::string configuration_
 
QuantLib::ext::shared_ptr< HwModelDatadata_
 
IrModel::Measure measure_
 
HwModel::Discretization discretization_
 
bool evaluateBankAccount_
 
const std::string referenceCalibrationGrid_
 
bool requiresCalibration_ = false
 
std::string currency_
 
Real error_
 
QuantLib::ext::shared_ptr< QuantExt::HwModelmodel_
 
Array params_
 
QuantLib::ext::shared_ptr< QuantExt::IrHwParametrizationparametrization_
 
std::vector< boolswaptionActive_
 
std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > swaptionBasket_
 
std::vector< Real > swaptionStrike_
 
std::vector< QuantLib::ext::shared_ptr< SimpleQuote > > swaptionBasketVols_
 
Array swaptionExpiries_
 
Array swaptionMaturities_
 
Date swaptionBasketRefDate_
 
RelinkableHandle< YieldTermStructure > modelDiscountCurve_
 
Handle< YieldTermStructure > calibrationDiscountCurve_
 
Handle< QuantLib::SwaptionVolatilityStructure > svts_
 
Handle< SwapIndex > swapIndex_
 
Handle< SwapIndex > shortSwapIndex_
 
QuantLib::ext::shared_ptr< OptimizationMethodoptimizationMethod_
 
EndCriteria endCriteria_
 
std::vector< QuantLib::Real > swaptionVolCache_
 
bool forceCalibration_ = false
 
QuantLib::ext::shared_ptr< QuantExt::MarketObservermarketObserver_
 
void forceRecalculate () override
 
bool requiresRecalibration () const override
 
void performCalculations () const override
 

Detailed Description

Builder for a Hull White model or a HW component for the CAM.

Definition at line 44 of file hwbuilder.hpp.

Constructor & Destructor Documentation

◆ HwBuilder()

HwBuilder ( const QuantLib::ext::shared_ptr< ore::data::Market > &  market,
const QuantLib::ext::shared_ptr< HwModelData > &  data,
const IrModel::Measure  measure = IrModel::Measure::BA,
const HwModel::Discretization  discretization = HwModel::Discretization::Euler,
const bool  evaluateBankAccount = true,
const std::string &  configuration = Market::defaultConfiguration,
Real  bootstrapTolerance = 0.001,
const bool  continueOnError = false,
const std::string &  referenceCalibrationGrid = "",
const bool  setCalibrationInfo = false 
)

The configuration should refer to the calibration configuration here, alternative discounting curves are then usually set in the pricing engines for swaptions etc.

Definition at line 43 of file hwbuilder.cpp.

48 : market_(market), configuration_(configuration), data_(data), measure_(measure), discretization_(discretization),
49 /*bootstrapTolerance_(bootstrapTolerance), continueOnError_(continueOnError),*/
50 referenceCalibrationGrid_(referenceCalibrationGrid), /*setCalibrationInfo_(setCalibrationInfo),*/
51 optimizationMethod_(QuantLib::ext::shared_ptr<OptimizationMethod>(new LevenbergMarquardt(1E-8, 1E-8, 1E-8))),
52 endCriteria_(EndCriteria(1000, 500, 1E-8, 1E-8, 1E-8))
53 /*,calibrationErrorType_(BlackCalibrationHelper::RelativePriceError)*/ {
54
55 marketObserver_ = QuantLib::ext::make_shared<MarketObserver>();
56 string qualifier = data_->qualifier();
58 QuantLib::ext::shared_ptr<IborIndex> index;
59 if (tryParseIborIndex(qualifier, index)) {
60 currency_ = index->currency().code();
61 }
62 LOG("HwCalibration for qualifier " << qualifier << " (ccy=" << currency_ << "), configuration is "
64 Currency ccy = parseCurrency(currency_);
65
67 (data_->calibrateSigma() || data_->calibrateKappa()) && data_->calibrationType() != CalibrationType::None;
68
69 QL_REQUIRE(!requiresCalibration_, "HwBuilder: HwModel does not support calibration at the moment.");
70
71 // the discount curve underlying the model might be relinked to a different curve outside this builder
72 // the calibration curve should always stay the same though, therefore we create a different handle for this
73 modelDiscountCurve_ = RelinkableHandle<YieldTermStructure>(*market_->discountCurve(currency_, configuration_));
74 calibrationDiscountCurve_ = Handle<YieldTermStructure>(*modelDiscountCurve_);
75
77 svts_ = market_->swaptionVol(data_->qualifier(), configuration_);
78 swapIndex_ = market_->swapIndex(market_->swapIndexBase(data_->qualifier(), configuration_), configuration_);
80 market_->swapIndex(market_->shortSwapIndexBase(data_->qualifier(), configuration_), configuration_);
81 registerWith(svts_);
82 marketObserver_->addObservable(swapIndex_->forwardingTermStructure());
83 marketObserver_->addObservable(swapIndex_->discountingTermStructure());
84 marketObserver_->addObservable(shortSwapIndex_->forwardingTermStructure());
85 marketObserver_->addObservable(shortSwapIndex_->discountingTermStructure());
86 }
88 registerWith(marketObserver_);
89 // notify observers of all market data changes, not only when not calculated
90 alwaysForwardNotifications();
91
92 swaptionActive_ = std::vector<bool>(data_->optionExpiries().size(), false);
93
95 // buildSwaptionBasket();
96 }
97
98 Array sigmaTimes(data_->sigmaTimes().begin(), data_->sigmaTimes().end());
99 Array kappaTimes(data_->kappaTimes().begin(), data_->kappaTimes().end());
100 std::vector<Matrix> sigma(data_->sigmaValues().begin(), data_->sigmaValues().end());
101 std::vector<Array> kappa(data_->kappaValues().begin(), data_->kappaValues().end());
102
103 QL_REQUIRE(data_->sigmaType() == ParamType::Constant,
104 "HwBuilder: only constant sigma is supported at the moment");
105 QL_REQUIRE(data_->kappaType() == ParamType::Constant,
106 "HwBuilder: only constant sigma is supported at the moment");
107
108 if (data_->sigmaType() == ParamType::Constant) {
109 QL_REQUIRE(data_->sigmaTimes().size() == 0,
110 "HwBuilder: empty volatility time grid expected for constant parameter type");
111 QL_REQUIRE(data_->sigmaValues().size() == 1,
112 "HwBuilder: initial volatility values should have size 1 for constant parameter type");
113 } else if (data_->sigmaType() == ParamType::Piecewise) {
114 if (data_->calibrateSigma() && data_->calibrationType() == CalibrationType::Bootstrap) {
115 if (data_->sigmaTimes().size() > 0) {
116 DLOG("overriding alpha time grid with swaption expiries, set all initial values to first given value");
117 }
118 QL_REQUIRE(swaptionExpiries_.size() > 0, "empty swaptionExpiries");
119 sigmaTimes = Array(swaptionExpiries_.begin(), swaptionExpiries_.end() - 1);
120 sigma = std::vector<Matrix>(sigmaTimes.size() + 1, data_->sigmaValues()[0]);
121 } else {
122 QL_REQUIRE(sigma.size() == sigmaTimes.size() + 1,
123 "HwBBuilder: hw volatility time and initial value array sizes do not match");
124 }
125 } else {
126 QL_FAIL("HwBuilder: volatility parameter type not covered");
127 }
128
129 if (data_->kappaType() == ParamType::Constant) {
130 QL_REQUIRE(data_->kappaTimes().size() == 0,
131 "HwBuilder: empty reversion time grid expected for constant parameter type");
132 QL_REQUIRE(data_->kappaValues().size() == 1,
133 "HwBuidler: initial reversion values should have size 1 for constant parameter type");
134 } else if (data_->kappaType() == ParamType::Piecewise) {
135 if (data_->calibrateKappa() && data_->calibrationType() == CalibrationType::Bootstrap) {
136 if (data_->kappaTimes().size() > 0) {
137 DLOG("overriding h time grid with swaption underlying maturities, set all initial values to first "
138 "given value");
139 }
140 kappaTimes = swaptionMaturities_;
141 kappa = std::vector<Array>(kappaTimes.size() + 1, data_->kappaValues()[0]);
142 } else { // use input time grid and input h array otherwise
143 QL_REQUIRE(kappa.size() == kappaTimes.size() + 1, "HwBuilder:: hw kappa grids do not match");
144 }
145 } else
146 QL_FAIL("HwBuilder: reversion parameter type case not covered");
147
148 DLOGGERSTREAM("before calibration: sigma times = " << sigmaTimes);
149 DLOGGERSTREAM("before calibration: kappa times = " << kappaTimes);
150
152 QuantLib::ext::make_shared<QuantExt::IrHwConstantParametrization>(ccy, modelDiscountCurve_, sigma[0], kappa[0]);
153
154 DLOG("alpha times size: " << sigmaTimes.size());
155 DLOG("lambda times size: " << kappaTimes.size());
156
157 model_ = QuantLib::ext::make_shared<QuantExt::HwModel>(parametrization_, measure_, discretization_, evaluateBankAccount_);
158 params_ = model_->params();
159}
std::vector< bool > swaptionActive_
Definition: hwbuilder.hpp:96
Handle< SwapIndex > swapIndex_
Definition: hwbuilder.hpp:107
IrModel::Measure measure_
Definition: hwbuilder.hpp:80
const std::string configuration_
Definition: hwbuilder.hpp:78
QuantLib::ext::shared_ptr< QuantExt::MarketObserver > marketObserver_
Definition: hwbuilder.hpp:120
QuantLib::ext::shared_ptr< QuantExt::HwModel > model_
Definition: hwbuilder.hpp:91
std::string currency_
Definition: hwbuilder.hpp:88
HwModel::Discretization discretization_
Definition: hwbuilder.hpp:81
QuantLib::ext::shared_ptr< HwModelData > data_
Definition: hwbuilder.hpp:79
Handle< YieldTermStructure > calibrationDiscountCurve_
Definition: hwbuilder.hpp:105
EndCriteria endCriteria_
Definition: hwbuilder.hpp:111
QuantLib::ext::shared_ptr< OptimizationMethod > optimizationMethod_
Definition: hwbuilder.hpp:110
QuantLib::ext::shared_ptr< ore::data::Market > market_
Definition: hwbuilder.hpp:77
Handle< SwapIndex > shortSwapIndex_
Definition: hwbuilder.hpp:107
const std::string referenceCalibrationGrid_
Definition: hwbuilder.hpp:85
QuantLib::ext::shared_ptr< QuantExt::IrHwParametrization > parametrization_
Definition: hwbuilder.hpp:93
std::string ccy()
Definition: hwbuilder.hpp:61
std::string qualifier()
Definition: hwbuilder.hpp:60
Handle< QuantLib::SwaptionVolatilityStructure > svts_
Definition: hwbuilder.hpp:106
RelinkableHandle< YieldTermStructure > modelDiscountCurve_
Definition: hwbuilder.hpp:104
bool tryParseIborIndex(const string &s, QuantLib::ext::shared_ptr< IborIndex > &index)
Try to convert std::string to QuantLib::IborIndex.
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
Definition: parsers.cpp:290
#define LOG(text)
Logging Macro (Level = Notice)
Definition: log.hpp:552
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
#define DLOGGERSTREAM(text)
Definition: log.hpp:632
+ Here is the call graph for this function:

Member Function Documentation

◆ error()

Real error ( ) const

Return calibration error.

Definition at line 161 of file hwbuilder.cpp.

161 {
162 calculate();
163 return error_;
164}

◆ qualifier()

std::string qualifier ( )

Definition at line 60 of file hwbuilder.hpp.

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

◆ ccy()

std::string ccy ( )

Definition at line 61 of file hwbuilder.hpp.

61{ return currency_; }
+ Here is the caller graph for this function:

◆ model()

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

Definition at line 166 of file hwbuilder.cpp.

166 {
167 calculate();
168 return model_;
169}

◆ parametrization()

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

Definition at line 171 of file hwbuilder.cpp.

171 {
172 calculate();
173 return parametrization_;
174}

◆ discountCurve()

RelinkableHandle< YieldTermStructure > discountCurve ( )

Definition at line 64 of file hwbuilder.hpp.

64{ return modelDiscountCurve_; }

◆ swaptionBasket()

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

Definition at line 176 of file hwbuilder.cpp.

176 {
177 calculate();
178 return swaptionBasket_;
179}
std::vector< QuantLib::ext::shared_ptr< BlackCalibrationHelper > > swaptionBasket_
Definition: hwbuilder.hpp:97

◆ forceRecalculate()

void forceRecalculate ( )
overridevirtual

Reimplemented from ModelBuilder.

Definition at line 196 of file hwbuilder.cpp.

196 {
197 forceCalibration_ = true;
199 forceCalibration_ = false;
200}
virtual void forceRecalculate()
+ Here is the call graph for this function:

◆ requiresRecalibration()

bool requiresRecalibration ( ) const
overridevirtual

Implements ModelBuilder.

Definition at line 181 of file hwbuilder.cpp.

181 {
182 // TODO
183 // return requiresCalibration_ &&
184 // (volSurfaceChanged(false) || marketObserver_->hasUpdated(false) || forceCalibration_);
185 return false;
186}

◆ performCalculations()

void performCalculations ( ) const
overrideprivate

Definition at line 188 of file hwbuilder.cpp.

188 {
189
190 DLOG("Recalibrate HW model for qualifier " << data_->qualifier() << " currency " << currency_);
191
192 // TODO...
193
194} // performCalculations()

Member Data Documentation

◆ market_

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

Definition at line 77 of file hwbuilder.hpp.

◆ configuration_

const std::string configuration_
private

Definition at line 78 of file hwbuilder.hpp.

◆ data_

QuantLib::ext::shared_ptr<HwModelData> data_
private

Definition at line 79 of file hwbuilder.hpp.

◆ measure_

IrModel::Measure measure_
private

Definition at line 80 of file hwbuilder.hpp.

◆ discretization_

HwModel::Discretization discretization_
private

Definition at line 81 of file hwbuilder.hpp.

◆ evaluateBankAccount_

bool evaluateBankAccount_
private

Definition at line 82 of file hwbuilder.hpp.

◆ referenceCalibrationGrid_

const std::string referenceCalibrationGrid_
private

Definition at line 85 of file hwbuilder.hpp.

◆ requiresCalibration_

bool requiresCalibration_ = false
private

Definition at line 87 of file hwbuilder.hpp.

◆ currency_

std::string currency_
private

Definition at line 88 of file hwbuilder.hpp.

◆ error_

Real error_
mutableprivate

Definition at line 90 of file hwbuilder.hpp.

◆ model_

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

Definition at line 91 of file hwbuilder.hpp.

◆ params_

Array params_
mutableprivate

Definition at line 92 of file hwbuilder.hpp.

◆ parametrization_

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

Definition at line 93 of file hwbuilder.hpp.

◆ swaptionActive_

std::vector<bool> swaptionActive_
mutableprivate

Definition at line 96 of file hwbuilder.hpp.

◆ swaptionBasket_

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

Definition at line 97 of file hwbuilder.hpp.

◆ swaptionStrike_

std::vector<Real> swaptionStrike_
mutableprivate

Definition at line 98 of file hwbuilder.hpp.

◆ swaptionBasketVols_

std::vector<QuantLib::ext::shared_ptr<SimpleQuote> > swaptionBasketVols_
mutableprivate

Definition at line 99 of file hwbuilder.hpp.

◆ swaptionExpiries_

Array swaptionExpiries_
mutableprivate

Definition at line 100 of file hwbuilder.hpp.

◆ swaptionMaturities_

Array swaptionMaturities_
mutableprivate

Definition at line 101 of file hwbuilder.hpp.

◆ swaptionBasketRefDate_

Date swaptionBasketRefDate_
mutableprivate

Definition at line 102 of file hwbuilder.hpp.

◆ modelDiscountCurve_

RelinkableHandle<YieldTermStructure> modelDiscountCurve_
private

Definition at line 104 of file hwbuilder.hpp.

◆ calibrationDiscountCurve_

Handle<YieldTermStructure> calibrationDiscountCurve_
private

Definition at line 105 of file hwbuilder.hpp.

◆ svts_

Handle<QuantLib::SwaptionVolatilityStructure> svts_
private

Definition at line 106 of file hwbuilder.hpp.

◆ swapIndex_

Handle<SwapIndex> swapIndex_
private

Definition at line 107 of file hwbuilder.hpp.

◆ shortSwapIndex_

Handle<SwapIndex> shortSwapIndex_
private

Definition at line 107 of file hwbuilder.hpp.

◆ optimizationMethod_

QuantLib::ext::shared_ptr<OptimizationMethod> optimizationMethod_
private

Definition at line 110 of file hwbuilder.hpp.

◆ endCriteria_

EndCriteria endCriteria_
private

Definition at line 111 of file hwbuilder.hpp.

◆ swaptionVolCache_

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

Definition at line 115 of file hwbuilder.hpp.

◆ forceCalibration_

bool forceCalibration_ = false
private

Definition at line 117 of file hwbuilder.hpp.

◆ marketObserver_

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

Definition at line 120 of file hwbuilder.hpp.