27#include <ql/errors.hpp>
30#include <boost/make_shared.hpp>
53 TradeBlock(Size inputDates, Size inputDepth, Size samples)
56 data_ = vector<T>(len, T());
59 Size
index(Size date, Size dep, Size sample)
const {
65 Size
indexT0(Size dep)
const {
return dep; }
67 bool isValid(Size date, Size dep, Size sample)
const {
76 return static_cast<Real
>(this->data_[
indexT0(dep)]);
82 void setT0(Real value, Size dep) {
86 QL_REQUIRE(
value == 0,
"Cannot set nonzero value for T0 dep = " << dep);
90 Real
get(Size date, Size sample, Size dep)
const {
92 if (this->
isValid(date, dep, sample)) {
93 return static_cast<Real
>(this->data_[
index(date, dep, sample)]);
99 void set(Real value, Size date, Size sample, Size dep) {
101 if (this->
isValid(date, dep, sample)) {
102 this->data_[
index(date, dep, sample)] =
static_cast<T
>(
value);
104 QL_REQUIRE(
value == 0,
105 "Cannot set nonzero value for date: " << date <<
", depth: " << dep <<
", sample: " << sample);
111 template <
class Archive>
void serialize(Archive& ar,
const unsigned int) {
122 virtual Size
depth(
const QuantLib::ext::shared_ptr<ore::data::Trade>& t)
const = 0;
128 Size
depth(
const QuantLib::ext::shared_ptr<ore::data::Trade>&)
const override {
return d_; }
153 void init(Date
asof, QuantLib::ext::shared_ptr<ore::data::Portfolio>& portfolio,
const vector<Date>&
dates, Size
samples,
165 for (
const auto& [tid, t] : portfolio->trades()) {
173 while (dateLen <
dates_.size() &&
dates_[dateLen] < t->maturity())
188 Real dateLenAvg = 0.0;
190 dateTotal += b.dateLen;
193 dateLenAvg = dateTotal /
blocks_.size();
202 depthTotal += b.depth;
203 depthAvg = depthTotal /
blocks_.size();
210 const std::vector<QuantLib::Date>&
dates()
const override {
return dates_; }
216 Real
getT0(Size i, Size d)
const override {
217 QL_REQUIRE(i <
blocks_.size(),
"Invalid trade index i " << i);
222 void setT0(Real value, Size i, Size d)
override {
223 QL_REQUIRE(i <
blocks_.size(),
"Invalid trade index i " << i);
229 Real
get(Size i, Size j, Size k, Size d)
const override {
231 return blocks_[i].get(j, k, d);
235 void set(Real value, Size i, Size j, Size k, Size d)
override {
242 void check(Size i, Size j, Size k, Size d)
const {
243 QL_REQUIRE(i <
numIds(),
"Out of bounds on ids (i=" << i <<
")");
244 QL_REQUIRE(j <
numDates(),
"Out of bounds on dates (j=" << j <<
")");
245 QL_REQUIRE(k <
samples(),
"Out of bounds on samples (k=" << k <<
")");
246 QL_REQUIRE(d <
depth(),
"Out of bounds on depth (d=" << d <<
")");
251 template <
class Archive>
void serialize(Archive& ar,
const unsigned int) {
261 std::map<std::string, Size>
ids_;
ConstantDepthCalculator(Size d=1)
Size depth(const QuantLib::ext::shared_ptr< ore::data::Trade > &) const override
virtual Size depth(const QuantLib::ext::shared_ptr< ore::data::Trade > &t) const =0
virtual ~DepthCalculator()
JaggedCube stores the cube in memory using a vector of trade specific blocks.
Size numDates() const override
void setT0(Real value, Size i, Size d) override
Set a value in the cube.
const std::map< std::string, Size > & idsAndIndexes() const override
Get the vector of ids for this cube.
Size numIds() const override
Return the length of each dimension.
Real getT0(Size i, Size d) const override
Get a T0 value from the cube.
void check(Size i, Size j, Size k, Size d) const
const std::vector< QuantLib::Date > & dates() const override
Get the vector of dates for this cube.
void init(Date asof, QuantLib::ext::shared_ptr< ore::data::Portfolio > &portfolio, const vector< Date > &dates, Size samples, const DepthCalculator &dc)
JaggedCube(Date asof, QuantLib::ext::shared_ptr< ore::data::Portfolio > &portfolio, const vector< Date > &dates, Size samples, const DepthCalculator &dc)
Size depth() const override
QuantLib::Date asof() const override
Return the asof date (T0 date)
Real get(Size i, Size j, Size k, Size d) const override
Get a value from the cube.
std::map< std::string, Size > ids_
friend class boost::serialization::access
Size samples() const override
vector< TradeBlock< T > > blocks_
void set(Real value, Size i, Size j, Size k, Size d) override
Set a value in the cube.
vector< QuantLib::Date > dates_
void serialize(Archive &ar, const unsigned int)
JaggedCube(Date asof, QuantLib::ext::shared_ptr< ore::data::Portfolio > &portfolio, const vector< Date > &dates, Size samples, Size depth)
NPV Cube class stores both future and current NPV values.
Real getT0(Size dep) const
bool isValidT0(Size dep) const
Real get(Size date, Size sample, Size dep) const
Size index(Size date, Size dep, Size sample) const
Size indexT0(Size dep) const
TradeBlock(Size inputDates, Size inputDepth, Size samples)
void set(Real value, Size date, Size sample, Size dep)
friend class boost::serialization::access
bool isValid(Size date, Size dep, Size sample) const
void serialize(Archive &ar, const unsigned int)
void setT0(Real value, Size dep)
SafeStack< ValueType > value