QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
treecapfloorengine.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/models/shortrate/onefactormodel.hpp>
21#include <ql/pricingengines/capfloor/discretizedcapfloor.hpp>
22#include <ql/pricingengines/capfloor/treecapfloorengine.hpp>
23#include <utility>
24
25namespace QuantLib {
26
27 TreeCapFloorEngine::TreeCapFloorEngine(const ext::shared_ptr<ShortRateModel>& model,
28 Size timeSteps,
29 Handle<YieldTermStructure> termStructure)
31 termStructure_(std::move(termStructure)) {
33 }
34
35 TreeCapFloorEngine::TreeCapFloorEngine(const ext::shared_ptr<ShortRateModel>& model,
36 const TimeGrid& timeGrid,
37 Handle<YieldTermStructure> termStructure)
39 termStructure_(std::move(termStructure)) {
41 }
42
44
45 QL_REQUIRE(!model_.empty(), "no model specified");
46
47 Date referenceDate;
48 DayCounter dayCounter;
49
50 ext::shared_ptr<TermStructureConsistentModel> tsmodel =
51 ext::dynamic_pointer_cast<TermStructureConsistentModel>(*model_);
52 if (tsmodel != nullptr) {
53 referenceDate = tsmodel->termStructure()->referenceDate();
54 dayCounter = tsmodel->termStructure()->dayCounter();
55 } else {
56 referenceDate = termStructure_->referenceDate();
57 dayCounter = termStructure_->dayCounter();
58 }
59
60 DiscretizedCapFloor capfloor(arguments_, referenceDate, dayCounter);
61 ext::shared_ptr<Lattice> lattice;
62
63 if (lattice_ != nullptr) {
64 lattice = lattice_;
65 } else {
66 std::vector<Time> times = capfloor.mandatoryTimes();
67 TimeGrid timeGrid(times.begin(), times.end(), timeSteps_);
68 lattice = model_->tree(timeGrid);
69 }
70
71 Time firstTime = dayCounter.yearFraction(referenceDate,
72 arguments_.startDates.front());
73 Time lastTime = dayCounter.yearFraction(referenceDate,
74 arguments_.endDates.back());
75 capfloor.initialize(lattice, lastTime);
76 capfloor.rollback(firstTime);
77
78 results_.value = capfloor.presentValue();
79 }
80
81}
82
83
Base class for cap-like instruments.
Definition: capfloor.hpp:55
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
void initialize(const ext::shared_ptr< Lattice > &, Time t)
std::vector< Time > mandatoryTimes() const override
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
time grid class
Definition: timegrid.hpp:43
void calculate() const override
TreeCapFloorEngine(const ext::shared_ptr< ShortRateModel > &model, Size timeSteps, Handle< YieldTermStructure > termStructure=Handle< YieldTermStructure >())
Handle< YieldTermStructure > termStructure_
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.