Coverage Report

Created: 2025-09-04 07:11

/src/quantlib/ql/pricingengines/exotic/analyticsimplechooserengine.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) 2010 Master IMAFA - Polytech'Nice Sophia - Université de Nice Sophia Antipolis
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
 <https://www.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/exotic/analyticsimplechooserengine.hpp>
22
#include <ql/instruments/payoffs.hpp>
23
#include <ql/math/distributions/normaldistribution.hpp>
24
#include <utility>
25
26
namespace QuantLib {
27
28
    AnalyticSimpleChooserEngine::AnalyticSimpleChooserEngine(
29
        ext::shared_ptr<GeneralizedBlackScholesProcess> process)
30
0
    : process_(std::move(process)) {
31
0
        registerWith(process_);
32
0
    }
33
34
0
    void AnalyticSimpleChooserEngine::calculate() const {
35
0
        Date today = Settings::instance().evaluationDate();
36
0
        DayCounter rfdc  = process_->riskFreeRate()->dayCounter();
37
0
        DayCounter divdc = process_->dividendYield()->dayCounter();
38
0
        DayCounter voldc = process_->blackVolatility()->dayCounter();
39
0
        QL_REQUIRE(rfdc==divdc,
40
0
                   "Risk-free rate and dividend yield must"
41
0
                   "have the same day counter");
42
0
        QL_REQUIRE(rfdc==voldc,
43
0
                   "Risk-free rate and volatility must"
44
0
                   "have the same day counter");
45
0
        Real spot = process_->stateVariable()->value();
46
0
        auto payoff = ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
47
0
        QL_REQUIRE(payoff, "non-plain payoff given");
48
0
        Real strike = payoff->strike();
49
0
        Volatility volatility = process_->blackVolatility()->blackVol(
50
0
                                                arguments_.exercise->lastDate(),
51
0
                                                strike);
52
0
        Date maturity = arguments_.exercise->lastDate();
53
0
        Real timeToMaturity = rfdc.yearFraction(today,maturity);
54
0
        Real timeToChoosing = rfdc.yearFraction(today,arguments_.choosingDate);
55
0
        Rate dividendRate =
56
0
            process_->dividendYield()->zeroRate(maturity, divdc,
57
0
                                                Continuous, NoFrequency);
58
0
        Rate riskFreeRate =
59
0
            process_->riskFreeRate()->zeroRate(maturity, rfdc,
60
0
                                               Continuous, NoFrequency);
61
62
0
        QL_REQUIRE(spot > 0.0, "negative or null spot value");
63
0
        QL_REQUIRE(strike > 0.0, "negative or null strike value");
64
0
        QL_REQUIRE(volatility > 0.0,
65
0
                   "negative or null volatility");
66
0
        QL_REQUIRE(timeToChoosing > 0.0,
67
0
                   "choosing date earlier than or equal to evaluation date");
68
69
0
        Real d = (std::log(spot/strike)
70
0
            + ((riskFreeRate-dividendRate) + volatility*volatility*0.5)*timeToMaturity)
71
0
            /(volatility*std::sqrt(timeToMaturity));
72
73
0
        Real y = (std::log(spot/strike) + (riskFreeRate-dividendRate)*timeToMaturity
74
0
            + (volatility*volatility*timeToChoosing/2))
75
0
            /(volatility*std::sqrt(timeToChoosing));
76
77
0
        CumulativeNormalDistribution f;
78
79
0
        results_.value = spot*std::exp(-dividendRate*timeToMaturity)*f(d)
80
0
            - strike*std::exp(-riskFreeRate*timeToMaturity)
81
0
            *f(d-volatility*std::sqrt(timeToMaturity))
82
0
            -spot*std::exp(-dividendRate*timeToMaturity)*f(-y)
83
0
            +strike*std::exp(-riskFreeRate*timeToMaturity)
84
0
            *f(-y+volatility*std::sqrt(timeToChoosing));
85
0
     }
86
87
}