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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate;
use Normal;
/// Performs a one-sample Z-test on the provided data.
///
/// The one-sample Z-test evaluates whether the mean of a sample differs significantly
/// from a known population mean, when the population standard deviation is known.
/// This test assumes the sampling distribution of the mean is approximately normal,
/// which is valid for large sample sizes (n ≥ 30) or when the population is normally distributed.
///
/// # Arguments
///
/// * `data` - An iterator containing the sample data (any type that can be converted to f64).
/// * `pop_mean` - The known population mean to test against.
/// * `pop_std` - The known population standard deviation (must be positive).
/// * `tail` - The type of tail (left, right, or two) for the test.
/// * `alpha` - The significance level (e.g., 0.05 for a 95% confidence interval).
///
/// # Returns
///
/// A `TestResult` struct containing the test statistic, p-value, confidence interval,
/// null/alternative hypotheses, and a boolean indicating whether the null hypothesis should be rejected.
///
/// # Errors
///
/// Returns a `StatError` if:
/// - The data is empty (`EmptyData`)
/// - The population standard deviation is not positive (`ComputeError`)
/// - There are issues with statistical calculations (`ComputeError`)
///
/// # Statistical Background
///
/// The Z-test statistic is calculated as:
/// ```text
/// Z = (sample_mean - population_mean) / (population_std / √n)
/// ```
///
/// Where:
/// - `sample_mean` is the mean of the sample data
/// - `population_mean` is the hypothesized population mean
/// - `population_std` is the known population standard deviation
/// - `n` is the sample size
///
/// # Example
///
/// ```rust
/// use hypors::z::z_test;
/// use hypors::common::TailType;
///
/// let data = vec![1.2, 2.3, 1.9, 2.5, 2.8, 2.1, 1.8, 2.4, 2.0, 2.6];
/// let pop_mean = 2.0; // Known population mean
/// let pop_std = 0.5; // Known population standard deviation
/// let tail = TailType::Two; // Two-tailed test
/// let alpha = 0.05; // 5% significance level
///
/// // Perform the one-sample Z-test
/// let result = z_test(data.iter().copied(), pop_mean, pop_std, tail, alpha).unwrap();
///
/// // Check if the p-value is within a valid range
/// assert!(result.p_value > 0.0 && result.p_value < 1.0);
/// // Verify if the null hypothesis should be rejected
/// assert_eq!(result.reject_null, result.p_value < alpha);
/// ```
///
/// # Example with different data sources
///
/// ```rust
/// use hypors::z::one_sample::z_test;
/// use hypors::common::TailType;
///
/// // Works with arrays
/// let array_data = [98.6, 99.1, 98.8, 99.3, 98.9, 99.0, 98.7, 99.2];
/// let result1 = z_test(array_data.iter().copied(), 99.0, 0.3, TailType::Two, 0.05).unwrap();
///
/// // Works with Vec
/// let vec_data = vec![150.0, 155.0, 148.0, 152.0, 149.0, 153.0];
/// let result2 = z_test(vec_data.iter().copied(), 150.0, 5.0, TailType::Right, 0.05).unwrap();
///
/// // Works with any iterator of numbers
/// let range_data = (95..=105).map(|x| x as f64);
/// let result3 = z_test(range_data, 100.0, 3.0, TailType::Two, 0.01).unwrap();
/// ```
///
/// # When to use Z-test vs t-test
///
/// - **Use Z-test when**: Population standard deviation is known, large sample size (n ≥ 30)
/// - **Use t-test when**: Population standard deviation is unknown, small sample size