QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
discretizedconvertible.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2005, 2006 Theo Boafo
5 Copyright (C) 2006, 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
24#include <utility>
25
26namespace QuantLib {
27
30 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
31 DividendSchedule dividends,
32 Handle<Quote> creditSpread,
33 const TimeGrid& grid)
34 : arguments_(std::move(args)), process_(std::move(process)),
35 creditSpread_(std::move(creditSpread)) {
36
37 for (const auto& dividend : dividends) {
38 if (!dividend->hasOccurred(arguments_.settlementDate, false)) {
39 dividends_.push_back(dividend);
40 dividendDates_.push_back(dividend->date());
41 }
42 }
43
44 dividendValues_ = Array(dividends_.size(), 0.0);
45
46 Date settlementDate = process_->riskFreeRate()->referenceDate();
47 for (Size i=0; i<dividends.size(); i++) {
48 if (dividends[i]->date() >= settlementDate) {
50 dividends[i]->amount() *
51 process_->riskFreeRate()->discount(
52 dividends[i]->date());
53 }
54 }
55
56 DayCounter dayCounter = process_->riskFreeRate()->dayCounter();
57 Date bondSettlement = arguments_.settlementDate;
58
59 stoppingTimes_.resize(arguments_.exercise->dates().size());
60 for (Size i=0; i<stoppingTimes_.size(); ++i)
62 dayCounter.yearFraction(bondSettlement,
63 arguments_.exercise->date(i));
64
66 for (Size i=0; i<callabilityTimes_.size(); ++i)
68 dayCounter.yearFraction(bondSettlement,
70
71 couponTimes_.clear();
72 couponAmounts_.clear();
73 for (Size i = 0; i < arguments_.cashflows.size() - 1; ++i) {
74 if (!arguments_.cashflows[i]->hasOccurred(bondSettlement, false)) {
75 couponTimes_.push_back(dayCounter.yearFraction(bondSettlement,
76 arguments_.cashflows[i]->date()));
77 couponAmounts_.push_back(arguments_.cashflows[i]->amount());
78 }
79 }
80
81 dividendTimes_.resize(dividendDates_.size());
82 for (Size i=0; i<dividendTimes_.size(); ++i)
84 dayCounter.yearFraction(bondSettlement,
86
87 if (!grid.empty()) {
88 // adjust times to grid
89 for (Real& stoppingTime : stoppingTimes_)
90 stoppingTime = grid.closestTime(stoppingTime);
91 for (Real& couponTime : couponTimes_)
92 couponTime = grid.closestTime(couponTime);
93 for (Real& callabilityTime : callabilityTimes_)
94 callabilityTime = grid.closestTime(callabilityTime);
95 for (Real& dividendTime : dividendTimes_)
96 dividendTime = grid.closestTime(dividendTime);
97 }
98 }
99
101
102 // Set to bond redemption values
104
105 // coupon amounts should be added when adjusting
106 // values_ = Array(size, arguments_.cashFlows.back()->amount());
107
108 conversionProbability_ = Array(size, 0.0);
109 spreadAdjustedRate_ = Array(size, 0.0);
110
111 DayCounter rfdc = process_->riskFreeRate()->dayCounter();
112
113 // this takes care of convertibility and conversion probabilities
114 adjustValues();
115
116 Real creditSpread = creditSpread_->value();
117
118 Date exercise = arguments_.exercise->lastDate();
119
120 Rate riskFreeRate =
121 process_->riskFreeRate()->zeroRate(exercise, rfdc,
123
124 // Calculate blended discount rate to be used on roll back.
125 for (Size j=0; j<values_.size(); j++) {
127 conversionProbability_[j] * riskFreeRate +
128 (1-conversionProbability_[j])*(riskFreeRate + creditSpread);
129 }
130 }
131
133
134 bool convertible = false;
135 switch (arguments_.exercise->type()) {
137 if (time() <= stoppingTimes_[1] && time() >= stoppingTimes_[0])
138 convertible = true;
139 break;
141 if (isOnTime(stoppingTimes_[0]))
142 convertible = true;
143 break;
145 for (Real stoppingTime : stoppingTimes_) {
146 if (isOnTime(stoppingTime))
147 convertible = true;
148 }
149 break;
150 default:
151 QL_FAIL("invalid option type");
152 }
153
154 for (Size i=0; i<callabilityTimes_.size(); i++) {
156 applyCallability(i,convertible);
157 }
158
159 for (Size i=0; i<couponTimes_.size(); i++) {
160 if (isOnTime(couponTimes_[i]))
161 addCoupon(i);
162 }
163
164 if (convertible)
166 }
167
169 Array grid = adjustedGrid();
170 for (Size j=0; j<values_.size(); j++) {
172 if (values_[j] <= payoff) {
173 values_[j] = payoff;
174 conversionProbability_[j] = 1.0;
175 }
176 }
177 }
178
180 Size j;
181 Array grid = adjustedGrid();
182 switch (arguments_.callabilityTypes[i]) {
185 Real conversionValue =
187 Real trigger =
188 conversionValue*arguments_.callabilityTriggers[i];
189 for (j=0; j<values_.size(); j++) {
190 // the callability is conditioned by the trigger...
191 if (grid[j] >= trigger) {
192 // ...and might trigger conversion
193 values_[j] =
194 std::min(std::max(
197 values_[j]);
198 }
199 }
200 } else if (convertible) {
201 for (j=0; j<values_.size(); j++) {
202 // exercising the callability might trigger conversion
203 values_[j] =
204 std::min(std::max(arguments_.callabilityPrices[i],
206 values_[j]);
207 }
208 } else {
209 for (j=0; j<values_.size(); j++) {
210 values_[j] = std::min(arguments_.callabilityPrices[i],
211 values_[j]);
212 }
213 }
214 break;
215 case Callability::Put:
216 for (j=0; j<values_.size(); j++) {
217 values_[j] = std::max(values_[j],
219 }
220 break;
221 default:
222 QL_FAIL("unknown callability type");
223 }
224 }
225
228 }
229
231 Time t = time();
232 Array grid = method()->grid(t);
233 // add back all dividend amounts in the future
234 for (Size i=0; i<dividends_.size(); i++) {
235 Time dividendTime = dividendTimes_[i];
236 if (dividendTime >= t || close(dividendTime,t)) {
237 const ext::shared_ptr<Dividend>& d = dividends_[i];
238 DiscountFactor dividendDiscount =
239 process_->riskFreeRate()->discount(dividendTime) /
240 process_->riskFreeRate()->discount(t);
241 for (Real& j : grid)
242 j += d->amount(j) * dividendDiscount;
243 }
244 }
245 return grid;
246 }
247
248}
249
Black-Scholes processes.
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
ext::shared_ptr< Exercise > exercise
std::vector< Callability::Type > callabilityTypes
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 ext::shared_ptr< Lattice > & method() const
ConvertibleBond::arguments arguments_
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
void applyCallability(Size, bool convertible)
DiscretizedConvertible(ConvertibleBond::arguments, ext::shared_ptr< GeneralizedBlackScholesProcess > process, DividendSchedule dividends, Handle< Quote > creditSpread, const TimeGrid &grid=TimeGrid())
Shared handle to an observable.
Definition: handle.hpp:41
template class providing a null value for a given type.
Definition: null.hpp:76
time grid class
Definition: timegrid.hpp:43
bool empty() const
Definition: timegrid.hpp:165
Time closestTime(Time t) const
returns the time on the grid closest to the given t
Definition: timegrid.hpp:148
floating-point comparisons
const DefaultType & t
discretized convertible
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Date d
@ NoFrequency
null frequency
Definition: frequency.hpp:37
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163
STL namespace.