QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
averageoiscouponpricer.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) 2016 Stefano Fondi
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
21#include <cmath>
22
23using std::vector;
24using std::exp;
25using std::pow;
26using std::log;
27
28namespace QuantLib {
29
31 const FloatingRateCoupon& coupon) {
32 coupon_ = dynamic_cast<const OvernightIndexedCoupon*>(&coupon);
33 QL_ENSURE(coupon_, "wrong coupon type");
34 }
35
37
38 ext::shared_ptr<OvernightIndex> index =
39 ext::dynamic_pointer_cast<OvernightIndex>(coupon_->index());
40
41 const vector<Date>& fixingDates = coupon_->fixingDates();
42 const vector<Time>& dt = coupon_->dt();
43
44 Size n = dt.size(),
45 i = 0;
46
47 Real accumulatedRate = 0.0;
48
49 // already fixed part
51 while (i < n && fixingDates[i] < today) {
52 // rate must have been fixed
54 index->name())[fixingDates[i]];
55 QL_REQUIRE(pastFixing != Null<Real>(),
56 "Missing " << index->name() <<
57 " fixing for " << fixingDates[i]);
58 accumulatedRate += pastFixing*dt[i];
59 ++i;
60 }
61
62 // today is a border case
63 if (i < n && fixingDates[i] == today) {
64 // might have been fixed
65 try {
67 index->name())[fixingDates[i]];
68 if (pastFixing != Null<Real>()) {
69 accumulatedRate += pastFixing*dt[i];
70 ++i;
71 }
72 else {
73 ; // fall through and forecast
74 }
75 }
76 catch (Error&) {
77 ; // fall through and forecast
78 }
79 }
80
81 /* forward part using telescopic property in order
82 to avoid the evaluation of multiple forward fixings
83 (approximation proposed by Katsumi Takada)*/
84 if (byApprox_ && i < n) {
86 index->forwardingTermStructure();
87 QL_REQUIRE(!curve.empty(),
88 "null term structure set to this instance of " <<
89 index->name());
90
91 const vector<Date>& dates = coupon_->valueDates();
92 DiscountFactor startDiscount = curve->discount(dates[i]);
93 DiscountFactor endDiscount = curve->discount(dates[n]);
94
95 accumulatedRate += log(startDiscount / endDiscount) -
96 convAdj1(curve->timeFromReference(dates[i]),
97 curve->timeFromReference(dates[n])) -
98 convAdj2(curve->timeFromReference(dates[i]),
99 curve->timeFromReference(dates[n]));
100 }
101 // otherwise
102 else if (i < n){
104 index->forwardingTermStructure();
105 QL_REQUIRE(!curve.empty(),
106 "null term structure set to this instance of " <<
107 index->name());
108
109 const vector<Date>& dates = coupon_->valueDates();
110 Time te = curve->timeFromReference(dates[n]);
111 while (i < n) {
112 // forcast fixing
113 Rate forecastFixing = index->fixing(fixingDates[i]);
114 Time ti1 = curve->timeFromReference(dates[i]);
115 Time ti2 = curve->timeFromReference(dates[i + 1]);
116 /*convexity adjustment due to payment dalay of each
117 overnight fixing, supposing an Hull-White short rate model*/
118 Real convAdj = exp( 0.5*pow(vol_, 2.0) / pow(mrs_, 3.0)*
119 (exp(2 * mrs_*ti1) - 1)*
120 (exp(-mrs_*ti2) - exp(-mrs_*te))*
121 (exp(-mrs_*ti2) - exp(-mrs_*ti1)) );
122 accumulatedRate += convAdj*(1 + forecastFixing*dt[i]) - 1;
123 ++i;
124 }
125 }
126
127 Rate rate = accumulatedRate / coupon_->accrualPeriod();
128 return coupon_->gearing() * rate + coupon_->spread();
129 }
130
132 Time ts, Time te) const {
133 return vol_ * vol_ / (4.0 * pow(mrs_, 3.0)) *
134 (1.0 - exp(-2.0*mrs_*ts)) *
135 pow((1.0 - exp(-mrs_*(te - ts))), 2.0);
136 }
137
139 Time ts, Time te) const {
140 return vol_ * vol_ / (2.0 * pow(mrs_, 2.0)) * ((te - ts) -
141 pow(1.0 - exp(-mrs_*(te - ts)), 2.0) / mrs_ -
142 (1.0 - exp(-2.0*mrs_*(te - ts))) / (2.0 * mrs_));
143 }
144
145}
146
pricer for arithmetically-averaged overnight-indexed coupons
void initialize(const FloatingRateCoupon &coupon) override
Time accrualPeriod() const
accrual period as fraction of year
Definition: coupon.cpp:44
Concrete date class.
Definition: date.hpp:125
Base error class.
Definition: errors.hpp:39
base floating-rate coupon class
Real gearing() const
index gearing, i.e. multiplicative coefficient for the index
const ext::shared_ptr< InterestRateIndex > & index() const
floating index
Spread spread() const
spread paid over the fixing of the underlying index
Shared handle to an observable.
Definition: handle.hpp:41
bool empty() const
checks if the contained shared pointer points to anything
Definition: handle.hpp:191
const TimeSeries< Real > & getHistory(const std::string &name) const
returns the (possibly empty) history of the index fixings
template class providing a null value for a given type.
Definition: null.hpp:76
const std::vector< Date > & valueDates() const
value dates for the rates to be compounded
const std::vector< Time > & dt() const
accrual (compounding) periods
const std::vector< Date > & fixingDates() const
fixing dates for the rates to be compounded
DateProxy & evaluationDate()
the date at which pricing is to be performed.
Definition: settings.hpp:147
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
#define QL_ENSURE(condition, message)
throw an error if the given post-condition is not verified
Definition: errors.hpp:130
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
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
Definition: any.hpp:35