Coverage Report

Created: 2026-01-25 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/inflation/cpicapfloorengines.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2011 Chris Kenyon
5
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
 <https://www.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
22
#include <ql/experimental/inflation/cpicapfloorengines.hpp>
23
#include <ql/experimental/inflation/cpicapfloortermpricesurface.hpp>
24
#include <ql/time/daycounters/actualactual.hpp>
25
#include <utility>
26
27
28
namespace QuantLib {
29
30
31
    InterpolatingCPICapFloorEngine::InterpolatingCPICapFloorEngine(
32
        Handle<CPICapFloorTermPriceSurface> priceSurf)
33
0
    : priceSurf_(std::move(priceSurf)) {
34
0
        registerWith(priceSurf_);
35
0
    }
36
37
38
    void InterpolatingCPICapFloorEngine::calculate() const
39
0
    {
40
0
        Real npv = 0.0;
41
42
        // what is the difference between the observationLag of the surface
43
        // and the observationLag of the cap/floor?
44
        // \TODO next line will fail if units are different
45
0
        Period lagDiff = arguments_.observationLag - priceSurf_->observationLag();
46
        // next line will fail if units are different if Period() is not well written
47
0
        QL_REQUIRE(lagDiff >= Period(0, Months), "InterpolatingCPICapFloorEngine: "
48
0
                   "lag difference must be non-negative: " << lagDiff);
49
50
        // we now need an effective maturity to use in the price surface because this uses
51
        // maturity of calibration instruments as its time axis, N.B. this must also
52
        // use the roll because the surface does
53
0
        Date effectiveMaturity = arguments_.payDate - lagDiff;
54
55
56
        // what interpolation do we use? Index / flat / linear
57
0
        if (arguments_.observationInterpolation == CPI::AsIndex) {
58
            // same as index means we can just use the price surface
59
            // since this uses the index
60
0
            if (arguments_.type == Option::Call) {
61
0
                npv = priceSurf_->capPrice(effectiveMaturity, arguments_.strike);
62
0
            } else {
63
0
                npv = priceSurf_->floorPrice(effectiveMaturity, arguments_.strike);
64
0
            }
65
66
67
0
        } else {
68
0
            std::pair<Date,Date> dd = inflationPeriod(effectiveMaturity, arguments_.index->frequency());
69
0
            Real priceStart = 0.0;
70
71
0
            if (arguments_.type == Option::Call) {
72
0
                priceStart = priceSurf_->capPrice(dd.first, arguments_.strike);
73
0
            } else {
74
0
                priceStart = priceSurf_->floorPrice(dd.first, arguments_.strike);
75
0
            }
76
77
            // if we use a flat index vs the interpolated one ...
78
0
            if (arguments_.observationInterpolation == CPI::Flat) {
79
                // then use the price for the first day in the period because the value cannot change after then
80
0
                npv = priceStart;
81
82
0
            } else {
83
                // linear interpolation will be very close
84
0
                Real priceEnd = 0.0;
85
0
                if (arguments_.type == Option::Call) {
86
0
                    priceEnd = priceSurf_->capPrice((dd.second+Period(1,Days)), arguments_.strike);
87
0
                } else {
88
0
                    priceEnd = priceSurf_->floorPrice((dd.second+Period(1,Days)), arguments_.strike);
89
0
                }
90
91
0
                npv = priceStart + (priceEnd - priceStart) * (effectiveMaturity - dd.first)
92
0
                / ( (dd.second+Period(1,Days)) - dd.first); // can't get to next period'
93
0
            }
94
95
0
        }
96
0
        results_.value = npv;
97
0
    }
98
99
}