QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
treecallablebondengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Allen Kuo
5 Copyright (C) 2021 Ralf Konrad Eckel
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
21#include <ql/experimental/callablebonds/discretizedcallablefixedratebond.hpp>
22#include <ql/experimental/callablebonds/treecallablebondengine.hpp>
23#include <ql/models/shortrate/onefactormodel.hpp>
24#include <utility>
25
26namespace QuantLib {
27
29 const ext::shared_ptr<ShortRateModel>& model,
30 const Size timeSteps,
31 Handle<YieldTermStructure> termStructure)
33 termStructure_(std::move(termStructure)) {
35 }
36
38 const ext::shared_ptr<ShortRateModel>& model,
39 const TimeGrid& timeGrid,
40 Handle<YieldTermStructure> termStructure)
42 termStructure_(std::move(termStructure)) {
44 }
45
47 return calculateWithSpread(arguments_.spread);
48 }
49
51 QL_REQUIRE(!model_.empty(), "no model specified");
52
53 ext::shared_ptr<TermStructureConsistentModel> tsmodel =
54 ext::dynamic_pointer_cast<TermStructureConsistentModel>(*model_);
55 Handle<YieldTermStructure> discountCurve =
56 tsmodel != nullptr ? tsmodel->termStructure() : termStructure_;
57
58 DiscretizedCallableFixedRateBond callableBond(arguments_, discountCurve);
59 ext::shared_ptr<Lattice> lattice;
60
61 if (lattice_ != nullptr) {
62 lattice = lattice_;
63 } else {
64 std::vector<Time> times = callableBond.mandatoryTimes();
65 TimeGrid timeGrid(times.begin(), times.end(), timeSteps_);
66 lattice = model_->tree(timeGrid);
67 }
68
69 if (s != 0.0) {
70 auto* sr = dynamic_cast<OneFactorModel::ShortRateTree*>(&(*lattice));
71 QL_REQUIRE(sr,
72 "Spread is not supported for trees other than OneFactorModel");
73 sr->setSpread(s);
74 }
75
76 auto referenceDate = discountCurve->referenceDate();
77 auto dayCounter = discountCurve->dayCounter();
78 Time redemptionTime = dayCounter.yearFraction(referenceDate, arguments_.redemptionDate);
79
80 callableBond.initialize(lattice, redemptionTime);
81 callableBond.rollback(0.0);
82
83 results_.value = callableBond.presentValue();
84
85 DiscountFactor d = discountCurve->discount(arguments_.settlementDate);
86 results_.settlementValue = results_.value / d;
87 }
88
89}
90
Callable bond base class.
void initialize(const ext::shared_ptr< Lattice > &, Time t)
Shared handle to an observable.
Definition: handle.hpp:41
bool empty() const
checks if the contained shared pointer points to anything
Definition: handle.hpp:166
Engine for a short-rate model specialized on a lattice.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Recombining trinomial tree discretizing the state variable.
time grid class
Definition: timegrid.hpp:43
TreeCallableFixedRateBondEngine(const ext::shared_ptr< ShortRateModel > &, Size timeSteps, Handle< YieldTermStructure > termStructure=Handle< YieldTermStructure >())
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Spread
spreads on interest rates
Definition: types.hpp:74
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.