Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
projectedvariatemultipathgenerator.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
20
21namespace QuantExt {
22
24 const QuantLib::ext::shared_ptr<StochasticProcess>& process, const TimeGrid& timeGrid,
25 const std::vector<Size>& stateProcessProjection,
26 const QuantLib::ext::shared_ptr<MultiPathVariateGeneratorBase>& variateGenerator)
27 : process_(process), timeGrid_(timeGrid), stateProcessProjection_(stateProcessProjection),
28 variateGenerator_(variateGenerator), next_(MultiPath(process->size(), timeGrid), 1.0) {
29
30 for (Size s = 0; s < process_->size(); ++s)
31 next_.value[s][0] = process_->initialValues()[s];
32
33 QL_REQUIRE(stateProcessProjection.size() == process->factors(),
34 "ProjectedVariateMultiPathGenerator: state process projection source size ("
35 << stateProcessProjection.size() << ") does not match process factors (" << process->factors());
36 QL_REQUIRE(!stateProcessProjection.empty(),
37 "ProjectedVariateMultiPathGenerator: state process projection is empty");
38
39 maxTargetIndex_ = *std::max_element(stateProcessProjection.begin(), stateProcessProjection.end());
40}
41
42const Sample<MultiPath>& ProjectedVariateMultiPathGenerator::next() const {
43
44 Sample<std::vector<Array>> v = variateGenerator_->next();
45 next_.weight = v.weight;
46
47 QL_REQUIRE(v.value.size() == timeGrid_.size() - 1,
48 "ProjectedVariateMultiPathGenerator::next(): variate generator returns "
49 << v.value.size() << " variates for " << (timeGrid_.size() - 1) << " time steps to evolve");
50
51 QL_REQUIRE(v.value.size() == 0 || maxTargetIndex_ < v.value.front().size(),
52 "ProjectedVariateMultiPathGenerator::next(): variate generator returns variate of size "
53 << v.value.front().size() << ", this is required to be > max target index (" << maxTargetIndex_
54 << ")");
55
56 Size dim = process_->factors();
57 Size stateSize = process_->size();
58
59 Array dw(dim, 0.0);
60 Array state = process_->initialValues();
61 for (Size i = 0; i < timeGrid_.size() - 1; ++i) {
62 for (Size d = 0; d < dim; ++d)
63 dw[d] = v.value[i][stateProcessProjection_[d]];
64 state = process_->evolve(timeGrid_[i], state, timeGrid_.dt(i), dw);
65 for (Size s = 0; s < stateSize; ++s)
66 next_.value[s][i + 1] = state[s];
67 }
68 return next_;
69}
70
72
73} // namespace QuantExt
const QuantLib::ext::shared_ptr< StochasticProcess > process_
const QuantLib::ext::shared_ptr< MultiPathVariateGeneratorBase > variateGenerator_
ProjectedVariateMultiPathGenerator(const QuantLib::ext::shared_ptr< StochasticProcess > &process, const TimeGrid &timeGrid, const std::vector< Size > &stateProcessProjection, const QuantLib::ext::shared_ptr< MultiPathVariateGeneratorBase > &variateGenerator)
const Sample< MultiPath > & next() const override
multi path generator projecting variates from another variate generator