diff --git a/examples/exRand.cpp b/examples/exRand.cpp index 3a288a2..a08a663 100644 --- a/examples/exRand.cpp +++ b/examples/exRand.cpp @@ -32,9 +32,10 @@ int main(void) for (double s = 1.; s < 5.; ++s) { auto ci = h.confidenceInterval(s); - + cout << static_cast(s) << " sigma(s) interval= ["; cout << ci.first << ", " << ci.second << "]" << endl; + cout << "P(X > " << s << ") = " << h.pValue(s) + 1. - h.pValue(-s) << endl; } p << PlotHistogram(h); p << PlotFunction(compile("return exp(-x_0^2/2)/sqrt(2*pi);", 1), -5., 5.); diff --git a/lib/LatAnalyze/Statistics/Histogram.cpp b/lib/LatAnalyze/Statistics/Histogram.cpp index 4f2cf06..463cf55 100644 --- a/lib/LatAnalyze/Statistics/Histogram.cpp +++ b/lib/LatAnalyze/Statistics/Histogram.cpp @@ -162,6 +162,20 @@ double Histogram::operator()(const double x) const return (*this)[static_cast(i)]; } +// p-value P(x > x0) /////////////////////////////////////////////////////////// +double Histogram::pValue(const double x0) const +{ + Index n = data_.size(); + double count = 0.; + + FOR_STAT_ARRAY(data_, s) + { + count += (data_[s] > x0) ? 1. : 0.; + } + + return count/n; +} + // percentiles & confidence interval /////////////////////////////////////////// double Histogram::percentile(const double p) const { diff --git a/lib/LatAnalyze/Statistics/Histogram.hpp b/lib/LatAnalyze/Statistics/Histogram.hpp index 43c24b1..8a3191b 100644 --- a/lib/LatAnalyze/Statistics/Histogram.hpp +++ b/lib/LatAnalyze/Statistics/Histogram.hpp @@ -54,6 +54,8 @@ public: double getX(const Index i) const; double operator[](const Index i) const; double operator()(const double x) const; + // p-value P(x > x0) + double pValue(const double x0) const; // percentiles & confidence interval double percentile(const double p) const; double median(void) const;