Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
fillemptymatrix.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19#include <ql/math/interpolations/linearinterpolation.hpp>
21
22namespace QuantExt {
23using namespace std;
24
25static void fillMatrixImpl(Matrix& mat, Real blank) {
26
27 // define entire axis
28 vector<Real> xAxis;
29 for (unsigned int i = 0; i < mat.columns(); i++) {
30 xAxis.push_back(i);
31 }
32
33 // loop over rows and interpolate
34 for (Size i = 0; i < mat.rows(); i++) {
35 vector<Real> y; // values for the expiries we have
36 vector<Real> x; // the required 'tics' on axis to interpolate
37 vector<Real> yDesired(xAxis.size()); // interpolated y over defined x_axis
38
39 // flat extrapolate short end
40 if (mat[i][0] == blank) {
41 Size pos1 = mat.columns();
42 for (Size j = 1; j < mat.columns(); j++) {
43 if (mat[i][j] != blank) {
44 pos1 = j;
45 break;
46 }
47 }
48 QL_REQUIRE(pos1 < mat.columns(), "Matrix has empty line.");
49 for (Size j = 0; j < pos1; j++) {
50 mat[i][j] = mat[i][pos1];
51 }
52 }
53
54 // flat extrapolate far end
55 if (mat[i][mat.columns() - 1] == blank) {
56 int pos1 = -1;
57 for (Size j = mat.columns() - 2; j >= 0; j--) {
58 if (mat[i][j] != blank) {
59 pos1 = j;
60 break;
61 }
62 }
63 for (Size j = pos1 + 1; j < mat.columns(); j++) {
64 mat[i][j] = mat[i][pos1];
65 }
66 }
67
68 // build x and y for row
69 for (unsigned int j = 0; j < mat.columns(); j++) {
70 if (mat[i][j] != blank) {
71 x.push_back(j);
72 y.push_back(mat[i][j]);
73 }
74 }
75
76 // interpolate over entire x_axis
77 Interpolation f = LinearInterpolation(x.begin(), x.end(), y.begin());
78 std::transform(xAxis.begin(), xAxis.end(), yDesired.begin(), f);
79
80 // set row in mat
81 for (Size j = 0; j < mat.columns(); j++) {
82 mat[i][j] = yDesired[j];
83 }
84 }
85}
86
87void fillIncompleteMatrix(Matrix& mat, bool interpRows = true, Real blank = QL_NULL_REAL) {
88 QL_REQUIRE(mat.columns() > 0 && mat.rows() > 0, "Matrix has no elements.");
89
90 // check if already complete
91 bool isFull = true;
92 for (Size i = 0; i < mat.rows(); i++) {
93 for (Size j = 0; j < mat.columns(); j++) {
94 if (mat[i][j] == blank) {
95 isFull = false;
96 }
97 if (!isFull) {
98 break;
99 }
100 }
101 if (!isFull) {
102 break;
103 }
104 }
105
106 if (!isFull) {
107 if (mat.columns() == 1 && mat.rows() == 1) {
108 QL_FAIL("1 X 1 empty matrix given to fill."); // !is_full and 1 X 1 matrix.
109 }
110 if (interpRows) {
111 QL_REQUIRE(mat.columns() > 1, "Too few columns in matrix to interpolate within rows.");
112 fillMatrixImpl(mat, blank);
113 } else {
114 QL_REQUIRE(mat.rows() > 1, "Too few rows in matrix to interpolate within columns.");
115 Matrix m2 = transpose(mat);
116 fillMatrixImpl(m2, blank);
117 Matrix m3 = transpose(m2);
118 mat.swap(m3);
119 }
120 }
121}
122} // namespace QuantExt
functions to compute delta or delta-gamma VaR numbers
void fillIncompleteMatrix(Matrix &mat, bool interpRows=true, Real blank=QL_NULL_REAL)
function that fills a matrix