// (C) Copyright Matt Borland 2021. // Use, modification and distribution are subject to the // Boost Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_MATH_STATISTICS_Z_TEST_HPP #define BOOST_MATH_STATISTICS_Z_TEST_HPP #include #include #include #include #include #include namespace boost { namespace math { namespace statistics { namespace detail { template ReturnType one_sample_z_test_impl(T sample_mean, T sample_variance, T sample_size, T assumed_mean) { using Real = typename std::tuple_element<0, ReturnType>::type; using std::sqrt; using no_promote_policy = boost::math::policies::policy, boost::math::policies::promote_double>; Real test_statistic = (sample_mean - assumed_mean) / (sample_variance / sqrt(sample_size)); auto z = boost::math::normal_distribution(sample_size - 1); Real pvalue; if(test_statistic > 0) { pvalue = 2*boost::math::cdf(z, -test_statistic); } else { pvalue = 2*boost::math::cdf(z, test_statistic); } return std::make_pair(test_statistic, pvalue); } template ReturnType one_sample_z_test_impl(ForwardIterator begin, ForwardIterator end, typename std::iterator_traits::value_type assumed_mean) { using Real = typename std::tuple_element<0, ReturnType>::type; std::pair temp = mean_and_sample_variance(begin, end); Real mu = std::get<0>(temp); Real s_sq = std::get<1>(temp); return one_sample_z_test_impl(mu, s_sq, Real(std::distance(begin, end)), Real(assumed_mean)); } template ReturnType two_sample_z_test_impl(T mean_1, T variance_1, T size_1, T mean_2, T variance_2, T size_2) { using Real = typename std::tuple_element<0, ReturnType>::type; using std::sqrt; using no_promote_policy = boost::math::policies::policy, boost::math::policies::promote_double>; Real test_statistic = (mean_1 - mean_2) / sqrt(variance_1/size_1 + variance_2/size_2); auto z = boost::math::normal_distribution(size_1 + size_2 - 1); Real pvalue; if(test_statistic > 0) { pvalue = 2*boost::math::cdf(z, -test_statistic); } else { pvalue = 2*boost::math::cdf(z, test_statistic); } return std::make_pair(test_statistic, pvalue); } template ReturnType two_sample_z_test_impl(ForwardIterator begin_1, ForwardIterator end_1, ForwardIterator begin_2, ForwardIterator end_2) { using Real = typename std::tuple_element<0, ReturnType>::type; using std::sqrt; auto n1 = std::distance(begin_1, end_1); auto n2 = std::distance(begin_2, end_2); ReturnType temp_1 = mean_and_sample_variance(begin_1, end_1); Real mean_1 = std::get<0>(temp_1); Real variance_1 = std::get<1>(temp_1); ReturnType temp_2 = mean_and_sample_variance(begin_2, end_2); Real mean_2 = std::get<0>(temp_2); Real variance_2 = std::get<1>(temp_2); return two_sample_z_test_impl(mean_1, variance_1, Real(n1), mean_2, variance_2, Real(n2)); } } // detail template::value, bool>::type = true> inline auto one_sample_z_test(Real sample_mean, Real sample_variance, Real sample_size, Real assumed_mean) -> std::pair { return detail::one_sample_z_test_impl>(sample_mean, sample_variance, sample_size, assumed_mean); } template::value, bool>::type = true> inline auto one_sample_z_test(Real sample_mean, Real sample_variance, Real sample_size, Real assumed_mean) -> std::pair { return detail::one_sample_z_test_impl>(sample_mean, sample_variance, sample_size, assumed_mean); } template::value_type, typename std::enable_if::value, bool>::type = true> inline auto one_sample_z_test(ForwardIterator begin, ForwardIterator end, Real assumed_mean) -> std::pair { return detail::one_sample_z_test_impl>(begin, end, assumed_mean); } template::value_type, typename std::enable_if::value, bool>::type = true> inline auto one_sample_z_test(ForwardIterator begin, ForwardIterator end, Real assumed_mean) -> std::pair { return detail::one_sample_z_test_impl>(begin, end, assumed_mean); } template::value, bool>::type = true> inline auto one_sample_z_test(Container const & v, Real assumed_mean) -> std::pair { return detail::one_sample_z_test_impl>(std::begin(v), std::end(v), assumed_mean); } template::value, bool>::type = true> inline auto one_sample_z_test(Container const & v, Real assumed_mean) -> std::pair { return detail::one_sample_z_test_impl>(std::begin(v), std::end(v), assumed_mean); } template::value_type, typename std::enable_if::value, bool>::type = true> inline auto two_sample_z_test(ForwardIterator begin_1, ForwardIterator end_1, ForwardIterator begin_2, ForwardIterator end_2) -> std::pair { return detail::two_sample_z_test_impl>(begin_1, end_1, begin_2, end_2); } template::value_type, typename std::enable_if::value, bool>::type = true> inline auto two_sample_z_test(ForwardIterator begin_1, ForwardIterator end_1, ForwardIterator begin_2, ForwardIterator end_2) -> std::pair { return detail::two_sample_z_test_impl>(begin_1, end_1, begin_2, end_2); } template::value, bool>::type = true> inline auto two_sample_z_test(Container const & u, Container const & v) -> std::pair { return detail::two_sample_z_test_impl>(std::begin(u), std::end(u), std::begin(v), std::end(v)); } template::value, bool>::type = true> inline auto two_sample_z_test(Container const & u, Container const & v) -> std::pair { return detail::two_sample_z_test_impl>(std::begin(u), std::end(u), std::begin(v), std::end(v)); } }}} // boost::math::statistics #endif // BOOST_MATH_STATISTICS_Z_TEST_HPP