QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
cpicapfloortermpricesurface.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) 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
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 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 : TermStructure(0, cal, dc),
43 zii_(std::move(zii)), interpolationType_(interpolationType), nominalTS_(std::move(yts)),
44 cStrikes_(cStrikes), fStrikes_(fStrikes), cfMaturities_(cfMaturities),
45 cPrice_(cPrice), fPrice_(fPrice), nominal_(nominal), bdc_(bdc),
46 observationLag_(observationLag), baseRate_(baseRate) {
47
48 // does the index have a TS?
49 QL_REQUIRE(!zii_->zeroInflationTermStructure().empty(), "ZITS missing from index");
50 QL_REQUIRE(!nominalTS_.empty(), "nominal TS missing");
51
52 // data consistency checking, enough data?
53 QL_REQUIRE(fStrikes_.size() > 1, "not enough floor strikes");
54 QL_REQUIRE(cStrikes_.size() > 1, "not enough cap strikes");
55 QL_REQUIRE(cfMaturities_.size() > 1, "not enough maturities");
56 QL_REQUIRE(fStrikes_.size() == fPrice.rows(),
57 "floor strikes vs floor price rows not equal");
58 QL_REQUIRE(cStrikes_.size() == cPrice.rows(),
59 "cap strikes vs cap price rows not equal");
60 QL_REQUIRE(cfMaturities_.size() == fPrice.columns(),
61 "maturities vs floor price columns not equal");
62 QL_REQUIRE(cfMaturities_.size() == cPrice.columns(),
63 "maturities vs cap price columns not equal");
64
65 // data has correct properties (positive, monotonic)?
66 for(Size j = 0; j <cfMaturities_.size(); j++) {
67 QL_REQUIRE( cfMaturities[j] > Period(0,Days), "non-positive maturities");
68 if(j>0) {
69 QL_REQUIRE( cfMaturities[j] > cfMaturities[j-1],
70 "non-increasing maturities");
71 }
72 for(Size i = 0; i <fPrice_.rows(); i++) {
73 QL_REQUIRE( fPrice_[i][j] > 0.0,
74 "non-positive floor price: " << fPrice_[i][j] );
75 if(i>0) {
76 QL_REQUIRE( fPrice_[i][j] >= fPrice_[i-1][j],
77 "non-increasing floor prices");
78 }
79 }
80 for(Size i = 0; i <cPrice_.rows(); i++) {
81 QL_REQUIRE( cPrice_[i][j] > 0.0,
82 "non-positive cap price: " << cPrice_[i][j] );
83 if(i>0) {
84 QL_REQUIRE( cPrice_[i][j] <= cPrice_[i-1][j],
85 "non-decreasing cap prices: "
86 << cPrice_[i][j] << " then " << cPrice_[i-1][j]);
87 }
88 }
89 }
90
91
92 // Get the set of strikes, noting that repeats, overlaps are
93 // expected between caps and floors but that no overlap in the
94 // output is allowed so no repeats or overlaps are used
95 cfStrikes_ = std::vector<Rate>();
96 for(Size i = 0; i <fStrikes_.size(); i++)
97 cfStrikes_.push_back( fStrikes[i] );
98 Real eps = 0.0000001;
99 Rate maxFstrike = fStrikes_.back();
100 for(Size i = 0; i < cStrikes_.size(); i++) {
101 Rate k = cStrikes[i];
102 if (k > maxFstrike + eps) cfStrikes_.push_back(k);
103 }
104
105 // final consistency checking
106 QL_REQUIRE(cfStrikes_.size() > 2, "overall not enough strikes");
107 for (Size i = 1; i < cfStrikes_.size(); i++)
109 "cfStrikes not increasing");
110 }
111
115
117 zii_->frequency(),
120
121 return T > 0.0 ? std::pow(F1 / F0, 1 / T) - 1.0 : baseRate();
122 }
123
125 {
127 }
128
129
131 return this->price(cpiOptionDateFromTenor(d), k);
132 }
133
134
136 return this->capPrice(cpiOptionDateFromTenor(d), k);
137 }
138
139
141 return this->floorPrice(cpiOptionDateFromTenor(d), k);
142 }
143
144}
145
virtual Date cpiOptionDateFromTenor(const Period &p) const
CPICapFloorTermPriceSurface(Real nominal, Real baseRate, const Period &observationLag, const Calendar &cal, const BusinessDayConvention &bdc, const DayCounter &dc, 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
Matrix used in linear algebra.
Definition: matrix.hpp:41
Size rows() const
Definition: matrix.hpp:504
Size columns() const
Definition: matrix.hpp:508
Basic term-structure functionality.
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
cpi inflation cap and floor term price structure.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
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 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?