|
21 | 21 | #include <orea/app/reportwriter.hpp> |
22 | 22 | #include <orea/app/structuredanalyticserror.hpp> |
23 | 23 | #include <orea/simm/utilities.hpp> |
| 24 | +#include <orea/scenario/scenariowriter.hpp> |
24 | 25 |
|
25 | 26 | #include <ored/utilities/marketdata.hpp> |
26 | 27 | #include <ored/portfolio/structuredtradeerror.hpp> |
@@ -2000,5 +2001,142 @@ void ReportWriter::writeScenarioDistributions(const boost::shared_ptr<ScenarioGe |
2000 | 2001 | report.end(); |
2001 | 2002 | } |
2002 | 2003 |
|
| 2004 | +void ReportWriter::writeHistoricalScenarioDetails( |
| 2005 | + const boost::shared_ptr<ore::analytics::HistoricalScenarioGenerator>& generator, ore::data::Report& report) { |
| 2006 | + |
| 2007 | + report.addColumn("PLDate1", Date()) |
| 2008 | + .addColumn("PLDate2", Date()) |
| 2009 | + .addColumn("Key", string()) |
| 2010 | + .addColumn("BaseValue", double(), 8) |
| 2011 | + .addColumn("AdjustmentFactor1", double(), 8) |
| 2012 | + .addColumn("AdjustmentFactor2", double(), 8) |
| 2013 | + .addColumn("ScenarioValue1", double(), 8) |
| 2014 | + .addColumn("ScenarioValue2", double(), 8) |
| 2015 | + .addColumn("ShiftType", string()) |
| 2016 | + .addColumn("Return", double(), 8) |
| 2017 | + .addColumn("ScenarioValue", double(), 8); |
| 2018 | + |
| 2019 | + Date asof = generator->baseScenario()->asof(); |
| 2020 | + for (Size i = 0; i < generator->startDates().size(); ++i) { |
| 2021 | + std::ignore = generator->next(asof); |
| 2022 | + for (auto const& d : generator->lastHistoricalScenarioCalculationDetails()) { |
| 2023 | + report.next() |
| 2024 | + .add(d.scenarioDate1) |
| 2025 | + .add(d.scenarioDate2) |
| 2026 | + .add(ore::data::to_string(d.key)) |
| 2027 | + .add(d.baseValue) |
| 2028 | + .add(d.adjustmentFactor1) |
| 2029 | + .add(d.adjustmentFactor2) |
| 2030 | + .add(d.scenarioValue1) |
| 2031 | + .add(d.scenarioValue2) |
| 2032 | + .add(ore::data::to_string(d.returnType)) |
| 2033 | + .add(d.returnValue) |
| 2034 | + .add(d.scenarioValue); |
| 2035 | + } |
| 2036 | + } |
| 2037 | + report.end(); |
| 2038 | +} |
| 2039 | + |
| 2040 | +void ReportWriter::writeStockSplitReport(const boost::shared_ptr<Scenario>& baseScenario, |
| 2041 | + const boost::shared_ptr<ore::analytics::HistoricalScenarioLoader>& hsloader, |
| 2042 | + const boost::shared_ptr<ore::data::AdjustmentFactors>& adjFactors, |
| 2043 | + const boost::shared_ptr<ore::data::Report>& report) { |
| 2044 | + |
| 2045 | + report->addColumn("EquityId", string()) |
| 2046 | + .addColumn("Date", Date()) |
| 2047 | + .addColumn("Price", double(), 8) |
| 2048 | + .addColumn("Factor", double(), 8) |
| 2049 | + .addColumn("CumulatedFactor", double(), 8) |
| 2050 | + .addColumn("AdjustedPrice", double(), 8); |
| 2051 | + |
| 2052 | + if (adjFactors) { |
| 2053 | + std::set<std::string> names; |
| 2054 | + for (auto const& k : baseScenario->keys()) { |
| 2055 | + if (k.keytype == RiskFactorKey::KeyType::EquitySpot) { |
| 2056 | + names.insert(k.name); |
| 2057 | + } |
| 2058 | + } |
| 2059 | + |
| 2060 | + std::vector<QuantLib::Date> hsdates = hsloader->dates(); |
| 2061 | + |
| 2062 | + for (auto const& name : names) { |
| 2063 | + |
| 2064 | + std::set<QuantLib::Date> dates = adjFactors->dates(name); |
| 2065 | + dates.insert(hsdates.begin(), hsdates.end()); |
| 2066 | + |
| 2067 | + for (auto const& d : dates) { |
| 2068 | + |
| 2069 | + Real price = Null<Real>(); |
| 2070 | + if (std::find(hsdates.begin(), hsdates.end(), d) != hsdates.end()) { |
| 2071 | + auto scen = hsloader->getHistoricalScenario(d); |
| 2072 | + RiskFactorKey rf(RiskFactorKey::KeyType::EquitySpot, name); |
| 2073 | + if (scen->has(rf)) |
| 2074 | + price = scen->get(rf); |
| 2075 | + } |
| 2076 | + Real factor = adjFactors->getFactorContribution(name, d); |
| 2077 | + Real cumFactor = adjFactors->getFactor(name, d); |
| 2078 | + Real adjPrice = price == Null<Real>() ? Null<Real>() : price * cumFactor; |
| 2079 | + |
| 2080 | + report->next().add(name).add(d).add(price).add(factor).add(cumFactor).add(adjPrice); |
| 2081 | + } |
| 2082 | + } |
| 2083 | + } |
| 2084 | + report->end(); |
| 2085 | +} |
| 2086 | + |
| 2087 | +void ReportWriter::writeHistoricalScenarioDistributions( |
| 2088 | + boost::shared_ptr<HistoricalScenarioGenerator>& hsgen, |
| 2089 | + const boost::shared_ptr<ore::analytics::ScenarioSimMarket>& simMarket, |
| 2090 | + const boost::shared_ptr<ore::analytics::ScenarioSimMarketParameters>& simMarketParams, |
| 2091 | + boost::shared_ptr<ore::data::Report> histScenDetailsReport, boost::shared_ptr<ore::data::Report> statReport, |
| 2092 | + boost::shared_ptr<ore::data::Report> distReport, Size distSteps) { |
| 2093 | + |
| 2094 | + // Don't leave it up to the caller to do this |
| 2095 | + simMarket->scenarioGenerator() = hsgen; |
| 2096 | + hsgen->baseScenario() = simMarket->baseScenario(); |
| 2097 | + |
| 2098 | + // If both report pointers are null, return early |
| 2099 | + if (!statReport && !distReport) |
| 2100 | + return; |
| 2101 | + |
| 2102 | + // Make a transformed generator i.e. discount -> zero etc. |
| 2103 | + auto hsgent = boost::make_shared<HistoricalScenarioGeneratorTransform>(hsgen, simMarket, simMarketParams); |
| 2104 | + |
| 2105 | + const vector<RiskFactorKey>& keys = hsgen->baseScenario()->keys(); |
| 2106 | + Size numScen = hsgen->numScenarios(); |
| 2107 | + Date asof = hsgen->baseScenario()->asof(); |
| 2108 | + |
| 2109 | + // Write the statistics report if requested |
| 2110 | + if (statReport) { |
| 2111 | + hsgent->reset(); |
| 2112 | + writeScenarioStatistics(hsgent, keys, numScen, {asof}, *statReport); |
| 2113 | + } |
| 2114 | + |
| 2115 | + // Write the distribution report if requested |
| 2116 | + if (distReport) { |
| 2117 | + QL_REQUIRE(distSteps != Null<Size>(), |
| 2118 | + "When creating a distribution report, a valid distribution step size is required"); |
| 2119 | + hsgent->reset(); |
| 2120 | + writeScenarioDistributions(hsgent, keys, numScen, {asof}, distSteps, *distReport); |
| 2121 | + } |
| 2122 | + |
| 2123 | + // Write the scenario report if requested |
| 2124 | + if (histScenDetailsReport) { |
| 2125 | + hsgent->reset(); |
| 2126 | + writeHistoricalScenarioDetails(hsgent, *histScenDetailsReport); |
| 2127 | + } |
| 2128 | +} |
| 2129 | + |
| 2130 | +void ReportWriter::writeHistoricalScenarios(const boost::shared_ptr<HistoricalScenarioLoader>& hsloader, |
| 2131 | + const boost::shared_ptr<ore::data::Report>& report) { |
| 2132 | + ScenarioWriter sw(nullptr, report); |
| 2133 | + auto scenarios = hsloader->historicalScenarios(); |
| 2134 | + bool writeHeader = true; |
| 2135 | + for (const auto& s : scenarios) { |
| 2136 | + sw.writeScenario(s, writeHeader); |
| 2137 | + writeHeader = false; |
| 2138 | + } |
| 2139 | +} |
| 2140 | + |
2003 | 2141 | } // namespace analytics |
2004 | 2142 | } // namespace ore |
0 commit comments