|
| 1 | +/* |
| 2 | + Copyright (C) 2023 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 <orea/app/hwhistoricalcalibrationdataloader.hpp> |
| 20 | +#include <orea/scenario/scenariofilereader.hpp> |
| 21 | +#include <orea/scenario/simplescenariofactory.hpp> |
| 22 | +#include <orea/scenario/scenarioloader.hpp> |
| 23 | + |
| 24 | +#include <ored/utilities/csvfilereader.hpp> |
| 25 | +#include <ored/utilities/parsers.hpp> |
| 26 | +#include <ored/utilities/indexparser.hpp> |
| 27 | +#include <ored/utilities/log.hpp> |
| 28 | + |
| 29 | +#include <sstream> |
| 30 | +#include <vector> |
| 31 | +#include <map> |
| 32 | +#include <string> |
| 33 | + |
| 34 | +namespace ore { |
| 35 | +namespace analytics { |
| 36 | + |
| 37 | +using namespace QuantLib; |
| 38 | +using namespace ore::data; |
| 39 | + |
| 40 | +HwHistoricalCalibrationDataLoader::HwHistoricalCalibrationDataLoader(const std::string& baseCurrency, |
| 41 | + const std::vector<std::string>& foreignCurrency, |
| 42 | + const std::vector<Period>& curveTenors, |
| 43 | + const Date& startDate, const Date& endDate) |
| 44 | + : baseCurrency_(baseCurrency), foreignCurrency_(foreignCurrency), tenors_(curveTenors), startDate_(startDate), |
| 45 | + endDate_(endDate) {} |
| 46 | + |
| 47 | +void HwHistoricalCalibrationDataLoader::loadFromScenarioFile(const std::string& fileName) { |
| 48 | + LOG("Load Historical time series data from scenario file " << fileName); |
| 49 | + |
| 50 | + ext::shared_ptr<SimpleScenarioFactory> scenarioFactory = ext::make_shared<SimpleScenarioFactory>(false); |
| 51 | + ext::shared_ptr<ScenarioFileReader> scenarioReader = |
| 52 | + ext::make_shared<ScenarioFileReader>(fileName, scenarioFactory); |
| 53 | + ext::shared_ptr<HistoricalScenarioLoader> historicalScenarioLoader = |
| 54 | + ext::make_shared<HistoricalScenarioLoader>(scenarioReader, startDate_, endDate_, NullCalendar()); |
| 55 | + |
| 56 | + QL_REQUIRE(historicalScenarioLoader->scenarios().size() == 1, |
| 57 | + "Only one scenario allowed for HW historical calibration."); |
| 58 | + std::map<Date, ext::shared_ptr<Scenario>> scenarioMap = historicalScenarioLoader->scenarios()[0]; |
| 59 | + QL_REQUIRE(scenarioMap.size() > 0, "Scenario file is empty."); |
| 60 | + const auto& keys = scenarioMap.begin()->second->keys(); |
| 61 | + for (const auto& [date, scenario] : scenarioMap) { |
| 62 | + for (const auto& key : keys) { |
| 63 | + if (key.keytype == RiskFactorKey::KeyType::IndexCurve) { |
| 64 | + std::string ccy = parseCurrency(key.name); |
| 65 | + if (ccy == baseCurrency_ || |
| 66 | + std::find(foreignCurrency_.begin(), foreignCurrency_.end(), ccy) != foreignCurrency_.end()) { |
| 67 | + loadIr(key.name, key.index, date, scenario->get(key)); |
| 68 | + } |
| 69 | + } else if (key.keytype == RiskFactorKey::KeyType::FXSpot) { |
| 70 | + for (const auto& foreignCcy : foreignCurrency_) { |
| 71 | + std::string pair1 = foreignCcy + baseCurrency_; |
| 72 | + std::string pair2 = baseCurrency_ + foreignCcy; |
| 73 | + Real fxRate; |
| 74 | + if (key.name == pair1) |
| 75 | + fxRate = scenario->get(key); |
| 76 | + else if (key.name == pair2) |
| 77 | + fxRate = 1.0 / scenario->get(key); |
| 78 | + else |
| 79 | + continue; |
| 80 | + loadFx(pair1, date, fxRate); |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + cleanData(); |
| 87 | +} |
| 88 | + |
| 89 | +void HwHistoricalCalibrationDataLoader::loadPCAFromCsv(const std::vector<std::string>& fileNames) { |
| 90 | + for (const auto& fileName : fileNames) { |
| 91 | + LOG("Load PCA data from file " << fileName); |
| 92 | + CSVFileReader dataReader(fileName, false); |
| 93 | + dataReader.next(); |
| 94 | + QL_REQUIRE(dataReader.numberOfColumns() == tenors_.size() + 1, |
| 95 | + "Number of columns in pca file must be number of tenor + 1."); |
| 96 | + QL_REQUIRE(dataReader.get(0) == "EigenValue", "EigenValue column must be the first column in the data file."); |
| 97 | + QL_REQUIRE(dataReader.get(1) == "EigenVector", "EigenVector column must exist in the data file."); |
| 98 | + QL_REQUIRE(dataReader.get(2) == "Currency", "Currency column must exist in the data file."); |
| 99 | + std::string ccy = dataReader.get(3); |
| 100 | + Size lines = 0; |
| 101 | + // std::vector<std::string> remove_chars = { "\"", "[", "]", " " }; |
| 102 | + vector<Real> eig_val; |
| 103 | + vector<vector<Real>> eig_vec; |
| 104 | + while (dataReader.next()) { |
| 105 | + ++lines; |
| 106 | + vector<Real> eig_vec_i; |
| 107 | + eig_val.push_back(parseReal(dataReader.get(0))); |
| 108 | + for (Size i = 0; i < tenors_.size(); i++) { |
| 109 | + eig_vec_i.push_back(parseReal(dataReader.get(i + 1))); |
| 110 | + } |
| 111 | + eig_vec.push_back(eig_vec_i); |
| 112 | + } |
| 113 | + Array eigenValue = Array(lines, 0.0); |
| 114 | + Matrix eigenVector = Matrix(lines, tenors_.size(), 0.0); |
| 115 | + for (Size i = 0; i < lines; i++) { |
| 116 | + eigenValue[i] = eig_val[i]; |
| 117 | + for (Size j = 0; j < tenors_.size(); j++) { |
| 118 | + eigenVector[i][j] = eig_vec[i][j]; |
| 119 | + } |
| 120 | + } |
| 121 | + loadEigenValue(ccy, eigenValue); |
| 122 | + loadEigenVector(ccy, eigenVector); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +void HwHistoricalCalibrationDataLoader::loadIr(const std::string& curveId, const Size& index, const Date& d, |
| 127 | + const Real& df) { |
| 128 | + std::map<Date, std::vector<Real>>& dateMap = irCurves_[curveId]; |
| 129 | + std::vector<Real>& discountFactors = dateMap[d]; |
| 130 | + if (discountFactors.empty()) { |
| 131 | + discountFactors.resize(tenors_.size(), 0.0); |
| 132 | + } |
| 133 | + QL_REQUIRE(index < tenors_.size(), "Tenor index " << index << " out of range for curve " << curveId << " on date " |
| 134 | + << d << " (max: " << tenors_.size() << ")"); |
| 135 | + discountFactors[index] = df; |
| 136 | +} |
| 137 | + |
| 138 | +void HwHistoricalCalibrationDataLoader::loadFx(const std::string& curveId, const Date& d, const Real& fxSpot) { |
| 139 | + // Check if the date is within the start and end date specified in ore.xml |
| 140 | + if (fxSpots_.find(curveId) != fxSpots_.end()) { |
| 141 | + if (fxSpots_[curveId].find(d) != fxSpots_[curveId].end()) { |
| 142 | + ALOG("Encounter duplicated records for curveId " << curveId << ", date " << d << " in the input file."); |
| 143 | + return; |
| 144 | + } |
| 145 | + } |
| 146 | + fxSpots_[curveId][d] = fxSpot; |
| 147 | +} |
| 148 | + |
| 149 | +void HwHistoricalCalibrationDataLoader::loadEigenValue(const std::string& ccy, const Array& eigenValue) { |
| 150 | + eigenValue_[ccy] = eigenValue; |
| 151 | + principalComponent_[ccy] = eigenValue.size(); |
| 152 | +} |
| 153 | + |
| 154 | +void HwHistoricalCalibrationDataLoader::loadEigenVector(const std::string& ccy, const Matrix& eigenVector) { |
| 155 | + eigenVector_[ccy] = transpose(eigenVector); |
| 156 | +} |
| 157 | + |
| 158 | +void HwHistoricalCalibrationDataLoader::cleanData() { |
| 159 | + // Check if all required currency exist in irCurves_ |
| 160 | + std::vector<std::string> requiredCcy = foreignCurrency_; |
| 161 | + requiredCcy.push_back(baseCurrency_); |
| 162 | + |
| 163 | + for (const auto& [curveId, dataMap] : irCurves_) { |
| 164 | + auto position = std::find(requiredCcy.begin(), requiredCcy.end(), parseCurrency(curveId)); |
| 165 | + if (position != requiredCcy.end()) { |
| 166 | + requiredCcy.erase(position); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + std::string missingCcy; |
| 171 | + for (auto& c : requiredCcy) |
| 172 | + missingCcy += c + " "; |
| 173 | + QL_REQUIRE(requiredCcy.size() == 0, "Discount factor for " << missingCcy << "are not found in input file."); |
| 174 | + |
| 175 | + // Check if all required fx spot exist in fxSpots |
| 176 | + std::vector<std::string> missingFxPairs; |
| 177 | + for (const auto& foreignCcy : foreignCurrency_) { |
| 178 | + std::string expectedPair = foreignCcy + baseCurrency_; |
| 179 | + |
| 180 | + if (fxSpots_.find(expectedPair) == fxSpots_.end()) { |
| 181 | + missingFxPairs.push_back(expectedPair); |
| 182 | + } |
| 183 | + } |
| 184 | + missingCcy = ""; |
| 185 | + for (auto& c : missingFxPairs) |
| 186 | + missingCcy += c + " "; |
| 187 | + QL_REQUIRE(missingFxPairs.size() == 0, "FX spot for " << missingCcy << "are not found in input file."); |
| 188 | +} |
| 189 | + |
| 190 | +std::string HwHistoricalCalibrationDataLoader::parseCurrency(const std::string& curveId) { |
| 191 | + vector<string> tokens; |
| 192 | + split(tokens, curveId, boost::is_any_of("-")); |
| 193 | + QL_REQUIRE(tokens.size() == 2 || tokens.size() == 3, |
| 194 | + "Two or three tokens required in " << curveId << ": CCY-INDEX or CCY-INDEX-TERM"); |
| 195 | + return tokens[0]; |
| 196 | +} |
| 197 | + |
| 198 | +} // namespace analytics |
| 199 | +} // namespace ore |
0 commit comments