Changeset 4036
- Timestamp:
- Jan 24, 2021, 2:02:57 AM (2 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/Makefile.am
r4035 r4036 135 135 136 136 # tests not passing through yet 137 XFAIL_TESTS = test/percentile_confidence_interval.test137 XFAIL_TESTS = 138 138 139 139 DISTRIBUTED_TESTS = \ -
trunk/test/percentile_confidence_interval.cc
r4035 r4036 29 29 #include <yat/random/copy_k_of_n.h> 30 30 31 #include <gsl/gsl_cdf.h> 32 #include <gsl/gsl_randist.h> 33 31 34 #include <set> 32 35 … … 40 43 for (size_t i=0; i<x.size(); ++i) 41 44 x[i] = i; 42 double m = statistics::median(x.begin(), x.end()); 43 suite.out() << "true median: " << m << "\n"; 45 double p = 25; 46 double q1 = statistics::percentile2(x.begin(), x.end(), p, true); 47 suite.out() << "true 1st quartile: " << q1 << "\n"; 48 49 size_t N = 100; 50 double alpha = 0.05; 51 52 double left_tail = 0; 53 double right_tail = 0; 54 for (size_t k=0; k<=N; ++k) { 55 double cdf = gsl_cdf_binomial_P(k, 0.01*p, N); 56 suite.out() << k << "\t" << gsl_ran_binomial_pdf(k, 0.01*p, N) 57 << "\t" << cdf << "\n"; 58 if (cdf <= alpha/2) { 59 left_tail = cdf; 60 } 61 else if (cdf >= 1.0 - alpha/2) { 62 right_tail = cdf; 63 break; 64 } 65 } 66 suite.out() << "left tail: " << left_tail << "\n"; 67 suite.out() << "right tail: " << 1.0-right_tail << "\n"; 44 68 45 69 statistics::Averager lower; 46 70 statistics::Averager inside; 47 71 statistics::Averager upper; 48 double alpha = 0.95;72 statistics::PercentileConfidenceInterval interval(p, 1.0-alpha); 49 73 for (size_t epoch=0; epoch<10000; ++epoch) { 50 std::vector<double> y(100); 51 random::copy_k_of_n(x.begin(), y.size(), x.size(), y.begin()); 52 statistics::PercentileConfidenceInterval 53 ci(y.begin(), y.end(), 50.0, 100*alpha); 54 if (m < ci.lower()) { 74 std::vector<double> y(N); 75 random::copy_k_of_n(x.begin(), N, x.size(), y.begin()); 76 interval(y.begin(), y.end(), true); 77 if (q1 < interval.lower()) { 55 78 lower.add(1.0); 56 79 upper.add(0.0); 57 80 inside.add(0.0); 58 81 } 59 else if ( m > ci.upper()) {82 else if (q1 > interval.upper()) { 60 83 lower.add(0.0); 61 84 upper.add(1.0); … … 71 94 << 100*inside.mean() << "%\t" 72 95 << 100*upper.mean() << "%\n"; 73 double slack = 0.00 5;74 if (!suite.add(suite.equal_fix(lower.mean(), (1.0-alpha)/2, slack)))96 double slack = 0.002; 97 if (!suite.add(suite.equal_fix(lower.mean(), left_tail, slack))) 75 98 suite.err() << "incorrect inside mean\n"; 76 if (!suite.add(suite.equal_fix(inside.mean(), alpha, 2*slack)))99 if (!suite.add(suite.equal_fix(inside.mean(), right_tail-left_tail, 2*slack))) 77 100 suite.err() << "incorrect inside mean\n"; 78 if (!suite.add(suite.equal_fix(upper.mean(), (1.0-alpha)/2, slack)))101 if (!suite.add(suite.equal_fix(upper.mean(), 1.0-right_tail, slack))) 79 102 suite.err() << "incorrect upper mean\n"; 80 103 return suite.return_value(); -
trunk/yat/statistics/Makefile.am
r4035 r4036 36 36 yat/statistics/LikelihoodRatioTestBinomial.cc \ 37 37 yat/statistics/Pearson.cc \ 38 yat/statistics/PearsonCorrelation.cc yat/statistics/Percentiler.cc \ 38 yat/statistics/PearsonCorrelation.cc \ 39 yat/statistics/PercentileConfidenceInterval.cc \ 40 yat/statistics/Percentiler.cc \ 39 41 yat/statistics/ROC.cc \ 40 42 yat/statistics/SAMScore.cc yat/statistics/Score.cc \ -
trunk/yat/statistics/PercentileConfidenceInterval.h
r4035 r4036 28 28 #include <boost/iterator/iterator_concepts.hpp> 29 29 30 #include <boost/math/distributions/binomial.hpp> 31 30 32 #include <algorithm> 31 33 #include <vector> … … 36 38 37 39 /** 40 Class calculates the confidence interval. It follows the method 41 suggested by Martin Bland in An Introduction to Medical 42 Statistics and <a 43 href=\"https://www-users.york.ac.uk/~mb55/intro/cicent.htm\">here</a>, 44 but uses the exact binomial distribution instead of a 45 large-sample normal approximation. 46 47 You need to load the class with data via the operator() before 48 the interval is available via the lower(void) and upper(void 49 functions. 50 51 A weighted input is currently not supported. 52 38 53 \since New in yat 0.19 39 54 */ … … 42 57 public: 43 58 /** 59 \param k The kth percentile, for example, 50 for median 60 \param level Confidence level, for example, 0.95 for 95% 61 confidence interval 62 */ 63 PercentileConfidenceInterval(double k, double level); 64 65 66 /** 44 67 \param first First element in range 45 68 \param last One past element in range 46 \param k The kth percentile, for example, 50 for median47 \param alpha Level of confidence, for example, 0.95 for 95%48 confidence interval49 69 \param sorted if \c true the range is assumed to be sorted; 50 70 otherwise the range is copied, the copy sorted, before the … … 57 77 */ 58 78 template<typename RandomAccessIterator> 59 PercentileConfidenceInterval(RandomAccessIterator first, 60 RandomAccessIterator last, 61 double k, double alpha, 62 bool sorted=false) 63 : alpha_(alpha), k_(k) 79 void operator()(RandomAccessIterator first, RandomAccessIterator last, 80 bool sorted=false) 64 81 { 65 82 typedef RandomAccessIterator T; … … 68 85 BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<T>)); 69 86 BOOST_CONCEPT_ASSERT((boost::Convertible<value_type, double>)); 70 87 prepare(last - first); 71 88 process(first, last, sorted); 72 89 } … … 75 92 \return alpha, for example 95 for 95% confidence interval 76 93 */ 77 double alpha(void) const { return alpha_; }94 double alpha(void) const; 78 95 79 96 /** 80 97 \return lower confidence interval 81 98 */ 82 double lower(void) const { return lower_; }99 double lower(void) const; 83 100 84 101 /** 85 102 For example, 50, for median. 86 103 */ 87 double k(void) const { return k_; }104 double k(void) const; 88 105 89 106 /** 90 107 \return upper confidence interval 91 108 */ 92 double upper(void) const { return upper_; }109 double upper(void) const; 93 110 94 111 private: 95 112 double alpha_; 96 113 double k_; 114 size_t n_; 115 size_t lower_index_; 97 116 double lower_; 117 size_t upper_index_; 98 118 double upper_; 119 120 void prepare(size_t n); 99 121 100 122 template<typename RandomAccessIterator> … … 106 128 std::sort(v.begin(), v.end()); 107 129 process(v.begin(), v.end(), true); 130 return; 108 131 } 109 110 // number of observations smaller than the percentile is an 111 // obervatioon from a Binomal distribution, Bin(n, p), where p = 112 // 0.01*perc_. 113 114 size_t n = last - first; 115 size_t lower_idx = 0; 116 size_t upper_idx = n - 1; 117 lower_ = first[lower_idx]; 118 upper_ = first[upper_idx]; 132 lower_ = first[lower_index_]; 133 upper_ = first[upper_index_]; 119 134 } 120 135
Note: See TracChangeset
for help on using the changeset viewer.