Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/pricingengines/basket/vectorbsmprocessextractor.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) 2024 Klaus Spanderen
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
/*! \file vectorbsmprocessextractor.cpp
21
*/
22
#include <ql/math/functional.hpp>
23
#include <ql/math/comparison.hpp>
24
25
#include <ql/pricingengines/basket/vectorbsmprocessextractor.hpp>
26
27
namespace QuantLib::detail {
28
29
        VectorBsmProcessExtractor::VectorBsmProcessExtractor(
30
            std::vector<ext::shared_ptr<GeneralizedBlackScholesProcess> > p)
31
0
          : processes_(std::move(p)) {
32
0
        }
33
34
        Array VectorBsmProcessExtractor::extractProcesses(
35
            const std::function<Real(
36
0
                const ext::shared_ptr<GeneralizedBlackScholesProcess>&)>& f) const {
37
38
0
            Array x(processes_.size());
39
0
            std::transform(processes_.begin(), processes_.end(), x.begin(), f);
40
41
0
            return x;
42
0
        }
43
44
        DiscountFactor VectorBsmProcessExtractor::getInterestRateDf(
45
0
            const Date& maturityDate) const {
46
0
            const Array dr = extractProcesses(
47
0
                [maturityDate](const auto& p) -> DiscountFactor {
48
0
                    return p->riskFreeRate()->discount(maturityDate);
49
0
                }
50
0
            );
51
52
0
            QL_REQUIRE(
53
0
                std::equal(
54
0
                    dr.begin()+1, dr.end(), dr.begin(),
55
0
                    [](Real a, Real b) -> bool { return close_enough(a, b);}
56
0
                ),
57
0
                "interest rates need to be the same for all underlyings"
58
0
            );
59
60
0
            return dr[0];
61
0
        }
62
63
0
        Array VectorBsmProcessExtractor::getSpot() const {
64
0
            return extractProcesses([](const auto& p) -> Real { return p->x0(); });
65
0
        }
66
67
        Array VectorBsmProcessExtractor::getDividendYieldDf(
68
0
            const Date& maturityDate) const {
69
0
            return extractProcesses(
70
0
                [maturityDate](const auto& p) -> DiscountFactor {
71
0
                    return p->dividendYield()->discount(maturityDate);
72
0
                }
73
0
            );
74
0
        }
75
76
        Array VectorBsmProcessExtractor::getBlackVariance(
77
0
            const Date& maturityDate) const {
78
0
            return extractProcesses(
79
0
                [maturityDate](const auto& p) -> Volatility {
80
0
                    return p->blackVolatility()->blackVariance(maturityDate, p->x0());
81
0
                }
82
0
            );
83
0
        }
84
85
        Array VectorBsmProcessExtractor::getBlackStdDev(
86
0
            const Date& maturityDate) const {
87
0
            return extractProcesses(
88
0
                [maturityDate](const auto& p) -> Volatility {
89
0
                    const Time maturity = p->blackVolatility()->timeFromReference(maturityDate);
90
0
                    return p->blackVolatility()->blackVol(maturityDate, p->x0())*std::sqrt(maturity);
91
0
                }
92
0
            );
93
0
        }
94
95
}
96