QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
discretizedcapfloor.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 Copyright (C) 2004, 2007 StatPro Italia srl
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/pricingengines/capfloor/discretizedcapfloor.hpp>
22
23namespace QuantLib {
24
26 const Date& referenceDate,
27 const DayCounter& dayCounter)
28 : arguments_(args) {
29
30 startTimes_.resize(args.startDates.size());
31 for (Size i=0; i<startTimes_.size(); ++i)
32 startTimes_[i] = dayCounter.yearFraction(referenceDate,
33 args.startDates[i]);
34
35 endTimes_.resize(args.endDates.size());
36 for (Size i=0; i<endTimes_.size(); ++i)
37 endTimes_[i] = dayCounter.yearFraction(referenceDate,
38 args.endDates[i]);
39 }
40
42 values_ = Array(size, 0.0);
44 }
45
46 std::vector<Time> DiscretizedCapFloor::mandatoryTimes() const {
47 std::vector<Time> times = startTimes_;
48 std::copy(endTimes_.begin(), endTimes_.end(),
49 std::back_inserter(times));
50 return times;
51 }
52
54 for (Size i=0; i<startTimes_.size(); i++) {
55 if (isOnTime(startTimes_[i])) {
56 Time end = endTimes_[i];
57 Time tenor = arguments_.accrualTimes[i];
59 bond.initialize(method(), end);
60 bond.rollback(time_);
61
63 Real gearing = arguments_.gearings[i];
64 Real nominal = arguments_.nominals[i];
65
66 if ( (type == CapFloor::Cap) ||
67 (type == CapFloor::Collar)) {
68 Real accrual = 1.0 + arguments_.capRates[i]*tenor;
69 Real strike = 1.0/accrual;
70 for (Size j=0; j<values_.size(); j++)
71 values_[j] += nominal*accrual*gearing*
72 std::max<Real>(strike - bond.values()[j], 0.0);
73 }
74
75 if ( (type == CapFloor::Floor) ||
76 (type == CapFloor::Collar)) {
77 Real accrual = 1.0 + arguments_.floorRates[i]*tenor;
78 Real strike = 1.0/accrual;
79 Real mult = (type == CapFloor::Floor)?1.0:-1.0;
80 for (Size j=0; j<values_.size(); j++)
81 values_[j] += nominal*accrual*mult*gearing*
82 std::max<Real>(bond.values()[j] - strike, 0.0);
83 }
84 }
85 }
86 }
87
89 for (Size i=0; i<endTimes_.size(); i++) {
90 if (isOnTime(endTimes_[i])) {
91 if (startTimes_[i] < 0.0) {
92 Real nominal = arguments_.nominals[i];
93 Time accrual = arguments_.accrualTimes[i];
94 Rate fixing = arguments_.forwards[i];
95 Real gearing = arguments_.gearings[i];
97
98 if (type == CapFloor::Cap || type == CapFloor::Collar) {
99 Rate cap = arguments_.capRates[i];
100 Rate capletRate = std::max(fixing-cap, 0.0);
101 values_ += capletRate*accrual*nominal*gearing;
102 }
103
104 if (type == CapFloor::Floor || type == CapFloor::Collar) {
105 Rate floor = arguments_.floorRates[i];
106 Rate floorletRate = std::max(floor-fixing, 0.0);
107 if (type == CapFloor::Floor)
108 values_ += floorletRate*accrual*nominal*gearing;
109 else
110 values_ -= floorletRate*accrual*nominal*gearing;
111 }
112 }
113 }
114 }
115 }
116
117}
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Arguments for cap/floor calculation
Definition: capfloor.hpp:138
std::vector< Time > accrualTimes
Definition: capfloor.hpp:145
std::vector< Rate > forwards
Definition: capfloor.hpp:148
std::vector< Date > startDates
Definition: capfloor.hpp:142
std::vector< Real > gearings
Definition: capfloor.hpp:149
std::vector< Rate > floorRates
Definition: capfloor.hpp:147
std::vector< Real > nominals
Definition: capfloor.hpp:151
std::vector< Date > endDates
Definition: capfloor.hpp:144
std::vector< Rate > capRates
Definition: capfloor.hpp:146
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
const Array & values() const
const ext::shared_ptr< Lattice > & method() const
void initialize(const ext::shared_ptr< Lattice > &, Time t)
std::vector< Time > mandatoryTimes() const override
DiscretizedCapFloor(const CapFloor::arguments &args, const Date &referenceDate, const DayCounter &dayCounter)
void reset(Size size) override
Useful discretized discount bond asset.
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35