Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes | List of all members
HistoricalScenarioGenerator Class Reference

Historical Scenario Generator. More...

#include <orea/scenario/historicalscenariogenerator.hpp>

+ Inheritance diagram for HistoricalScenarioGenerator:
+ Collaboration diagram for HistoricalScenarioGenerator:

Classes

struct  HistoricalScenarioCalculationDetails
 

Public Member Functions

 HistoricalScenarioGenerator (const QuantLib::ext::shared_ptr< HistoricalScenarioLoader > &historicalScenarioLoader, const QuantLib::ext::shared_ptr< ScenarioFactory > &scenarioFactory, const QuantLib::Calendar &cal, const QuantLib::ext::shared_ptr< ore::data::AdjustmentFactors > &adjFactors=nullptr, const Size mporDays=10, const bool overlapping=true, const ReturnConfiguration &returnConfiguration=ReturnConfiguration(), const std::string &labelPrefix="")
 Default constructor. More...
 
 HistoricalScenarioGenerator (const boost::shared_ptr< HistoricalScenarioLoader > &historicalScenarioLoader, const boost::shared_ptr< ScenarioFactory > &scenarioFactory, const boost::shared_ptr< ore::data::AdjustmentFactors > &adjFactors=nullptr, const ReturnConfiguration &returnConfiguration=ReturnConfiguration(), const std::string &labelPrefix="")
 Constructor with no mporDays/Calendar, construct historical shift scenario between each scneario. More...
 
QuantLib::ext::shared_ptr< Scenario > & baseScenario ()
 Set base scenario, this also defines the asof date. More...
 
const QuantLib::ext::shared_ptr< Scenario > & baseScenario () const
 Get base scenario. More...
 
const QuantLib::Calendar & cal () const
 Get calendar. More...
 
QuantLib::Size mporDays () const
 Get mpor days. More...
 
bool overlapping () const
 Are scenarios overlapping. More...
 
const ReturnConfigurationreturnConfiguration () const
 Return configuration. More...
 
virtual QuantLib::Real scaling (const RiskFactorKey &key, const QuantLib::Real &keyReturn)
 Scaling. More...
 
QuantLib::ext::shared_ptr< Scenarionext (const QuantLib::Date &d) override
 Return the next scenario for the given date. More...
 
const std::vector< HistoricalScenarioCalculationDetails > & lastHistoricalScenarioCalculationDetails () const
 Return the calculation details of the last generated scenario *‍/. More...
 
void reset () override
 Reset the generator so calls to next() return the first scenario. More...
 
virtual QuantLib::Size numScenarios () const
 Number of scenarios this generator can generate. More...
 
virtual void setDates ()
 set the start and end dates, can be overwritten in derived class More...
 
const std::vector< QuantLib::Date > & startDates () const
 start dates from which the scenarios were generated from More...
 
const std::vector< QuantLib::Date > & endDates () const
 end dates from which the scenarios were generated from More...
 
const QuantLib::ext::shared_ptr< HistoricalScenarioLoader > & scenarioLoader () const
 Get the HistoricalScenarioLoader. More...
 
const QuantLib::ext::shared_ptr< ScenarioFactory > & scenarioFactory () const
 Get the ScenarioFactory. More...
 
const QuantLib::ext::shared_ptr< ore::data::AdjustmentFactors > & adjFactors () const
 Get the adj factors. More...
 
std::vector< std::pair< QuantLib::Date, QuantLib::Date > > filteredScenarioDates (const ore::data::TimePeriod &period) const
 Get (start, end) scenario date pairs filtered on the given period. More...
 
const std::string & labelPrefix () const
 Get the scenario label prefix. More...
 
- Public Member Functions inherited from ScenarioGenerator
virtual ~ScenarioGenerator ()
 Default destructor. More...
 
virtual QuantLib::ext::shared_ptr< Scenarionext (const Date &d)=0
 Return the next scenario for the given date. More...
 
virtual void reset ()=0
 Reset the generator so calls to next() return the first scenario. More...
 

Protected Member Functions

std::pair< QuantLib::ext::shared_ptr< Scenario >, QuantLib::ext::shared_ptr< Scenario > > scenarioPair ()
 The Scenario Pairs for a given index. More...
 
QuantLib::Real adjustedPrice (RiskFactorKey key, QuantLib::Date d, QuantLib::Real price)
 Returns the adjusted price. More...
 

Protected Attributes

Size i_
 
QuantLib::ext::shared_ptr< HistoricalScenarioLoaderhistoricalScenarioLoader_
 
std::vector< QuantLib::Date > startDates_
 
std::vector< QuantLib::Date > endDates_
 
QuantLib::ext::shared_ptr< ScenarioFactoryscenarioFactory_
 
QuantLib::ext::shared_ptr< ScenariobaseScenario_
 
std::vector< HistoricalScenarioCalculationDetailscalculationDetails_
 
QuantLib::Calendar cal_
 
QuantLib::Size mporDays_ = 10
 

Private Attributes

QuantLib::ext::shared_ptr< ore::data::AdjustmentFactorsadjFactors_
 
bool overlapping_ = true
 
ReturnConfiguration returnConfiguration_
 
std::string labelPrefix_
 

Detailed Description

Historical Scenario Generator.

A Scenario Generator that takes historical scenarios and builds new scenarios by applying historical shifts to the base scenario (which typically comes from todays market).

This generator can be used for historical VAR and backtesting calculations, unlike a PRNG based generator it is limited in the number of scenarios it can generate.

The scenarios generated are based on the scenario differences between t and t+mpor, these differences are typically a relative change and this change is then applied to the baseScenario to give a new scenario which is asof Today or Today+mpor.

Definition at line 85 of file historicalscenariogenerator.hpp.

Constructor & Destructor Documentation

◆ HistoricalScenarioGenerator() [1/2]

HistoricalScenarioGenerator ( const QuantLib::ext::shared_ptr< HistoricalScenarioLoader > &  historicalScenarioLoader,
const QuantLib::ext::shared_ptr< ScenarioFactory > &  scenarioFactory,
const QuantLib::Calendar &  cal,
const QuantLib::ext::shared_ptr< ore::data::AdjustmentFactors > &  adjFactors = nullptr,
const Size  mporDays = 10,
const bool  overlapping = true,
const ReturnConfiguration returnConfiguration = ReturnConfiguration(),
const std::string &  labelPrefix = "" 
)

Default constructor.

Parameters
historicalScenarioLoaderHistorical Scenario Loader containing all scenarios
scenarioFactoryScenario factory to use
calCalendar to use
adjFactorsoptional adjustment factors for stock splits etc
mporDaysMpor days or step size
overlappingoverlapping scenarios
returnConfigurationreturn configuration
labelPrefixstring prepended to label of all scenarios generated

Definition at line 166 of file historicalscenariogenerator.cpp.

172 : i_(0), historicalScenarioLoader_(historicalScenarioLoader), scenarioFactory_(scenarioFactory),
175
176 QL_REQUIRE(mporDays > 0, "Invalid mpor days of 0");
177 QL_REQUIRE(historicalScenarioLoader_->numScenarios() > 1,
178 "HistoricalScenarioGenerator: require more than 1 scenario from historicalScenarioLoader_");
179
180 // check they are in order and all before the base scenario
181 // we do not make any assumptions here about the length
182 for (Size i = 1; i < historicalScenarioLoader_->numScenarios(); ++i) {
183 QL_REQUIRE(historicalScenarioLoader_->dates()[i] > historicalScenarioLoader_->dates()[i - 1],
184 "historical scenarios are not ordered");
185 }
186 setDates();
187}
const QuantLib::ext::shared_ptr< ore::data::AdjustmentFactors > & adjFactors() const
Get the adj factors.
virtual void setDates()
set the start and end dates, can be overwritten in derived class
bool overlapping() const
Are scenarios overlapping.
const QuantLib::ext::shared_ptr< ScenarioFactory > & scenarioFactory() const
Get the ScenarioFactory.
QuantLib::ext::shared_ptr< HistoricalScenarioLoader > historicalScenarioLoader_
QuantLib::ext::shared_ptr< ore::data::AdjustmentFactors > adjFactors_
const std::string & labelPrefix() const
Get the scenario label prefix.
QuantLib::ext::shared_ptr< ScenarioFactory > scenarioFactory_
const QuantLib::Calendar & cal() const
Get calendar.
const ReturnConfiguration & returnConfiguration() const
Return configuration.
+ Here is the call graph for this function:

◆ HistoricalScenarioGenerator() [2/2]

HistoricalScenarioGenerator ( const boost::shared_ptr< HistoricalScenarioLoader > &  historicalScenarioLoader,
const boost::shared_ptr< ScenarioFactory > &  scenarioFactory,
const boost::shared_ptr< ore::data::AdjustmentFactors > &  adjFactors = nullptr,
const ReturnConfiguration returnConfiguration = ReturnConfiguration(),
const std::string &  labelPrefix = "" 
)

Constructor with no mporDays/Calendar, construct historical shift scenario between each scneario.

Parameters
historicalScenarioLoaderHistorical Scenario Loader containing all scenarios
scenarioFactoryScenario factory to use
adjFactorsoptional adjustment factors for stock splits etc
returnConfigurationreturn configuration
labelPrefixstring prepended to label of all scenarios generated

Member Function Documentation

◆ baseScenario() [1/2]

QuantLib::ext::shared_ptr< Scenario > & baseScenario ( )

Set base scenario, this also defines the asof date.

Definition at line 136 of file historicalscenariogenerator.hpp.

136{ return baseScenario_; }
QuantLib::ext::shared_ptr< Scenario > baseScenario_
+ Here is the caller graph for this function:

◆ baseScenario() [2/2]

const QuantLib::ext::shared_ptr< Scenario > & baseScenario ( ) const

Get base scenario.

Definition at line 138 of file historicalscenariogenerator.hpp.

138{ return baseScenario_; }

◆ cal()

const QuantLib::Calendar & cal ( ) const

Get calendar.

Definition at line 141 of file historicalscenariogenerator.hpp.

141{ return cal_; }

◆ mporDays()

QuantLib::Size mporDays ( ) const

Get mpor days.

Definition at line 143 of file historicalscenariogenerator.hpp.

143{ return mporDays_; }
+ Here is the caller graph for this function:

◆ overlapping()

bool overlapping ( ) const

Are scenarios overlapping.

Definition at line 145 of file historicalscenariogenerator.hpp.

145{ return overlapping_; }

◆ returnConfiguration()

const ReturnConfiguration & returnConfiguration ( ) const

Return configuration.

Definition at line 148 of file historicalscenariogenerator.hpp.

148{ return returnConfiguration_; }

◆ scaling()

virtual QuantLib::Real scaling ( const RiskFactorKey key,
const QuantLib::Real &  keyReturn 
)
virtual

Scaling.

Definition at line 151 of file historicalscenariogenerator.hpp.

151{ return 1; };
+ Here is the caller graph for this function:

◆ next()

QuantLib::ext::shared_ptr< Scenario > next ( const QuantLib::Date &  d)
override

Return the next scenario for the given date.

Date should be asof or asof+mporDays, this class only checks that date is >= the asof date. Whatever you put in here will be in the returned scenario.

Generator returns scenarios in order of shifts and throws if we have run out of historicals

If Mpor > 1 than the scenarios will overlap.

Definition at line 253 of file historicalscenariogenerator.cpp.

253 {
254
255 QL_REQUIRE(baseScenario_ != nullptr, "HistoricalScenarioGenerator: base scenario not set");
256
257 std::pair<QuantLib::ext::shared_ptr<Scenario>, QuantLib::ext::shared_ptr<Scenario>> scens = scenarioPair();
258 QuantLib::ext::shared_ptr<Scenario> s1 = scens.first;
259 QuantLib::ext::shared_ptr<Scenario> s2 = scens.second;
260
261 // build the scenarios
262 QL_REQUIRE(d >= baseScenario_->asof(), "Cannot generate a scenario in the past");
263 QuantLib::ext::shared_ptr<Scenario> scen = scenarioFactory_->buildScenario(d, true, std::string(), 1.0);
264
265 // loop over all keys
266 calculationDetails_.resize(baseScenario_->keys().size());
267 Size calcDetailsCounter = 0;
268 for (auto const& key : baseScenario_->keys()) {
269 Real base = baseScenario_->get(key);
270 Real v1 = 1.0, v2 = 1.0;
271 if (!s1->has(key) || !s2->has(key)) {
272 DLOG("Missing key in historical scenario (" << io::iso_date(s1->asof()) << "," << io::iso_date(s2->asof())
273 << "): " << key << " => no move in this factor");
274 } else {
275 v1 = adjustedPrice(key, s1->asof(), s1->get(key));
276 v2 = adjustedPrice(key, s2->asof(), s2->get(key));
277 }
278 Real value = 0.0;
279
280 // Calculate the returned value
281 Real returnVal = returnConfiguration_.returnValue(key, v1, v2, s1->asof(), s2->asof());
282 // Adjust return for any scaling
283 Real scaling = this->scaling(key, returnVal);
284 returnVal = returnVal * scaling;
285 // Calculate the shifted value
286 value = returnConfiguration_.applyReturn(key, base, returnVal);
287 if (std::isinf(value)) {
288 ALOG("Value is inf for " << key << " from date " << s1->asof() << " to " << s2->asof());
289 }
290 // Add it
291 scen->add(key, value);
292 // Populate calculation details
293 calculationDetails_[calcDetailsCounter].scenarioDate1 = s1->asof();
294 calculationDetails_[calcDetailsCounter].scenarioDate2 = s2->asof();
295 calculationDetails_[calcDetailsCounter].key = key;
296 calculationDetails_[calcDetailsCounter].baseValue = base;
297 calculationDetails_[calcDetailsCounter].adjustmentFactor1 =
298 adjFactors_ ? adjFactors_->getFactor(key.name, s1->asof()) : 1.0;
299 calculationDetails_[calcDetailsCounter].adjustmentFactor2 =
300 adjFactors_ ? adjFactors_->getFactor(key.name, s2->asof()) : 1.0;
301 calculationDetails_[calcDetailsCounter].scenarioValue1 = v1;
302 calculationDetails_[calcDetailsCounter].scenarioValue2 = v2;
303 calculationDetails_[calcDetailsCounter].returnType = returnConfiguration_.returnTypes().at(key.keytype);
304 calculationDetails_[calcDetailsCounter].scaling = scaling;
305 calculationDetails_[calcDetailsCounter].returnValue = returnVal;
306 calculationDetails_[calcDetailsCounter].scenarioValue = value;
307 ++calcDetailsCounter;
308 }
309
310 // Label the scenario
311 string label = labelPrefix_ + ore::data::to_string(io::iso_date(s1->asof())) + "_" +
312 ore::data::to_string(io::iso_date(s2->asof()));
313 scen->label(label);
314
315 // return it.
316 ++i_;
317 return scen;
318}
std::vector< HistoricalScenarioCalculationDetails > calculationDetails_
virtual QuantLib::Real scaling(const RiskFactorKey &key, const QuantLib::Real &keyReturn)
Scaling.
QuantLib::Real adjustedPrice(RiskFactorKey key, QuantLib::Date d, QuantLib::Real price)
Returns the adjusted price.
std::pair< QuantLib::ext::shared_ptr< Scenario >, QuantLib::ext::shared_ptr< Scenario > > scenarioPair()
The Scenario Pairs for a given index.
QuantLib::Real applyReturn(const RiskFactorKey &key, const QuantLib::Real baseValue, const QuantLib::Real returnValue) const
apply return from v1, v2 to base value
const std::map< RiskFactorKey::KeyType, ReturnType > returnTypes() const
get return types
QuantLib::Real returnValue(const RiskFactorKey &key, const QuantLib::Real v1, const QuantLib::Real v2, const QuantLib::Date &d1, const QuantLib::Date &d2) const
SafeStack< ValueType > value
#define DLOG(text)
#define ALOG(text)
std::string to_string(const LocationInfo &l)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lastHistoricalScenarioCalculationDetails()

const std::vector< HistoricalScenarioGenerator::HistoricalScenarioCalculationDetails > & lastHistoricalScenarioCalculationDetails ( ) const

Return the calculation details of the last generated scenario *‍/.

Definition at line 321 of file historicalscenariogenerator.cpp.

321 {
322 return calculationDetails_;
323}

◆ reset()

void reset ( )
overridevirtual

Reset the generator so calls to next() return the first scenario.

This allows re-generation of scenarios if required.

Implements ScenarioGenerator.

Reimplemented in HistoricalScenarioGeneratorRandom, and HistoricalScenarioGeneratorWithFilteredDates.

Definition at line 168 of file historicalscenariogenerator.hpp.

168{ i_ = 0; }
+ Here is the caller graph for this function:

◆ numScenarios()

Size numScenarios ( ) const
virtual

Number of scenarios this generator can generate.

Definition at line 325 of file historicalscenariogenerator.cpp.

325 {
326 // We have a start date for each valid scenario
327 return startDates_.size();
328}
+ Here is the caller graph for this function:

◆ setDates()

void setDates ( )
virtual

set the start and end dates, can be overwritten in derived class

Definition at line 189 of file historicalscenariogenerator.cpp.

189 {
190 // construct the vectors of start and end dates
191 for (Size i = 0; i < historicalScenarioLoader_->numScenarios();) {
192 Date sDate = historicalScenarioLoader_->dates()[i];
193 Date eDate = cal_.advance(sDate, mporDays_ * Days);
194 auto it =
195 std::find(historicalScenarioLoader_->dates().begin(), historicalScenarioLoader_->dates().end(), eDate);
196 if (it != historicalScenarioLoader_->dates().end()) {
197 startDates_.push_back(sDate);
198 endDates_.push_back(eDate);
199 }
200 if (overlapping_) {
201 ++i;
202 } else {
203 if (it != historicalScenarioLoader_->dates().end()) {
204 i = std::distance(historicalScenarioLoader_->dates().begin(), it);
205 } else {
206 i = std::distance(historicalScenarioLoader_->dates().begin(),
207 std::upper_bound(historicalScenarioLoader_->dates().begin(),
208 historicalScenarioLoader_->dates().end(), eDate));
209 }
210 }
211 }
212}
+ Here is the caller graph for this function:

◆ startDates()

const std::vector< QuantLib::Date > & startDates ( ) const

start dates from which the scenarios were generated from

Definition at line 177 of file historicalscenariogenerator.hpp.

177{ return startDates_; }

◆ endDates()

const std::vector< QuantLib::Date > & endDates ( ) const

end dates from which the scenarios were generated from

Definition at line 179 of file historicalscenariogenerator.hpp.

179{ return endDates_; }

◆ scenarioLoader()

const QuantLib::ext::shared_ptr< HistoricalScenarioLoader > & scenarioLoader ( ) const

Get the HistoricalScenarioLoader.

Definition at line 182 of file historicalscenariogenerator.hpp.

◆ scenarioFactory()

const QuantLib::ext::shared_ptr< ScenarioFactory > & scenarioFactory ( ) const

Get the ScenarioFactory.

Definition at line 185 of file historicalscenariogenerator.hpp.

185{ return scenarioFactory_; }

◆ adjFactors()

const QuantLib::ext::shared_ptr< ore::data::AdjustmentFactors > & adjFactors ( ) const

Get the adj factors.

Definition at line 188 of file historicalscenariogenerator.hpp.

188{ return adjFactors_; }

◆ filteredScenarioDates()

std::vector< std::pair< Date, Date > > filteredScenarioDates ( const ore::data::TimePeriod period) const

Get (start, end) scenario date pairs filtered on the given period.

Definition at line 330 of file historicalscenariogenerator.cpp.

330 {
331 std::vector<std::pair<Date, Date>> result;
332 for (Size s = 0; s < startDates_.size(); ++s) {
333 if (period.contains(startDates_[s]) && period.contains(endDates_[s])) {
334 result.push_back(std::make_pair(startDates_[s], endDates_[s]));
335 }
336 }
337 return result;
338}
bool contains(const Date &d) const
+ Here is the call graph for this function:

◆ labelPrefix()

const std::string & labelPrefix ( ) const

Get the scenario label prefix.

Definition at line 194 of file historicalscenariogenerator.hpp.

194{ return labelPrefix_; }

◆ scenarioPair()

std::pair< QuantLib::ext::shared_ptr< Scenario >, QuantLib::ext::shared_ptr< Scenario > > scenarioPair ( )
protected

The Scenario Pairs for a given index.

Definition at line 234 of file historicalscenariogenerator.cpp.

234 {
235 // Get the two historicals we are using
236 QL_REQUIRE(i_ < numScenarios(),
237 "Cannot generate any more scenarios (i=" << i_ << " numScenarios=" << numScenarios() << ")");
238 QuantLib::ext::shared_ptr<Scenario> s1 = historicalScenarioLoader_->getHistoricalScenario(startDates_[i_]);
239 QuantLib::ext::shared_ptr<Scenario> s2 = historicalScenarioLoader_->getHistoricalScenario(endDates_[i_]);
240 return std::pair<QuantLib::ext::shared_ptr<Scenario>, QuantLib::ext::shared_ptr<Scenario>>(s1, s2);
241}
virtual QuantLib::Size numScenarios() const
Number of scenarios this generator can generate.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ adjustedPrice()

Real adjustedPrice ( RiskFactorKey  key,
QuantLib::Date  d,
QuantLib::Real  price 
)
protected

Returns the adjusted price.

Scenarios may contian unadjusted market prices e.g equity spot prices, apply adjustment factors to ensure no jumps between 2 scenarios Only handles equity spot adjustments at the moment

Definition at line 243 of file historicalscenariogenerator.cpp.

243 {
244 if (adjFactors_) {
245 if (key.keytype == RiskFactorKey::KeyType::EquitySpot) {
246 // uses the ORE Fixing name convention
247 return price * adjFactors_->getFactor(key.name, d);
248 }
249 }
250 return price;
251}
+ Here is the caller graph for this function:

Member Data Documentation

◆ i_

Size i_
protected

Definition at line 198 of file historicalscenariogenerator.hpp.

◆ historicalScenarioLoader_

QuantLib::ext::shared_ptr<HistoricalScenarioLoader> historicalScenarioLoader_
protected

Definition at line 200 of file historicalscenariogenerator.hpp.

◆ startDates_

std::vector<QuantLib::Date> startDates_
protected

Definition at line 201 of file historicalscenariogenerator.hpp.

◆ endDates_

std::vector<QuantLib::Date> endDates_
protected

Definition at line 201 of file historicalscenariogenerator.hpp.

◆ scenarioFactory_

QuantLib::ext::shared_ptr<ScenarioFactory> scenarioFactory_
protected

Definition at line 203 of file historicalscenariogenerator.hpp.

◆ baseScenario_

QuantLib::ext::shared_ptr<Scenario> baseScenario_
protected

Definition at line 204 of file historicalscenariogenerator.hpp.

◆ calculationDetails_

std::vector<HistoricalScenarioCalculationDetails> calculationDetails_
protected

Definition at line 216 of file historicalscenariogenerator.hpp.

◆ cal_

QuantLib::Calendar cal_
protected

Definition at line 219 of file historicalscenariogenerator.hpp.

◆ mporDays_

QuantLib::Size mporDays_ = 10
protected

Definition at line 220 of file historicalscenariogenerator.hpp.

◆ adjFactors_

QuantLib::ext::shared_ptr<ore::data::AdjustmentFactors> adjFactors_
private

Definition at line 223 of file historicalscenariogenerator.hpp.

◆ overlapping_

bool overlapping_ = true
private

Definition at line 224 of file historicalscenariogenerator.hpp.

◆ returnConfiguration_

ReturnConfiguration returnConfiguration_
private

Definition at line 225 of file historicalscenariogenerator.hpp.

◆ labelPrefix_

std::string labelPrefix_
private

Definition at line 226 of file historicalscenariogenerator.hpp.