8#ifndef INCLUDED_BDLSTA_MOMENT
9#define INCLUDED_BDLSTA_MOMENT
107#include <bdlscm_version.h>
109#include <bsl_cmath.h>
133template <MomentLevel::Enum ML>
221template <MomentLevel::Enum ML>
345 d_data.d_sum += value;
353 const double delta = value - d_data.d_mean;
354 d_data.d_sum += value;
356 d_data.d_mean = d_data.d_sum /
static_cast<double>(d_data.d_count);
357 const double delta2 = value - d_data.d_mean;
358 d_data.d_M2 += delta * delta2;
366 const double delta = value - d_data.d_mean;
367 const double nm1 = d_data.d_count;
368 d_data.d_sum += value;
370 const double n = d_data.d_count;
371 const double deltaN = delta / n;
372 d_data.d_mean = d_data.d_sum / n;
373 const double term1 = delta * deltaN * nm1;
374 d_data.d_M3 += term1 * deltaN * (n - 2.0) - 3.0 * deltaN * d_data.d_M2;
375 d_data.d_M2 += term1;
384 const double delta = value - d_data.d_mean;
385 const double nm1 = d_data.d_count;
386 d_data.d_sum += value;
388 const double n = d_data.d_count;
389 const double n2 = n * n;
390 const double deltaN = delta / n;
391 d_data.d_mean = d_data.d_sum / n;
392 const double term1 = delta * deltaN * nm1;
393 const double deltaN2 = deltaN * deltaN;
394 d_data.d_M4 += term1 * deltaN2 * (n2 - 3.0 * n + 3.0) +
395 6 * deltaN2 * d_data.d_M2 - 4.0 * deltaN * d_data.d_M3;
396 d_data.d_M3 += term1 * deltaN * (n - 2.0) - 3.0 * deltaN * d_data.d_M2;
397 d_data.d_M2 += term1;
401template <MomentLevel::Enum ML>
405 return d_data.d_count;
412 BSLS_ASSERT(4 <= d_data.d_count && 0.0 != d_data.d_M2);
414 const double n =
static_cast<double>(d_data.d_count);
415 const double n1 = (n - 1.0);
416 const double n2n3 = (n - 2.0) * (n - 3.0);
417 return n * (n + 1.0) * n1 / n2n3 * d_data.d_M4 / d_data.d_M2 / d_data.d_M2
418 - 3.0 * n1 * n1 / n2n3;
425 if (4 > d_data.d_count || 0.0 == d_data.d_M2) {
426 return e_INADEQUATE_DATA;
428 *result = kurtosis();
432template <MomentLevel::Enum ML>
438 return d_data.d_sum /
static_cast<double>(d_data.d_count);
441template <MomentLevel::Enum ML>
445 if (1 > d_data.d_count) {
446 return e_INADEQUATE_DATA;
452template <MomentLevel::Enum ML>
456 BSLS_ASSERT(3 <= d_data.d_count && 0.0 != d_data.d_M2);
458 const double n =
static_cast<double>(d_data.d_count);
459 return bsl::sqrt(n - 1.0) * n / (n- 2.0) * d_data.d_M3
460 / bsl::pow(d_data.d_M2, 1.5);
463template <MomentLevel::Enum ML>
466 if (3 > d_data.d_count || 0.0 == d_data.d_M2) {
467 return e_INADEQUATE_DATA;
473template <MomentLevel::Enum ML>
479 return d_data.d_M2 / (d_data.d_count - 1);
482template <MomentLevel::Enum ML>
486 if (2 > d_data.d_count) {
487 return e_INADEQUATE_DATA;
489 *result = variance();
Definition bdlsta_moment.h:222
double variance() const
Definition bdlsta_moment.h:475
double skew() const
Definition bdlsta_moment.h:454
int count() const
Returns the number of elements in the data set.
Definition bdlsta_moment.h:403
int kurtosisIfValid(double *result) const
@ e_INADEQUATE_DATA
Definition bdlsta_moment.h:237
@ e_SUCCESS
Definition bdlsta_moment.h:236
int varianceIfValid(double *result) const
Definition bdlsta_moment.h:484
double mean() const
Definition bdlsta_moment.h:434
int skewIfValid(double *result) const
Definition bdlsta_moment.h:464
int meanIfValid(double *result) const
Definition bdlsta_moment.h:443
void add(double value)
Add the specified value to the data set.
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlsta_linefit.h:110
Definition bdlsta_moment.h:117
Enum
Definition bdlsta_moment.h:119
@ e_M4
Definition bdlsta_moment.h:125
@ e_M3
Definition bdlsta_moment.h:124
@ e_M1
Definition bdlsta_moment.h:122
@ e_M2
Definition bdlsta_moment.h:123
int d_count
Definition bdlsta_moment.h:141
double d_sum
Definition bdlsta_moment.h:142
double d_sum
Definition bdlsta_moment.h:156
double d_mean
Definition bdlsta_moment.h:157
int d_count
Definition bdlsta_moment.h:155
double d_M2
Definition bdlsta_moment.h:158
double d_mean
Definition bdlsta_moment.h:173
double d_M2
Definition bdlsta_moment.h:174
int d_count
Definition bdlsta_moment.h:171
double d_M3
Definition bdlsta_moment.h:175
double d_sum
Definition bdlsta_moment.h:172
int d_count
Definition bdlsta_moment.h:188
double d_M2
Definition bdlsta_moment.h:191
double d_mean
Definition bdlsta_moment.h:190
double d_M3
Definition bdlsta_moment.h:192
double d_M4
Definition bdlsta_moment.h:193
double d_sum
Definition bdlsta_moment.h:189
Definition bdlsta_moment.h:134