Skip to content

Commit

Permalink
Merge pull request #1027.
Browse files Browse the repository at this point in the history
Added shout option engine in new-style FD framework
  • Loading branch information
lballabio committed Feb 10, 2021
2 parents dace187 + e9152a9 commit 7cbc755
Show file tree
Hide file tree
Showing 16 changed files with 369 additions and 9 deletions.
2 changes: 1 addition & 1 deletion LICENSE.TXT
Expand Up @@ -19,7 +19,7 @@ QuantLib is
Copyright (C) 2004 M-Dimension Consulting Inc.
Copyright (C) 2004 Mike Parker
Copyright (C) 2004 Walter Penschke
Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Klaus Spanderen
Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Klaus Spanderen

Copyright (C) 2005 Charles Whitmore
Copyright (C) 2005 Sercan Atalik
Expand Down
6 changes: 5 additions & 1 deletion QuantLib.vcxproj
Expand Up @@ -1255,6 +1255,7 @@
<ClInclude Include="ql\methods\finitedifferences\utilities\fdminnervaluecalculator.hpp" />
<ClInclude Include="ql\methods\finitedifferences\utilities\fdmmesherintegral.hpp" />
<ClInclude Include="ql\methods\finitedifferences\utilities\fdmquantohelper.hpp" />
<ClInclude Include="ql\methods\finitedifferences\utilities\fdmshoutloginnervaluecalculator.hpp" />
<ClInclude Include="ql\methods\finitedifferences\utilities\fdmtimedepdirichletboundary.hpp" />
<ClInclude Include="ql\methods\finitedifferences\utilities\gbsmrndcalculator.hpp" />
<ClInclude Include="ql\methods\finitedifferences\utilities\hestonrndcalculator.hpp" />
Expand Down Expand Up @@ -1566,6 +1567,7 @@
<ClInclude Include="ql\pricingengines\vanilla\discretizedvanillaoption.hpp" />
<ClInclude Include="ql\pricingengines\vanilla\exponentialfittinghestonengine.hpp" />
<ClInclude Include="ql\pricingengines\vanilla\fdbatesvanillaengine.hpp" />
<ClInclude Include="ql\pricingengines\vanilla\fdblackscholesshoutengine.hpp" />
<ClInclude Include="ql\pricingengines\vanilla\fdblackscholesvanillaengine.hpp" />
<ClInclude Include="ql\pricingengines\vanilla\fdcevvanillaengine.hpp" />
<ClInclude Include="ql\pricingengines\vanilla\fdconditions.hpp" />
Expand Down Expand Up @@ -2367,6 +2369,7 @@
<ClCompile Include="ql\methods\finitedifferences\utilities\fdminnervaluecalculator.cpp" />
<ClCompile Include="ql\methods\finitedifferences\utilities\fdmmesherintegral.cpp" />
<ClCompile Include="ql\methods\finitedifferences\utilities\fdmquantohelper.cpp" />
<ClCompile Include="ql\methods\finitedifferences\utilities\fdmshoutloginnervaluecalculator.cpp" />
<ClCompile Include="ql\methods\finitedifferences\utilities\fdmtimedepdirichletboundary.cpp" />
<ClCompile Include="ql\methods\finitedifferences\utilities\gbsmrndcalculator.cpp" />
<ClCompile Include="ql\methods\finitedifferences\utilities\hestonrndcalculator.cpp" />
Expand Down Expand Up @@ -2585,6 +2588,7 @@
<ClCompile Include="ql\pricingengines\vanilla\discretizedvanillaoption.cpp" />
<ClCompile Include="ql\pricingengines\vanilla\exponentialfittinghestonengine.cpp" />
<ClCompile Include="ql\pricingengines\vanilla\fdbatesvanillaengine.cpp" />
<ClCompile Include="ql\pricingengines\vanilla\fdblackscholesshoutengine.cpp" />
<ClCompile Include="ql\pricingengines\vanilla\fdblackscholesvanillaengine.cpp" />
<ClCompile Include="ql\pricingengines\vanilla\fdcevvanillaengine.cpp" />
<ClCompile Include="ql\pricingengines\vanilla\fdhestonhullwhitevanillaengine.cpp" />
Expand Down Expand Up @@ -2781,4 +2785,4 @@
<Import Project=".\Build.props" Condition="Exists('.\Build.props')" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
14 changes: 13 additions & 1 deletion QuantLib.vcxproj.filters
Expand Up @@ -4364,6 +4364,12 @@
<ClInclude Include="ql\experimental\termstructures\crosscurrencyratehelpers.hpp">
<Filter>experimental\termstructures</Filter>
</ClInclude>
<ClInclude Include="ql\pricingengines\vanilla\fdblackscholesshoutengine.hpp">
<Filter>pricingengines\vanilla</Filter>
</ClInclude>
<ClInclude Include="ql\methods\finitedifferences\utilities\fdmshoutloginnervaluecalculator.hpp">
<Filter>methods\finitedifferences\utilities</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ql\methods\montecarlo\brownianbridge.cpp">
Expand Down Expand Up @@ -7054,5 +7060,11 @@
<ClCompile Include="ql\experimental\asian\analytic_discr_geom_av_price_heston.cpp">
<Filter>experimental\asian</Filter>
</ClCompile>
<ClCompile Include="ql\pricingengines\vanilla\fdblackscholesshoutengine.cpp">
<Filter>pricingengines\vanilla</Filter>
</ClCompile>
<ClCompile Include="ql\methods\finitedifferences\utilities\fdmshoutloginnervaluecalculator.cpp">
<Filter>methods\finitedifferences\utilities</Filter>
</ClCompile>
</ItemGroup>
</Project>
</Project>
4 changes: 4 additions & 0 deletions ql/CMakeLists.txt
Expand Up @@ -509,6 +509,7 @@ set(QuantLib_SRC
methods/finitedifferences/utilities/fdmdividendhandler.cpp
methods/finitedifferences/utilities/fdmindicesonboundary.cpp
methods/finitedifferences/utilities/fdminnervaluecalculator.cpp
methods/finitedifferences/utilities/fdmshoutloginnervaluecalculator.cpp
methods/finitedifferences/utilities/fdmmesherintegral.cpp
methods/finitedifferences/utilities/fdmquantohelper.cpp
methods/finitedifferences/utilities/fdmtimedepdirichletboundary.cpp
Expand Down Expand Up @@ -733,6 +734,7 @@ set(QuantLib_SRC
pricingengines/vanilla/exponentialfittinghestonengine.cpp
pricingengines/vanilla/fdbatesvanillaengine.cpp
pricingengines/vanilla/fdblackscholesvanillaengine.cpp
pricingengines/vanilla/fdblackscholesshoutengine.cpp
pricingengines/vanilla/fdcirvanillaengine.cpp
pricingengines/vanilla/fdcevvanillaengine.cpp
pricingengines/vanilla/fdhestonhullwhitevanillaengine.cpp
Expand Down Expand Up @@ -1694,6 +1696,7 @@ set(QuantLib_HDR
methods/finitedifferences/utilities/fdmdividendhandler.hpp
methods/finitedifferences/utilities/fdmindicesonboundary.hpp
methods/finitedifferences/utilities/fdminnervaluecalculator.hpp
methods/finitedifferences/utilities/fdmshoutloginnervaluecalculator.hpp
methods/finitedifferences/utilities/fdmmesherintegral.hpp
methods/finitedifferences/utilities/fdmquantohelper.hpp
methods/finitedifferences/utilities/fdmtimedepdirichletboundary.hpp
Expand Down Expand Up @@ -2015,6 +2018,7 @@ set(QuantLib_HDR
pricingengines/vanilla/exponentialfittinghestonengine.hpp
pricingengines/vanilla/fdbatesvanillaengine.hpp
pricingengines/vanilla/fdblackscholesvanillaengine.hpp
pricingengines/vanilla/fdblackscholesshoutengine.hpp
pricingengines/vanilla/fdcirvanillaengine.hpp
pricingengines/vanilla/fdcevvanillaengine.hpp
pricingengines/vanilla/fdconditions.hpp
Expand Down
2 changes: 2 additions & 0 deletions ql/methods/finitedifferences/utilities/Makefile.am
Expand Up @@ -14,6 +14,7 @@ this_include_HEADERS = \
fdmdividendhandler.hpp \
fdmindicesonboundary.hpp \
fdminnervaluecalculator.hpp \
fdmshoutloginnervaluecalculator.hpp \
fdmmesherintegral.hpp \
fdmquantohelper.hpp \
fdmtimedepdirichletboundary.hpp \
Expand All @@ -33,6 +34,7 @@ cpp_files = \
fdmdividendhandler.cpp \
fdmindicesonboundary.cpp \
fdminnervaluecalculator.cpp \
fdmshoutloginnervaluecalculator.cpp \
fdmmesherintegral.cpp \
fdmquantohelper.cpp \
fdmtimedepdirichletboundary.cpp \
Expand Down
1 change: 1 addition & 0 deletions ql/methods/finitedifferences/utilities/all.hpp
Expand Up @@ -11,6 +11,7 @@
#include <ql/methods/finitedifferences/utilities/fdmdividendhandler.hpp>
#include <ql/methods/finitedifferences/utilities/fdmindicesonboundary.hpp>
#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
#include <ql/methods/finitedifferences/utilities/fdmshoutloginnervaluecalculator.hpp>
#include <ql/methods/finitedifferences/utilities/fdmmesherintegral.hpp>
#include <ql/methods/finitedifferences/utilities/fdmquantohelper.hpp>
#include <ql/methods/finitedifferences/utilities/fdmtimedepdirichletboundary.hpp>
Expand Down
@@ -0,0 +1,75 @@
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
Copyright (C) 2021 Klaus Spanderen
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/

/*! \file fdmshoutinnervaluecalculator.cpp
\brief inner value for a shout option
*/

#include <ql/instruments/payoffs.hpp>
#include <ql/pricingengines/blackformula.hpp>
#include <ql/processes/blackscholesprocess.hpp>
#include <ql/methods/finitedifferences/meshers/fdmmesher.hpp>
#include <ql/methods/finitedifferences/utilities/fdmshoutloginnervaluecalculator.hpp>

namespace QuantLib {

FdmShoutLogInnerValueCalculator::FdmShoutLogInnerValueCalculator(
const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
Time maturity,
const ext::shared_ptr<PlainVanillaPayoff>& payoff,
const ext::shared_ptr<FdmMesher>& mesher,
Size direction)
: process_(process),
maturity_(maturity),
payoff_(payoff),
mesher_(mesher),
direction_(direction) { }


Real FdmShoutLogInnerValueCalculator::innerValue(
const FdmLinearOpIterator& iter, Time t) {

const Real s_t = std::exp(mesher_->location(iter, direction_));

const DiscountFactor qf =
process_->dividendYield()->discount(maturity_)/
process_->dividendYield()->discount(t);

const DiscountFactor df =
process_->riskFreeRate()->discount(maturity_)/
process_->riskFreeRate()->discount(t);

const Real fwd = s_t*qf/df;
const Volatility stdDev = process_->blackVolatility()->blackForwardVol(
t, maturity_, s_t)*std::sqrt(maturity_-t);

const Real npv = blackFormula(
payoff_->optionType(), s_t, fwd, stdDev, df);

const Real intrinsic = (payoff_->optionType() == Option::Call)
? s_t - payoff_->strike() : payoff_->strike() - s_t;

return std::max(0.0, npv + intrinsic*df);
}

Real FdmShoutLogInnerValueCalculator::avgInnerValue(
const FdmLinearOpIterator& iter, Time t) {
return innerValue(iter, t);
}
}
@@ -0,0 +1,55 @@
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
Copyright (C) 2021 Klaus Spanderen
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/

/*! \file fdmshoutloginnervaluecalculator.hpp
\brief inner value for a shout option
*/

#ifndef quantlib_fdm_shout_log_inner_value_calculator_hpp
#define quantlib_fdm_shout_log_inner_value_calculator_hpp

#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>

namespace QuantLib {

class PlainVanillaPayoff;
class GeneralizedBlackScholesProcess;

class FdmShoutLogInnerValueCalculator: public FdmInnerValueCalculator {
public:
FdmShoutLogInnerValueCalculator(
const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
Time maturity,
const ext::shared_ptr<PlainVanillaPayoff>& payoff,
const ext::shared_ptr<FdmMesher>& mesher,
Size direction);

Real innerValue(const FdmLinearOpIterator& iter, Time t) override;
Real avgInnerValue(const FdmLinearOpIterator& iter, Time t) override;

private:
const ext::shared_ptr<GeneralizedBlackScholesProcess> process_;
const Time maturity_;
const ext::shared_ptr<PlainVanillaPayoff> payoff_;
const ext::shared_ptr<FdmMesher> mesher_;
const Size direction_;
};
}

#endif
2 changes: 2 additions & 0 deletions ql/pricingengines/vanilla/Makefile.am
Expand Up @@ -28,6 +28,7 @@ this_include_HEADERS = \
juquadraticengine.hpp \
fdbatesvanillaengine.hpp \
fdblackscholesvanillaengine.hpp \
fdblackscholesshoutengine.hpp \
fdcevvanillaengine.hpp \
fddividendengine.hpp \
fddividendshoutengine.hpp \
Expand Down Expand Up @@ -73,6 +74,7 @@ cpp_files = \
juquadraticengine.cpp \
fdbatesvanillaengine.cpp \
fdblackscholesvanillaengine.cpp \
fdblackscholesshoutengine.cpp \
fdcevvanillaengine.cpp \
fdhestonhullwhitevanillaengine.cpp \
fdhestonvanillaengine.cpp \
Expand Down
1 change: 1 addition & 0 deletions ql/pricingengines/vanilla/all.hpp
Expand Up @@ -25,6 +25,7 @@
#include <ql/pricingengines/vanilla/juquadraticengine.hpp>
#include <ql/pricingengines/vanilla/fdbatesvanillaengine.hpp>
#include <ql/pricingengines/vanilla/fdblackscholesvanillaengine.hpp>
#include <ql/pricingengines/vanilla/fdblackscholesshoutengine.hpp>
#include <ql/pricingengines/vanilla/fdcevvanillaengine.hpp>
#include <ql/pricingengines/vanilla/fddividendengine.hpp>
#include <ql/pricingengines/vanilla/fddividendshoutengine.hpp>
Expand Down
90 changes: 90 additions & 0 deletions ql/pricingengines/vanilla/fdblackscholesshoutengine.cpp
@@ -0,0 +1,90 @@
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
Copyright (C) 2021 Klaus Spanderen
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/

/*! \file fdblackscholesshoutengine.cpp
*/

#include <ql/exercise.hpp>
#include <ql/processes/blackscholesprocess.hpp>
#include <ql/methods/finitedifferences/solvers/fdmblackscholessolver.hpp>
#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
#include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp>
#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
#include <ql/methods/finitedifferences/utilities/fdmshoutloginnervaluecalculator.hpp>
#include <ql/pricingengines/vanilla/fdblackscholesshoutengine.hpp>

namespace QuantLib {

FdBlackScholesShoutEngine::FdBlackScholesShoutEngine(
const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
Size tGrid, Size xGrid, Size dampingSteps,
const FdmSchemeDesc& schemeDesc)
: process_(process),
tGrid_(tGrid),
xGrid_(xGrid),
dampingSteps_(dampingSteps),
schemeDesc_(schemeDesc) {

registerWith(process_);
}

void FdBlackScholesShoutEngine::calculate() const {
const Date exerciseDate = arguments_.exercise->lastDate();
const Time maturity = process_->time(exerciseDate);

const auto payoff =
ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff);

QL_REQUIRE(payoff, "non plain vanilla payoff given");

const auto mesher = ext::make_shared<FdmMesherComposite>(
ext::make_shared<FdmBlackScholesMesher>(
xGrid_, process_, maturity, payoff->strike(),
Null<Real>(), Null<Real>(), 0.0001, 1.5,
std::pair<Real, Real>(payoff->strike(), 0.1)));

const auto innerValuecalculator =
ext::make_shared<FdmShoutLogInnerValueCalculator>(
process_, maturity, payoff, mesher, 0);

const auto conditions =
FdmStepConditionComposite::vanillaComposite(
DividendSchedule(), arguments_.exercise, mesher,
innerValuecalculator,
process_->riskFreeRate()->referenceDate(),
process_->riskFreeRate()->dayCounter());

const FdmSolverDesc solverDesc = {
mesher, FdmBoundaryConditionSet(),
conditions, innerValuecalculator,
maturity, tGrid_, dampingSteps_ };

const auto solver =
ext::make_shared<FdmBlackScholesSolver>(
Handle<GeneralizedBlackScholesProcess>(process_),
payoff->strike(), solverDesc, schemeDesc_);

const Real spot = process_->x0();

results_.value = solver->valueAt(spot);
results_.delta = solver->deltaAt(spot);
results_.gamma = solver->gammaAt(spot);
results_.theta = solver->thetaAt(spot);
}
}

0 comments on commit 7cbc755

Please sign in to comment.