File: z_test.qbk

package info (click to toggle)
scipy 1.16.0-1exp7
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 234,820 kB
  • sloc: cpp: 503,145; python: 344,611; ansic: 195,638; javascript: 89,566; fortran: 56,210; cs: 3,081; f90: 1,150; sh: 848; makefile: 785; pascal: 284; csh: 135; lisp: 134; xml: 56; perl: 51
file content (115 lines) | stat: -rw-r--r-- 4,200 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
[/
Copyright (c) 2021 Matt Borland
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)
]

[section:z_test /z/-tests]

[heading Synopsis]

```
#include <boost/math/statistics/z_test.hpp>

namespace boost { namespace math { namespace statistics {

template<typename Real>
std::pair<Real, Real> one_sample_z_test(Real sample_mean, Real sample_variance, Real num_samples, Real assumed_mean);

template<typename ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type>
std::pair<Real, Real> one_sample_z_test(ForwardIterator begin, ForwardIterator end, Real assumed_mean);

template<typename Container>
std::pair<Real, Real> one_sample_z_test(Container const & v, typename Container::value_type assumed_mean);

template<typename ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type>
std::pair<Real, Real> two_sample_z_test(ForwardIterator begin_1, ForwardIterator end_1, ForwardIterator begin_2, ForwardIterator begin_2);

template<typename Container, typename Real = typename Container::value_type>
std::pair<Real, Real> two_sample_z_test(Container const & u, Container const & v);

template<typename ForwardIterator, typename Real = typename std::iterator_traits<ForwardIterator>::value_type>
std::pair<Real, Real> paired_samples_z_test(ForwardIterator begin_1, ForwardIterator end_1, ForwardIterator begin_2, ForwardIterator begin_2);

template<typename Container, typename Real = typename Container::value_type>
std::pair<Real, Real> paired_samples_z_test(Container const & u, Container const & v);

}}}
```

[heading Background]

A set of C++11 compatible functions for various one sample and independent sample /z/-tests. 
The input can be any real number or set of real numbers.
In the event that the input is an integer or a set of integers typename Real will be deduced as a double precision type.

[heading One-sample /z/-test]

A one-sample /z/-test is used to determine whether two population means are different when the variances are known and the sample sizes are large.
The /z/-test is closely related to the /t/-test but can be performed using a large sample size.

[$../graphs/one_sample_z_test_statistic.svg]

where

[$../graphs/one_sample_z_s_value.svg]

with /X/ being the test-statistic and ยต[sub 0] being the assumed mean

the test statistic /X/ can be assumed to come from a uniform real distribution.
Since we wish to know if the sample mean deviates from the true mean in either direction, the test is two-tailed.
Hence the /p/-value is straightforward to calculate from the uniform real distribution on /n/ - 1 degrees of freedom, but nonetheless it is convenient to have it computed here.

An example usage is as follows:

```
#include <vector>
#include <random>
#include <boost/math/statistics/z_test.hpp>

std::random_device rd;
std::mt19937 gen{rd()};
std::normal_distribution<double> dis{0,1};
std::vector<double> v(1024);
for (auto & x : v) {
  x = dis(gen);
}

auto [t, p] = boost::math::statistics::one_sample_z_test(v, 0.0);
```

The test statistic is the first element of the pair, and the /p/-value is the second element.

[heading Independent two-sample /z/-test]

A two-sample /z/-test determines if the means of two sets of data have a statistically significant difference from each other.

[$../graphs/two_sample_z_statistic.svg]
[/Z=\frac{(\bar{X_1}-\bar{X_2})-(\mu_1-\mu_2)}{\sqrt{\sigma_{\bar{X_1}}^2+\sigma_{\bar{X_2}}^2}} = 
\frac{(\bar{X_1}-\bar{X_2})-(\mu_1-\mu_2)}{\sqrt{\frac{\sigma_1^2}{n_1}+\frac{\sigma_1^2}{n_2}}}]
An example of usage is as follows:

```
#include <vector>
#include <random>
#include <boost/math/statistics/z_test.hpp>

std::random_device rd;
std::mt19937 gen{rd()};
std::normal_distribution<double> dis{0,1};
std::vector<double> u(1024);
std::vector<double> v(1024);
for(std::size_t i = 0; i < u.size(); ++i)
{
  u[i] = dis(gen);
  v[i] = dis(gen);
}

auto [t, p] = boost::math::statistics::two_sample_z_test(u, v);
```

/Nota bene:/ The sample sizes for the two sets of data do not need to be equal.

[endsect]
[/section:z_test]