/src/quantlib/ql/pricingengines/cliquet/analyticperformanceengine.cpp
Line | Count | Source |
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 | | <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/blackcalculator.hpp> |
22 | | #include <ql/pricingengines/cliquet/analyticperformanceengine.hpp> |
23 | | #include <utility> |
24 | | |
25 | | namespace QuantLib { |
26 | | |
27 | | AnalyticPerformanceEngine::AnalyticPerformanceEngine( |
28 | | ext::shared_ptr<GeneralizedBlackScholesProcess> process) |
29 | 0 | : process_(std::move(process)) { |
30 | 0 | registerWith(process_); |
31 | 0 | } |
32 | | |
33 | 0 | void AnalyticPerformanceEngine::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 | | |
58 | 0 | auto payoff = ext::make_shared<PlainVanillaPayoff>(moneyness->optionType(), 1.0); |
59 | |
|
60 | 0 | results_.value = 0.0; |
61 | 0 | results_.delta = results_.gamma = 0.0; |
62 | 0 | results_.theta = 0.0; |
63 | 0 | results_.rho = results_.dividendRho = 0.0; |
64 | 0 | results_.vega = 0.0; |
65 | |
|
66 | 0 | for (Size i = 1; i < resetDates.size(); i++) { |
67 | |
|
68 | 0 | DiscountFactor discount = |
69 | 0 | process_->riskFreeRate()->discount(resetDates[i-1]); |
70 | 0 | DiscountFactor rDiscount = |
71 | 0 | process_->riskFreeRate()->discount(resetDates[i]) / |
72 | 0 | process_->riskFreeRate()->discount(resetDates[i-1]); |
73 | 0 | DiscountFactor qDiscount = |
74 | 0 | process_->dividendYield()->discount(resetDates[i]) / |
75 | 0 | process_->dividendYield()->discount(resetDates[i-1]); |
76 | 0 | Real forward = (1.0/moneyness->strike())*qDiscount/rDiscount; |
77 | 0 | Real variance = |
78 | 0 | process_->blackVolatility()->blackForwardVariance( |
79 | 0 | resetDates[i-1],resetDates[i], |
80 | 0 | underlying * moneyness->strike()); |
81 | |
|
82 | 0 | BlackCalculator black(payoff, forward, std::sqrt(variance), rDiscount); |
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 += discount * moneyness->strike() * black.value(); |
89 | 0 | results_.delta += 0.0; |
90 | 0 | results_.gamma += 0.0; |
91 | 0 | results_.theta += process_->riskFreeRate()->forwardRate( |
92 | 0 | resetDates[i-1], resetDates[i], rfdc, Continuous, NoFrequency) * |
93 | 0 | discount * moneyness->strike() * black.value(); |
94 | |
|
95 | 0 | Time dt = rfdc.yearFraction(resetDates[i-1],resetDates[i]); |
96 | 0 | Time t = rfdc.yearFraction( |
97 | 0 | process_->riskFreeRate()->referenceDate(), |
98 | 0 | resetDates[i-1]); |
99 | 0 | results_.rho += discount * moneyness->strike() * |
100 | 0 | (black.rho(dt) - t * black.value()); |
101 | |
|
102 | 0 | dt = divdc.yearFraction(resetDates[i-1],resetDates[i]); |
103 | 0 | results_.dividendRho += discount * moneyness->strike() * |
104 | 0 | black.dividendRho(dt); |
105 | |
|
106 | 0 | dt = voldc.yearFraction(resetDates[i-1], resetDates[i]); |
107 | 0 | results_.vega += discount * moneyness->strike() * black.vega(dt); |
108 | 0 | } |
109 | |
|
110 | 0 | } |
111 | | |
112 | | } |