QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
cpicapfloortermpricesurface.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2010 Chris Kenyon
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
21#include <ql/experimental/inflation/cpicapfloortermpricesurface.hpp>
22#include <utility>
23
24
25namespace QuantLib {
26
28 Real nominal,
29 Real baseRate, // avoids an uncontrolled crash if index has no TS
30 const Period& observationLag,
31 const Calendar& cal, // calendar in index may not be useful
32 const BusinessDayConvention& bdc,
33 const DayCounter& dc,
34 const ext::shared_ptr<ZeroInflationIndex>& zii,
35 CPI::InterpolationType interpolationType,
37 const std::vector<Rate>& cStrikes,
38 const std::vector<Rate>& fStrikes,
39 const std::vector<Period>& cfMaturities,
40 const Matrix& cPrice,
41 const Matrix& fPrice)
42 : InflationTermStructure(0, cal, baseRate, observationLag, zii->frequency(), dc),
43 zii_(zii), interpolationType_(interpolationType), nominalTS_(std::move(yts)),
44 cStrikes_(cStrikes), fStrikes_(fStrikes), cfMaturities_(cfMaturities),
45 cPrice_(cPrice), fPrice_(fPrice), nominal_(nominal), bdc_(bdc) {
46
47 // does the index have a TS?
48 QL_REQUIRE(!zii_->zeroInflationTermStructure().empty(), "ZITS missing from index");
49 QL_REQUIRE(!nominalTS_.empty(), "nominal TS missing");
50
51 // data consistency checking, enough data?
52 QL_REQUIRE(fStrikes_.size() > 1, "not enough floor strikes");
53 QL_REQUIRE(cStrikes_.size() > 1, "not enough cap strikes");
54 QL_REQUIRE(cfMaturities_.size() > 1, "not enough maturities");
55 QL_REQUIRE(fStrikes_.size() == fPrice.rows(),
56 "floor strikes vs floor price rows not equal");
57 QL_REQUIRE(cStrikes_.size() == cPrice.rows(),
58 "cap strikes vs cap price rows not equal");
59 QL_REQUIRE(cfMaturities_.size() == fPrice.columns(),
60 "maturities vs floor price columns not equal");
61 QL_REQUIRE(cfMaturities_.size() == cPrice.columns(),
62 "maturities vs cap price columns not equal");
63
64 // data has correct properties (positive, monotonic)?
65 for(Size j = 0; j <cfMaturities_.size(); j++) {
66 QL_REQUIRE( cfMaturities[j] > Period(0,Days), "non-positive maturities");
67 if(j>0) {
68 QL_REQUIRE( cfMaturities[j] > cfMaturities[j-1],
69 "non-increasing maturities");
70 }
71 for(Size i = 0; i <fPrice_.rows(); i++) {
72 QL_REQUIRE( fPrice_[i][j] > 0.0,
73 "non-positive floor price: " << fPrice_[i][j] );
74 if(i>0) {
75 QL_REQUIRE( fPrice_[i][j] >= fPrice_[i-1][j],
76 "non-increasing floor prices");
77 }
78 }
79 for(Size i = 0; i <cPrice_.rows(); i++) {
80 QL_REQUIRE( cPrice_[i][j] > 0.0,
81 "non-positive cap price: " << cPrice_[i][j] );
82 if(i>0) {
83 QL_REQUIRE( cPrice_[i][j] <= cPrice_[i-1][j],
84 "non-decreasing cap prices: "
85 << cPrice_[i][j] << " then " << cPrice_[i-1][j]);
86 }
87 }
88 }
89
90
91 // Get the set of strikes, noting that repeats, overlaps are
92 // expected between caps and floors but that no overlap in the
93 // output is allowed so no repeats or overlaps are used
94 cfStrikes_ = std::vector<Rate>();
95 for(Size i = 0; i <fStrikes_.size(); i++)
96 cfStrikes_.push_back( fStrikes[i] );
97 Real eps = 0.0000001;
98 Rate maxFstrike = fStrikes_.back();
99 for(Size i = 0; i < cStrikes_.size(); i++) {
100 Rate k = cStrikes[i];
101 if (k > maxFstrike + eps) cfStrikes_.push_back(k);
102 }
103
104 // final consistency checking
105 QL_REQUIRE(cfStrikes_.size() > 2, "overall not enough strikes");
106 for (Size i = 1; i < cfStrikes_.size(); i++)
107 QL_REQUIRE( cfStrikes_[i] > cfStrikes_[i-1],
108 "cfStrikes not increasing");
109 }
110
114
116 zii_->frequency(),
119
120 return T > 0.0 ? std::pow(F1 / F0, 1 / T) - 1.0 : baseRate();
121 }
122
124 {
126 }
127
128
130 return this->price(cpiOptionDateFromTenor(d), k);
131 }
132
133
135 return this->capPrice(cpiOptionDateFromTenor(d), k);
136 }
137
138
140 return this->floorPrice(cpiOptionDateFromTenor(d), k);
141 }
142
143}
144
virtual Date cpiOptionDateFromTenor(const Period &p) const
CPICapFloorTermPriceSurface(Real nominal, Real baseRate, const Period &observationLag, const Calendar &cal, const BusinessDayConvention &bdc, const DayCounter &dc, const ext::shared_ptr< ZeroInflationIndex > &zii, CPI::InterpolationType interpolationType, Handle< YieldTermStructure > yts, const std::vector< Rate > &cStrikes, const std::vector< Rate > &fStrikes, const std::vector< Period > &cfMaturities, const Matrix &cPrice, const Matrix &fPrice)
virtual Real capPrice(const Period &d, Rate k) const
ext::shared_ptr< ZeroInflationIndex > zii_
virtual Real price(const Period &d, Rate k) const
virtual BusinessDayConvention businessDayConvention() const
virtual Real floorPrice(const Period &d, Rate k) const
calendar class
Definition: calendar.hpp:61
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Definition: calendar.cpp:84
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Shared handle to an observable.
Definition: handle.hpp:41
Interface for inflation term structures.
Matrix used in linear algebra.
Definition: matrix.hpp:41
Size rows() const
Definition: matrix.hpp:504
Size columns() const
Definition: matrix.hpp:508
virtual const Date & referenceDate() const
the date at which discount = 1.0 and/or variance = 0.0
virtual Calendar calendar() const
the calendar used for reference and/or option date calculation
virtual DayCounter dayCounter() const
the day counter used for date/time conversion
BusinessDayConvention
Business Day conventions.
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
bool isInterpolated(const ext::shared_ptr< ZeroInflationIndex > &index, const QuantLib::CPI::InterpolationType &type=QuantLib::CPI::AsIndex)
Definition: any.hpp:35
Time inflationYearFraction(Frequency f, bool indexIsInterpolated, const DayCounter &dayCounter, const Date &d1, const Date &d2)
STL namespace.
static Real laggedFixing(const ext::shared_ptr< ZeroInflationIndex > &index, const Date &date, const Period &observationLag, InterpolationType interpolationType)
interpolated inflation fixing
InterpolationType
when you observe an index, how do you interpolate between fixings?