Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
scenariostatisticsanalytic.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2022 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
27
30
31using namespace ore::data;
32using namespace boost::filesystem;
33
34namespace ore {
35namespace analytics {
36
37/******************************************************************************
38 * ScenarioStatistics Analytic: Scenario_Statistics
39 ******************************************************************************/
40
42 LOG("ScenarioStatisticsAnalytic::setUpConfigurations() called");
43 analytic()->configurations().todaysMarketParams = inputs_->todaysMarketParams();
44 analytic()->configurations().simMarketParams = inputs_->exposureSimMarketParams();
45 analytic()->configurations().scenarioGeneratorData = inputs_->scenarioGeneratorData();
46 analytic()->configurations().crossAssetModelData = inputs_->crossAssetModelData();
47}
48
50
51 std::string configuration = inputs_->marketConfig("simulation");
52 simMarket_ = QuantLib::ext::make_shared<ScenarioSimMarket>(
53 analytic()->market(),
54 analytic()->configurations().simMarketParams,
55 QuantLib::ext::make_shared<FixingManager>(inputs_->asof()),
56 configuration,
57 *inputs_->curveConfigs().get(),
58 *analytic()->configurations().todaysMarketParams,
59 inputs_->continueOnError(),
60 false, true, false,
61 *inputs_->iborFallbackConfig(),
62 false);
63}
64
65void ScenarioStatisticsAnalyticImpl::buildScenarioGenerator(const bool continueOnCalibrationError) {
66 if (!model_)
67 buildCrossAssetModel(continueOnCalibrationError);
68 ScenarioGeneratorBuilder sgb(analytic()->configurations().scenarioGeneratorData);
69 QuantLib::ext::shared_ptr<ScenarioFactory> sf = QuantLib::ext::make_shared<SimpleScenarioFactory>(true);
70 string config = inputs_->marketConfig("simulation");
71 scenarioGenerator_ = sgb.build(model_, sf, analytic()->configurations().simMarketParams, inputs_->asof(), analytic()->market(), config);
72 QL_REQUIRE(scenarioGenerator_, "failed to build the scenario generator");
74 LOG("simulation grid size " << grid_->size());
75 LOG("simulation grid valuation dates " << grid_->valuationDates().size());
76 LOG("simulation grid close-out dates " << grid_->closeOutDates().size());
77 LOG("simulation grid front date " << io::iso_date(grid_->dates().front()));
78 LOG("simulation grid back date " << io::iso_date(grid_->dates().back()));
79
80 if (inputs_->writeScenarios()) {
81 auto report = QuantLib::ext::make_shared<InMemoryReport>();
82 analytic()->reports()["SCENARIO_STATISTICS"]["scenario"] = report;
83 scenarioGenerator_ = QuantLib::ext::make_shared<ScenarioWriter>(scenarioGenerator_, report);
84 }
85}
86
87void ScenarioStatisticsAnalyticImpl::buildCrossAssetModel(const bool continueOnCalibrationError) {
88 LOG("SCENARIO_STATISTICS: Build Simulation Model (continueOnCalibrationError = "
89 << std::boolalpha << continueOnCalibrationError << ")");
90 CrossAssetModelBuilder modelBuilder(
91 analytic()->market(), analytic()->configurations().crossAssetModelData, inputs_->marketConfig("lgmcalibration"),
92 inputs_->marketConfig("fxcalibration"), inputs_->marketConfig("eqcalibration"),
93 inputs_->marketConfig("infcalibration"), inputs_->marketConfig("crcalibration"),
94 inputs_->marketConfig("simulation"), false, continueOnCalibrationError, "",
95 inputs_->salvageCorrelationMatrix() ? SalvagingAlgorithm::Spectral : SalvagingAlgorithm::None,
96 "xva cam building");
97 model_ = *modelBuilder.model();
98}
99
100void ScenarioStatisticsAnalyticImpl::runAnalytic(const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader,
101 const std::set<std::string>& runTypes) {
102
103 LOG("Scenario analytic called with asof " << io::iso_date(inputs_->asof()));
104
105 Settings::instance().evaluationDate() = inputs_->asof();
106 //ObservationMode::instance().setMode(inputs_->exposureObservationModel());
107
108 LOG("SCENARIO_STATISTICS: Build Today's Market");
109 CONSOLEW("SCENARIO_STATISTICS: Build Market");
110 analytic()->buildMarket(loader);
111 CONSOLE("OK");
112
114
115 LOG("SCENARIO_STATISTICS: Build simulation market");
117
118 LOG("SCENARIO_STATISTICS: Build Scenario Generator");
119 auto globalParams = inputs_->simulationPricingEngine()->globalParameters();
120 auto continueOnCalErr = globalParams.find("ContinueOnCalibrationError");
121 bool continueOnErr = (continueOnCalErr != globalParams.end()) && parseBool(continueOnCalErr->second);
122 buildScenarioGenerator(continueOnErr);
123
124 LOG("SCENARIO_STATISTICS: Attach Scenario Generator to ScenarioSimMarket");
125 simMarket_->scenarioGenerator() = scenarioGenerator_;
126
127 MEM_LOG;
128
129 // Output scenario statistics and distribution reports
130 const vector<RiskFactorKey>& keys = simMarket_->baseScenario()->keys();
131 QuantLib::ext::shared_ptr<ScenarioGenerator> scenarioGenerator =
132 inputs_->scenarioOutputZeroRate()
133 ? QuantLib::ext::make_shared<ScenarioGeneratorTransform>(scenarioGenerator_, simMarket_,
134 analytic()->configurations().simMarketParams)
136
137 auto statsReport = QuantLib::ext::make_shared<InMemoryReport>();
138 scenarioGenerator->reset();
140 *statsReport);
141 analytic()->reports()["SCENARIO_STATISTICS"]["scenario_statistics"] = statsReport;
142
143 auto distributionReport = QuantLib::ext::make_shared<InMemoryReport>();
144 scenarioGenerator->reset();
146 inputs_->scenarioDistributionSteps(), *distributionReport);
147 analytic()->reports()["SCENARIO_STATISTICS"]["scenario_distribution"] = distributionReport;
148}
149
150} // namespace analytics
151} // namespace ore
Analytic * analytic() const
Definition: analytic.hpp:193
QuantLib::ext::shared_ptr< InputParameters > inputs_
Definition: analytic.hpp:216
analytic_reports & reports()
Result reports.
Definition: analytic.hpp:131
Configurations & configurations()
Definition: analytic.hpp:128
virtual void buildMarket(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const bool marketRequired=true)
Definition: analytic.cpp:178
Write ORE outputs to reports.
virtual void writeScenarioStatistics(const QuantLib::ext::shared_ptr< ore::analytics::ScenarioGenerator > &generator, const std::vector< ore::analytics::RiskFactorKey > &keys, QuantLib::Size numPaths, const std::vector< QuantLib::Date > &dates, ore::data::Report &report)
virtual void writeScenarioDistributions(const QuantLib::ext::shared_ptr< ore::analytics::ScenarioGenerator > &generator, const std::vector< ore::analytics::RiskFactorKey > &keys, QuantLib::Size numPaths, const std::vector< QuantLib::Date > &dates, QuantLib::Size distSteps, ore::data::Report &report)
QuantLib::ext::shared_ptr< ScenarioGenerator > build(QuantLib::ext::shared_ptr< QuantExt::CrossAssetModel > model, QuantLib::ext::shared_ptr< ScenarioFactory > sf, QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > marketConfig, Date asof, QuantLib::ext::shared_ptr< ore::data::Market > initMarket, const std::string &configuration=ore::data::Market::defaultConfiguration, const QuantLib::ext::shared_ptr< PathGeneratorFactory > &pf=QuantLib::ext::make_shared< MultiPathGeneratorFactory >())
Build function.
QuantLib::ext::shared_ptr< ScenarioGenerator > scenarioGenerator()
virtual void runAnalytic(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const std::set< std::string > &runTypes={}) override
QuantLib::ext::shared_ptr< ScenarioSimMarket > simMarket_
QuantLib::ext::shared_ptr< ScenarioGenerator > scenarioGenerator_
QuantLib::ext::shared_ptr< CrossAssetModel > model_
Handle< QuantExt::CrossAssetModel > model() const
Scenario generation using cross asset model paths.
bool parseBool(const string &s)
#define MEM_LOG
#define LOG(text)
#define CONSOLEW(text)
#define CONSOLE(text)
A Class to write ORE outputs to reports.
Transformer class used for transform discount factors in the scenario into zero rates.
ORE Scenario Statistics Analytics.
ScenarioWriter class.
factory classes for simple scenarios
QuantLib::ext::shared_ptr< ore::analytics::CrossAssetModelData > crossAssetModelData
Definition: analytic.hpp:72
QuantLib::ext::shared_ptr< ore::data::TodaysMarketParameters > todaysMarketParams
Definition: analytic.hpp:68
QuantLib::ext::shared_ptr< ore::analytics::ScenarioSimMarketParameters > simMarketParams
Definition: analytic.hpp:69
QuantLib::ext::shared_ptr< ore::analytics::ScenarioGeneratorData > scenarioGeneratorData
Definition: analytic.hpp:71
Structured analytics error.
Class for structured analytics warnings.