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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/// A module containing functions to work with Lists.
pub struct Generic;
impl Generic {
/// Calculates the mean of a list of numbers.
///
/// The mean is calculated as the sum of all numbers divided by the count of numbers.
///
/// # Parameters
///
/// - `list`: A slice of numbers for which the mean will be calculated.
///
/// # Returns
///
/// The calculated mean.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::Generic;
///
/// let numbers = [3.0, 5.0, 8.0, 12.0, 15.0];
/// let mean = Generic::mean(&numbers);
///
/// println!("Mean: {}", mean);
/// ```
/// <hr/>
pub fn mean(list: &[f64]) -> f64 {
let sum: f64 = list.iter().sum();
let count = list.len() as f64;
sum / count
}
/// Calculates the sum of a list of numbers.
///
/// The sum is calculated as the total of all numbers in the list.
///
/// # Parameters
///
/// - `list`: A slice of numbers to be summed.
///
/// # Returns
///
/// The calculated sum.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::Generic;
///
/// let numbers = [3.0, 5.0, 8.0, 12.0, 15.0];
/// let sum = Generic::sum(&numbers);
///
/// println!("Sum: {}", sum);
/// ```
/// <hr/>
pub fn sum(list: &[f64]) -> f64 {
list.iter().sum()
}
/// Calculates the sum of squared values in a list of numbers.
///
/// Each number in the list is squared, and then the sum of these squared values is calculated.
///
/// # Parameters
///
/// - `list`: A slice of numbers for which the sum of squared values will be calculated.
///
/// # Returns
///
/// The calculated sum of squared values.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::Generic;
///
/// let numbers = [3.0, 5.0, 8.0, 12.0, 15.0];
/// let sum_squared = Generic::sum_squared(&numbers);
///
/// println!("Sum of Squared Values: {}", sum_squared);
/// ```
/// <hr/>
pub fn sum_squared(list: &[f64]) -> f64 {
list.iter().map(|&x| x * x).sum()
}
/// Retrieves the number of elements in a list.
///
/// This function returns the count of elements in the provided list.
///
/// # Parameters
///
/// - `list`: A slice of numbers for which the size will be determined.
///
/// # Returns
///
/// The size (count) of the list.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::Generic;
///
/// let numbers = [3.0, 5.0, 8.0, 12.0, 15.0];
/// let size = Generic::size(&numbers);
///
/// println!("Size of List: {}", size);
/// ```
/// <hr/>
pub fn size(list: &[f64]) -> usize {
list.len()
}
/// Calculates the population variance of a list of numbers.
///
/// The population variance is the average of the squared differences between each number
/// and the mean of the entire population.
///
/// # Parameters
///
/// - `list`: A slice of numbers for which the population variance will be calculated.
///
/// # Returns
///
/// The calculated population variance.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::Generic;
///
/// let numbers = [3.0, 5.0, 8.0, 12.0, 15.0];
/// let variance = Generic::population_variance(&numbers);
///
/// println!("Population Variance: {}", variance);
/// ```
/// <hr/>
pub fn population_variance(list: &[f64]) -> f64 {
let mu = Self::mean(list);
let squared_deviations_sum = list.iter().map(|x| (x - mu).powi(2)).sum::<f64>();
squared_deviations_sum / Self::size(list) as f64
}
/// Calculates the sample variance of a list of numbers.
///
/// The sample variance is an estimate of the variance within a sample from a larger population.
///
/// # Parameters
///
/// - `list`: A slice of numbers for which the sample variance will be calculated.
///
/// # Returns
///
/// The calculated sample variance.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::Generic;
///
/// let numbers = [3.0, 5.0, 8.0, 12.0, 15.0];
/// let sample_variance = Generic::sample_variance(&numbers);
///
/// println!("Sample Variance: {}", sample_variance);
/// ```
/// <hr/>
pub fn sample_variance(list: &[f64]) -> f64 {
let mean = Self::mean(list);
let squared_deviations_sum = list.iter().map(|x| (x - mean).powi(2)).sum::<f64>();
squared_deviations_sum / (Self::size(list) - 1) as f64
}
/// Calculates the population standard deviation of a list of numbers.
///
/// The population standard deviation is the square root of the population variance.
///
/// # Parameters
///
/// - `list`: A slice of numbers for which the population standard deviation will be calculated.
///
/// # Returns
///
/// The calculated population standard deviation.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::Generic;
///
/// let numbers = [3.0, 5.0, 8.0, 12.0, 15.0];
/// let population_sd = Generic::population_sd(&numbers);
///
/// println!("Population Standard Deviation: {}", population_sd);
/// ```
/// <hr/>
pub fn population_sd(list: &[f64]) -> f64 {
Self::population_variance(list).sqrt()
}
/// Calculates the sample standard deviation of a list of numbers.
///
/// The sample standard deviation is calculated using the sample variance.
///
/// # Parameters
///
/// - `list`: A slice of numbers for which the sample standard deviation will be calculated.
///
/// # Returns
///
/// The calculated sample standard deviation.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::Generic;
///
/// let numbers = [3.0, 5.0, 8.0, 12.0, 15.0];
/// let sd = Generic::sample_sd(&numbers);
///
/// println!("Sample Standard Deviation: {}", sd);
/// ```
/// <hr/>
pub fn sample_sd(list: &[f64]) -> f64 {
Self::sample_variance(list).sqrt()
}
/// Calculates the five-number summary of a list of numbers.
///
/// The five-number summary consists of the minimum, first quartile (Q1), median, third quartile (Q3),
/// and maximum of the given list of numbers.
///
/// # Parameters
///
/// - `list`: A slice of numbers for which the five-number summary will be calculated.
///
/// # Returns
///
/// A vector containing the minimum, Q1, median, Q3, and maximum.
///
/// # Example
///
/// ```rust
/// use numerilib::stats::Generic;
///
/// let numbers = [3.0, 5.0, 8.0, 12.0, 15.0, 19.0, 21.0, 25.0, 28.0, 31.0];
/// let summary = Generic::five_number_summary(&numbers);
///
/// println!("Five-Number Summary: {:?}", summary);
/// ```
/// <hr/>
pub fn five_number_summary(list: &[f64]) -> Vec<f64> {
let mut sorted_list = list.to_vec();
sorted_list.sort_by(|a, b| a.partial_cmp(b).unwrap());
let min = sorted_list[0];
let max = sorted_list[sorted_list.len() - 1];
let median_index = (sorted_list.len() as f64 / 2.0).floor() as usize;
let median = if sorted_list.len() % 2 == 0 {
(sorted_list[median_index - 1] + sorted_list[median_index]) / 2.0
} else {
sorted_list[median_index]
};
let lower_half = &sorted_list[..median_index];
let upper_half = &sorted_list[median_index + sorted_list.len() % 2..];
let q1_index = (lower_half.len() as f64 / 2.0).floor() as usize;
let q1 = if lower_half.len() % 2 == 0 {
(lower_half[q1_index - 1] + lower_half[q1_index]) / 2.0
} else {
lower_half[q1_index]
};
let q3_index = (upper_half.len() as f64 / 2.0).floor() as usize;
let q3 = if upper_half.len() % 2 == 0 {
(upper_half[q3_index - 1] + upper_half[q3_index]) / 2.0
} else {
upper_half[q3_index]
};
vec![min, q1, median, q3, max]
}
}