Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
simmanalytic.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2023 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
23
24using namespace ore::data;
25using namespace boost::filesystem;
26
27namespace ore {
28namespace analytics {
29
31 analytic()->configurations().todaysMarketParams = inputs_->todaysMarketParams();
32}
33
34void SimmAnalyticImpl::runAnalytic(const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader,
35 const std::set<std::string>& runTypes) {
36 if (!analytic()->match(runTypes))
37 return;
38
39 LOG("SimmAnalytic::runAnalytic called");
40
41 analytic()->buildMarket(loader, false);
42
43 auto simmAnalytic = static_cast<SimmAnalytic*>(analytic());
44 QL_REQUIRE(simmAnalytic, "Analytic must be of type SimmAnalytic");
45
46 LOG("Get CRIF records from CRIF loader and fill amountUSD");
47 simmAnalytic->loadCrifRecords(loader);
48
49 if (analytic()->getWriteIntermediateReports()) {
50 QuantLib::ext::shared_ptr<InMemoryReport> crifReport = QuantLib::ext::make_shared<InMemoryReport>();
51 ReportWriter(inputs_->reportNaString()).writeCrifReport(crifReport, simmAnalytic->crif());
52 analytic()->reports()[LABEL]["crif"] = crifReport;
53 LOG("CRIF report generated");
54
55 Crif simmDataCrif = simmAnalytic->crif().aggregate();
56 QuantLib::ext::shared_ptr<InMemoryReport> simmDataReport = QuantLib::ext::make_shared<InMemoryReport>();
57 ReportWriter(inputs_->reportNaString())
58 .writeSIMMData(simmAnalytic->crif(), simmDataReport);
59 analytic()->reports()[LABEL]["simm_data"] = simmDataReport;
60 LOG("SIMM data report generated");
61 }
62 MEM_LOG;
63
64 LOG("Calculating SIMM");
65
66 // Save SIMM calibration data to output
67 if (inputs_->simmCalibrationData())
68 inputs_->simmCalibrationData()->toFile((inputs_->resultsPath() / "simmcalibration.xml").string());
69
70 auto simmConfig = inputs_->getSimmConfiguration();
71 simmConfig->bucketMapper()->updateFromCrif(simmAnalytic->crif());
72
73 // Calculate SIMM
74 auto simm = QuantLib::ext::make_shared<SimmCalculator>(simmAnalytic->crif(),
75 simmConfig,
76 inputs_->simmCalculationCurrencyCall(),
77 inputs_->simmCalculationCurrencyPost(),
78 inputs_->simmResultCurrency(),
79 analytic()->market(),
80 simmAnalytic->determineWinningRegulations(),
81 inputs_->enforceIMRegulations());
82
83 Real fxSpot = 1.0;
84 if (!inputs_->simmReportingCurrency().empty()) {
85 fxSpot = analytic()->market()
86 ->fxRate(inputs_->simmResultCurrency() + inputs_->simmReportingCurrency())
87 ->value();
88 LOG("SIMM reporting currency is " << inputs_->simmReportingCurrency() << " with fxSpot " << fxSpot);
89 }
90
91 QuantLib::ext::shared_ptr<InMemoryReport> simmRegulationBreakdownReport = QuantLib::ext::make_shared<InMemoryReport>();
92 ReportWriter(inputs_->reportNaString())
93 .writeSIMMReport(simm->simmResults(), simmRegulationBreakdownReport, simmAnalytic->hasNettingSetDetails(),
94 inputs_->simmResultCurrency(), inputs_->simmCalculationCurrencyCall(),
95 inputs_->simmCalculationCurrencyPost(), inputs_->simmReportingCurrency(), false, fxSpot);
96 LOG("SIMM regulation breakdown report generated");
97 analytic()->reports()[LABEL]["regulation_breakdown_simm"] = simmRegulationBreakdownReport;
98
99
100 QuantLib::ext::shared_ptr<InMemoryReport> simmReport = QuantLib::ext::make_shared<InMemoryReport>();
101 ReportWriter(inputs_->reportNaString())
102 .writeSIMMReport(simm->finalSimmResults(), simmReport, simmAnalytic->hasNettingSetDetails(),
103 inputs_->simmResultCurrency(), inputs_->simmCalculationCurrencyCall(),
104 inputs_->simmCalculationCurrencyPost(), inputs_->simmReportingCurrency(), fxSpot);
105 analytic()->reports()[LABEL]["simm"] = simmReport;
106 LOG("SIMM report generated");
107 MEM_LOG;
108
109}
110
111void SimmAnalytic::loadCrifRecords(const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader) {
112 QL_REQUIRE(inputs_, "Inputs not set");
113 QL_REQUIRE(!inputs_->crif().empty(), "CRIF loader does not contain any records");
114
115 crif_ = inputs_->crif();
118}
119
120} // namespace analytics
121} // 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
const QuantLib::ext::shared_ptr< ore::data::Market > & market() const
Definition: analytic.hpp:117
QuantLib::ext::shared_ptr< InputParameters > inputs_
contains all the input parameters for the run
Definition: analytic.hpp:156
Crif aggregate() const
Aggregate all existing records.
Definition: crif.cpp:160
bool hasNettingSetDetails() const
Check if netting set details are used anywhere, instead of just the netting set ID.
Definition: crif.cpp:342
void fillAmountUsd(const QuantLib::ext::shared_ptr< ore::data::Market > market)
Definition: crif.cpp:351
Write ORE outputs to reports.
virtual void writeSIMMReport(const std::map< SimmConfiguration::SimmSide, std::map< NettingSetDetails, std::pair< std::string, SimmResults > > > &simmResultsMap, const QuantLib::ext::shared_ptr< ore::data::Report > report, const bool hasNettingSetDetails=false, const std::string &simmResultCcy="", const std::string &simmCalcCcyCall="", const std::string &simmCalcCcyPost="", const std::string &reportCcy="", QuantLib::Real fxSpot=1.0, QuantLib::Real outputThreshold=0.005)
virtual void writeSIMMData(const ore::analytics::Crif &simmData, const QuantLib::ext::shared_ptr< ore::data::Report > &dataReport, const bool hasNettingSetDetails=false)
Write the SIMM data report i.e. the netted CRIF records used in a SIMM calculation.
virtual void writeCrifReport(const QuantLib::ext::shared_ptr< ore::data::Report > &report, const ore::analytics::Crif &crifRecords)
Write out CRIF records to a report.
virtual void loadCrifRecords(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader)
Load CRIF from external source, override to generate CRIF.
void runAnalytic(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const std::set< std::string > &runTypes={}) override
static constexpr const char * LABEL
#define MEM_LOG
#define LOG(text)
A Class to write ORE outputs to reports.
ORE SIMM Analytic.
Class for calculating SIMM.
QuantLib::ext::shared_ptr< ore::data::TodaysMarketParameters > todaysMarketParams
Definition: analytic.hpp:68
supporting utilities