Skip to content

Commit 69b4f17

Browse files
pcaspersjenkins
authored andcommitted
QPR-12028 use acc, fix case where n is zero
1 parent 6a35d84 commit 69b4f17

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

QuantExt/qle/math/randomvariable.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <boost/accumulators/accumulators.hpp>
2929
#include <boost/accumulators/statistics/stats.hpp>
3030
#include <boost/accumulators/statistics/variance.hpp>
31+
#include <boost/accumulators/statistics/mean.hpp>
3132

3233
#include <iostream>
3334
#include <map>
@@ -146,7 +147,7 @@ Filter& Filter::operator=(Filter&& r) {
146147
return *this;
147148
}
148149

149-
Filter::Filter(const Size n, const bool value) : n_(n), constantData_(value), data_(nullptr), deterministic_(true) {}
150+
Filter::Filter(const Size n, const bool value) : n_(n), constantData_(value), data_(nullptr), deterministic_(n != 0) {}
150151

151152
void Filter::clear() {
152153
n_ = 0;
@@ -370,7 +371,7 @@ RandomVariable& RandomVariable::operator=(RandomVariable&& r) {
370371
}
371372

372373
RandomVariable::RandomVariable(const Size n, const Real value, const Real time)
373-
: n_(n), constantData_(value), data_(nullptr), deterministic_(true), time_(time) {}
374+
: n_(n), constantData_(value), data_(nullptr), deterministic_(n != 0), time_(time) {}
374375

375376
RandomVariable::RandomVariable(const Filter& f, const Real valueTrue, const Real valueFalse, const Real time) {
376377
if (!f.initialised()) {
@@ -1169,11 +1170,10 @@ RandomVariable expectation(const RandomVariable& r) {
11691170
if (r.deterministic())
11701171
return r;
11711172
resumeCalcStats();
1172-
Real sum = 0.0;
1173+
boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::mean>> acc;
11731174
for (Size i = 0; i < r.size(); ++i)
1174-
sum += r[i];
1175-
stopCalcStats(r.size());
1176-
return RandomVariable(r.size(), sum / static_cast<Real>(r.size()));
1175+
acc(r[i]);
1176+
return RandomVariable(r.size(), boost::accumulators::mean(acc));
11771177
}
11781178

11791179
RandomVariable variance(const RandomVariable& r) {

0 commit comments

Comments
 (0)