Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
historicalsimulationvar.cpp
Go to the documentation of this file.
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
23
24#include <boost/accumulators/accumulators.hpp>
25#include <boost/accumulators/statistics.hpp>
26#include <boost/accumulators/statistics/tail_quantile.hpp>
27
28using namespace boost::accumulators;
29using namespace ore::data;
30using namespace QuantLib;
31
32namespace ore {
33namespace analytics {
34
36 const std::string& baseCurrency, const QuantLib::ext::shared_ptr<Portfolio>& portfolio,
37 const string& portfolioFilter, const vector<Real>& p, boost::optional<TimePeriod> period,
38 const ext::shared_ptr<HistoricalScenarioGenerator>& hisScenGen, std::unique_ptr<FullRevalArgs> fullRevalArgs,
39 const bool breakdown)
40 : VarReport(baseCurrency, portfolio, portfolioFilter, p, period, hisScenGen, nullptr, std::move(fullRevalArgs)) {
41 fullReval_ = true;
42}
43
45 varCalculator_ = QuantLib::ext::make_shared<HistoricalSimulationVarCalculator>(pnls_);
46}
47
48void HistoricalSimulationVarReport::handleFullRevalResults(const ext::shared_ptr<MarketRiskReport::Reports>& reports,
49 const ext::shared_ptr<MarketRiskGroupBase>& riskGroup,
50 const ext::shared_ptr<TradeGroupBase>& tradeGroup) {
52}
53
54Real HistoricalSimulationVarCalculator::var(Real confidence, const bool isCall,
55 const set<pair<string, Size>>& tradeIds) {
56
57 // Use boost to calculate the quantile based on confidence_
58 Size c = static_cast<Size>(std::floor(pnls_.size() * (1.0 - confidence) + 0.5)) + 2;
59 typedef accumulator_set<double, stats<boost::accumulators::tag::tail_quantile<boost::accumulators::right>>>
60 accumulator;
61 accumulator acc(boost::accumulators::tag::tail<boost::accumulators::right>::cache_size = c);
62
63 for (const auto& pnl : pnls_) {
64 acc(isCall ? pnl : -pnl);
65 }
66
67 return quantile(acc, quantile_probability = confidence);
68}
69
70} // namespace analytics
71} // namespace ore
QuantLib::Real var(QuantLib::Real confidence, const bool isCall=true, const std::set< std::pair< std::string, QuantLib::Size > > &tradeIds={}) override
HistoricalSimulationVarReport(const std::string &baseCurrency, const QuantLib::ext::shared_ptr< Portfolio > &portfolio, const std::string &portfolioFilter, const vector< Real > &p, boost::optional< ore::data::TimePeriod > period, const QuantLib::ext::shared_ptr< HistoricalScenarioGenerator > &hisScenGen=nullptr, std::unique_ptr< FullRevalArgs > fullRevalArgs=nullptr, const bool breakdown=false)
void handleFullRevalResults(const QuantLib::ext::shared_ptr< MarketRiskReport::Reports > &reports, const QuantLib::ext::shared_ptr< MarketRiskGroupBase > &riskGroup, const QuantLib::ext::shared_ptr< TradeGroupBase > &tradeGroup) override
boost::optional< ore::data::TimePeriod > period_
std::set< std::pair< std::string, QuantLib::Size > > tradeIdIdxPairs_
QuantLib::ext::shared_ptr< ore::analytics::HistoricalPnlGenerator > histPnlGen_
QuantLib::ext::shared_ptr< VarCalculator > varCalculator_
Class for generating portfolio P&Ls based on historical scenarios.
Perform historical simulation var calculation for a given portfolio.
A cube implementation that stores the cube in memory.