36#include <ql/math/matrixutilities/symmetricschurdecomposition.hpp>
37#include <ql/utilities/null.hpp>
39#include <boost/make_shared.hpp>
40#include <boost/regex.hpp>
55std::vector<std::string>
loadFactorList(
const std::string& inputFileName,
const char delim) {
56 LOG(
"Load factor list from file " << inputFileName);
58 file.open(inputFileName);
59 QL_REQUIRE(file.is_open(),
"error opening file " << inputFileName);
60 std::vector<string> result;
64 getline(file, line, delim);
67 result.push_back(line);
69 }
catch (std::exception& e) {
71 QL_FAIL(
"error during reading file: " << e.what());
74 LOG(
"Loaded factor list of size " << result.size());
78std::vector<std::vector<double>>
loadScenarios(
const std::string& inputFileName,
const char delim) {
79 LOG(
"Load scenarios from file " << inputFileName);
81 file.open(inputFileName);
82 QL_REQUIRE(file.is_open(),
"error opening file " << inputFileName);
83 std::vector<std::vector<double>> result;
86 Size currentScenario = Null<Size>();
88 getline(file, line, delim);
91 std::vector<string> tokens;
93 boost::split(tokens, line, boost::is_any_of(
",;\t "), boost::token_compress_off);
94 QL_REQUIRE(tokens.size() == 3,
"loadScenarios, expected 3 tokens in line: " << line);
97 if (currentScenario != tmpSc) {
98 result.push_back(std::vector<double>());
99 currentScenario = tmpSc;
103 result.back().push_back(move);
105 }
catch (std::exception& e) {
107 QL_FAIL(
"error during reading file: " << e.what());
110 LOG(
"Loaded " << result.size() <<
" scenarios, first entry contains " << result.front().size() <<
" factors");
115 LOG(
"Load covariance matrix from file " << inputFileName);
117 file.open(inputFileName);
118 QL_REQUIRE(file.is_open(),
"error opening file " << inputFileName);
119 std::vector<std::pair<Size, Size>> pos;
120 std::vector<double> val;
121 Size maxI = 0, maxJ = 0;
123 while (!file.eof()) {
125 getline(file, line, delim);
126 if (line.size() == 0)
128 std::vector<string> tokens;
130 boost::split(tokens, line, boost::is_any_of(
",;\t "), boost::token_compress_off);
131 QL_REQUIRE(tokens.size() == 3,
"loadCovarianceMatrix, expected 3 tokens in line: " << line);
135 pos.push_back(std::make_pair(i, j));
137 maxI = std::max(maxI, i);
138 maxJ = std::max(maxJ, j);
140 }
catch (std::exception& e) {
142 QL_FAIL(
"error during reading file: " << e.what());
145 LOG(
"Loaded " << val.size() <<
" data points, dimension of matrix is " << maxI + 1 <<
"x" << maxJ + 1);
146 QL_REQUIRE(maxI == maxJ,
"Expected quadratic matrix");
148 Matrix result(maxI + 1, maxI + 1);
149 for (Size i = 0; i < pos.size(); ++i) {
150 result[pos[i].first][pos[i].second] = val[i];
151 result[pos[i].second][pos[i].first] = val[i];
155 QuantLib::SymmetricSchurDecomposition ssd(result);
156 for (Size i = 0; i < ssd.eigenvalues().
size(); ++i) {
157 LOG(
"Eigenvalue " << i <<
" = " << ssd.eigenvalues()[i]);
185 QL_REQUIRE(versionMap.count(version) > 0,
186 "Could not parse SIMM version string " << version <<
" to a valid version");
188 return versionMap.at(version);
192 const QuantLib::ext::shared_ptr<SimmBucketMapper>& simmBucketMapper,
193 const QuantLib::ext::shared_ptr<SimmCalibrationData>& simmCalibrationData,
194 const Size& mporDays) {
197 if (simmCalibrationData) {
198 const auto& simmCalibration = simmCalibrationData->getBySimmVersion(simmVersion);
199 if (simmCalibration) {
200 auto simmConfiguration = QuantLib::ext::make_shared<SimmConfigurationCalibration>(simmBucketMapper, simmCalibration, mporDays);
201 return simmConfiguration;
209 return QuantLib::ext::make_shared<SimmConfiguration_ISDA_V1_0>(simmBucketMapper);
212 return QuantLib::ext::make_shared<SimmConfiguration_ISDA_V1_3>(simmBucketMapper);
215 return QuantLib::ext::make_shared<SimmConfiguration_ISDA_V1_3_38>(simmBucketMapper);
218 return QuantLib::ext::make_shared<SimmConfiguration_ISDA_V2_0>(simmBucketMapper);
221 return QuantLib::ext::make_shared<SimmConfiguration_ISDA_V2_1>(simmBucketMapper);
224 return QuantLib::ext::make_shared<SimmConfiguration_ISDA_V2_2>(simmBucketMapper, mporDays);
227 return QuantLib::ext::make_shared<SimmConfiguration_ISDA_V2_3>(simmBucketMapper, mporDays);
230 return QuantLib::ext::make_shared<SimmConfiguration_ISDA_V2_3_8>(simmBucketMapper, mporDays);
233 return QuantLib::ext::make_shared<SimmConfiguration_ISDA_V2_5>(simmBucketMapper, mporDays);
236 return QuantLib::ext::make_shared<SimmConfiguration_ISDA_V2_5A>(simmBucketMapper, mporDays);
239 return QuantLib::ext::make_shared<SimmConfiguration_ISDA_V2_6>(simmBucketMapper, mporDays);
245 QL_FAIL(
"SIMM configuration for version '" << simmVersion <<
"' cannot be built");
249 std::string result = str;
250 if (result.find(
',') != string::npos && csvQuoteChar ==
'\0') {
251 result =
'\"' + result +
'\"';
Real parseReal(const string &s)
Integer parseInteger(const string &s)
std::vector< std::vector< double > > loadScenarios(const std::string &inputFileName, const char delim)
std::vector< std::string > loadFactorList(const std::string &inputFileName, const char delim)
SimmVersion
Ordered SIMM versions.
std::string escapeCommaSeparatedList(const std::string &str, const char &csvQuoteChar)
SimmVersion parseSimmVersion(const string &version)
QuantLib::ext::shared_ptr< SimmConfiguration > buildSimmConfiguration(const string &simmVersion, const QuantLib::ext::shared_ptr< SimmBucketMapper > &simmBucketMapper, const QuantLib::ext::shared_ptr< SimmCalibrationData > &simmCalibrationData, const Size &mporDays)
Matrix loadCovarianceMatrix(const std::string &inputFileName, const char delim)
Size size(const ValueType &v)
SIMM configuration built for SIMM calibration.
SIMM configuration for SIMM version R1.0 (v3.15)
SIMM configuration for SIMM version R1.3 (3.29)
SIMM configuration for SIMM version 1.3.38.
SIMM configuration for SIMM version 2.0 (1.3.44)
SIMM configuration for SIMM version 2.1 (2.0.6)
SIMM configuration for SIMM version 2.2.
SIMM configuration for SIMM version 2.3.
SIMM configuration for SIMM version 2.3.8.
SIMM configuration for SIMM version 2.5.
SIMM configuration for SIMM version 2.5A.
SIMM configuration for SIMM version 2.6.