QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
integralengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2003, 2004 Ferdinando Ametrano
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>
22#include <ql/math/integrals/segmentintegral.hpp>
23#include <ql/pricingengines/vanilla/integralengine.hpp>
24#include <utility>
25
26namespace QuantLib {
27
28 namespace {
29
30 class Integrand {
31 public:
32 Integrand(ext::shared_ptr<Payoff> payoff, Real s0, Rate drift, Real variance)
33 : payoff_(std::move(payoff)), s0_(s0), drift_(drift), variance_(variance) {}
34 Real operator()(Real x) const {
35 Real temp = s0_ * std::exp(x);
36 Real result = (*payoff_)(temp);
37 return result *
38 std::exp(-(x - drift_)*(x -drift_)/(2.0*variance_)) ;
39 }
40 private:
41 ext::shared_ptr<Payoff> payoff_;
42 Real s0_;
43 Rate drift_;
44 Real variance_;
45 };
46 }
47
48 IntegralEngine::IntegralEngine(ext::shared_ptr<GeneralizedBlackScholesProcess> process)
49 : process_(std::move(process)) {
50 registerWith(process_);
51 }
52
54
55 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
56 "not an European Option");
57
58 ext::shared_ptr<StrikedTypePayoff> payoff =
59 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
60 QL_REQUIRE(payoff, "non-striked payoff given");
61
62 Real variance =
63 process_->blackVolatility()->blackVariance(
64 arguments_.exercise->lastDate(), payoff->strike());
65
66 DiscountFactor dividendDiscount =
67 process_->dividendYield()->discount(
68 arguments_.exercise->lastDate());
69 DiscountFactor riskFreeDiscount =
70 process_->riskFreeRate()->discount(arguments_.exercise->lastDate());
71 Rate drift = std::log(dividendDiscount/riskFreeDiscount)-0.5*variance;
72
73 Integrand f(arguments_.payoff,
74 process_->stateVariable()->value(),
75 drift, variance);
76 SegmentIntegral integrator(5000);
77
78 Real infinity = 10.0*std::sqrt(variance);
79 results_.value =
80 process_->riskFreeRate()->discount(
81 arguments_.exercise->lastDate()) /
82 std::sqrt(2.0*M_PI*variance) *
83 integrator(f, drift-infinity, drift+infinity);
84 }
85
86}
87
void calculate() const override
IntegralEngine(ext::shared_ptr< GeneralizedBlackScholesProcess >)
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
Integral of a one-dimensional function.
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Rate
interest rates
Definition: types.hpp:70
Definition: any.hpp:35
STL namespace.