Coverage Report

Created: 2026-01-25 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/pricingengines/exotic/analyticamericanmargrabeengine.cpp
Line
Count
Source
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/analyticamericanmargrabeengine.hpp>
22
#include <ql/pricingengines/vanilla/bjerksundstenslandengine.hpp>
23
#include <ql/quotes/simplequote.hpp>
24
#include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp>
25
#include <ql/termstructures/yield/flatforward.hpp>
26
#include <ql/time/calendars/nullcalendar.hpp>
27
#include <utility>
28
29
namespace QuantLib {
30
31
    AnalyticAmericanMargrabeEngine::AnalyticAmericanMargrabeEngine(
32
        ext::shared_ptr<GeneralizedBlackScholesProcess> process1,
33
        ext::shared_ptr<GeneralizedBlackScholesProcess> process2,
34
        Real correlation)
35
0
    : process1_(std::move(process1)), process2_(std::move(process2)), rho_(correlation) {
36
0
        registerWith(process1_);
37
0
        registerWith(process2_);
38
0
    }
39
40
0
    void AnalyticAmericanMargrabeEngine::calculate() const {
41
42
0
        QL_REQUIRE(arguments_.exercise->type() == Exercise::American,
43
0
                   "not an American option");
44
45
0
        ext::shared_ptr<AmericanExercise> exercise =
46
0
            ext::dynamic_pointer_cast<AmericanExercise>(arguments_.exercise);
47
0
        QL_REQUIRE(exercise, "not an American option");
48
49
0
        ext::shared_ptr<NullPayoff> payoff0 =
50
0
            ext::dynamic_pointer_cast<NullPayoff>(arguments_.payoff);
51
0
        QL_REQUIRE(payoff0, "not a null payoff");
52
53
        // The option can be priced as an American single-asset option
54
        // with an adjusted process and payoff.
55
56
0
        Date today = Settings::instance().evaluationDate();
57
58
0
        DayCounter rfdc  = process1_->riskFreeRate()->dayCounter();
59
0
        Time t = rfdc.yearFraction(process1_->riskFreeRate()->referenceDate(),
60
0
                                   arguments_.exercise->lastDate());
61
62
0
        Real s1 = process1_->stateVariable()->value();
63
0
        Real s2 = process2_->stateVariable()->value();
64
65
0
        ext::shared_ptr<SimpleQuote> spot(new SimpleQuote(arguments_.Q1*s1));
66
67
0
        ext::shared_ptr<StrikedTypePayoff> payoff(
68
0
                      new PlainVanillaPayoff(Option::Call, arguments_.Q2*s2));
69
70
0
        DiscountFactor dividendDiscount1 =
71
0
            process1_->dividendYield()->discount(exercise->lastDate());
72
0
        Rate q1 = -std::log(dividendDiscount1)/t;
73
74
0
        DiscountFactor dividendDiscount2 =
75
0
            process2_->dividendYield()->discount(exercise->lastDate());
76
0
        Rate q2 = -std::log(dividendDiscount2)/t;
77
78
0
        ext::shared_ptr<YieldTermStructure> qTS(
79
0
                                            new FlatForward(today, q1, rfdc));
80
81
0
        ext::shared_ptr<YieldTermStructure> rTS(
82
0
                                            new FlatForward(today, q2, rfdc));
83
84
0
        Real variance1 = process1_->blackVolatility()->blackVariance(
85
0
                                                exercise->lastDate(), s1);
86
0
        Real variance2 = process2_->blackVolatility()->blackVariance(
87
0
                                                exercise->lastDate(), s2);
88
0
        Real variance = variance1 + variance2
89
0
                      - 2*rho_*std::sqrt(variance1)*std::sqrt(variance2);
90
0
        Volatility volatility = std::sqrt(variance/t);
91
92
0
        ext::shared_ptr<BlackVolTermStructure> volTS(
93
0
               new BlackConstantVol(today, NullCalendar(), volatility, rfdc));
94
95
0
        ext::shared_ptr<BlackScholesMertonProcess> stochProcess(new
96
0
            BlackScholesMertonProcess(Handle<Quote>(spot),
97
0
                                      Handle<YieldTermStructure>(qTS),
98
0
                                      Handle<YieldTermStructure>(rTS),
99
0
                                      Handle<BlackVolTermStructure>(volTS)));
100
101
0
        ext::shared_ptr<PricingEngine> engine(
102
0
                     new BjerksundStenslandApproximationEngine(stochProcess));
103
104
0
        VanillaOption option(payoff, exercise);
105
0
        option.setPricingEngine(engine);
106
107
0
        results_.value = option.NPV();
108
0
    }
109
110
}