Coverage Report

Created: 2025-08-11 06:28

/src/quantlib/ql/pricingengines/cliquet/analyticcliquetengine.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) 2004, 2007 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/exercise.hpp>
21
#include <ql/pricingengines/blackcalculator.hpp>
22
#include <ql/pricingengines/cliquet/analyticcliquetengine.hpp>
23
#include <utility>
24
25
namespace QuantLib {
26
27
    AnalyticCliquetEngine::AnalyticCliquetEngine(
28
        ext::shared_ptr<GeneralizedBlackScholesProcess> process)
29
0
    : process_(std::move(process)) {
30
0
        registerWith(process_);
31
0
    }
32
33
0
    void AnalyticCliquetEngine::calculate() const {
34
35
0
        QL_REQUIRE(arguments_.accruedCoupon == Null<Real>() &&
36
0
                   arguments_.lastFixing == Null<Real>(),
37
0
                   "this engine cannot price options already started");
38
0
        QL_REQUIRE(arguments_.localCap == Null<Real>() &&
39
0
                   arguments_.localFloor == Null<Real>() &&
40
0
                   arguments_.globalCap == Null<Real>() &&
41
0
                   arguments_.globalFloor == Null<Real>(),
42
0
                   "this engine cannot price capped/floored options");
43
44
0
        QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
45
0
                   "not an European option");
46
47
0
        ext::shared_ptr<PercentageStrikePayoff> moneyness =
48
0
            ext::dynamic_pointer_cast<PercentageStrikePayoff>(
49
0
                                                           arguments_.payoff);
50
0
        QL_REQUIRE(moneyness, "wrong payoff given");
51
52
0
        std::vector<Date> resetDates = arguments_.resetDates;
53
0
        resetDates.push_back(arguments_.exercise->lastDate());
54
55
0
        Real underlying = process_->stateVariable()->value();
56
0
        QL_REQUIRE(underlying > 0.0, "negative or null underlying");
57
0
        Real strike = underlying * moneyness->strike();
58
0
        ext::shared_ptr<StrikedTypePayoff> payoff(
59
0
                      new PlainVanillaPayoff(moneyness->optionType(),strike));
60
61
0
        results_.value = 0.0;
62
0
        results_.delta = results_.gamma = 0.0;
63
0
        results_.theta = 0.0;
64
0
        results_.rho = results_.dividendRho = 0.0;
65
0
        results_.vega = 0.0;
66
67
0
        for (Size i = 1; i < resetDates.size(); i++) {
68
69
0
            Real weight =
70
0
                process_->dividendYield()->discount(resetDates[i-1]);
71
0
            DiscountFactor discount =
72
0
                process_->riskFreeRate()->discount(resetDates[i]) /
73
0
                process_->riskFreeRate()->discount(resetDates[i-1]);
74
0
            DiscountFactor qDiscount =
75
0
                process_->dividendYield()->discount(resetDates[i]) /
76
0
                process_->dividendYield()->discount(resetDates[i-1]);
77
0
            Real forward = underlying*qDiscount/discount;
78
0
            Real variance =
79
0
                process_->blackVolatility()->blackForwardVariance(
80
0
                                        resetDates[i-1],resetDates[i],strike);
81
82
0
            BlackCalculator black(payoff, forward, std::sqrt(variance), discount);
83
84
0
            DayCounter rfdc  = process_->riskFreeRate()->dayCounter();
85
0
            DayCounter divdc = process_->dividendYield()->dayCounter();
86
0
            DayCounter voldc = process_->blackVolatility()->dayCounter();
87
88
0
            results_.value += weight * black.value();
89
0
            results_.delta += weight * (black.delta(underlying) +
90
0
                                        moneyness->strike() * discount *
91
0
                                        black.beta());
92
0
            results_.gamma += 0.0;
93
0
            results_.theta += process_->dividendYield()->forwardRate(
94
0
                resetDates[i-1], resetDates[i], rfdc, Continuous, NoFrequency) *
95
0
                weight * black.value();
96
97
0
            Time dt = rfdc.yearFraction(resetDates[i-1],resetDates[i]);
98
0
            results_.rho += weight * black.rho(dt);
99
100
0
            Time t = divdc.yearFraction(
101
0
                                    process_->dividendYield()->referenceDate(),
102
0
                                    resetDates[i-1]);
103
0
            dt = divdc.yearFraction(resetDates[i-1],resetDates[i]);
104
0
            results_.dividendRho += weight * (black.dividendRho(dt) -
105
0
                                              t * black.value());
106
107
0
            dt = voldc.yearFraction(resetDates[i-1], resetDates[i]);
108
0
            results_.vega += weight * black.vega(dt);
109
0
        }
110
111
0
    }
112
113
}
114