Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
paylog.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 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
20#include <set>
21
22namespace ore {
23namespace data {
24
25void PayLog::write(RandomVariable value, const Filter& filter, const Date& obs, const Date& pay, const std::string& ccy,
26 const Size legNo, const std::string& cashflowType, const Size slot) {
27
28 // if a slot is given, we erase the results that we already have for this slot
29
30 if (slot != 0) {
31 for (Size i = 0; i < slots_.size(); ++i) {
32 if (slot == slots_[i]) {
34 }
35 }
36 }
37
38 // determine the index where the result belongs, i.e. look for an existing entry for the given
39 // pay date, ccy, legNo, cashflowType and slot
40
41 Size idx = Null<Size>();
42 for (Size i = 0; i < slots_.size(); ++i) {
43 if (dates_[i] == pay && currencies_[i] == ccy && legNos_[i] == legNo && cashflowTypes_[i] == cashflowType &&
44 (slot == slots_[i]))
45 idx = i;
46 }
47
48 // if we did not find a slot, we create one
49
50 if (idx == Null<Size>()) {
51 slots_.push_back(slot);
52 amounts_.push_back(RandomVariable(value.size(), 0.0));
53 dates_.push_back(pay);
54 currencies_.push_back(ccy);
55 legNos_.push_back(legNo);
56 cashflowTypes_.push_back(cashflowType);
57 idx = slots_.size() - 1;
58 }
59
60 // add the value
61
63}
64
66
67 // Create set of (legNo, payDate, payCcy, cfType), index in amounts / dates / ... vectors.
68 // This will also create the sorting we want. Ignore slots from here on.
69
70 std::set<std::tuple<Size, Date, std::string, std::string, Size>> dates;
71 for (Size i = 0; i < slots_.size(); ++i) {
72 dates.insert(std::make_tuple(legNos_[i], dates_[i], currencies_[i], cashflowTypes_[i], i));
73 }
74
75 // build consolidated and sorted vector
76
77 std::vector<Date> resultDates;
78 std::vector<std::string> resultCurrencies;
79 std::vector<Size> resultLegNos;
80 std::vector<std::string> resultCashflowTypes;
81 std::vector<RandomVariable> resultAmounts;
82 Date lastDate = Null<Date>();
83 std::string lastCurrency;
84 Size lastLegNo = Null<Size>();
85 std::string lastCashflowType;
86 for (auto const& d : dates) {
87 if (std::get<1>(d) == lastDate && std::get<2>(d) == lastCurrency && std::get<0>(d) == lastLegNo &&
88 std::get<3>(d) == lastCashflowType)
89 resultAmounts.back() += amounts_[std::get<4>(d)];
90 else {
91 resultAmounts.push_back(amounts_[std::get<4>(d)]);
92 resultDates.push_back(std::get<1>(d));
93 resultCurrencies.push_back(std::get<2>(d));
94 resultLegNos.push_back(std::get<0>(d));
95 resultCashflowTypes.push_back(std::get<3>(d));
96 }
97 lastDate = std::get<1>(d);
98 lastCurrency = std::get<2>(d);
99 lastLegNo = std::get<0>(d);
100 lastCashflowType = std::get<3>(d);
101 }
102
103 // overwrite the existing members
104
105 amounts_ = std::move(resultAmounts);
106 dates_ = std::move(resultDates);
107 currencies_ = std::move(resultCurrencies);
108 legNos_ = std::move(resultLegNos);
109 cashflowTypes_ = std::move(resultCashflowTypes);
110
111 // reset slots
112
113 slots_ = std::vector<Size>(amounts_.size(), 0);
114}
115
116} // namespace data
117} // namespace ore
std::vector< std::string > cashflowTypes_
Definition: paylog.hpp:59
void write(RandomVariable value, const Filter &filter, const Date &obs, const Date &pay, const std::string &ccy, const Size legNo, const std::string &cashflowType, const Size slot=0)
Definition: paylog.cpp:25
std::vector< Size > slots_
Definition: paylog.hpp:54
std::vector< Date > dates_
Definition: paylog.hpp:56
const std::vector< Date > & dates() const
Definition: paylog.hpp:48
void consolidateAndSort()
Definition: paylog.cpp:65
std::vector< RandomVariable > amounts_
Definition: paylog.hpp:55
std::vector< Size > legNos_
Definition: paylog.hpp:58
std::vector< std::string > currencies_
Definition: paylog.hpp:57
SafeStack< ValueType > value
SafeStack< Filter > filter
@ data
Definition: log.hpp:77
RandomVariable applyInverseFilter(RandomVariable x, const Filter &f)
RandomVariable applyFilter(RandomVariable x, const Filter &f)
Serializable Credit Default Swap.
Definition: namespaces.docs:23
repository for cashflows generated by the PAYLOG() function