QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
analyticcontinuouspartialfixedlookback.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2006 Warren Chou
5 Copyright (C) 2007 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/exercise.hpp>
23#include <utility>
24
25namespace QuantLib {
26
28 ext::shared_ptr<GeneralizedBlackScholesProcess> process)
29 : process_(std::move(process)) {
31 }
32
34
35 ext::shared_ptr<PlainVanillaPayoff> payoff =
36 ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff);
37 QL_REQUIRE(payoff, "Non-plain payoff given");
38
39 QL_REQUIRE(process_->x0() > 0.0, "negative or null underlying");
40
41 switch (payoff->optionType()) {
42 case Option::Call:
43 QL_REQUIRE(payoff->strike()>=0.0,
44 "Strike must be positive or null");
45 results_.value = A(1);
46 break;
47 case Option::Put:
48 QL_REQUIRE(payoff->strike()>0.0,
49 "Strike must be positive");
50 results_.value = A(-1);
51 break;
52 default:
53 QL_FAIL("Unknown type");
54 }
55 }
56
57
59 return process_->x0();
60 }
61
63 ext::shared_ptr<PlainVanillaPayoff> payoff =
64 ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff);
65 QL_REQUIRE(payoff, "Non-plain payoff given");
66 return payoff->strike();
67 }
68
70 return process_->time(arguments_.exercise->lastDate());
71 }
72
74 return process_->blackVolatility()->blackVol(residualTime(), strike());
75 }
76
78 return volatility() * std::sqrt(residualTime());
79 }
80
82 return process_->riskFreeRate()->zeroRate(residualTime(), Continuous,
84 }
85
87 const {
88 return process_->riskFreeRate()->discount(residualTime());
89 }
90
92 return process_->dividendYield()->zeroRate(residualTime(),
94 }
95
97 const {
98 return process_->dividendYield()->discount(residualTime());
99 }
100
103 }
104
106 bool differentStartOfLookback = lookbackPeriodStartTime() != residualTime();
107 Real carry = riskFreeRate() - dividendYield();
108
109 Volatility vol = volatility();
110 Real x = 2.0*carry/(vol*vol);
111 Real s = underlying()/strike();
112 Real ls = std::log(s);
113 Real d1 = ls/stdDeviation() + 0.5*(x+1.0)*stdDeviation();
114 Real d2 = d1 - stdDeviation();
115
116 Real e1 = 0, e2 = 0;
117 if (differentStartOfLookback)
118 {
119 e1 = (carry + vol * vol / 2) * (residualTime() - lookbackPeriodStartTime()) / (vol * std::sqrt(residualTime() - lookbackPeriodStartTime()));
120 e2 = e1 - vol * std::sqrt(residualTime() - lookbackPeriodStartTime());
121 }
122
123 Real f1 = (ls + (carry + vol * vol / 2) * lookbackPeriodStartTime()) / (vol * std::sqrt(lookbackPeriodStartTime()));
124 Real f2 = f1 - vol * std::sqrt(lookbackPeriodStartTime());
125
126 Real n1 = f_(eta*d1);
127 Real n2 = f_(eta*d2);
128
129 BivariateCumulativeNormalDistributionWe04DP cnbn1(-1), cnbn2(0), cnbn3(0);
130 if (differentStartOfLookback) {
134 }
135
136 Real n3 = cnbn1(eta*(d1-x*stdDeviation()), eta*(-f1+2.0* carry * std::sqrt(lookbackPeriodStartTime()) / vol));
137 Real n4 = cnbn2(eta*e1, eta*d1);
138 Real n5 = cnbn3(-eta*e1, eta*d1);
139 Real n6 = cnbn1(eta*f2, -eta*d2);
140 Real n7 = f_(eta*f1);
141 Real n8 = f_(-eta*e2);
142
143 Real pow_s = std::pow(s, -x);
144 Real carryDiscount = std::exp(-carry * (residualTime() - lookbackPeriodStartTime()));
145 return eta*(underlying() * dividendDiscount() * n1
146 - strike() * riskFreeDiscount() * n2
147 + underlying() * riskFreeDiscount() / x
148 * (-pow_s * n3 + dividendDiscount() / riskFreeDiscount() * n4)
149 - underlying() * dividendDiscount() * n5
150 - strike() * riskFreeDiscount() * n6
151 + carryDiscount * dividendDiscount()
152 * (1 - 0.5 * vol * vol / carry) *
153 underlying() * n7 * n8);
154 }
155}
156
Analytic engine for continuous fixed-strike lookback.
AnalyticContinuousPartialFixedLookbackEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process)
Cumulative bivariate normal distibution function (West 2004)
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Option exercise classes and payoff function.
@ NoFrequency
null frequency
Definition: frequency.hpp:37
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Volatility
volatility
Definition: types.hpp:78
Real Rate
interest rates
Definition: types.hpp:70
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
STL namespace.