QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
singleproductcomposite.cpp
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
20#include <ql/models/marketmodels/products/singleproductcomposite.hpp>
21
22namespace QuantLib {
23
25 return 1;
26 }
27
28
30 Size result = 0;
31 for (const auto& component : components_)
32 result += component.product->maxNumberOfCashFlowsPerProductPerStep();
33 return result;
34 }
35
36
38 const CurveState& currentState,
39 std::vector<Size>& numberCashFlowsThisStep,
40 std::vector<std::vector<CashFlow> >& cashFlowsGenerated) {
41 QL_REQUIRE(finalized_, "composite not finalized");
42 bool done = true;
43 Size n = 0, totalCashflows = 0;
44 // for each sub-product...
45 for (auto i = components_.begin(); i != components_.end(); ++i, ++n) {
46 if (isInSubset_[n][currentIndex_] && !i->done) {
47 // ...make it evolve...
48 bool thisDone = i->product->nextTimeStep(currentState,
49 i->numberOfCashflows,
50 i->cashflows);
51 // ...and copy the results. Time indices need to be remapped
52 // so that they point into all cash-flow times. Amounts need
53 // to be adjusted by the corresponding multiplier.
54 for (Size j=0; j<i->product->numberOfProducts(); ++j) {
55 Size offset = totalCashflows;
56 totalCashflows += i->numberOfCashflows[j];
57 for (Size k=0; k<i->numberOfCashflows[j]; ++k) {
58 CashFlow& from = i->cashflows[j][k];
59 CashFlow& to = cashFlowsGenerated[0][k+offset];
60 to.timeIndex = i->timeIndices[from.timeIndex];
61 to.amount = from.amount * i->multiplier;
62 }
63 numberCashFlowsThisStep[0] = totalCashflows;
64 }
65 // finally, set done to false if this product isn't done
66 done = done && thisDone;
67 }
68 }
70 return done;
71 }
72
73 std::unique_ptr<MarketModelMultiProduct>
75 return std::unique_ptr<MarketModelMultiProduct>(new SingleProductComposite(*this));
76 }
77
78}
79
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
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35