QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
compositeproduct.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2006 StatPro Italia srl
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
22
23namespace QuantLib {
24
26 QL_REQUIRE(finalized_, "composite not finalized");
27 return evolution_;
28 }
29
31 QL_REQUIRE(finalized_, "composite not finalized");
33 }
34
36 QL_REQUIRE(finalized_, "composite not finalized");
37 return cashflowTimes_;
38 }
39
41 for (auto& component : components_) {
42 component.product->reset();
43 component.done = false;
44 }
45 currentIndex_ = 0;
46 }
47
48
49
51 const Clone<MarketModelMultiProduct>& product,
52 Real multiplier) {
53 QL_REQUIRE(!finalized_, "product already finalized");
54 EvolutionDescription d = product->evolution();
55 if (!components_.empty()) {
56 // enforce preconditions
58 components_.front().product->evolution();
59 const std::vector<Time>& rateTimes1 = d1.rateTimes();
60 const std::vector<Time>& rateTimes2 = d.rateTimes();
61 QL_REQUIRE(rateTimes1.size() == rateTimes2.size() &&
62 std::equal(rateTimes1.begin(), rateTimes1.end(),
63 rateTimes2.begin()),
64 "incompatible rate times");
65 }
66 components_.emplace_back();
67 components_.back().product = product;
68 components_.back().multiplier = multiplier;
69 components_.back().done = false;
70 allEvolutionTimes_.push_back(d.evolutionTimes());
71 }
72
74 const Clone<MarketModelMultiProduct>& product,
75 Real multiplier) {
76 add(product, -multiplier);
77 }
78
80 QL_REQUIRE(!finalized_, "product already finalized");
81 QL_REQUIRE(!components_.empty(), "no sub-product provided");
82
83 // fetch the rate times from the first subproduct (we checked
84 // they're all the same)
85 EvolutionDescription description =
86 components_.front().product->evolution();
87 rateTimes_ = description.rateTimes();
88
90
91 std::vector<Time> allCashflowTimes;
92
93 // now, for each subproduct...
94 iterator i;
95 for (i=components_.begin(); i!=components_.end(); ++i) {
96 EvolutionDescription d = i->product->evolution();
97 // ...collect all possible cash-flow times...
98 const std::vector<Time>& cashflowTimes =
99 i->product->possibleCashFlowTimes();
100 allCashflowTimes.insert(allCashflowTimes.end(),
101 cashflowTimes.begin(),
102 cashflowTimes.end());
103 // ...allocate working vectors...
104 i->numberOfCashflows =
105 std::vector<Size>(i->product->numberOfProducts());
106 i->cashflows =
107 std::vector<std::vector<CashFlow> >(
108 i->product->numberOfProducts(),
109 std::vector<CashFlow>(i->product
110 ->maxNumberOfCashFlowsPerProductPerStep()));
111 }
112
113 // all information having been collected, we can sort and
114 // compact the vector of all cash-flow times...
115 std::sort(allCashflowTimes.begin(), allCashflowTimes.end());
116 auto end = std::unique(allCashflowTimes.begin(), allCashflowTimes.end());
117 //std::copy(allCashflowTimes.begin(), end,
118 // std::back_inserter(cashflowTimes_));
119 cashflowTimes_.insert(cashflowTimes_.end(),
120 allCashflowTimes.begin(), end);
121 // ...and map each product's cash-flow time into the total vector.
122 for (i=components_.begin(); i!=components_.end(); ++i) {
123 const std::vector<Time>& productTimes =
124 i->product->possibleCashFlowTimes();
125 i->timeIndices = std::vector<Size>(productTimes.size());
126 for (Size j=0; j<productTimes.size(); ++j) {
127 i->timeIndices[j] =
128 std::find(cashflowTimes_.begin(), cashflowTimes_.end(),
129 productTimes[j]) - cashflowTimes_.begin();
130 }
131 }
132
134
135 // all done.
136 finalized_ = true;
137 }
138
140 return components_.size();
141 }
142
144 return *(components_.at(i).product);
145 }
146
148 return *(components_.at(i).product);
149 }
150
152 return components_.at(i).multiplier;
153 }
154
155}
cloning proxy to an underlying object
Definition: clone.hpp:40
Market-model evolution description.
const std::vector< Time > & rateTimes() const
std::vector< Size > suggestedNumeraires() const override
std::vector< std::valarray< bool > > isInSubset_
std::vector< std::vector< Time > > allEvolutionTimes_
std::vector< Time > cashflowTimes_
std::vector< Time > possibleCashFlowTimes() const override
const EvolutionDescription & evolution() const override
std::vector< SubProduct >::iterator iterator
const MarketModelMultiProduct & item(Size i) const
void add(const Clone< MarketModelMultiProduct > &, Real multiplier=1.0)
void subtract(const Clone< MarketModelMultiProduct > &, Real multiplier=1.0)
std::vector< SubProduct > components_
void reset() override
during simulation put product at start of path
std::vector< Time > evolutionTimes_
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
std::vector< Size > terminalMeasure(const EvolutionDescription &evolution)
Terminal measure: the last bond is used as numeraire.
void mergeTimes(const std::vector< std::vector< Time > > &times, std::vector< Time > &mergedTimes, std::vector< std::valarray< bool > > &isPresent)
Definition: utilities.cpp:29