37template <
typename Iterator>
42 throw std::invalid_argument(
"statcpp::standard_error: need at least 2 elements");
45 return s / std::sqrt(
static_cast<double>(n));
59template <
typename Iterator,
typename Projection>
64 throw std::invalid_argument(
"statcpp::standard_error: need at least 2 elements");
67 return s / std::sqrt(
static_cast<double>(n));
80template <
typename Iterator>
81double standard_error(Iterator first, Iterator last,
double precomputed_stddev)
85 throw std::invalid_argument(
"statcpp::standard_error: empty range");
87 return precomputed_stddev / std::sqrt(
static_cast<double>(n));
118template <
typename Iterator>
121 if (confidence <= 0.0 || confidence >= 1.0) {
122 throw std::invalid_argument(
"statcpp::ci_mean: confidence must be in (0, 1)");
127 throw std::invalid_argument(
"statcpp::ci_mean: need at least 2 elements");
132 double df =
static_cast<double>(n - 1);
134 double alpha = 1.0 - confidence;
135 double t_crit =
t_quantile(1.0 - alpha / 2.0, df);
137 double margin = t_crit * se;
139 return {mean_val - margin, mean_val + margin, mean_val, confidence};
154template <
typename Iterator,
typename Projection>
157 if (confidence <= 0.0 || confidence >= 1.0) {
158 throw std::invalid_argument(
"statcpp::ci_mean: confidence must be in (0, 1)");
163 throw std::invalid_argument(
"statcpp::ci_mean: need at least 2 elements");
168 double df =
static_cast<double>(n - 1);
170 double alpha = 1.0 - confidence;
171 double t_crit =
t_quantile(1.0 - alpha / 2.0, df);
173 double margin = t_crit * se;
175 return {mean_val - margin, mean_val + margin, mean_val, confidence};
193template <
typename Iterator>
196 if (confidence <= 0.0 || confidence >= 1.0) {
197 throw std::invalid_argument(
"statcpp::ci_mean_z: confidence must be in (0, 1)");
200 throw std::invalid_argument(
"statcpp::ci_mean_z: sigma must be positive");
205 throw std::invalid_argument(
"statcpp::ci_mean_z: empty range");
209 double se = sigma / std::sqrt(
static_cast<double>(n));
211 double alpha = 1.0 - confidence;
214 double margin = z_crit * se;
216 return {mean_val - margin, mean_val + margin, mean_val, confidence};
234 if (confidence <= 0.0 || confidence >= 1.0) {
235 throw std::invalid_argument(
"statcpp::ci_proportion: confidence must be in (0, 1)");
238 throw std::invalid_argument(
"statcpp::ci_proportion: trials must be positive");
240 if (successes > trials) {
241 throw std::invalid_argument(
"statcpp::ci_proportion: successes cannot exceed trials");
244 double p_hat =
static_cast<double>(successes) /
static_cast<double>(trials);
245 double se = std::sqrt(p_hat * (1.0 - p_hat) /
static_cast<double>(trials));
247 double alpha = 1.0 - confidence;
250 double margin = z_crit * se;
251 double lower = std::max(0.0, p_hat - margin);
252 double upper = std::min(1.0, p_hat + margin);
254 return {lower, upper, p_hat, confidence};
271 if (confidence <= 0.0 || confidence >= 1.0) {
272 throw std::invalid_argument(
"statcpp::ci_proportion_wilson: confidence must be in (0, 1)");
275 throw std::invalid_argument(
"statcpp::ci_proportion_wilson: trials must be positive");
277 if (successes > trials) {
278 throw std::invalid_argument(
"statcpp::ci_proportion_wilson: successes cannot exceed trials");
281 double n =
static_cast<double>(trials);
282 double p_hat =
static_cast<double>(successes) / n;
284 double alpha = 1.0 - confidence;
288 double denom = 1.0 + z2 / n;
289 double center = (p_hat + z2 / (2.0 * n)) / denom;
290 double margin = z * std::sqrt((p_hat * (1.0 - p_hat) + z2 / (4.0 * n)) / n) / denom;
292 return {center - margin, center + margin, p_hat, confidence};
309template <
typename Iterator>
312 if (confidence <= 0.0 || confidence >= 1.0) {
313 throw std::invalid_argument(
"statcpp::ci_variance: confidence must be in (0, 1)");
318 throw std::invalid_argument(
"statcpp::ci_variance: need at least 2 elements");
322 double df =
static_cast<double>(n - 1);
324 double alpha = 1.0 - confidence;
328 double lower = df *
var / chi2_upper;
329 double upper = df *
var / chi2_lower;
331 return {lower, upper,
var, confidence};
353template <
typename Iterator1,
typename Iterator2>
355 Iterator2 first2, Iterator2 last2,
356 double confidence = 0.95)
358 if (confidence <= 0.0 || confidence >= 1.0) {
359 throw std::invalid_argument(
"statcpp::ci_mean_diff: confidence must be in (0, 1)");
365 if (n1 < 2 || n2 < 2) {
366 throw std::invalid_argument(
"statcpp::ci_mean_diff: need at least 2 elements in each sample");
374 double diff = mean1 - mean2;
377 double df =
static_cast<double>(n1 + n2 - 2);
378 double sp2 = ((n1 - 1) * var1 + (n2 - 1) * var2) / df;
379 double se = std::sqrt(sp2 * (1.0 / n1 + 1.0 / n2));
381 double alpha = 1.0 - confidence;
382 double t_crit =
t_quantile(1.0 - alpha / 2.0, df);
384 double margin = t_crit * se;
386 return {
diff - margin,
diff + margin,
diff, confidence};
404template <
typename Iterator1,
typename Iterator2>
406 Iterator2 first2, Iterator2 last2,
407 double confidence = 0.95)
409 if (confidence <= 0.0 || confidence >= 1.0) {
410 throw std::invalid_argument(
"statcpp::ci_mean_diff_welch: confidence must be in (0, 1)");
416 if (n1 < 2 || n2 < 2) {
417 throw std::invalid_argument(
"statcpp::ci_mean_diff_welch: need at least 2 elements in each sample");
425 double diff = mean1 - mean2;
427 double se1 = var1 / n1;
428 double se2 = var2 / n2;
429 double se = std::sqrt(se1 + se2);
432 double num = (se1 + se2) * (se1 + se2);
433 double denom = (se1 * se1) / (n1 - 1) + (se2 * se2) / (n2 - 1);
437 throw std::invalid_argument(
"statcpp::ci_mean_diff_welch: cannot compute degrees of freedom with zero variances");
440 double df = num / denom;
442 double alpha = 1.0 - confidence;
443 double t_crit =
t_quantile(1.0 - alpha / 2.0, df);
445 double margin = t_crit * se;
447 return {
diff - margin,
diff + margin,
diff, confidence};
466template <
typename Iterator>
469 if (confidence <= 0.0 || confidence >= 1.0) {
470 throw std::invalid_argument(
"statcpp::margin_of_error_mean: confidence must be in (0, 1)");
475 throw std::invalid_argument(
"statcpp::margin_of_error_mean: need at least 2 elements");
479 double df =
static_cast<double>(n - 1);
480 double alpha = 1.0 - confidence;
481 double t_crit =
t_quantile(1.0 - alpha / 2.0, df);
498template <
typename Iterator,
typename Projection>
501 if (confidence <= 0.0 || confidence >= 1.0) {
502 throw std::invalid_argument(
"statcpp::margin_of_error_mean: confidence must be in (0, 1)");
507 throw std::invalid_argument(
"statcpp::margin_of_error_mean: need at least 2 elements");
511 double df =
static_cast<double>(n - 1);
512 double alpha = 1.0 - confidence;
513 double t_crit =
t_quantile(1.0 - alpha / 2.0, df);
531 if (confidence <= 0.0 || confidence >= 1.0) {
532 throw std::invalid_argument(
"statcpp::margin_of_error_proportion: confidence must be in (0, 1)");
535 throw std::invalid_argument(
"statcpp::margin_of_error_proportion: n must be positive");
538 throw std::invalid_argument(
"statcpp::margin_of_error_proportion: successes cannot exceed n");
541 double p =
static_cast<double>(successes) /
static_cast<double>(n);
542 double se = std::sqrt(p * (1.0 - p) /
static_cast<double>(n));
544 double alpha = 1.0 - confidence;
562 if (confidence <= 0.0 || confidence >= 1.0) {
563 throw std::invalid_argument(
"statcpp::margin_of_error_proportion_worst_case: confidence must be in (0, 1)");
566 throw std::invalid_argument(
"statcpp::margin_of_error_proportion_worst_case: n must be positive");
569 double alpha = 1.0 - confidence;
572 return z_crit * 0.5 / std::sqrt(
static_cast<double>(n));
592 double confidence_level = 0.95,
593 double p_estimate = 0.5)
595 if (margin_of_error <= 0.0 || margin_of_error >= 1.0) {
596 throw std::invalid_argument(
"statcpp::sample_size_for_moe_proportion: margin_of_error must be in (0, 1)");
598 if (confidence_level <= 0.0 || confidence_level >= 1.0) {
599 throw std::invalid_argument(
"statcpp::sample_size_for_moe_proportion: confidence_level must be in (0, 1)");
601 if (p_estimate <= 0.0 || p_estimate >= 1.0) {
602 throw std::invalid_argument(
"statcpp::sample_size_for_moe_proportion: p_estimate must be in (0, 1)");
605 double alpha = 1.0 - confidence_level;
609 double n_exact = std::pow(z / margin_of_error, 2.0) * p_estimate * (1.0 - p_estimate);
612 return static_cast<std::size_t
>(std::ceil(n_exact));
629 double confidence_level = 0.95)
631 if (margin_of_error <= 0.0) {
632 throw std::invalid_argument(
"statcpp::sample_size_for_moe_mean: margin_of_error must be positive");
635 throw std::invalid_argument(
"statcpp::sample_size_for_moe_mean: sigma must be positive");
637 if (confidence_level <= 0.0 || confidence_level >= 1.0) {
638 throw std::invalid_argument(
"statcpp::sample_size_for_moe_mean: confidence_level must be in (0, 1)");
641 double alpha = 1.0 - confidence_level;
645 double n_exact = std::pow(z * sigma / margin_of_error, 2.0);
648 return static_cast<std::size_t
>(std::ceil(n_exact));
668template <
typename Iterator1,
typename Iterator2>
670 Iterator2 first2, Iterator2 last2,
671 double confidence = 0.95)
674 return ci_mean_diff(first1, last1, first2, last2, confidence);
693 std::size_t successes2, std::size_t n2,
694 double confidence = 0.95)
696 if (confidence <= 0.0 || confidence >= 1.0) {
697 throw std::invalid_argument(
"statcpp::ci_proportion_diff: confidence must be in (0, 1)");
699 if (n1 == 0 || n2 == 0) {
700 throw std::invalid_argument(
"statcpp::ci_proportion_diff: sample sizes must be positive");
702 if (successes1 > n1 || successes2 > n2) {
703 throw std::invalid_argument(
"statcpp::ci_proportion_diff: successes cannot exceed sample size");
706 double p1 =
static_cast<double>(successes1) /
static_cast<double>(n1);
707 double p2 =
static_cast<double>(successes2) /
static_cast<double>(n2);
709 double diff = p1 - p2;
712 double se1 = p1 * (1.0 - p1) /
static_cast<double>(n1);
713 double se2 = p2 * (1.0 - p2) /
static_cast<double>(n2);
714 double se = std::sqrt(se1 + se2);
716 double alpha = 1.0 - confidence;
719 double margin = z_crit * se;
721 return {
diff - margin,
diff + margin,
diff, confidence};
Basic statistical computation functions.
Continuous distribution functions.
Dispersion and variance calculation functions.
double normal_quantile(double p, double mu=0.0, double sigma=1.0)
Normal distribution quantile function (inverse CDF, percent point function)
confidence_interval ci_mean(Iterator first, Iterator last, double confidence=0.95)
Calculate confidence interval for mean (t-distribution based)
confidence_interval ci_mean_diff(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2, double confidence=0.95)
Calculate confidence interval for difference of two-sample means (independent samples,...
double sample_stddev(Iterator first, Iterator last)
Sample standard deviation.
double sample_variance(Iterator first, Iterator last)
Sample variance (unbiased variance)
double margin_of_error_proportion_worst_case(std::size_t n, double confidence=0.95)
Calculate worst-case margin of error for proportion.
confidence_interval ci_mean_diff_pooled(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2, double confidence=0.95)
Calculate confidence interval for two-sample mean difference (assuming equal variances)
double var(Iterator first, Iterator last, std::size_t ddof=0)
Variance (ddof = Delta Degrees of Freedom)
std::size_t sample_size_for_moe_proportion(double margin_of_error, double confidence_level=0.95, double p_estimate=0.5)
Calculate sample size for proportion estimation.
double norm_quantile(double p)
Standard normal quantile function.
confidence_interval ci_variance(Iterator first, Iterator last, double confidence=0.95)
Calculate confidence interval for variance (chi-square distribution based)
confidence_interval ci_mean_diff_welch(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2, double confidence=0.95)
Calculate confidence interval for difference of two-sample means (Welch method, not assuming equal va...
std::size_t sample_size_for_moe_mean(double margin_of_error, double sigma, double confidence_level=0.95)
Calculate sample size for mean estimation (known population standard deviation)
double chisq_quantile(double p, double df)
Chi-square distribution quantile function.
confidence_interval ci_proportion_wilson(std::size_t successes, std::size_t trials, double confidence=0.95)
Calculate confidence interval for proportion (Wilson method, recommended)
double mean(Iterator first, Iterator last)
Arithmetic mean.
double t_quantile(double p, double df)
t-distribution quantile function (Newton-Raphson method)
double standard_error(Iterator first, Iterator last)
Calculate standard error of the mean.
std::vector< double > diff(Iterator first, Iterator last, std::size_t order=1)
Difference series (first-order or d-th order differencing)
double margin_of_error_mean(Iterator first, Iterator last, double confidence=0.95)
Calculate margin of error for mean.
double margin_of_error_proportion(std::size_t successes, std::size_t n, double confidence=0.95)
Calculate margin of error for proportion.
confidence_interval ci_mean_z(Iterator first, Iterator last, double sigma, double confidence=0.95)
Calculate confidence interval for mean (z-distribution based, known variance)
std::size_t count(Iterator first, Iterator last)
Data count.
confidence_interval ci_proportion_diff(std::size_t successes1, std::size_t n1, std::size_t successes2, std::size_t n2, double confidence=0.95)
Calculate confidence interval for two-sample proportion difference.
confidence_interval ci_proportion(std::size_t successes, std::size_t trials, double confidence=0.95)
Calculate confidence interval for proportion (Wald method)
Structure to store confidence interval results.
double upper
Upper bound of confidence interval.
double lower
Lower bound of confidence interval.
double confidence_level
Confidence level.
double point_estimate
Point estimate.