|
| 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 | + |
| 19 | +#include <orea/app/analytics/scenariostatisticsanalytic.hpp> |
| 20 | +#include <orea/app/reportwriter.hpp> |
| 21 | +#include <orea/app/structuredanalyticserror.hpp> |
| 22 | +#include <orea/app/structuredanalyticswarning.hpp> |
| 23 | +#include <orea/scenario/scenariowriter.hpp> |
| 24 | +#include <orea/scenario/simplescenariofactory.hpp> |
| 25 | +#include <orea/scenario/crossassetmodelscenariogenerator.hpp> |
| 26 | +#include <orea/scenario/scenariogeneratortransform.hpp> |
| 27 | + |
| 28 | +#include <ored/model/crossassetmodelbuilder.hpp> |
| 29 | +#include <ored/portfolio/structuredtradeerror.hpp> |
| 30 | + |
| 31 | +using namespace ore::data; |
| 32 | +using namespace boost::filesystem; |
| 33 | + |
| 34 | +namespace ore { |
| 35 | +namespace analytics { |
| 36 | + |
| 37 | +/****************************************************************************** |
| 38 | + * ScenarioStatistics Analytic: Scenario_Statistics |
| 39 | + ******************************************************************************/ |
| 40 | + |
| 41 | +void ScenarioStatisticsAnalyticImpl::setUpConfigurations() { |
| 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 | + |
| 49 | +void ScenarioStatisticsAnalyticImpl::buildScenarioSimMarket() { |
| 50 | + |
| 51 | + std::string configuration = inputs_->marketConfig("simulation"); |
| 52 | + simMarket_ = boost::make_shared<ScenarioSimMarket>( |
| 53 | + analytic()->market(), |
| 54 | + analytic()->configurations().simMarketParams, |
| 55 | + boost::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 | + |
| 65 | +void ScenarioStatisticsAnalyticImpl::buildScenarioGenerator(const bool continueOnCalibrationError) { |
| 66 | + if (!model_) |
| 67 | + buildCrossAssetModel(continueOnCalibrationError); |
| 68 | + ScenarioGeneratorBuilder sgb(analytic()->configurations().scenarioGeneratorData); |
| 69 | + boost::shared_ptr<ScenarioFactory> sf = boost::make_shared<SimpleScenarioFactory>(); |
| 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"); |
| 73 | + samples_ = analytic()->configurations().scenarioGeneratorData->samples(); |
| 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 = boost::make_shared<InMemoryReport>(); |
| 82 | + analytic()->reports()["SCENARIO_STATISTICS"]["scenario"] = report; |
| 83 | + scenarioGenerator_ = boost::make_shared<ScenarioWriter>(scenarioGenerator_, report); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void 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 | + |
| 100 | +void ScenarioStatisticsAnalyticImpl::runAnalytic(const boost::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 | + |
| 113 | + grid_ = analytic()->configurations().scenarioGeneratorData->getGrid(); |
| 114 | + |
| 115 | + LOG("SCENARIO_STATISTICS: Build simulation market"); |
| 116 | + buildScenarioSimMarket(); |
| 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 | + boost::shared_ptr<ScenarioGenerator> scenarioGenerator = |
| 132 | + inputs_->scenarioOutputZeroRate() |
| 133 | + ? boost::make_shared<ScenarioGeneratorTransform>(scenarioGenerator_, simMarket_, |
| 134 | + analytic()->configurations().simMarketParams) |
| 135 | + : scenarioGenerator_; |
| 136 | + |
| 137 | + auto statsReport = boost::make_shared<InMemoryReport>(); |
| 138 | + scenarioGenerator->reset(); |
| 139 | + ReportWriter().writeScenarioStatistics(scenarioGenerator, keys, samples_, grid_->valuationDates(), |
| 140 | + *statsReport); |
| 141 | + analytic()->reports()["SCENARIO_STATISTICS"]["scenario_statistics"] = statsReport; |
| 142 | + |
| 143 | + auto distributionReport = boost::make_shared<InMemoryReport>(); |
| 144 | + scenarioGenerator->reset(); |
| 145 | + ReportWriter().writeScenarioDistributions(scenarioGenerator, keys, samples_, grid_->valuationDates(), |
| 146 | + inputs_->scenarioDistributionSteps(), *distributionReport); |
| 147 | + analytic()->reports()["SCENARIO_STATISTICS"]["scenario_distribution"] = distributionReport; |
| 148 | +} |
| 149 | + |
| 150 | +} // namespace analytics |
| 151 | +} // namespace ore |
0 commit comments