Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
scriptedinstrumentpricingenginecg.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 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/*! \file ored/scripting/engines/scriptedinstrumentpricingenginecg.hpp
20 \brief scripted instrument pricing engine using a cg model
21*/
22
23#pragma once
24
26
34
36
37namespace ore {
38namespace data {
39
41public:
42 ScriptedInstrumentPricingEngineCG(const std::string& npv,
43 const std::vector<std::pair<std::string, std::string>>& additionalResults,
44 const QuantLib::ext::shared_ptr<ModelCG>& model, const ASTNodePtr ast,
45 const QuantLib::ext::shared_ptr<Context>& context,
46 const Model::McParams& mcParams, const std::string& script = "",
47 const bool interactive = false, const bool generateAdditionalResults = false,
48 const bool includePastCashflows = false, const bool useCachedSensis = false,
49 const bool useExternalComputeFramework = false,
50 const bool useDoublePrecisionForExternalCalculation = false);
52
54 const std::string& npvName() const { return npv_; }
55
56 void buildComputationGraph() const;
57
58private:
59 void calculate() const override;
60
61 // calculation state, true iff calculate() was called at least once and last call went without errors
62
63 mutable bool lastCalculationWasValid_ = false;
64
65 // computation graph version built in model
66
67 mutable std::size_t cgVersion_ = 0;
68
69 // external compute framework calculation id and results
70
71 mutable std::size_t externalCalculationId_ = 0;
72 mutable std::vector<std::vector<double>> externalOutput_;
73 mutable std::vector<double*> externalOutputPtr_;
74
75 // variables populated during cg building
76
77 mutable std::vector<ComputationGraphBuilder::PayLogEntry> payLogEntries_;
78 mutable std::set<std::size_t> keepNodes_;
79 mutable QuantLib::ext::shared_ptr<Context> workingContext_;
80
81 // computation graph associated ops
82
83 mutable std::vector<RandomVariableOpNodeRequirements> opNodeRequirements_;
84
85 // if no external compute framework used
86 mutable std::vector<RandomVariableOp> ops_;
87 mutable std::vector<RandomVariableGrad> grads_;
88
89 // for external compute framework
90 mutable std::vector<ExternalRandomVariableOp> opsExternal_;
91 mutable std::vector<ExternalRandomVariableGrad> gradsExternal_;
92
93 // store base scenario model parameters to compute sensi-based NPVs
94
95 mutable bool haveBaseValues_ = false;
96 mutable double baseNpv_;
97 mutable std::vector<std::pair<std::size_t, double>> baseModelParams_;
98 mutable std::vector<double> sensis_;
99 mutable std::map<std::string, boost::any> instrumentAdditionalResults_;
100
101 // inputs
102
103 const std::string npv_;
104 const std::vector<std::pair<std::string, std::string>> additionalResults_;
105 const QuantLib::ext::shared_ptr<ModelCG> model_;
107 const QuantLib::ext::shared_ptr<Context> context_;
109 const std::string script_;
110 const bool interactive_;
116};
117
118} // namespace data
119} // namespace ore
abstract syntax tree for payoff scripting
std::string script
QuantLib::GenericEngine< arguments, results > engine
std::vector< ExternalRandomVariableGrad > gradsExternal_
std::vector< ComputationGraphBuilder::PayLogEntry > payLogEntries_
std::vector< std::pair< std::size_t, double > > baseModelParams_
std::vector< RandomVariableOpNodeRequirements > opNodeRequirements_
const std::vector< std::pair< std::string, std::string > > additionalResults_
std::map< std::string, boost::any > instrumentAdditionalResults_
computation graph builder
script engine context holding variable names and values
Currency and instrument specific conventions/defaults.
@ data
Definition: log.hpp:77
interface for model against which a script can be run
interface for model against which a script can be run
QuantLib::ext::shared_ptr< ASTNode > ASTNodePtr
Definition: ast.hpp:46
Serializable Credit Default Swap.
Definition: namespaces.docs:23
repository for cashflows generated by the PAYLOG() function
scripted instrument