Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
randomvariable_io.cpp
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
21
22namespace QuantExt {
23
24namespace {
25
26int get_index_randomvariable_output_size() {
27 static int index = std::ios_base::xalloc();
28 return index;
29}
30
31int get_index_randomvariable_output_pattern() {
32 static int index = std::ios_base::xalloc();
33 return index;
34}
35
36} // namespace
37
38std::ostream& operator<<(std::ostream& out, const randomvariable_output_size& r) {
39 out.iword(get_index_randomvariable_output_size()) = static_cast<long>(r.n());
40 return out;
41}
42
43std::ostream& operator<<(std::ostream& out, const randomvariable_output_pattern& p) {
44 out.iword(get_index_randomvariable_output_pattern()) = static_cast<long>(p.getPattern());
45 return out;
46}
47
48namespace {
49
50const RandomVariable& convertToRandomVariable(const RandomVariable& c) { return c; }
51RandomVariable convertToRandomVariable(const Filter& c) { return RandomVariable(c); }
52
53template <typename C> std::ostream& output(std::ostream& out, const C& c) {
54 if (!c.initialised()) {
55 out << "na";
56 } else if (c.deterministic()) {
57 out << std::boolalpha << c.at(0);
58 } else {
59
60 // get format flags
61
62 long os = out.iword(get_index_randomvariable_output_size());
63 long pt = out.iword(get_index_randomvariable_output_pattern());
64
65 // set final format flags
66
67 Size n = std::min<Size>(c.size(), os == 0 ? 10 : static_cast<Size>(os));
70
71 // output random variable or filter
72
74 out << "[";
75 for (Size i = 0; i < n; ++i)
76 out << c.at(i) << (i < n - 1 ? "," : "");
77 if (n < c.size())
78 out << "...";
79 out << "]";
81 out << "[";
82 Size s = std::max<Size>(n / 3, 1);
83 if (c.size() <= 3 * s) {
84 for (Size i = 0; i < c.size(); ++i)
85 out << c.at(i) << (i < c.size() - 1 ? "," : "");
86 } else {
87 for (Size i = 0; i < s; ++i)
88 out << c.at(i) << ",";
89 out << "...,";
90 for (Size i = c.size() / 2 - s / 2; i < c.size() / 2 - s / 2 + s; ++i)
91 out << c.at(i) << ",";
92 out << "...,";
93 for (Size i = c.size() - s; i < c.size(); ++i)
94 out << c.at(i) << (i < c.size() - 1 ? "," : "");
95 out << "]";
96 }
98 out << expectation(convertToRandomVariable(c)) << (!c.deterministic() ? " (avg)" : "");
99 } else {
100 out << "<unknown output pattern>";
101 }
102 }
103
104 return out;
105}
106} // namespace
107
108std::ostream& operator<<(std::ostream& out, const Filter& f) { return output(out, f); }
109
110std::ostream& operator<<(std::ostream& out, const RandomVariable& r) {
111 output(out, r);
112 if (r.time() != Null<Real>())
113 out << " t=" << r.time();
114 return out;
115}
116
117} // namespace QuantExt
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
RandomVariable expectation(const RandomVariable &r)