Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added shout option engine in new-style FD framework
- Loading branch information
Showing
16 changed files
with
369 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
ql/methods/finitedifferences/utilities/fdmshoutloginnervaluecalculator.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
ql/methods/finitedifferences/utilities/fdmshoutloginnervaluecalculator.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.