28 #include <boost/concept_check.hpp>
29 #include <boost/function.hpp>
53 template <
class Iterator,
class Value_type>
56 mean(Iterator begin, Iterator end,
const Value_type& )
58 using namespace boost;
59 BOOST_CONCEPT_ASSERT((DefaultConstructible<Value_type>));
60 BOOST_CONCEPT_ASSERT((InputIterator<Iterator>));
63 typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
69 difference_type size = std::distance(begin, end);
71 const Value_type& first = *begin;
72 std::advance(begin, 1);
89 template <
class Iterator>
90 typename std::iterator_traits<Iterator>::value_type
91 mean(Iterator begin, Iterator end = Iterator())
93 using namespace boost;
97 BOOST_CONCEPT_ASSERT((InputIterator<Iterator>));
99 return mean(begin, end, *begin);
130 template<
class InputIterator,
class Distribution,
class Statistic>
135 const Distribution&
dist,
136 const Statistic& statistic,
143 if(num_params + 1 >= num_bins)
148 if(alpha <= 0 || alpha >= 1)
153 int N = std::distance(first, last);
160 std::vector<int> expected;
161 std::vector<int> observed;
163 for(std::map<double, int>::const_iterator pair_p = h.
as_map().begin(); pair_p != h.
as_map().end(); pair_p++)
165 if(pair_p->second < 0)
170 if(pair_p->second > 0)
172 double low_lim = pair_p->first;
173 double up_lim = low_lim + h.
bin_size();
174 int val = round(N * (
cdf(dist, up_lim) -
cdf(dist, low_lim)));
175 expected.push_back(val + (val == 0 ? 1 : 0));
176 observed.push_back(pair_p->second);
180 int num_ne_bins = expected.size();
181 int dof = num_ne_bins - (num_params + 1);
182 double X = statistic(observed.begin(), observed.end(), expected.begin());
185 double q = 1 + ((num_ne_bins + 1) / (6 * N));
188 return !((X / q) > X_alpha);
204 template<
class InputIterator,
class Distribution,
class Statistic>
209 const Distribution& dist,
210 const Statistic& statistic,
215 if(alpha <= 0 || alpha >= 1)
220 int N = std::distance(first, last);
227 std::vector<int> expected;
228 std::vector<int> observed;
230 for(std::map<double, int>::const_iterator pair_p = h.
as_map().begin(); pair_p != h.
as_map().end(); pair_p++)
232 if(pair_p->second < 0)
237 if(pair_p->second > 0)
239 int val = round(N *
pdf(dist, pair_p->first));
240 expected.push_back(val + (val == 0 ? 1 : 0));
241 observed.push_back(pair_p->second);
245 double X = statistic(observed.begin(), observed.end(), expected.begin());
248 return !(X > X_alpha);
258 return ((fabs(O - E) - 0.5) * (fabs(O - E) - 0.5)) / E;
267 template<
class InputIterator>
271 std::vector<double> sum_elems(std::distance(first1, last1));
272 std::transform(first1, last1, first2, sum_elems.begin(), std::ptr_fun(
chi2stat_helper));
294 return O_i * std::log(O_i / E);
303 template<
class InputIterator>
305 double g_statistic(InputIterator first1, InputIterator last1, InputIterator first2)
307 std::vector<double> sum_elems(std::distance(first1, last1));
308 std::transform(first1, last1, first2, sum_elems.begin(), std::ptr_fun(
gstat_helper));
324 template<
class InputIterator,
class Distribution>
330 const Distribution& dist,
337 typedef std::vector<int>::const_iterator IntIterator;
338 boost::function3<double, IntIterator, IntIterator, IntIterator> stat = &chi_square_statistic<IntIterator>;
352 template<
class InputIterator,
class Distribution>
357 const Distribution& dist,
363 typedef std::vector<int>::const_iterator IntIterator;
364 boost::function3<double, IntIterator, IntIterator, IntIterator> stat = &chi_square_statistic<IntIterator>;
379 template<
class InputIterator,
class Distribution>
385 const Distribution& dist,
391 typedef std::vector<int>::const_iterator IntIterator;
392 boost::function3<double, IntIterator, IntIterator, IntIterator> stat = &g_statistic<IntIterator>;
406 template<
class InputIterator,
class Distribution>
411 const Distribution& dist,
416 typedef std::vector<int>::const_iterator IntIterator;
417 boost::function3<double, IntIterator, IntIterator, IntIterator> stat = &g_statistic<IntIterator>;
boost::math::chi_squared Chi_square_distribution
Definition: prob_distribution.h:63
Definition of various standard probability distributions.
double accumulate(const Matrix_d< R, C, T > &mat, double init)
Definition: m_matrix_d.impl.h:432
double chi2stat_helper(int O, int E)
Compute the chi-square test value for a single element.
Definition: prob_stat.h:255
bool goodness_of_fit_test(InputIterator first, InputIterator last, const Distribution &dist, const Statistic &statistic, double alpha, int num_params, int num_bins)
Definition: prob_stat.h:132
double bin_size() const
Return the bin size of this histogram.
Definition: prob_histogram.h:83
double pdf(const MV_gaussian_distribution &P, const Vector &x)
Computes the joint PDF of a multivariate Gaussian at x.
Definition: prob_pdf.cpp:32
bool g_test(InputIterator first, InputIterator last, const Distribution &dist, double alpha, int num_params, int num_bins)
Definition: prob_stat.h:382
double chi_square_statistic(InputIterator first1, InputIterator last1, InputIterator first2)
Definition: prob_stat.h:269
Value_type mean(Iterator begin, Iterator end, const Value_type &)
Definition: prob_stat.h:56
double dist(const pt a, const pt b)
compute approx. Great Circle distance between two UTM points
Definition: layer.cpp:45
PDFs and CDFs for the different distributions defined in "prob_distribution.h".
const std::map< double, int > & as_map() const
Return a vector of the bins, represented by their lower bounds.
Definition: prob_histogram.h:92
double g_statistic(InputIterator first1, InputIterator last1, InputIterator first2)
Definition: prob_stat.h:305
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
sum(zmx.*zmy) sum(zmy.^2)]
bool chi_square_test(InputIterator first, InputIterator last, const Distribution &dist, double alpha, int num_params, int num_bins)
Definition: prob_stat.h:327
A class that represents a histogram of data. ATM, the data must be doubles.
Definition: prob_histogram.h:47
double cdf(const Log_normal_distribution &dist, double x)
Computes the cdf of a log-normal distribution at x.
Definition: prob_pdf.h:151
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
Support for error handling exception classes in libKJB.
double gstat_helper(int O, int E)
Compute the G-test value for a single element.
Definition: prob_stat.h:281
Object thrown when computation fails somehow during execution.
Definition: l_exception.h:321