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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//!
//! # Basic math functions for series
//!
//! This module provides access to many useful function that are not provided by the base Rust.
//!
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// # Finds the maximum value in a slice
///
/// ## Definition
/// Looks for the largest number in the given set.
///
/// ## Inputs
/// - `val`: the slice with the numbers to compare
///
/// Returns the largest in the set.
///
/// ## Example
/// ```
/// # use scilib::math::series::max_slice;
/// let v: Vec<f64> = vec![0.0, 1.2, -0.1, 5.2, 0.254, 2.8];
/// let m: f64 = max_slice(&v);
/// assert_eq!(m, 5.2);
/// ```
/// # Finds the minimum value in a slice
///
/// ## Definition
/// Looks for the smallest number in the given set.
///
/// ## Inputs
/// - `val`: the slice with the numbers to compare
///
/// Returns the smallest in the set.
///
/// ## Example
/// ```
/// # use scilib::math::series::min_slice;
/// let v: Vec<f64> = vec![0.0, 1.2, -0.1, 5.2, 0.254, 2.8];
/// let m: f64 = min_slice(&v);
/// assert_eq!(m, -0.1);
/// ```
/// # Mean value of a series
///
/// ## Definition
/// We follow the mathematical definition of the mean:
/// $$
/// m = \frac{1}{n} \sum^{n}_{i = 1} x_i
/// $$
///
/// ## Inputs
/// - `val`: the slice of the series to compute
///
/// Returns the mean value of the series.
///
/// ## Example
/// ```
/// # use scilib::math::series::mean;
/// # use scilib::range;
/// let x: Vec<f64> = range::linear(0, 5, 6);
/// let m: f64 = mean(&x);
/// assert_eq!(m, 2.5);
/// ```
/// # Variance of a series
///
/// ## Definition
/// The definition of the variance is:
/// $$
/// V = \frac{1}{n - 1} \sum^{n}_{i = 1} (x_i - m)^2
/// $$
/// Where $m$ is the mean of the series and $V$ the variance.
///
/// ## Inputs
/// - `val`: the slice of the series to compute
///
/// ## Example
/// ```
/// # use scilib::math::series::variance;
/// # use scilib::range;
/// let x: Vec<f64> = range::linear(0, 5, 6);
/// let v: f64 = variance(&x);
/// assert!((v - 3.5).abs() < 1e-10);
/// ```
/// # Standard deviation of a series
///
/// ## Definition
/// We follow the mathematical definition of the standard deviation:
/// $$
/// \sigma = \sqrt{ \frac{1}{n} \sum^{n}_{i = 1} (x_i - m)^2 } = \sqrt{V}
/// $$
/// Where $m$ is the mean of the series and $V$ the variance.
///
/// ## Inputs
/// - `val`: the slice of the series to compute
///
/// Returns the standard deviation value of the series.
///
/// ## Example
/// ```
/// # use scilib::math::series::std_dev;
/// # use scilib::range;
/// let x: Vec<f64> = range::linear(0, 5, 6);
/// let s: f64 = std_dev(&x);
/// assert!((s - 1.870828693386).abs() < 1e-10);
/// ```
/// # Skewness of a series
///
/// ## Definition
/// We follow the mathematical definition of the skewness:
/// $$
/// S = \frac{1}{n} \sum_{i-1}^{n} \left( \frac{x_i - m}{\sigma} \right)^3
/// $$
/// Where $m$ is the mean of the series and $\sigma$ the standard deviation.
///
/// ## Inputs
/// - `val`: the slice of the series to compute
///
/// ## Example
/// ```
/// # use scilib::math::basic;
/// # use scilib::math::series::skewness;
/// # use scilib::math::distribution;
/// # use scilib::range;
/// let r: Vec<f64> = range::linear(-10, 10, 10000);
/// let g: Vec<f64> = r.iter().map(|x| basic::gaussian(1.0, 0.0, 1.7, *x)).collect();
/// let s: f64 = skewness(&g);
/// assert!((s - 1.348759).abs() <= 1e-3);
/// ```
/// # Kurtosis of a series
///
/// ## Definition
/// We follow the mathematical definition of the kurtosis:
/// $$
/// S = \left[\frac{1}{n} \sum_{i-1}^{n} \left( \frac{x_i - m}{\sigma} \right)^4 \right] - 3
/// $$
/// Where $m$ is the mean of the series and $\sigma$ the standard deviation.
///
/// ## Inputs
/// - `val`: the slice of the series to compute
///
/// ## Example
/// ```
/// # use scilib::math::basic;
/// # use scilib::math::series::kurtosis;
/// # use scilib::math::distribution;
/// # use scilib::range;
/// let r: Vec<f64> = range::linear(-10, 10, 10000);
/// let g: Vec<f64> = r.iter().map(|x| basic::gaussian(1.0, 0.0, 1.7, *x)).collect();
/// let s: f64 = kurtosis(&g);
/// assert!((s - 0.298867).abs() <= 1e-3);
/// ```
/// # Student's t value
///
/// ## Definition
/// Computes the difference of means between two series. The $t$ value is simply defined as:
/// $$
/// t = \frac{m_a - m_b}{s_D}
/// $$
/// where $m_a$ and $m_b$ are the means of series A and B, respectively. $s_D$ is the pooled invariance
/// of the two series, and is defined as:
/// $$
/// s_D=\sqrt{\frac{\sum_{a}(x_a-m_a)^2+\sum_{b}(x_b-m_b)^2}{N_a+N_b-2}\left(\frac{1}{N_a}+\frac{1}{N_b}\right)}
/// $$
/// where $x_a$ and $x_b$ are the points of their respective series, and $N_a$ and $N_b$ their number of points.
///
/// ## Inputs
/// - `val_a`: first series
/// - `val_b`: second series
///
/// ## Example
/// ```
/// # use scilib::math::basic;
/// # use scilib::math::series::student_t;
/// # use scilib::math::distribution;
/// # use scilib::range;
/// let r: Vec<f64> = range::linear(-10, 10, 1000);
/// let g: Vec<f64> = r.iter().map(|x| basic::gaussian(1.0, 0.0, 1.7, *x)).collect();
/// let h: Vec<f64> = r.iter().map(|x| basic::gaussian(1.1, -0.2, 1.5, *x)).collect();
/// let t = student_t(&g, &h);
/// ```
/// # Pearson r coefficient
///
/// ## Definition
/// The 
/// is a correlation coefficient. Its use is widespread to check the correlation between two series
/// of data points. It is defined as:
/// $$
/// \rho_{X, Y} = \frac{\mathrm{cov}(X, Y)}{\sigma_X\sigma_Y}
/// = \frac{\sum_{i=0}^{n}(x_i - \bar x)(y_i - \bar y)}{\sqrt{\sum_{i=0}^{n} (x_i-\bar x)^2}\sqrt{\sum_{i=0}^{n} (y_i-\bar y)^2}}
/// $$
///
/// ## Inputs
/// - `sample_x`: the first series of values to check
/// - `sample_y`: the second series of values to check
///
/// Returns the Pearson r correlation coefficient between both series.
///
/// ## Example
/// # Min-Max scaling of a series
///
/// ## Definition
/// Min-Max scaling compresses all the data points passed in a series between two arbitrary values a and b.
/// $$
/// x_{s} = \frac{a + (x - min(x))(b - a)}{max(x) - min(x)}
/// $$
///
/// ## Inputs
/// - `val`: the series to scale
/// - `a`: the minimum to scale to
/// - `b`: the maximum to scale to
///
/// Returns the new series between a and b.
///
/// ## Example
/// ```
/// # use scilib::range;
/// # use scilib::math::series::scale_min_max;
/// let x: Vec<f64> = range::linear(1, 6, 7);
/// let n: Vec<f64> = scale_min_max(&x, 2.0, -1.0);
/// assert_eq!(n[0], 2.0);
/// assert_eq!(n[3], 0.5);
/// assert_eq!(n[6], -1.0);
/// ```
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////