Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
varanalytic.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
28using namespace ore::data;
29using namespace boost::filesystem;
30using namespace QuantLib::ext;
31
32namespace ore {
33namespace analytics {
34
35/***********************************************************************************
36 * VAR Analytic: DELTA-VAR, DELTA-GAMMA-NORMAL-VAR, MONTE-CARLO-VAR
37 ***********************************************************************************/
38
40 analytic()->configurations().todaysMarketParams = inputs_->todaysMarketParams();
41}
42
43void VarAnalyticImpl::runAnalytic(const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader,
44 const std::set<std::string>& runTypes) {
45 MEM_LOG;
46 LOG("Running parametric VaR");
47
48 Settings::instance().evaluationDate() = inputs_->asof();
49 ObservationMode::instance().setMode(inputs_->observationModel());
50
51 LOG("VAR: Build Market");
52 CONSOLEW("Risk: Build Market for VaR");
53 analytic()->buildMarket(loader);
54 CONSOLE("OK");
55
56 CONSOLEW("Risk: Build Portfolio for VaR");
58 CONSOLE("OK");
59
60 setVarReport(loader);
61 QL_REQUIRE(varReport_, "No Var Report created");
62
63 LOG("Call VaR calculation");
64 CONSOLEW("Risk: VaR Calculation");
65 ext::shared_ptr<MarketRiskReport::Reports> reports = ext::make_shared<MarketRiskReport::Reports>();
66 QuantLib::ext::shared_ptr<InMemoryReport> varReport = QuantLib::ext::make_shared<InMemoryReport>();
67 reports->add(varReport);
68
69 varReport_->calculate(reports);
70 CONSOLE("OK");
71
72 analytic()->reports()[label_]["var"] = varReport;
73
74 LOG("VaR completed");
75 MEM_LOG;
76}
77
80 if (inputs_->covarianceData().size() == 0) {
81 analytic()->configurations().sensiScenarioData = inputs_->sensiScenarioData();
82 analytic()->configurations().simMarketParams = inputs_->sensiSimMarketParams();
83 }
84}
85
86void ParametricVarAnalyticImpl::setVarReport(const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader) {
87 LOG("Build trade to portfolio id mapping");
88 ParametricVarCalculator::ParametricVarParams varParams(inputs_->varMethod(), inputs_->mcVarSamples(),
89 inputs_->mcVarSeed());
90
91 QuantLib::ext::shared_ptr<SensitivityStream> ss = sensiStream(loader);
92
93 LOG("Build VaR calculator");
94 if (inputs_->covarianceData().size() > 0) {
95 std::unique_ptr<MarketRiskReport::SensiRunArgs> sensiArgs =
96 std::make_unique<MarketRiskReport::SensiRunArgs>(ss, nullptr, 0.01, inputs_->covarianceData());
97
98 varReport_ = ext::make_shared<ParametricVarReport>(
99 inputs_->baseCurrency(), analytic()->portfolio(), inputs_->portfolioFilter(), inputs_->varQuantiles(),
100 varParams,
101 inputs_->salvageCovariance(), boost::none, std::move(sensiArgs), inputs_->varBreakDown());
102 } else {
103 TimePeriod benchmarkVarPeriod(parseListOfValues<Date>(inputs_->benchmarkVarPeriod(), &parseDate),
104 inputs_->mporDays(), inputs_->mporCalendar());
105
106 QuantLib::ext::shared_ptr<ore::data::AdjustmentFactors> adjFactors;
107 if (auto adjLoader = QuantLib::ext::dynamic_pointer_cast<AdjustedInMemoryLoader>(loader))
108 adjFactors = QuantLib::ext::make_shared<ore::data::AdjustmentFactors>(adjLoader->adjustmentFactors());
109
110 auto scenarios = buildHistoricalScenarioGenerator(inputs_->historicalScenarioReader(), adjFactors,
111 benchmarkVarPeriod, inputs_->mporCalendar(), inputs_->mporDays(), analytic()->configurations().simMarketParams,
112 analytic()->configurations().todaysMarketParams, inputs_->mporOverlappingPeriods());
113
114 if (inputs_->outputHistoricalScenarios())
116 scenarios->scenarioLoader(),
117 QuantLib::ext::make_shared<CSVFileReport>(path(inputs_->resultsPath() / "backtest_histscenrios.csv").string(),
118 ',', false, inputs_->csvQuoteChar(), inputs_->reportNaString()));
119
120 auto simMarket = QuantLib::ext::make_shared<ScenarioSimMarket>(
121 analytic()->market(), analytic()->configurations().simMarketParams, Market::defaultConfiguration,
122 *analytic()->configurations().curveConfig, *analytic()->configurations().todaysMarketParams, true, false,
123 false, false, *inputs_->iborFallbackConfig());
124 simMarket->scenarioGenerator() = scenarios;
125 scenarios->baseScenario() = simMarket->baseScenario();
126
127 QuantLib::ext::shared_ptr<ScenarioShiftCalculator> shiftCalculator = QuantLib::ext::make_shared<ScenarioShiftCalculator>(
128 analytic()->configurations().sensiScenarioData, analytic()->configurations().simMarketParams);
129
130 std::unique_ptr<MarketRiskReport::SensiRunArgs> sensiArgs =
131 std::make_unique<MarketRiskReport::SensiRunArgs>(ss, shiftCalculator, 0.01, inputs_->covarianceData());
132
133 varReport_ = ext::make_shared<ParametricVarReport>(
134 inputs_->baseCurrency(), analytic()->portfolio(), inputs_->portfolioFilter(), scenarios,
135 inputs_->varQuantiles(), varParams,
136 inputs_->salvageCovariance(), benchmarkVarPeriod, std::move(sensiArgs), inputs_->varBreakDown());
137 }
138}
139
142 analytic()->configurations().simMarketParams = inputs_->histVarSimMarketParams();
143}
144
146 const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader) {
147
148 LOG("Build VaR calculator");
149 TimePeriod benchmarkVarPeriod(parseListOfValues<Date>(inputs_->benchmarkVarPeriod(), &parseDate), inputs_->mporDays(), inputs_->mporCalendar());
150
151 QuantLib::ext::shared_ptr<ore::data::AdjustmentFactors> adjFactors;
152 if (auto adjLoader = QuantLib::ext::dynamic_pointer_cast<AdjustedInMemoryLoader>(loader))
153 adjFactors = QuantLib::ext::make_shared<ore::data::AdjustmentFactors>(adjLoader->adjustmentFactors());
154
155 auto scenarios =
156 buildHistoricalScenarioGenerator(inputs_->historicalScenarioReader(), adjFactors, benchmarkVarPeriod, inputs_->mporCalendar(),
157 inputs_->mporDays(), analytic()->configurations().simMarketParams,
158 analytic()->configurations().todaysMarketParams, inputs_->mporOverlappingPeriods());
159
160 if (inputs_->outputHistoricalScenarios())
162 scenarios->scenarioLoader(),
163 QuantLib::ext::make_shared<CSVFileReport>(path(inputs_->resultsPath() / "var_histscenarios.csv").string(), ',',
164 false, inputs_->csvQuoteChar(), inputs_->reportNaString()));
165
166 auto simMarket = QuantLib::ext::make_shared<ScenarioSimMarket>(
167 analytic()->market(), analytic()->configurations().simMarketParams, Market::defaultConfiguration,
168 *analytic()->configurations().curveConfig, *analytic()->configurations().todaysMarketParams, true, false, false,
169 false, *inputs_->iborFallbackConfig());
170 simMarket->scenarioGenerator() = scenarios;
171 scenarios->baseScenario() = simMarket->baseScenario();
172
173 std::unique_ptr<MarketRiskReport::FullRevalArgs> fullRevalArgs = std::make_unique<MarketRiskReport::FullRevalArgs>(
174 simMarket, inputs_->pricingEngine(), inputs_->refDataManager(), *inputs_->iborFallbackConfig());
175
176 varReport_ = ext::make_shared<HistoricalSimulationVarReport>(
177 inputs_->baseCurrency(), analytic()->portfolio(), inputs_->portfolioFilter(),
178 inputs_->varQuantiles(), benchmarkVarPeriod, scenarios, std::move(fullRevalArgs), inputs_->varBreakDown());
179
180}
181
182} // namespace analytics
183} // namespace ore
Analytic * analytic() const
Definition: analytic.hpp:193
std::string label_
label for logging purposes primarily
Definition: analytic.hpp:219
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
virtual void buildPortfolio()
Definition: analytic.cpp:222
void setVarReport(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader) override
virtual void setUpConfigurations() override
Definition: varanalytic.cpp:78
virtual QuantLib::ext::shared_ptr< SensitivityStream > sensiStream(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader)
Definition: varanalytic.hpp:65
void setVarReport(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader) override
Definition: varanalytic.cpp:86
Write ORE outputs to reports.
void writeHistoricalScenarios(const QuantLib::ext::shared_ptr< HistoricalScenarioLoader > &hsloader, const QuantLib::ext::shared_ptr< ore::data::Report > &report)
virtual void setUpConfigurations() override
Definition: varanalytic.cpp:39
virtual void runAnalytic(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const std::set< std::string > &runTypes={}) override
Definition: varanalytic.cpp:43
virtual void setVarReport(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader)
Definition: varanalytic.hpp:45
QuantLib::ext::shared_ptr< VarReport > varReport_
Definition: varanalytic.hpp:43
static const string defaultConfiguration
Date parseDate(const string &s)
Perform historical simulation var calculation for a given portfolio.
#define MEM_LOG
#define LOG(text)
#define CONSOLEW(text)
#define CONSOLE(text)
QuantLib::ext::shared_ptr< HistoricalScenarioGenerator > buildHistoricalScenarioGenerator(const QuantLib::ext::shared_ptr< HistoricalScenarioReader > &hsr, const QuantLib::ext::shared_ptr< ore::data::AdjustmentFactors > &adjFactors, const TimePeriod &period, Calendar calendar, Size mporDays, const QuantLib::ext::shared_ptr< ScenarioSimMarketParameters > &simParams, const QuantLib::ext::shared_ptr< TodaysMarketParameters > &marketParams, const bool overlapping)
Singleton class to hold global Observation Mode.
Perform parametric var calculation for a given portfolio.
A Class to write ORE outputs to reports.
QuantLib::ext::shared_ptr< ore::analytics::SensitivityScenarioData > sensiScenarioData
Definition: analytic.hpp:70
QuantLib::ext::shared_ptr< ore::data::TodaysMarketParameters > todaysMarketParams
Definition: analytic.hpp:68
QuantLib::ext::shared_ptr< ore::analytics::ScenarioSimMarketParameters > simMarketParams
Definition: analytic.hpp:69
A container for holding the parametric VAR parameters.