QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
multiproductcomposite.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4Copyright (C) 2006 StatPro Italia srl
5
6This file is part of QuantLib, a free-software/open-source library
7for financial quantitative analysts and developers - http://quantlib.org/
8
9QuantLib is free software: you can redistribute it and/or modify it
10under the terms of the QuantLib license. You should have received a
11copy 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
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
21
22namespace QuantLib {
23
25 Size result = 0;
26 for (const auto& component : components_)
27 result += component.product->numberOfProducts();
28 return result;
29 }
30
31
33 Size result = 0;
34 for (const auto& component : components_)
35 result = std::max(result, component.product->maxNumberOfCashFlowsPerProductPerStep());
36 return result;
37 }
38
39
41 const CurveState& currentState,
42 std::vector<Size>& numberCashFlowsThisStep,
43 std::vector<std::vector<CashFlow> >& cashFlowsGenerated) {
44 QL_REQUIRE(finalized_, "composite not finalized");
45 bool done = true;
46 Size n = 0, offset = 0;
47 // for each sub-product...
48 for (auto i = components_.begin(); i != components_.end(); ++i, ++n) {
49 if (isInSubset_[n][currentIndex_] && !i->done) {
50 // ...make it evolve...
51 bool thisDone = i->product->nextTimeStep(currentState,
52 i->numberOfCashflows,
53 i->cashflows);
54 // ...and copy the results. Time indices need to be remapped
55 // so that they point into all cash-flow times. Amounts need
56 // to be adjusted by the corresponding multiplier.
57 for (Size j=0; j<i->product->numberOfProducts(); ++j) {
58 numberCashFlowsThisStep[j+offset] =
59 i->numberOfCashflows[j];
60 for (Size k=0; k<i->numberOfCashflows[j]; ++k) {
61 CashFlow& from = i->cashflows[j][k];
62 CashFlow& to = cashFlowsGenerated[j+offset][k];
63 to.timeIndex = i->timeIndices[from.timeIndex];
64 to.amount = from.amount * i->multiplier;
65 }
66 }
67 // finally, set done to false if this product isn't done
68 done = done && thisDone;
69 }
70 else
71 for (Size j=0; j<i->product->numberOfProducts(); ++j)
72 numberCashFlowsThisStep[j+offset] =0;
73
74 // the offset is updated whether or not the product was evolved
75 offset += i->product->numberOfProducts();
76 }
78 return done;
79 }
80
81 std::unique_ptr<MarketModelMultiProduct>
83 return std::unique_ptr<MarketModelMultiProduct>(new MultiProductComposite(*this));
84 }
85
86}
Curve state for market-model simulations
Definition: curvestate.hpp:41
std::vector< std::valarray< bool > > isInSubset_
std::vector< SubProduct > components_
Composition of one or more market-model products.
std::unique_ptr< MarketModelMultiProduct > clone() const override
returns a newly-allocated copy of itself
bool nextTimeStep(const CurveState &currentState, std::vector< Size > &numberCashFlowsThisStep, std::vector< std::vector< CashFlow > > &cashFlowsGenerated) override
return value indicates whether path is finished, TRUE means done
Size maxNumberOfCashFlowsPerProductPerStep() const override
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35