27using namespace boost::filesystem;
37 if (find(begin(
analytic()->analyticTypes()), end(
analytic()->analyticTypes()),
"SENSITIVITY") !=
51 const QuantLib::ext::shared_ptr<ore::data::InMemoryLoader>& loader,
52 const std::set<std::string>& runTypes) {
54 Settings::instance().evaluationDate() =
inputs_->asof();
55 ObservationMode::instance().setMode(
inputs_->observationModel());
57 QL_REQUIRE(
inputs_->portfolio(),
"PricingAnalytic::run: No portfolio loaded.");
63 CONSOLEW(
"Pricing: Build Portfolio");
68 for (
const auto& rt : runTypes) {
69 if (std::find(
analytic()->analyticTypes().begin(),
analytic()->analyticTypes().end(), rt) ==
71 DLOG(
"requested analytic " << rt <<
" not covered by the PricingAnalytic");
79 for (
const auto& type :
analytic()->analyticTypes()) {
80 QuantLib::ext::shared_ptr<InMemoryReport> report = QuantLib::ext::make_shared<InMemoryReport>();
83 if (runTypes.find(type) == runTypes.end())
86 std::string effectiveResultCurrency =
95 if (
inputs_->outputAdditionalResults()) {
96 CONSOLEW(
"Pricing: Additional Results");
97 QuantLib::ext::shared_ptr<InMemoryReport> addReport = QuantLib::ext::make_shared<InMemoryReport>();;
100 effectiveResultCurrency,
inputs_->additionalResultsReportPrecision());
106 LOG(
"Write curves report");
107 QuantLib::ext::shared_ptr<InMemoryReport> curvesReport = QuantLib::ext::make_shared<InMemoryReport>();
109 std::string config =
inputs_->curvesMarketConfig();
117 else if (type ==
"CASHFLOW") {
118 CONSOLEW(
"Pricing: Cashflow Report");
119 string marketConfig =
inputs_->marketConfig(
"pricing");
123 marketConfig,
inputs_->includePastCashflows());
127 else if (type ==
"CASHFLOWNPV") {
128 CONSOLEW(
"Pricing: Cashflow NPV report");
129 string marketConfig =
inputs_->marketConfig(
"pricing");
133 marketConfig,
inputs_->includePastCashflows());
136 effectiveResultCurrency,
inputs_->cashflowHorizon());
140 else if (type ==
"SENSITIVITY") {
141 CONSOLEW(
"Risk: Sensitivity Report");
142 LOG(
"Sensi Analysis - Initialise");
143 bool recalibrateModels =
true;
144 bool ccyConv =
false;
145 std::string configuration =
inputs_->marketConfig(
"pricing");
146 QuantLib::ext::shared_ptr<SensitivityAnalysis> sensiAnalysis;
147 if (
inputs_->nThreads() == 1) {
148 LOG(
"Single-threaded sensi analysis");
149 sensiAnalysis = QuantLib::ext::make_shared<SensitivityAnalysis>(
151 analytic()->configurations().simMarketParams,
analytic()->configurations().sensiScenarioData,
152 recalibrateModels,
analytic()->configurations().curveConfig,
153 analytic()->configurations().todaysMarketParams, ccyConv,
inputs_->refDataManager(),
155 LOG(
"Single-threaded sensi analysis created");
158 LOG(
"Multi-threaded sensi analysis");
159 sensiAnalysis = QuantLib::ext::make_shared<SensitivityAnalysis>(
161 inputs_->pricingEngine(),
analytic()->configurations().simMarketParams,
162 analytic()->configurations().sensiScenarioData, recalibrateModels,
163 analytic()->configurations().curveConfig,
analytic()->configurations().todaysMarketParams, ccyConv,
165 LOG(
"Multi-threaded sensi analysis created");
169 QuantLib::ext::shared_ptr<ParSensitivityAnalysis> parAnalysis =
nullptr;
171 parAnalysis= QuantLib::ext::make_shared<ParSensitivityAnalysis>(
173 *
analytic()->configurations().sensiScenarioData,
"",
174 true, typesDisabled);
176 LOG(
"Sensi analysis - align pillars (for the par conversion or because alignPillars is enabled)");
177 parAnalysis->alignPillars();
178 sensiAnalysis->overrideTenors(
true);
180 LOG(
"Sensi analysis - skip aligning pillars");
184 LOG(
"Sensi analysis - generate");
185 sensiAnalysis->registerProgressIndicator(QuantLib::ext::make_shared<ProgressLog>(
"sensitivities", 100, oreSeverity::notice));
186 sensiAnalysis->generateSensitivities();
188 LOG(
"Sensi analysis - write sensitivity report in memory");
189 auto baseCurrency = sensiAnalysis->simMarketData()->baseCcy();
190 auto ss = QuantLib::ext::make_shared<SensitivityCubeStream>(sensiAnalysis->sensiCubes(), baseCurrency);
195 LOG(
"Sensi analysis - write sensitivity scenario report in memory");
196 QuantLib::ext::shared_ptr<InMemoryReport> scenarioReport = QuantLib::ext::make_shared<InMemoryReport>();
202 auto simmSensitivityConfigReport = QuantLib::ext::make_shared<InMemoryReport>();
205 sensiAnalysis->scenarioGenerator()->shiftSizes(),
206 sensiAnalysis->scenarioGenerator()->baseValues(),
207 sensiAnalysis->scenarioGenerator()->keyToFactor());
208 analytic()->
reports()[type][
"sensitivity_config"] = simmSensitivityConfigReport;
211 LOG(
"Sensi analysis - par conversion");
213 if (
inputs_->optimiseRiskFactors()){
214 std::set<RiskFactorKey> collectRiskFactors;
216 for(
auto const& c : sensiAnalysis->sensiCubes()){
217 auto currentRF = c->relevantRiskFactors();
219 collectRiskFactors.insert(currentRF.begin(), currentRF.end());
221 parAnalysis->relevantRiskFactors() = collectRiskFactors;
222 LOG(
"optimiseRiskFactors active : parSensi risk factors set to zeroSensi risk factors");
224 parAnalysis->computeParInstrumentSensitivities(sensiAnalysis->simMarket());
225 QuantLib::ext::shared_ptr<ParSensitivityConverter> parConverter =
226 QuantLib::ext::make_shared<ParSensitivityConverter>(parAnalysis->parSensitivities(), parAnalysis->shiftSizes());
227 auto parCube = QuantLib::ext::make_shared<ZeroToParCube>(sensiAnalysis->sensiCubes(), parConverter, typesDisabled,
true);
228 LOG(
"Sensi analysis - write par sensitivity report in memory");
229 QuantLib::ext::shared_ptr<ParSensitivityCubeStream> pss =
230 QuantLib::ext::make_shared<ParSensitivityCubeStream>(parCube, baseCurrency);
233 QuantLib::ext::shared_ptr<InMemoryReport> parSensiReport = QuantLib::ext::make_shared<InMemoryReport>();
239 QuantLib::ext::shared_ptr<InMemoryReport> jacobiReport = QuantLib::ext::make_shared<InMemoryReport>();
243 QuantLib::ext::shared_ptr<InMemoryReport> jacobiInverseReport = QuantLib::ext::make_shared<InMemoryReport>();
244 parConverter->writeConversionMatrix(*jacobiInverseReport);
249 LOG(
"Sensi Analysis - skip par conversion");
252 LOG(
"Sensi Analysis - Completed");
256 QL_FAIL(
"PricingAnalytic type " << type <<
" invalid");
Analytic * analytic() const
void setGenerateAdditionalResults(const bool generateAdditionalResults)
QuantLib::ext::shared_ptr< InputParameters > inputs_
analytic_reports & reports()
Result reports.
Configurations & configurations()
virtual void modifyPortfolio()
virtual void buildMarket(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const bool marketRequired=true)
virtual void buildPortfolio()
void setUpConfigurations() override
void runAnalytic(const QuantLib::ext::shared_ptr< ore::data::InMemoryLoader > &loader, const std::set< std::string > &runTypes={}) override
Write ORE outputs to reports.
virtual void writeAdditionalResultsReport(ore::data::Report &report, QuantLib::ext::shared_ptr< ore::data::Portfolio > portfolio, QuantLib::ext::shared_ptr< Market > market, const std::string &baseCurrency, const std::size_t precision=6)
virtual void writeCashflowNpv(ore::data::Report &report, const ore::data::InMemoryReport &cashflowReport, QuantLib::ext::shared_ptr< ore::data::Market > market, const std::string &configuration, const std::string &baseCcy, const Date &horizon=Date::maxDate())
virtual void writeCurves(ore::data::Report &report, const std::string &configID, const DateGrid &grid, const TodaysMarketParameters &marketConfig, const QuantLib::ext::shared_ptr< Market > &market, const bool continueOnError=false)
virtual void writeSensitivityReport(ore::data::Report &report, const QuantLib::ext::shared_ptr< SensitivityStream > &ss, QuantLib::Real outputThreshold=0.0, QuantLib::Size outputPrecision=2)
virtual void writeScenarioReport(ore::data::Report &report, const std::vector< QuantLib::ext::shared_ptr< SensitivityCube > > &sensitivityCubes, QuantLib::Real outputThreshold=0.0)
virtual void writeCashflow(ore::data::Report &report, const std::string &baseCurrency, QuantLib::ext::shared_ptr< ore::data::Portfolio > portfolio, QuantLib::ext::shared_ptr< ore::data::Market > market=QuantLib::ext::shared_ptr< ore::data::Market >(), const std::string &configuration=ore::data::Market::defaultConfiguration, const bool includePastCashflows=false)
virtual void writeSensitivityConfigReport(ore::data::Report &report, const std::map< RiskFactorKey, QuantLib::Real > &shiftSizes, const std::map< RiskFactorKey, QuantLib::Real > &baseValues, const std::map< RiskFactorKey, std::string > &keyToFactor)
virtual void writeNpv(ore::data::Report &report, const std::string &baseCurrency, QuantLib::ext::shared_ptr< ore::data::Market > market, const std::string &configuration, QuantLib::ext::shared_ptr< Portfolio > portfolio)
void writeParConversionMatrix(const ParSensitivityAnalysis::ParContainer &parSensitivities, Report &report)
Write par instrument sensitivity report.
Singleton class to hold global Observation Mode.
Perfrom sensitivity analysis for a given portfolio.
Class for streaming SensitivityRecords from a par sensitivity cube.
A Class to write ORE outputs to reports.
bool sensitivityConfigRequired
QuantLib::ext::shared_ptr< ore::analytics::SensitivityScenarioData > sensiScenarioData
QuantLib::ext::shared_ptr< ore::data::TodaysMarketParameters > todaysMarketParams
bool simulationConfigRequired
Booleans to determine if these configs are needed.
QuantLib::ext::shared_ptr< ore::analytics::ScenarioSimMarketParameters > simMarketParams