QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mcdigitalengine.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) 2004 Ferdinando Ametrano
5 Copyright (C) 2003 Neil Firth
6 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
23#include <utility>
24
25namespace QuantLib {
26
27 DigitalPathPricer::DigitalPathPricer(ext::shared_ptr<CashOrNothingPayoff> payoff,
28 ext::shared_ptr<AmericanExercise> exercise,
30 ext::shared_ptr<StochasticProcess1D> diffProcess,
31 PseudoRandom::ursg_type sequenceGen)
32 : payoff_(std::move(payoff)), exercise_(std::move(exercise)),
33 diffProcess_(std::move(diffProcess)), sequenceGen_(std::move(sequenceGen)),
34 discountTS_(std::move(discountTS)) {}
35
37 Size n = path.length();
38 QL_REQUIRE(n>1, "the path cannot be empty");
39
40 Real log_asset_price = std::log(path.front());
41 Real x, y;
42 Volatility vol;
43 const TimeGrid& timeGrid = path.timeGrid();
44 Time dt;
45 std::vector<Real> u = sequenceGen_.nextSequence().value;
46 Real log_strike = std::log(payoff_->strike());
47
48 Size i;
49 switch (payoff_->optionType()) {
50 case Option::Call:
51 for (i=0; i<n-1; i++) {
52 x = std::log(path[i+1]/path[i]);
53 // terminal or initial vol?
54 vol = diffProcess_->diffusion(timeGrid[i+1],
55 std::exp(log_asset_price));
56 // vol = diffProcess_->diffusion(timeGrid[i+2],
57 // std::exp(log_asset_price+x));
58 dt = timeGrid.dt(i);
59 y = log_asset_price +
60 0.5*(x + std::sqrt(x*x-2*vol*vol*dt*std::log((1-u[i]))));
61 // cross the strike
62 if (y >= log_strike) {
63 if (exercise_->payoffAtExpiry()) {
64 return payoff_->cashPayoff() *
65 discountTS_->discount(path.timeGrid().back());
66 } else {
67 // the discount should be calculated at the exercise
68 // time between path.timeGrid()[i+1] and
69 // path.timeGrid()[i+2]
70 return payoff_->cashPayoff() *
71 discountTS_->discount(path.timeGrid()[i+1]);
72 }
73 }
74 log_asset_price += x;
75 }
76 break;
77 case Option::Put:
78 for (i=0; i<n-1; i++) {
79 x = std::log(path[i+1]/path[i]);
80 // terminal or initial vol?
81 // initial (timeGrid[i+1]) for the time being
82 vol = diffProcess_->diffusion(timeGrid[i+1],
83 std::exp(log_asset_price));
84 // vol = diffProcess_->diffusion(timeGrid[i+2],
85 // std::exp(log_asset_price+x));
86 dt = timeGrid.dt(i);
87 y = log_asset_price +
88 0.5*(x - std::sqrt(x*x - 2*vol*vol*dt*std::log(u[i])));
89 if (y <= log_strike) {
90 if (exercise_->payoffAtExpiry()) {
91 return payoff_->cashPayoff() *
92 discountTS_->discount(path.timeGrid().back());
93 } else {
94 // the discount should be calculated at the exercise
95 // time between path.timeGrid()[i+1] and
96 // path.timeGrid()[i+2]
97 return payoff_->cashPayoff() *
98 discountTS_->discount(path.timeGrid()[i+1]);
99 }
100 }
101 log_asset_price += x;
102 }
103 break;
104 default:
105 QL_FAIL("unknown option type");
106 }
107
108 return 0.0;
109 }
110
111}
112
ext::shared_ptr< AmericanExercise > exercise_
ext::shared_ptr< CashOrNothingPayoff > payoff_
PseudoRandom::ursg_type sequenceGen_
Handle< YieldTermStructure > discountTS_
DigitalPathPricer(ext::shared_ptr< CashOrNothingPayoff > payoff, ext::shared_ptr< AmericanExercise > exercise, Handle< YieldTermStructure > discountTS, ext::shared_ptr< StochasticProcess1D > diffProcess, PseudoRandom::ursg_type sequenceGen)
Real operator()(const Path &path) const override
ext::shared_ptr< StochasticProcess1D > diffProcess_
Shared handle to an observable.
Definition: handle.hpp:41
single-factor random walk
Definition: path.hpp:40
Size length() const
Definition: path.hpp:94
const TimeGrid & timeGrid() const
time grid
Definition: path.hpp:142
Real front() const
initial asset value
Definition: path.hpp:122
Random sequence generator based on a pseudo-random number generator.
const sample_type & nextSequence() const
time grid class
Definition: timegrid.hpp:43
Time back() const
Definition: timegrid.hpp:171
Time dt(Size i) const
Definition: timegrid.hpp:154
#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
const ext::shared_ptr< Payoff > payoff_
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
std::size_t Size
size of a container
Definition: types.hpp:58
ext::shared_ptr< QuantLib::Payoff > payoff
digital option Monte Carlo engine
Definition: any.hpp:35
STL namespace.
const ParametricExercise & exercise_