Coverage Report

Created: 2025-08-11 06:28

/src/quantlib/ql/models/marketmodels/accountingengine.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2006 Mark Joshi
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/accountingengine.hpp>
21
#include <ql/models/marketmodels/curvestate.hpp>
22
#include <ql/models/marketmodels/discounter.hpp>
23
#include <ql/models/marketmodels/evolutiondescription.hpp>
24
#include <ql/models/marketmodels/evolver.hpp>
25
#include <algorithm>
26
#include <utility>
27
28
namespace QuantLib {
29
30
    AccountingEngine::AccountingEngine(ext::shared_ptr<MarketModelEvolver> evolver,
31
                                       const Clone<MarketModelMultiProduct>& product,
32
                                       Real initialNumeraireValue)
33
0
    : evolver_(std::move(evolver)), product_(product),
34
0
      initialNumeraireValue_(initialNumeraireValue), numberProducts_(product->numberOfProducts()),
35
0
      numerairesHeld_(product->numberOfProducts()),
36
0
      numberCashFlowsThisStep_(product->numberOfProducts()),
37
0
      cashFlowsGenerated_(product->numberOfProducts()) {
38
0
        for (Size i=0; i<numberProducts_; ++i)
39
0
            cashFlowsGenerated_[i].resize(
40
0
                       product_->maxNumberOfCashFlowsPerProductPerStep());
41
42
0
        const std::vector<Time>& cashFlowTimes =
43
0
            product_->possibleCashFlowTimes();
44
0
        const std::vector<Rate>& rateTimes = product_->evolution().rateTimes();
45
0
        discounters_.reserve(cashFlowTimes.size());
46
0
        for (Real cashFlowTime : cashFlowTimes)
47
0
            discounters_.emplace_back(cashFlowTime, rateTimes);
48
0
    }
49
50
0
    Real AccountingEngine::singlePathValues(std::vector<Real>& values) {
51
0
        std::fill(numerairesHeld_.begin(), numerairesHeld_.end(), 0.0);
52
0
        Real weight = evolver_->startNewPath();
53
0
        product_->reset();
54
0
        Real principalInNumerairePortfolio = 1.0;
55
56
0
        bool done = false;
57
0
        do {
58
0
            Size thisStep = evolver_->currentStep();
59
0
            weight *= evolver_->advanceStep();
60
0
            done = product_->nextTimeStep(evolver_->currentState(),
61
0
                                          numberCashFlowsThisStep_,
62
0
                                          cashFlowsGenerated_);
63
0
            Size numeraire =
64
0
                evolver_->numeraires()[thisStep];
65
66
            // for each product...
67
0
            for (Size i=0; i<numberProducts_; ++i) {
68
                // ...and each cash flow...
69
0
                const std::vector<MarketModelMultiProduct::CashFlow>& cashflows =
70
0
                    cashFlowsGenerated_[i];
71
0
                for (Size j=0; j<numberCashFlowsThisStep_[i]; ++j) {
72
                    // ...convert the cash flow to numeraires.
73
                    // This is done by calculating the number of
74
                    // numeraire bonds corresponding to such cash flow...
75
0
                    const MarketModelDiscounter& discounter =
76
0
                        discounters_[cashflows[j].timeIndex];
77
78
0
                    Real bonds = cashflows[j].amount *
79
0
                        discounter.numeraireBonds(evolver_->currentState(),
80
0
                                                  numeraire);
81
82
                    // ...and adding the newly bought bonds to the number
83
                    // of numeraires held.
84
0
                    numerairesHeld_[i] += bonds/principalInNumerairePortfolio;
85
0
                }
86
0
            }
87
88
0
            if (!done) {
89
90
                // The numeraire might change between steps. This implies
91
                // that we might have to convert the numeraire bonds for
92
                // this step into a corresponding amount of numeraire
93
                // bonds for the next step. This can be done by changing
94
                // the principal of the numeraire and updating the number
95
                // of bonds in the numeraire portfolio accordingly.
96
97
0
                Size nextNumeraire = evolver_->numeraires()[thisStep+1];
98
99
0
                principalInNumerairePortfolio *=
100
0
                    evolver_->currentState().discountRatio(numeraire,
101
0
                                                           nextNumeraire);
102
0
            }
103
104
0
        } while (!done);
105
106
0
        for (Size i=0; i<numerairesHeld_.size(); ++i)
107
0
            values[i] = numerairesHeld_[i] * initialNumeraireValue_;
108
109
0
        return weight;
110
0
    }
111
112
    void AccountingEngine::multiplePathValues(SequenceStatisticsInc& stats,
113
                                              Size numberOfPaths)
114
0
    {
115
0
        std::vector<Real> values(product_->numberOfProducts());
116
0
        for (Size i=0; i<numberOfPaths; ++i) {
117
0
            Real weight = singlePathValues(values);
118
0
            stats.add(values,weight);
119
0
        }
120
0
    }
121
122
}