24#ifndef quantlib_sequence_statistics_hpp
25#define quantlib_sequence_statistics_hpp
49 template <
class StatisticsType>
54 typedef std::vector<typename StatisticsType::value_type>
value_type;
76 std::vector<Real>
mean()
const;
86 std::vector<Real>
min()
const;
87 std::vector<Real>
max()
const;
114 template <
class Sequence>
115 void add(
const Sequence& sample,
117 add(sample.begin(), sample.end(), weight);
119 template <
class Iterator>
125 QL_REQUIRE(end>begin,
"sample error: end<=begin");
126 Size dimension = std::distance(begin, end);
132 " required, " << std::distance(begin, end) <<
159 template <
class Stat>
164 template <
class Stat>
166 return (stats_.empty()) ? 0 : stats_[0].samples();
169 template <
class Stat>
171 return (stats_.empty()) ? 0.0 : stats_[0].weightSum();
178 #define DEFINE_SEQUENCE_STAT_CONST_METHOD_VOID(METHOD) \
179 template <class Stat> \
181 GenericSequenceStatistics<Stat>::METHOD() const { \
182 for (Size i=0; i<dimension_; i++) \
183 results_[i] = stats_[i].METHOD(); \
198 #undef DEFINE_SEQUENCE_STAT_CONST_METHOD_VOID
202 #define DEFINE_SEQUENCE_STAT_CONST_METHOD_DOUBLE(METHOD) \
203 template <class Stat> \
205 GenericSequenceStatistics<Stat>::METHOD(Real x) const { \
206 for (Size i=0; i<dimension_; i++) \
207 results_[i] = stats_[i].METHOD(x); \
225 #undef DEFINE_SEQUENCE_STAT_CONST_METHOD_DOUBLE
228 template <
class Stat>
232 if (dimension == dimension_) {
233 for (
Size i=0; i<dimension_; ++i)
236 dimension_ = dimension;
237 stats_ = std::vector<Stat>(dimension);
238 results_ = std::vector<Real>(dimension);
240 quadraticSum_ =
Matrix(dimension_, dimension_, 0.0);
242 dimension_ = dimension;
246 template <
class Stat>
248 Real sampleWeight = weightSum();
250 "sampleWeight=0, unsufficient");
252 Real sampleNumber =
static_cast<Real>(samples());
254 "sample number <=1, unsufficient");
256 std::vector<Real> m = mean();
257 Real inv = 1.0/sampleWeight;
259 Matrix result = inv*quadraticSum_;
263 result *= (sampleNumber/(sampleNumber-1.0));
268 template <
class Stat>
270 Matrix correlation = covariance();
272 for (
Size i=0; i<dimension_; i++){
273 for (
Size j=0; j<dimension_; j++){
275 if (variances[i]==0.0) {
276 correlation[i][j] = 1.0;
279 1.0/std::sqrt(variances[i]*variances[j]);
282 if (variances[i]==0.0 && variances[j]==0) {
283 correlation[i][j] = 1.0;
284 }
else if (variances[i]==0.0 || variances[j]==0.0) {
285 correlation[i][j] = 0.0;
288 1.0/std::sqrt(variances[i]*variances[j]);
const Instrument::results * results_
1-D array used in linear algebra.
Statistics analysis of N-dimensional (sequence) data.
std::vector< Real > expectedShortfall(Real percentile) const
std::vector< Real > max() const
std::vector< Real > gaussianShortfall(Real target) const
void add(const Sequence &sample, Real weight=1.0)
std::vector< Real > gaussianAverageShortfall(Real target) const
std::vector< Real > semiDeviation() const
StatisticsType statistics_type
std::vector< Real > gaussianValueAtRisk(Real percentile) const
std::vector< Real > standardDeviation() const
std::vector< Real > min() const
std::vector< Real > shortfall(Real target) const
std::vector< Real > variance() const
void reset(Size dimension=0)
std::vector< Real > gaussianExpectedShortfall(Real percentile) const
std::vector< Real > kurtosis() const
std::vector< Real > errorEstimate() const
std::vector< statistics_type > stats_
std::vector< Real > results_
Matrix covariance() const
returns the covariance Matrix
std::vector< typename StatisticsType::value_type > value_type
std::vector< Real > downsideDeviation() const
std::vector< Real > valueAtRisk(Real percentile) const
std::vector< Real > potentialUpside(Real percentile) const
std::vector< Real > downsideVariance() const
std::vector< Real > percentile(Real y) const
std::vector< Real > regret(Real target) const
std::vector< Real > skewness() const
std::vector< Real > averageShortfall(Real target) const
std::vector< Real > gaussianPotentialUpside(Real percentile) const
std::vector< Real > gaussianPercentile(Real y) const
void add(Iterator begin, Iterator end, Real weight=1.0)
Matrix correlation() const
returns the correlation Matrix
std::vector< Real > semiVariance() const
std::vector< Real > mean() const
GenericSequenceStatistics(Size dimension=0)
Matrix used in linear algebra.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
LinearInterpolation variance
QL_INTEGER Integer
integer number
std::size_t Size
size of a container
statistics tool based on incremental accumulation in the meantime, this is just a wrapper to the boos...
matrix used in linear algebra.
GenericSequenceStatistics< Statistics > SequenceStatistics
default multi-dimensional statistics tool
Matrix outerProduct(const Array &v1, const Array &v2)
GenericSequenceStatistics< IncrementalStatistics > SequenceStatisticsInc
#define DEFINE_SEQUENCE_STAT_CONST_METHOD_DOUBLE(METHOD)
#define DEFINE_SEQUENCE_STAT_CONST_METHOD_VOID(METHOD)
statistics tool with risk measures