QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mc_discr_geom_av_price.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) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2003, 2004 Ferdinando Ametrano
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
22
23namespace QuantLib {
24
26 Option::Type type,
27 Real strike, DiscountFactor discount,
28 Real runningProduct, Size pastFixings)
29 : payoff_(type, strike), discount_(discount),
30 runningProduct_(runningProduct), pastFixings_(pastFixings) {
31 QL_REQUIRE(strike>=0.0, "negative strike given");
32 }
33
35 Size n = path.length() - 1;
36 QL_REQUIRE(n>0, "the path cannot be empty");
37
38 Real averagePrice;
39 Real product = runningProduct_;
40 Size fixings = n+pastFixings_;
41 if (path.timeGrid().mandatoryTimes()[0]==0.0) {
42 fixings += 1;
43 product *= path.front();
44 }
45 // care must be taken not to overflow product
46 constexpr double maxValue = QL_MAX_REAL;
47 averagePrice = 1.0;
48 for (Size i=1; i<n+1; i++) {
49 Real price = path[i];
50 if (product < maxValue/price) {
51 product *= price;
52 } else {
53 averagePrice *= std::pow(product, 1.0/fixings);
54 product = price;
55 }
56 }
57 averagePrice *= std::pow(product, 1.0/fixings);
58 return discount_ * payoff_(averagePrice);
59 }
60
61}
GeometricAPOPathPricer(Option::Type type, Real strike, DiscountFactor discount, Real runningProduct=1.0, Size pastFixings=0)
Real operator()(const Path &path) const override
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
const std::vector< Time > & mandatoryTimes() const
Definition: timegrid.hpp:151
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
const ext::shared_ptr< Payoff > payoff_
#define QL_MAX_REAL
Definition: qldefines.hpp:176
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
std::size_t Size
size of a container
Definition: types.hpp:58
Monte Carlo engine for discrete geometric average price Asian.
Definition: any.hpp:35