Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
computationgraph.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 qle/ad/computationgraph.hpp
20 \brief computation graph
21*/
22
23#pragma once
24
25#include <boost/integer.hpp>
26
27#include <map>
28#include <set>
29#include <string>
30#include <vector>
31
32namespace QuantExt {
33
34/*! - opId = 0 should refer to "no operation" */
36public:
37 enum class VarDoesntExist { Nan, Create, Throw };
38 static std::size_t nan;
39
40 void clear();
41
42 std::size_t size() const;
43 std::size_t insert(const std::string& label = std::string());
44 std::size_t insert(const std::vector<std::size_t>& predecessors, const std::size_t opId,
45 const std::string& label = std::string());
46 const std::vector<std::size_t>& predecessors(const std::size_t node) const;
47 std::size_t opId(const std::size_t node) const;
48
49 std::size_t maxNodeRequiringArg(const std::size_t node) const;
50
51 std::size_t constant(const double c);
52 const std::map<double, std::size_t>& constants() const;
53 bool isConstant(const std::size_t node) const;
54 double constantValue(const std::size_t node) const;
55
56 std::size_t variable(const std::string& name, const VarDoesntExist v = VarDoesntExist::Throw);
57 const std::map<std::string, std::size_t>& variables() const;
58 void setVariable(const std::string& name, const std::size_t node);
59
60 void enableLabels(const bool b = true);
61 const std::map<std::size_t, std::set<std::string>>& labels() const;
62
63 void startRedBlock();
64 void endRedBlock();
65 std::size_t redBlockId(const std::size_t node) const;
66 const std::vector<std::pair<std::size_t, std::size_t>>& redBlockRanges() const;
67 const std::set<std::size_t>& redBlockDependencies() const;
68
69private:
70 std::vector<std::vector<std::size_t>> predecessors_;
71 std::vector<std::size_t> opId_;
72 std::vector<bool> isConstant_;
73 std::vector<double> constantValue_;
74 std::vector<std::size_t> maxNodeRequiringArg_;
75 std::vector<std::size_t> redBlockId_;
76
77 std::map<double, std::size_t> constants_;
78
79 std::map<std::string, std::size_t> variables_;
80 std::map<std::string, std::size_t> variableVersion_;
81
82 bool enableLabels_ = false;
83 std::map<std::size_t, std::set<std::string>> labels_;
84
85 std::size_t currentRedBlockId_ = 0;
86 std::size_t nextRedBlockId_ = 0;
87 std::vector<std::pair<std::size_t, std::size_t>> redBlockRange_;
88 std::set<std::size_t> redBlockDependencies_;
89};
90
91// methods to construct cg
92
93std::size_t cg_const(ComputationGraph& g, const double value);
94std::size_t cg_insert(ComputationGraph& g, const std::string& label = std::string());
95std::size_t cg_var(ComputationGraph& g, const std::string& name,
97std::size_t cg_add(ComputationGraph& g, const std::size_t a, const std::size_t b,
98 const std::string& label = std::string());
99std::size_t cg_subtract(ComputationGraph& g, const std::size_t a, const std::size_t b, const std::string& label = std::string());
100std::size_t cg_negative(ComputationGraph& g, const std::size_t a, const std::string& label = std::string());
101std::size_t cg_mult(ComputationGraph& g, const std::size_t a, const std::size_t b, const std::string& label = std::string());
102std::size_t cg_div(ComputationGraph& g, const std::size_t a, const std::size_t b, const std::string& label = std::string());
103std::size_t cg_conditionalExpectation(ComputationGraph& g, const std::size_t regressand,
104 const std::vector<std::size_t>& regressor, const std::size_t filter,
105 const std::string& label = std::string());
106std::size_t cg_indicatorEq(ComputationGraph& g, const std::size_t a, const std::size_t b,
107 const std::string& label = std::string());
108std::size_t cg_indicatorGt(ComputationGraph& g, const std::size_t a, const std::size_t b,
109 const std::string& label = std::string());
110std::size_t cg_indicatorGeq(ComputationGraph& g, const std::size_t a, const std::size_t b,
111 const std::string& label = std::string());
112std::size_t cg_min(ComputationGraph& g, const std::size_t a, const std::size_t b, const std::string& label = std::string());
113std::size_t cg_max(ComputationGraph& g, const std::size_t a, const std::size_t b, const std::string& label = std::string());
114std::size_t cg_abs(ComputationGraph& g, const std::size_t a, const std::string& label = std::string());
115std::size_t cg_exp(ComputationGraph& g, const std::size_t a, const std::string& label = std::string());
116std::size_t cg_sqrt(ComputationGraph& g, const std::size_t a, const std::string& label = std::string());
117std::size_t cg_log(ComputationGraph& g, const std::size_t a, const std::string& label = std::string());
118std::size_t cg_pow(ComputationGraph& g, const std::size_t a, const std::size_t b, const std::string& label = std::string());
119std::size_t cg_normalCdf(ComputationGraph& g, const std::size_t a, const std::string& label = std::string());
120std::size_t cg_normalPdf(ComputationGraph& g, const std::size_t a, const std::string& label = std::string());
121
122} // namespace QuantExt
std::set< std::size_t > redBlockDependencies_
const std::set< std::size_t > & redBlockDependencies() const
const std::map< std::size_t, std::set< std::string > > & labels() const
const std::map< double, std::size_t > & constants() const
std::vector< bool > isConstant_
std::size_t constant(const double c)
std::map< std::size_t, std::set< std::string > > labels_
std::size_t variable(const std::string &name, const VarDoesntExist v=VarDoesntExist::Throw)
std::map< std::string, std::size_t > variableVersion_
std::map< double, std::size_t > constants_
std::vector< std::pair< std::size_t, std::size_t > > redBlockRange_
void setVariable(const std::string &name, const std::size_t node)
std::vector< double > constantValue_
const std::vector< std::size_t > & predecessors(const std::size_t node) const
std::vector< std::size_t > redBlockId_
double constantValue(const std::size_t node) const
std::size_t maxNodeRequiringArg(const std::size_t node) const
std::map< std::string, std::size_t > variables_
std::size_t redBlockId(const std::size_t node) const
std::size_t insert(const std::string &label=std::string())
const std::map< std::string, std::size_t > & variables() const
std::vector< std::vector< std::size_t > > predecessors_
bool isConstant(const std::size_t node) const
std::vector< std::size_t > opId_
const std::vector< std::pair< std::size_t, std::size_t > > & redBlockRanges() const
std::vector< std::size_t > maxNodeRequiringArg_
void enableLabels(const bool b=true)
std::size_t opId(const std::size_t node) const
std::size_t cg_min(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_const(ComputationGraph &g, const double value)
std::size_t cg_insert(ComputationGraph &g, const std::string &label)
std::size_t cg_indicatorGeq(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_subtract(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_max(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_pow(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_exp(ComputationGraph &g, const std::size_t a, const std::string &label)
std::size_t cg_negative(ComputationGraph &g, const std::size_t a, const std::string &label)
std::size_t cg_mult(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_add(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_normalCdf(ComputationGraph &g, const std::size_t a, const std::string &label)
std::size_t cg_log(ComputationGraph &g, const std::size_t a, const std::string &label)
std::size_t cg_abs(ComputationGraph &g, const std::size_t a, const std::string &label)
std::size_t cg_conditionalExpectation(ComputationGraph &g, const std::size_t regressand, const std::vector< std::size_t > &regressor, const std::size_t filter, const std::string &label)
std::size_t cg_var(ComputationGraph &g, const std::string &name, const ComputationGraph::VarDoesntExist v)
std::size_t cg_sqrt(ComputationGraph &g, const std::size_t a, const std::string &label)
std::size_t cg_div(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_normalPdf(ComputationGraph &g, const std::size_t a, const std::string &label)
std::size_t cg_indicatorEq(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_indicatorGt(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)