/src/quantlib/ql/pricingengines/capfloor/discretizedcapfloor.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 | | 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 | | |
23 | | namespace QuantLib { |
24 | | |
25 | | DiscretizedCapFloor::DiscretizedCapFloor(const CapFloor::arguments& args, |
26 | | const Date& referenceDate, |
27 | | const DayCounter& dayCounter) |
28 | 0 | : arguments_(args) { |
29 | |
|
30 | 0 | startTimes_.resize(args.startDates.size()); |
31 | 0 | for (Size i=0; i<startTimes_.size(); ++i) |
32 | 0 | startTimes_[i] = dayCounter.yearFraction(referenceDate, |
33 | 0 | args.startDates[i]); |
34 | |
|
35 | 0 | endTimes_.resize(args.endDates.size()); |
36 | 0 | for (Size i=0; i<endTimes_.size(); ++i) |
37 | 0 | endTimes_[i] = dayCounter.yearFraction(referenceDate, |
38 | 0 | args.endDates[i]); |
39 | 0 | } |
40 | | |
41 | 0 | void DiscretizedCapFloor::reset(Size size) { |
42 | 0 | values_ = Array(size, 0.0); |
43 | 0 | adjustValues(); |
44 | 0 | } |
45 | | |
46 | 0 | std::vector<Time> DiscretizedCapFloor::mandatoryTimes() const { |
47 | 0 | std::vector<Time> times = startTimes_; |
48 | 0 | std::copy(endTimes_.begin(), endTimes_.end(), |
49 | 0 | std::back_inserter(times)); |
50 | 0 | return times; |
51 | 0 | } |
52 | | |
53 | 0 | void DiscretizedCapFloor::preAdjustValuesImpl() { |
54 | 0 | for (Size i=0; i<startTimes_.size(); i++) { |
55 | 0 | if (isOnTime(startTimes_[i])) { |
56 | 0 | Time end = endTimes_[i]; |
57 | 0 | Time tenor = arguments_.accrualTimes[i]; |
58 | 0 | DiscretizedDiscountBond bond; |
59 | 0 | bond.initialize(method(), end); |
60 | 0 | bond.rollback(time_); |
61 | |
|
62 | 0 | CapFloor::Type type = arguments_.type; |
63 | 0 | Real gearing = arguments_.gearings[i]; |
64 | 0 | Real nominal = arguments_.nominals[i]; |
65 | |
|
66 | 0 | if ( (type == CapFloor::Cap) || |
67 | 0 | (type == CapFloor::Collar)) { |
68 | 0 | Real accrual = 1.0 + arguments_.capRates[i]*tenor; |
69 | 0 | Real strike = 1.0/accrual; |
70 | 0 | for (Size j=0; j<values_.size(); j++) |
71 | 0 | values_[j] += nominal*accrual*gearing* |
72 | 0 | std::max<Real>(strike - bond.values()[j], 0.0); |
73 | 0 | } |
74 | |
|
75 | 0 | if ( (type == CapFloor::Floor) || |
76 | 0 | (type == CapFloor::Collar)) { |
77 | 0 | Real accrual = 1.0 + arguments_.floorRates[i]*tenor; |
78 | 0 | Real strike = 1.0/accrual; |
79 | 0 | Real mult = (type == CapFloor::Floor)?1.0:-1.0; |
80 | 0 | for (Size j=0; j<values_.size(); j++) |
81 | 0 | values_[j] += nominal*accrual*mult*gearing* |
82 | 0 | std::max<Real>(bond.values()[j] - strike, 0.0); |
83 | 0 | } |
84 | 0 | } |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | 0 | void DiscretizedCapFloor::postAdjustValuesImpl() { |
89 | 0 | for (Size i=0; i<endTimes_.size(); i++) { |
90 | 0 | if (isOnTime(endTimes_[i])) { |
91 | 0 | if (startTimes_[i] < 0.0) { |
92 | 0 | Real nominal = arguments_.nominals[i]; |
93 | 0 | Time accrual = arguments_.accrualTimes[i]; |
94 | 0 | Rate fixing = arguments_.forwards[i]; |
95 | 0 | Real gearing = arguments_.gearings[i]; |
96 | 0 | CapFloor::Type type = arguments_.type; |
97 | |
|
98 | 0 | if (type == CapFloor::Cap || type == CapFloor::Collar) { |
99 | 0 | Rate cap = arguments_.capRates[i]; |
100 | 0 | Rate capletRate = std::max(fixing-cap, 0.0); |
101 | 0 | values_ += capletRate*accrual*nominal*gearing; |
102 | 0 | } |
103 | |
|
104 | 0 | if (type == CapFloor::Floor || type == CapFloor::Collar) { |
105 | 0 | Rate floor = arguments_.floorRates[i]; |
106 | 0 | Rate floorletRate = std::max(floor-fixing, 0.0); |
107 | 0 | if (type == CapFloor::Floor) |
108 | 0 | values_ += floorletRate*accrual*nominal*gearing; |
109 | 0 | else |
110 | 0 | values_ -= floorletRate*accrual*nominal*gearing; |
111 | 0 | } |
112 | 0 | } |
113 | 0 | } |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | | } |