QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
onefactormodel.cpp
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
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20#include <ql/math/solvers1d/brent.hpp>
21#include <ql/models/shortrate/onefactormodel.hpp>
22#include <ql/stochasticprocess.hpp>
23#include <utility>
24
25namespace QuantLib {
26
27 //Private function used by solver to determine time-dependent parameter
28 class OneFactorModel::ShortRateTree::Helper {
29 public:
30 Helper(Size i,
31 Real discountBondPrice,
32 ext::shared_ptr<TermStructureFittingParameter::NumericalImpl> theta,
33 ShortRateTree& tree)
34 : size_(tree.size(i)), i_(i), statePrices_(tree.statePrices(i)),
35 discountBondPrice_(discountBondPrice), theta_(std::move(theta)), tree_(tree) {
36 theta_->set(tree.timeGrid()[i], 0.0);
37 }
38
39 Real operator()(Real theta) const {
40 Real value = discountBondPrice_;
41 theta_->change(theta);
42 for (Size j=0; j<size_; j++)
43 value -= statePrices_[j]*tree_.discount(i_,j);
44 return value;
45 }
46
47 private:
48 Size size_;
49 Size i_;
50 const Array& statePrices_;
51 Real discountBondPrice_;
52 ext::shared_ptr<TermStructureFittingParameter::NumericalImpl> theta_;
53 ShortRateTree& tree_;
54 };
55
57 const ext::shared_ptr<TrinomialTree>& tree,
58 ext::shared_ptr<ShortRateDynamics> dynamics,
59 const ext::shared_ptr<TermStructureFittingParameter::NumericalImpl>& theta,
60 const TimeGrid& timeGrid)
61 : TreeLattice1D<OneFactorModel::ShortRateTree>(timeGrid, tree->size(1)), tree_(tree),
62 dynamics_(std::move(dynamics)), spread_(0.0) {
63
64 theta->reset();
65 Real value = 1.0;
66 Real vMin = -100.0;
67 Real vMax = 100.0;
68 for (Size i=0; i<(timeGrid.size() - 1); i++) {
69 Real discountBond = theta->termStructure()->discount(t_[i+1]);
70 Helper finder(i, discountBond, theta, *this);
71 Brent s1d;
72 s1d.setMaxEvaluations(1000);
73 value = s1d.solve(finder, 1e-7, value, vMin, vMax);
74 // vMin = value - 1.0;
75 // vMax = value + 1.0;
76 theta->change(value);
77 }
78 }
79
80 OneFactorModel::ShortRateTree::ShortRateTree(const ext::shared_ptr<TrinomialTree>& tree,
81 ext::shared_ptr<ShortRateDynamics> dynamics,
82 const TimeGrid& timeGrid)
83 : TreeLattice1D<OneFactorModel::ShortRateTree>(timeGrid, tree->size(1)), tree_(tree),
84 dynamics_(std::move(dynamics)), spread_(0.0) {}
85
87 : ShortRateModel(nArguments) {}
88
89 ext::shared_ptr<Lattice>
90 OneFactorModel::tree(const TimeGrid& grid) const {
91 ext::shared_ptr<TrinomialTree> trinomial(
92 new TrinomialTree(dynamics()->process(), grid));
93 return ext::shared_ptr<Lattice>(
94 new ShortRateTree(trinomial, dynamics(), grid));
95 }
96
98 Real x0 = dynamics()->process()->x0();
99 Rate r0 = dynamics()->shortRate(0.0, x0);
100 return discountBond(0.0, t, r0);
101 }
102
103}
104
Brent 1-D solver
Definition: brent.hpp:37
Real value(const Array &params, const std::vector< ext::shared_ptr< CalibrationHelper > > &)
Definition: model.cpp:117
const TimeGrid & timeGrid() const
Real discountBond(Time now, Time maturity, Array factors) const override
DiscountFactor discount(Time t) const override
Implied discount curve.
Recombining trinomial tree discretizing the state variable.
ShortRateTree(const ext::shared_ptr< TrinomialTree > &tree, ext::shared_ptr< ShortRateDynamics > dynamics, const TimeGrid &timeGrid)
Plain tree build-up from short-rate dynamics.
Single-factor short-rate model abstract class.
OneFactorModel(Size nArguments)
ext::shared_ptr< Lattice > tree(const TimeGrid &grid) const override
Return by default a trinomial recombining tree.
virtual ext::shared_ptr< ShortRateDynamics > dynamics() const =0
returns the short-rate dynamics
Abstract short-rate model class.
Definition: model.hpp:141
void setMaxEvaluations(Size evaluations)
Definition: solver1d.hpp:238
Real solve(const F &f, Real accuracy, Real guess, Real step) const
Definition: solver1d.hpp:84
time grid class
Definition: timegrid.hpp:43
Size size() const
Definition: timegrid.hpp:164
One-dimensional tree-based lattice.
Definition: lattice1d.hpp:39
const Array & statePrices(Size i) const
Definition: lattice.hpp:114
Recombining trinomial tree class.
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 Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.