/src/quantlib/ql/models/shortrate/onefactormodel.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
25 | | namespace 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 | 0 | : size_(tree.size(i)), i_(i), statePrices_(tree.statePrices(i)), |
35 | 0 | discountBondPrice_(discountBondPrice), theta_(std::move(theta)), tree_(tree) { |
36 | 0 | theta_->set(tree.timeGrid()[i], 0.0); |
37 | 0 | } |
38 | | |
39 | 0 | Real operator()(Real theta) const { |
40 | 0 | Real value = discountBondPrice_; |
41 | 0 | theta_->change(theta); |
42 | 0 | for (Size j=0; j<size_; j++) |
43 | 0 | value -= statePrices_[j]*tree_.discount(i_,j); |
44 | 0 | return value; |
45 | 0 | } |
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 | | |
56 | | OneFactorModel::ShortRateTree::ShortRateTree( |
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 | 0 | : TreeLattice1D<OneFactorModel::ShortRateTree>(timeGrid, tree->size(1)), tree_(tree), |
62 | 0 | dynamics_(std::move(dynamics)), spread_(0.0) { |
63 | |
|
64 | 0 | theta->reset(); |
65 | 0 | Real value = 1.0; |
66 | 0 | Real vMin = -100.0; |
67 | 0 | Real vMax = 100.0; |
68 | 0 | for (Size i=0; i<(timeGrid.size() - 1); i++) { |
69 | 0 | Real discountBond = theta->termStructure()->discount(t_[i+1]); |
70 | 0 | Helper finder(i, discountBond, theta, *this); |
71 | 0 | Brent s1d; |
72 | 0 | s1d.setMaxEvaluations(1000); |
73 | 0 | value = s1d.solve(finder, 1e-7, value, vMin, vMax); |
74 | | // vMin = value - 1.0; |
75 | | // vMax = value + 1.0; |
76 | 0 | theta->change(value); |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | | OneFactorModel::ShortRateTree::ShortRateTree(const ext::shared_ptr<TrinomialTree>& tree, |
81 | | ext::shared_ptr<ShortRateDynamics> dynamics, |
82 | | const TimeGrid& timeGrid) |
83 | 0 | : TreeLattice1D<OneFactorModel::ShortRateTree>(timeGrid, tree->size(1)), tree_(tree), |
84 | 0 | dynamics_(std::move(dynamics)), spread_(0.0) {} |
85 | | |
86 | | OneFactorModel::OneFactorModel(Size nArguments) |
87 | 0 | : ShortRateModel(nArguments) {} |
88 | | |
89 | | ext::shared_ptr<Lattice> |
90 | 0 | OneFactorModel::tree(const TimeGrid& grid) const { |
91 | 0 | ext::shared_ptr<TrinomialTree> trinomial( |
92 | 0 | new TrinomialTree(dynamics()->process(), grid)); |
93 | 0 | return ext::shared_ptr<Lattice>( |
94 | 0 | new ShortRateTree(trinomial, dynamics(), grid)); |
95 | 0 | } |
96 | | |
97 | 0 | DiscountFactor OneFactorAffineModel::discount(Time t) const { |
98 | 0 | Real x0 = dynamics()->process()->x0(); |
99 | 0 | Rate r0 = dynamics()->shortRate(0.0, x0); |
100 | 0 | return discountBond(0.0, t, r0); |
101 | 0 | } |
102 | | |
103 | | } |
104 | | |