Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
lgmscenariogenerator.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21using namespace QuantLib;
22
23namespace ore {
24namespace analytics {
25
26LgmScenarioGenerator::LgmScenarioGenerator(QuantLib::ext::shared_ptr<QuantExt::LGM> model,
27 QuantLib::ext::shared_ptr<QuantExt::MultiPathGeneratorBase> pathGenerator,
28 QuantLib::ext::shared_ptr<ScenarioFactory> scenarioFactory,
29 QuantLib::ext::shared_ptr<ScenarioSimMarketParameters> simMarketConfig, Date today,
30 DateGrid grid)
31 : ScenarioPathGenerator(today, grid.dates(), grid.timeGrid()), model_(model), pathGenerator_(pathGenerator),
32 scenarioFactory_(scenarioFactory), simMarketConfig_(simMarketConfig) {
33 QL_REQUIRE(timeGrid_.size() == dates_.size() + 1, "date/time grid size mismatch");
34}
35
36std::vector<QuantLib::ext::shared_ptr<Scenario>> LgmScenarioGenerator::nextPath() {
37 std::vector<QuantLib::ext::shared_ptr<Scenario>> scenarios(dates_.size());
38 Sample<MultiPath> sample = pathGenerator_->next();
39
40 DayCounter dc = model_->parametrization()->termStructure()->dayCounter();
41
42 std::string ccy = model_->parametrization()->currency().code();
43 vector<RiskFactorKey> keys;
44 for (Size k = 0; k < simMarketConfig_->yieldCurveTenors(ccy).size(); k++)
45 keys.emplace_back(RiskFactorKey::KeyType::DiscountCurve, ccy, k);
46
47 for (Size i = 0; i < dates_.size(); i++) {
48 Real t = timeGrid_[i + 1]; // recall: time grid has inserted t=0
49
50 scenarios[i] = scenarioFactory_->buildScenario(dates_[i], true);
51
52 // Set numeraire, numeraire currency and the (deterministic) domestic discount
53 // Asset index 0 in sample.value[0][i+1] refers to the domestic currency process,
54 // the only one here.
55 Real z0 = sample.value[0][i + 1]; // second index = 0 holds initial values
56 scenarios[i]->setNumeraire(model_->numeraire(t, z0));
57
58 Real z = sample.value[0][i + 1]; // LGM factor value, second index = 0 holds initial values
59 for (Size k = 0; k < simMarketConfig_->yieldCurveTenors(ccy).size(); k++) {
60 Date d = dates_[i] + simMarketConfig_->yieldCurveTenors(ccy)[k];
61 Real T = dc.yearFraction(dates_[i], d);
62 Real discount = model_->discountBond(t, t + T, z);
63 scenarios[i]->add(keys[k], discount);
64 }
65 }
66 return scenarios;
67}
68} // namespace analytics
69} // namespace ore
std::vector< QuantLib::ext::shared_ptr< Scenario > > nextPath() override
QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > simMarketConfig_
QuantLib::ext::shared_ptr< QuantExt::MultiPathGeneratorBase > pathGenerator_
LgmScenarioGenerator(QuantLib::ext::shared_ptr< QuantExt::LGM > model, QuantLib::ext::shared_ptr< QuantExt::MultiPathGeneratorBase > multiPathGenerator, QuantLib::ext::shared_ptr< ScenarioFactory > scenarioFactory, QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > simMarketConfig, Date today, DateGrid grid)
Constructor.
QuantLib::ext::shared_ptr< ScenarioFactory > scenarioFactory_
QuantLib::ext::shared_ptr< QuantExt::LGM > model_
Scenario generator that generates an entire path.
const QuantLib::ext::shared_ptr< ModelCG > model_
Scenario generation using LGM paths.
Size size(const ValueType &v)