-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvec_utils.rs
210 lines (184 loc) · 6.41 KB
/
vec_utils.rs
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
#[macro_use]
extern crate assert_float_eq;
use jiro_nn::vec_utils::*;
#[test]
fn test_avg_vector_empty() {
let vec = vec![];
assert!(avg_vector(&vec).is_nan());
}
#[test]
fn test_avg_vector_single() {
let vec = vec![42.0];
assert_eq!(avg_vector(&vec), 42.0);
}
#[test]
fn test_avg_vector_two() {
let vec = vec![2.0, 4.0];
assert_eq!(avg_vector(&vec), 3.0);
}
#[test]
fn test_avg_vector_negative() {
let vec = vec![-1.0, 0.0, 1.0];
assert_eq!(avg_vector(&vec), 0.0);
}
#[test]
fn test_avg_vector_fractional() {
let vec = vec![1.5, 2.5, 3.5];
assert_eq!(avg_vector(&vec), 2.5);
}
#[test]
fn test_median_vector_empty() {
let vec = vec![];
assert!(median_vector(&vec).is_nan());
}
#[test]
fn test_median_vector_single() {
let vec = vec![42.0];
assert_eq!(median_vector(&vec), 42.0);
}
#[test]
fn test_median_vector_two() {
let vec = vec![2.0, 4.0];
assert_eq!(median_vector(&vec), 3.0);
}
#[test]
fn test_median_vector_negative() {
let vec = vec![-1.0, 0.0, 1.0];
assert_eq!(median_vector(&vec), 0.0);
}
#[test]
fn test_median_vector_fractional() {
let vec = vec![1.5, 2.5, 3.5];
assert_eq!(median_vector(&vec), 2.5);
}
#[test]
fn test_median_vector_even() {
let vec = vec![1.0, 2.0, 3.0, 4.0];
assert_eq!(median_vector(&vec), 2.5);
}
#[test]
fn test_quartiles_vector_even_len() {
let vec = vec![1.0, 2.0, 3.0, 4.0];
let (q1, q2, q3) = quartiles_vector(&vec);
assert_float_relative_eq!(q1, 1.5, 0.00001);
assert_float_relative_eq!(q2, 2.5, 0.00001);
assert_float_relative_eq!(q3, 3.5, 0.00001);
}
#[test]
fn test_quartiles_vector_odd_len() {
let vec = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let (q1, q2, q3) = quartiles_vector(&vec);
assert_float_relative_eq!(q1, 1.5, 0.00001);
assert_float_relative_eq!(q2, 3.0, 0.00001);
assert_float_relative_eq!(q3, 3.5, 0.00001);
}
#[test]
fn test_quartiles_vector_duplicate_values() {
let vec = vec![1.0, 2.0, 2.0, 3.0, 4.0, 4.0, 4.0, 5.0];
let (q1, q2, q3) = quartiles_vector(&vec);
println!("{} {} {}", q1, q2, q3);
assert_float_relative_eq!(q1, 2.0, 0.00001);
assert_float_relative_eq!(q2, 3.5, 0.00001);
assert_float_relative_eq!(q3, 4.0, 0.00001);
}
#[test]
fn test_vector_quartiles_iqr_odd() {
let vals = vec![1.0, 3.0, 5.0, 7.0, 9.0];
let (q1, q2, q3, min, max) = vector_quartiles_iqr(&vals);
assert_float_relative_eq!(q1, 2.0, 0.00001);
assert_float_relative_eq!(q2, 5.0, 0.00001);
assert_float_relative_eq!(q3, 6.0, 0.00001);
assert_float_relative_eq!(min, -4.0, 0.00001);
assert_float_relative_eq!(max, 12.0, 0.00001);
}
#[test]
fn test_vector_quartiles_iqr_even() {
let vals = vec![1.0, 3.0, 5.0, 7.0];
let (q1, q2, q3, min, max) = vector_quartiles_iqr(&vals);
println!("{} {} {}", q1, q2, q3);
assert_float_relative_eq!(q1, 2.0, 0.00001);
assert_float_relative_eq!(q2, 4.0, 0.00001);
assert_float_relative_eq!(q3, 6.0, 0.00001);
assert_float_relative_eq!(min, -4.0, 0.00001);
assert_float_relative_eq!(max, 12.0, 0.00001);
}
#[test]
fn test_normalize_vector() {
let vec = vec![1.0, 2.0, 3.0, 4.0];
let (normalized_vec, min, max) = normalize_vector(&vec);
assert_float_relative_eq!(normalized_vec[0], 0.0, 0.00001);
assert_float_relative_eq!(normalized_vec[1], 0.33333, 0.00001);
assert_float_relative_eq!(normalized_vec[2], 0.66666, 0.00001);
assert_float_relative_eq!(normalized_vec[3], 1.0, 0.00001);
assert_float_relative_eq!(min, 1.0, 0.00001);
assert_float_relative_eq!(max, 4.0, 0.00001);
}
#[test]
fn test_denormalize_vector() {
let vec = vec![0.0, 0.33333, 0.66666, 1.0];
let denormalized_vec = denormalize_vector(&vec, 1.0, 4.0);
assert_float_relative_eq!(denormalized_vec[0], 1.0, 0.00001);
assert_float_relative_eq!(denormalized_vec[1], 2.0, 0.00001);
assert_float_relative_eq!(denormalized_vec[2], 3.0, 0.00001);
assert_float_relative_eq!(denormalized_vec[3], 4.0, 0.00001);
}
#[test]
fn test_normalize_vector2() {
let vec = vec![
vec![1.0, 2.0, 3.0],
vec![4.0, 5.0, 6.0],
vec![7.0, 8.0, 9.0],
];
let (normalized_vec, min, max) = normalize_vector2(&vec);
assert_float_relative_eq!(normalized_vec[0][0], 0.0, 0.00001);
assert_float_relative_eq!(normalized_vec[0][1], 0.125, 0.00001);
assert_float_relative_eq!(normalized_vec[0][2], 0.25, 0.00001);
assert_float_relative_eq!(normalized_vec[1][0], 0.375, 0.00001);
assert_float_relative_eq!(normalized_vec[1][1], 0.5, 0.00001);
assert_float_relative_eq!(normalized_vec[1][2], 0.625, 0.00001);
assert_float_relative_eq!(normalized_vec[2][0], 0.75, 0.00001);
assert_float_relative_eq!(normalized_vec[2][1], 0.875, 0.00001);
assert_float_relative_eq!(normalized_vec[2][2], 1.0, 0.00001);
assert_float_relative_eq!(min, 1.0, 0.00001);
assert_float_relative_eq!(max, 9.0, 0.00001);
}
#[test]
fn test_denormalize_vector2() {
let input = vec![vec![0.0, 0.25], vec![0.5, 0.75], vec![1.0, 1.0]];
let min = 0.0;
let max = 1.0;
let expected_output = vec![
vec![min, (0.25 * (max - min)) + min],
vec![(0.5 * (max - min)) + min, (0.75 * (max - min)) + min],
vec![max, max],
];
let output = denormalize_vector2(&input, min, max);
for i in 0..input.len() {
for j in 0..input[i].len() {
assert_float_relative_eq!(output[i][j], expected_output[i][j], 0.00001);
}
}
}
#[test]
fn test_vectors_correlation() {
let vec1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let vec2 = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let corr = vectors_correlation(&vec1, &vec2);
assert_float_relative_eq!(corr.unwrap(), 1.0, 0.00001);
let vec1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let vec2 = vec![5.0, 4.0, 3.0, 2.0, 1.0];
let corr = vectors_correlation(&vec1, &vec2);
assert_float_relative_eq!(corr.unwrap(), -1.0, 0.00001);
let vec1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let vec2 = vec![2.0, 4.0, 6.0, 8.0, 10.0];
let corr = vectors_correlation(&vec1, &vec2);
assert_float_relative_eq!(corr.unwrap(), 1.0, 0.00001);
let vec1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let vec2 = vec![5.0, 4.0, 3.0, 4.0, 5.0];
let corr = vectors_correlation(&vec1, &vec2);
assert_float_relative_eq!(corr.unwrap(), 0.0, 0.00001);
let vec1 = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let vec2 = vec![3.0, 3.0, 3.0, 4.0, 5.0];
let corr = vectors_correlation(&vec1, &vec2);
assert_float_relative_eq!(corr.unwrap(), 0.88388, 0.00001);
}