|
| 1 | +/* |
| 2 | + Copyright (C) 2024 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 <ored/configuration/parametricsmileconfiguration.hpp> |
| 20 | +#include <ored/utilities/parsers.hpp> |
| 21 | + |
| 22 | +using namespace QuantLib; |
| 23 | + |
| 24 | +namespace ore { |
| 25 | +namespace data { |
| 26 | + |
| 27 | +void ParametricSmileConfiguration::Parameter::fromXML(XMLNode* node) { |
| 28 | + XMLUtils::checkNode(node, "Parameter"); |
| 29 | + name = XMLUtils::getChildValue(node, "Name", true); |
| 30 | + initialValue = parseReal(XMLUtils::getChildValue(node, "InitialValue", true)); |
| 31 | + isFixed = parseBool(XMLUtils::getChildValue(node, "IsFixed", true)); |
| 32 | +} |
| 33 | + |
| 34 | +XMLNode* ParametricSmileConfiguration::Parameter::toXML(XMLDocument& doc) const { |
| 35 | + XMLNode* n = doc.allocNode("Parameter"); |
| 36 | + XMLUtils::addChild(doc, n, "Name", name); |
| 37 | + XMLUtils::addChild(doc, n, "InitialValue", initialValue); |
| 38 | + XMLUtils::addChild(doc, n, "IsFixed", isFixed); |
| 39 | + return n; |
| 40 | +} |
| 41 | + |
| 42 | +void ParametricSmileConfiguration::Calibration::fromXML(XMLNode* node) { |
| 43 | + XMLUtils::checkNode(node, "Calibration"); |
| 44 | + maxCalibrationAttempts = parseInteger(XMLUtils::getChildValue(node, "MaxCalibrationAttempts", true)); |
| 45 | + exitEarlyErrorThreshold = parseReal(XMLUtils::getChildValue(node, "ExitEarlyErrorThreshold", true)); |
| 46 | + maxAcceptableError = parseBool(XMLUtils::getChildValue(node, "MaxAcceptableError", true)); |
| 47 | +} |
| 48 | + |
| 49 | +XMLNode* ParametricSmileConfiguration::Calibration::toXML(XMLDocument& doc) const { |
| 50 | + XMLNode* n = doc.allocNode("Calibration"); |
| 51 | + XMLUtils::addChild(doc, n, "MaxCalibrationAttempts", (Integer)maxCalibrationAttempts); |
| 52 | + XMLUtils::addChild(doc, n, "ExitEarlyErrorThreshold", exitEarlyErrorThreshold); |
| 53 | + XMLUtils::addChild(doc, n, "MaxAcceptableError", maxAcceptableError); |
| 54 | + return n; |
| 55 | +} |
| 56 | + |
| 57 | +ParametricSmileConfiguration::ParametricSmileConfiguration(std::vector<Parameter> parameters, Calibration calibration) |
| 58 | + : parameters_(std::move(parameters)), calibration_(std::move(calibration)) {} |
| 59 | + |
| 60 | +void ParametricSmileConfiguration::fromXML(XMLNode* node) { |
| 61 | + |
| 62 | + XMLUtils::checkNode(node, "ParametricSmileConfiguration"); |
| 63 | + |
| 64 | + parameters_.clear(); |
| 65 | + |
| 66 | + if (XMLNode* n = XMLUtils::getChildNode(node, "Parameters")) { |
| 67 | + for (auto m : XMLUtils::getChildrenNodes(n, "Parameter")) { |
| 68 | + ParametricSmileConfiguration::Parameter p; |
| 69 | + p.fromXML(m); |
| 70 | + parameters_.push_back(p); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (XMLNode* n = XMLUtils::getChildNode(node, "Calibration")) { |
| 75 | + calibration_.fromXML(n); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +XMLNode* ParametricSmileConfiguration::toXML(XMLDocument& doc) const { |
| 80 | + |
| 81 | + XMLNode* node = doc.allocNode("ParametricSmileConfiguration"); |
| 82 | + |
| 83 | + XMLNode* m = XMLUtils::addChild(doc, node, "Parameters"); |
| 84 | + for (auto const& p : parameters_) { |
| 85 | + XMLUtils::appendNode(m, p.toXML(doc)); |
| 86 | + } |
| 87 | + |
| 88 | + XMLUtils::appendNode(node, calibration_.toXML(doc)); |
| 89 | + |
| 90 | + return node; |
| 91 | +} |
| 92 | + |
| 93 | +const ParametricSmileConfiguration::Parameter& ParametricSmileConfiguration::parameter(const std::string& name) const { |
| 94 | + auto p = |
| 95 | + std::find_if(parameters_.begin(), parameters_.end(), [&name](const Parameter& p) { return p.name == name; }); |
| 96 | + QL_REQUIRE(p != parameters_.end(), "ParametricSmileConfiguration: parameter '" << name << "' is not present."); |
| 97 | + return p->second; |
| 98 | +} |
| 99 | + |
| 100 | +const ParametricSmileConfiguration::Calibration& ParametricSmileConfiguration::calibration() const { |
| 101 | + return calibration_; |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace data |
| 105 | +} // namespace ore |
0 commit comments