QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
extendedcoxingersollross.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) 2001, 2002, 2003 Sadruddin Rejeb
5 Copyright (C) 2021 Magnus Mencke
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
24
25namespace QuantLib {
26
28 const Handle<YieldTermStructure>& termStructure,
29 Real theta, Real k, Real sigma, Real x0,
30 bool withFellerConstraint)
31 : CoxIngersollRoss(x0, theta, k, sigma, withFellerConstraint),
32 TermStructureConsistentModel(termStructure){
34 }
35
36 ext::shared_ptr<Lattice> ExtendedCoxIngersollRoss::tree(
37 const TimeGrid& grid) const {
39 ext::shared_ptr<Dynamics> numericDynamics(
40 new Dynamics(phi, theta(), k(), sigma(), x0()));
41
42 ext::shared_ptr<TrinomialTree> trinomial(
43 new TrinomialTree(numericDynamics->process(), grid, true));
44
46 ext::shared_ptr<NumericalImpl> impl =
47 ext::dynamic_pointer_cast<NumericalImpl>(phi.implementation());
48
49 return ext::shared_ptr<Lattice>(
50 new ShortRateTree(trinomial, numericDynamics, impl, grid));
51 }
52
54 Real pt = termStructure()->discount(t);
55 Real ps = termStructure()->discount(s);
56 Real value = CoxIngersollRoss::A(t,s)*std::exp(B(t,s)*phi_(t))*
57 (ps*CoxIngersollRoss::A(0.0,t)*std::exp(-B(0.0,t)*x0()))/
58 (pt*CoxIngersollRoss::A(0.0,s)*std::exp(-B(0.0,s)*x0()));
59 return value;
60 }
61
63 Real strike,
64 Time t, Time s) const {
65
66 QL_REQUIRE(strike>0.0, "strike must be positive");
67
68 DiscountFactor discountT = termStructure()->discount(t);
69 DiscountFactor discountS = termStructure()->discount(s);
70 if (t < QL_EPSILON) {
71 switch(type) {
72 case Option::Call:
73 return std::max<Real>(discountS - strike, 0.0);
74 case Option::Put:
75 return std::max<Real>(strike - discountS, 0.0);
76 default: QL_FAIL("unsupported option type");
77 }
78 }
79
80 Real sigma2 = sigma()*sigma();
81 Real h = std::sqrt(k()*k() + 2.0*sigma2);
82 Real r0 = termStructure()->forwardRate(0.0, 0.0,
84 Real b = B(t,s);
85
86 Real rho = 2.0*h/(sigma2*(std::exp(h*t) - 1.0));
87 Real psi = (k() + h)/sigma2;
88
89 Real df = 4.0*k()*theta()/sigma2;
90 Real ncps = 2.0*rho*rho*(r0-phi_(0.0))*std::exp(h*t)/(rho+psi+b);
91 Real ncpt = 2.0*rho*rho*(r0-phi_(0.0))*std::exp(h*t)/(rho+psi);
92
95
96 Real discountShift = (discountT*CoxIngersollRoss::A(0.0,s)*std::exp(-B(0.0,s)*x0()))/
97 (discountS*CoxIngersollRoss::A(0.0,t)*std::exp(-B(0.0,t)*x0()));
98
99 Real z = (std::log(CoxIngersollRoss::A(t,s)/strike)-std::log(discountShift))/b;
100 Real call = discountS*chis(2.0*z*(rho+psi+b)) -
101 strike*discountT*chit(2.0*z*(rho+psi));
102 if (type == Option::Call)
103 return call;
104 else
105 return call - discountS + strike*discountT;
106 }
107
108}
Chi-square (central and non-central) distributions.
Real value(const Array &params, const std::vector< ext::shared_ptr< CalibrationHelper > > &)
Definition: model.cpp:117
Cox-Ingersoll-Ross model class.
Real B(Time t, Time T) const override
Real A(Time t, Time T) const override
Short-rate dynamics in the extended Cox-Ingersoll-Ross model.
Real discountBondOption(Option::Type type, Real strike, Time maturity, Time bondMaturity) const override
ext::shared_ptr< Lattice > tree(const TimeGrid &grid) const override
Return by default a trinomial recombining tree.
ExtendedCoxIngersollRoss(const Handle< YieldTermStructure > &termStructure, Real theta=0.1, Real k=0.1, Real sigma=0.1, Real x0=0.05, bool withFellerConstraint=true)
Real A(Time t, Time T) const override
Shared handle to an observable.
Definition: handle.hpp:41
Recombining trinomial tree discretizing the state variable.
const ext::shared_ptr< Impl > & implementation() const
Definition: parameter.hpp:59
Term-structure consistent model class.
Definition: model.hpp:73
const Handle< YieldTermStructure > & termStructure() const
Definition: model.hpp:77
Deterministic time-dependent parameter used for yield-curve fitting.
Definition: parameter.hpp:145
time grid class
Definition: timegrid.hpp:43
Recombining trinomial tree class.
const DefaultType & t
#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
Extended Cox-Ingersoll-Ross model.
ext::function< Real(Real)> b
@ NoFrequency
null frequency
Definition: frequency.hpp:37
#define QL_EPSILON
Definition: qldefines.hpp:178
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real theta
Real rho
Real sigma
Definition: any.hpp:35
Trinomial tree class.