Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
dependencygraph.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 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 marketdata/dependencygraph.hpp
20 \brief DependencyGraph class to establish build order of marketObjects and its dependency
21 \ingroup marketdata
22*/
23
24#pragma once
25
30
31#include <boost/graph/adjacency_list.hpp>
32#include <boost/graph/directed_graph.hpp>
33#include <boost/graph/graph_traits.hpp>
34#include <ql/shared_ptr.hpp>
35
36#include <boost/graph/depth_first_search.hpp>
37#include <boost/graph/tiernan_all_cycles.hpp>
38
39#include <map>
40
41namespace ore {
42namespace data {
43
44namespace {
45
46//! Helper class to output cycles in the dependency graph
47template <class OutputStream> struct CyclePrinter {
48 CyclePrinter(OutputStream& os) : os_(os) {}
49 template <typename Path, typename Graph> void cycle(const Path& p, const Graph& g) {
50 typename Path::const_iterator i, end = p.end();
51 for (i = p.begin(); i != end; ++i) {
52 os_ << g[*i] << " ";
53 }
54 os_ << "*** ";
55 }
56 OutputStream& os_;
57};
58
59//! Helper functions returning a string describing all circles in a graph
60template <typename Graph> string getCycles(const Graph& g) {
61 std::ostringstream cycles;
62 CyclePrinter<std::ostringstream> cyclePrinter(cycles);
63 boost::tiernan_all_cycles(g, cyclePrinter);
64 return cycles.str();
65}
66
67//! Helper class to find the dependent nodes from a given start node and a topological order for them
68template <typename Vertex> struct DfsVisitor : public boost::default_dfs_visitor {
69 DfsVisitor(std::vector<Vertex>& order, bool& foundCycle) : order_(order), foundCycle_(foundCycle) {}
70 template <typename Graph> void finish_vertex(Vertex u, const Graph& g) { order_.push_back(u); }
71 template <typename Edge, typename Graph> void back_edge(Edge e, const Graph& g) { foundCycle_ = true; }
72 std::vector<Vertex>& order_;
74};
75
76} // namespace
77
79
80public:
82 //! The asof date of the T0 market instance
83 const Date& asof,
84 //! Description of the market composition
85 const QuantLib::ext::shared_ptr<TodaysMarketParameters>& params,
86 //! Description of curve compositions
87 const QuantLib::ext::shared_ptr<const CurveConfigurations>& curveConfigs,
88 //! Ibor fallback config
89 const IborFallbackConfig& iborFallbackConfig = IborFallbackConfig::defaultConfig())
90 : asof_(asof), params_(params), curveConfigs_(curveConfigs),
91 iborFallbackConfig_(iborFallbackConfig){};
92
93 // data structure for a vertex in the graph
94 struct Node {
95 MarketObject obj; // the market object to build
96 std::string name; // the LHS of the todays market mapping
97 std::string mapping; // the RHS of the todays market mapping
98 QuantLib::ext::shared_ptr<CurveSpec> curveSpec; // the parsed curve spec, if applicable, null otherwise
99 bool built; // true if we have built this node
100 };
101
102 // some typedefs for graph related data types
103 using Graph = boost::directed_graph<Node>;
104 using IndexMap = boost::property_map<Graph, boost::vertex_index_t>::type;
105 using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
106 using VertexIterator = boost::graph_traits<Graph>::vertex_iterator;
107
108 // build a graph whose vertices represent the market objects to build (DiscountCurve, IndexCurve, EquityVol, ...)
109 // and an edge from x to y means that x must be built before y, since y depends on it. */
110 void buildDependencyGraph(const std::string& configuration, std::map<std::string, std::string>& buildErrors);
111
112 std::map<std::string, Graph> dependencies() { return dependencies_; }
113
114private:
115 friend std::ostream& operator<<(std::ostream& o, const Node& n);
116
117 // the dependency graphs for each configuration
118 std::map<std::string, Graph> dependencies_;
119
120 const Date asof_;
121 const QuantLib::ext::shared_ptr<TodaysMarketParameters> params_;
122 const QuantLib::ext::shared_ptr<const CurveConfigurations> curveConfigs_;
124};
125
126} // namespace data
127} // namespace ore
std::map< std::string, Graph > dependencies()
void buildDependencyGraph(const std::string &configuration, std::map< std::string, std::string > &buildErrors)
boost::graph_traits< Graph >::vertex_iterator VertexIterator
const QuantLib::ext::shared_ptr< const CurveConfigurations > curveConfigs_
boost::property_map< Graph, boost::vertex_index_t >::type IndexMap
const QuantLib::ext::shared_ptr< TodaysMarketParameters > params_
friend std::ostream & operator<<(std::ostream &o, const Node &n)
boost::directed_graph< Node > Graph
std::map< std::string, Graph > dependencies_
DependencyGraph(const Date &asof, const QuantLib::ext::shared_ptr< TodaysMarketParameters > &params, const QuantLib::ext::shared_ptr< const CurveConfigurations > &curveConfigs, const IborFallbackConfig &iborFallbackConfig=IborFallbackConfig::defaultConfig())
boost::graph_traits< Graph >::vertex_descriptor Vertex
const IborFallbackConfig iborFallbackConfig_
static IborFallbackConfig defaultConfig()
Currency and instrument specific conventions/defaults.
Curve configuration repository.
OutputStream & os_
bool & foundCycle_
ibor fallback configuration
@ data
Definition: log.hpp:77
MarketObject
Definition: market.hpp:65
Serializable Credit Default Swap.
Definition: namespaces.docs:23
const Size order_
QuantLib::ext::shared_ptr< CurveSpec > curveSpec
vector< string > curveConfigs
A class to hold todays market configuration(s)