numrs2 0.3.3

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
Documentation
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use numrs2::array::Array;
use numrs2::random::{self, set_seed};

// This file contains statistical property tests for random distributions.
// Instead of testing specific values, we test that the distributions
// satisfy statistical properties like mean, variance, etc.
// Sample size for statistical tests
const SAMPLE_SIZE: usize = 10000;

// Helper function to calculate the mean of an array
fn calculate_mean(arr: &Array<f64>) -> f64 {
    let data = arr.to_vec();
    let sum: f64 = data.iter().sum();
    sum / data.len() as f64
}

// Helper function to calculate the variance of an array
fn calculate_variance(arr: &Array<f64>, mean: f64) -> f64 {
    let data = arr.to_vec();
    let sum_sq_diff: f64 = data.iter().map(|x| (x - mean).powi(2)).sum();
    sum_sq_diff / data.len() as f64
}

#[test]
fn test_normal_distribution_statistics() {
    // Test normal distributions with different parameters
    let mean = 3.0;
    let std_dev = 2.0;

    set_seed(42);
    let samples = random::normal(mean, std_dev, &[SAMPLE_SIZE]).unwrap();

    // Calculate statistics
    let sample_mean = calculate_mean(&samples);
    let sample_variance = calculate_variance(&samples, sample_mean);
    let _sample_std_dev = sample_variance.sqrt();

    // Check if mean is within expected range
    assert!(
        (sample_mean - mean).abs() < 0.1 * std_dev,
        "Normal distribution: Expected mean close to {}, got {}",
        mean,
        sample_mean
    );

    // Check if variance is within expected range
    assert!(
        (sample_variance - std_dev * std_dev).abs() < 0.15 * std_dev * std_dev,
        "Normal distribution: Expected variance close to {}, got {}",
        std_dev * std_dev,
        sample_variance
    );
}

#[test]
fn test_uniform_distribution_statistics() {
    // Test uniform distribution
    let low = 2.0;
    let high = 7.0;
    let range = high - low;

    set_seed(42);
    let samples = random::uniform(low, high, &[SAMPLE_SIZE]).unwrap();

    // Calculate statistics
    let sample_mean = calculate_mean(&samples);
    let sample_variance = calculate_variance(&samples, sample_mean);

    // Expected values for uniform distribution
    let expected_mean = (low + high) / 2.0;
    let expected_variance = range * range / 12.0;

    // Check if mean is within expected range
    assert!(
        (sample_mean - expected_mean).abs() < 0.1 * range,
        "Uniform distribution: Expected mean close to {}, got {}",
        expected_mean,
        sample_mean
    );

    // Check if variance is within expected range
    assert!(
        (sample_variance - expected_variance).abs() < 0.15 * expected_variance,
        "Uniform distribution: Expected variance close to {}, got {}",
        expected_variance,
        sample_variance
    );

    // Check if all values are within the specified range
    let all_in_range = samples.to_vec().iter().all(|&x| x >= low && x <= high);
    assert!(
        all_in_range,
        "Uniform distribution: Some values are outside [{}, {}]",
        low, high
    );
}

#[test]
fn test_beta_distribution_statistics() {
    // Test beta distribution
    let alpha = 2.0;
    let beta = 5.0;

    set_seed(42);
    let samples = random::beta(alpha, beta, &[SAMPLE_SIZE]).unwrap();

    // Calculate statistics
    let sample_mean = calculate_mean(&samples);
    let sample_variance = calculate_variance(&samples, sample_mean);

    // Expected values for beta distribution
    let expected_mean = alpha / (alpha + beta);
    let expected_variance = (alpha * beta) / ((alpha + beta).powi(2) * (alpha + beta + 1.0));

    // Check if mean is within expected range
    assert!(
        (sample_mean - expected_mean).abs() < 0.1 * expected_mean,
        "Beta distribution: Expected mean close to {}, got {}",
        expected_mean,
        sample_mean
    );

    // Check if variance is within expected range
    assert!(
        (sample_variance - expected_variance).abs() < 0.2 * expected_variance,
        "Beta distribution: Expected variance close to {}, got {}",
        expected_variance,
        sample_variance
    );

    // Check if all values are within [0, 1]
    let all_in_range = samples.to_vec().iter().all(|&x| (0.0..=1.0).contains(&x));
    assert!(
        all_in_range,
        "Beta distribution: Some values are outside [0, 1]"
    );
}

#[test]
fn test_exponential_distribution_statistics() {
    // Test exponential distribution
    let scale = 3.0;

    set_seed(42);
    let samples = random::exponential(scale, &[SAMPLE_SIZE]).unwrap();

    // Calculate statistics
    let sample_mean = calculate_mean(&samples);
    let sample_variance = calculate_variance(&samples, sample_mean);

    // Expected values for exponential distribution
    let expected_mean = scale;
    let expected_variance = scale * scale;

    // Check if mean is within expected range
    assert!(
        (sample_mean - expected_mean).abs() < 0.15 * expected_mean,
        "Exponential distribution: Expected mean close to {}, got {}",
        expected_mean,
        sample_mean
    );

    // Check if variance is within expected range
    assert!(
        (sample_variance - expected_variance).abs() < 0.2 * expected_variance,
        "Exponential distribution: Expected variance close to {}, got {}",
        expected_variance,
        sample_variance
    );

    // Check if all values are positive
    let all_positive = samples.to_vec().iter().all(|&x| x > 0.0);
    assert!(
        all_positive,
        "Exponential distribution: Some values are not positive"
    );
}

#[test]
fn test_gamma_distribution_statistics() {
    // Test gamma distribution
    let shape = 3.0;
    let scale = 2.0;

    set_seed(42);
    let samples = random::gamma(shape, scale, &[SAMPLE_SIZE]).unwrap();

    // Calculate statistics
    let sample_mean = calculate_mean(&samples);
    let sample_variance = calculate_variance(&samples, sample_mean);

    // Expected values for gamma distribution
    let expected_mean = shape * scale;
    let expected_variance = shape * scale * scale;

    // Check if mean is within expected range
    assert!(
        (sample_mean - expected_mean).abs() < 0.15 * expected_mean,
        "Gamma distribution: Expected mean close to {}, got {}",
        expected_mean,
        sample_mean
    );

    // Check if variance is within expected range
    assert!(
        (sample_variance - expected_variance).abs() < 0.2 * expected_variance,
        "Gamma distribution: Expected variance close to {}, got {}",
        expected_variance,
        sample_variance
    );

    // Check if all values are positive
    let all_positive = samples.to_vec().iter().all(|&x| x > 0.0);
    assert!(
        all_positive,
        "Gamma distribution: Some values are not positive"
    );
}

#[test]
fn test_lognormal_distribution_statistics() {
    // Test lognormal distribution
    let mu = 0.0;
    let sigma = 1.0;

    set_seed(42);
    let samples = random::lognormal(mu, sigma, &[SAMPLE_SIZE]).unwrap();

    // Calculate statistics
    let sample_mean = calculate_mean(&samples);
    let sample_variance = calculate_variance(&samples, sample_mean);

    // Expected values for lognormal distribution
    let expected_mean = (mu + sigma * sigma / 2.0).exp();
    let expected_variance = ((sigma * sigma).exp() - 1.0) * (2.0 * mu + sigma * sigma).exp();

    // Check if mean is within expected range
    assert!(
        (sample_mean - expected_mean).abs() < 0.2 * expected_mean,
        "Lognormal distribution: Expected mean close to {}, got {}",
        expected_mean,
        sample_mean
    );

    // Check if variance is within expected range (wider tolerance due to high variance of lognormal)
    assert!(
        (sample_variance - expected_variance).abs() < 0.3 * expected_variance,
        "Lognormal distribution: Expected variance close to {}, got {}",
        expected_variance,
        sample_variance
    );

    // Check if all values are positive
    let all_positive = samples.to_vec().iter().all(|&x| x > 0.0);
    assert!(
        all_positive,
        "Lognormal distribution: Some values are not positive"
    );
}

#[test]
fn test_weibull_distribution_statistics() {
    // Test Weibull distribution
    let shape = 2.0;
    let scale = 3.0;

    set_seed(42);
    let samples = random::weibull(shape, scale, &[SAMPLE_SIZE]).unwrap();

    // Calculate statistics
    let sample_mean = calculate_mean(&samples);

    // Expected mean for Weibull distribution with shape k=2, scale λ=3
    // For Weibull, mean = λ * Γ(1 + 1/k), where Γ is the gamma function
    // With k=2, λ=3: mean = 3 * Γ(1.5) = 3 * sqrt(π)/2 ≈ 2.66
    let expected_mean = 2.66;

    // Check if mean is within expected range
    assert!(
        (sample_mean - expected_mean).abs() < 0.15 * expected_mean,
        "Weibull distribution: Expected mean close to {}, got {}",
        expected_mean,
        sample_mean
    );

    // Check if all values are positive
    let all_positive = samples.to_vec().iter().all(|&x| x > 0.0);
    assert!(
        all_positive,
        "Weibull distribution: Some values are not positive"
    );
}

#[test]
fn test_binomial_distribution_statistics() {
    // Test binomial distribution
    let n = 20u64;
    let p = 0.3;

    set_seed(42);
    let samples = random::binomial::<u64>(n, p, &[SAMPLE_SIZE]).unwrap();

    // Convert to f64 for calculation
    let samples_f64 = Array::from_vec(samples.to_vec().iter().map(|&x| x as f64).collect());

    // Calculate statistics
    let sample_mean = calculate_mean(&samples_f64);
    let sample_variance = calculate_variance(&samples_f64, sample_mean);

    // Expected values for binomial distribution
    let expected_mean = n as f64 * p;
    let expected_variance = n as f64 * p * (1.0 - p);

    // Check if mean is within expected range
    assert!(
        (sample_mean - expected_mean).abs() < 0.1 * expected_mean,
        "Binomial distribution: Expected mean close to {}, got {}",
        expected_mean,
        sample_mean
    );

    // Check if variance is within expected range
    assert!(
        (sample_variance - expected_variance).abs() < 0.15 * expected_variance,
        "Binomial distribution: Expected variance close to {}, got {}",
        expected_variance,
        sample_variance
    );

    // Check if all values are within allowed range [0, n]
    let all_in_range = samples.to_vec().iter().all(|&x| x <= n);
    assert!(
        all_in_range,
        "Binomial distribution: Some values are outside [0, {}]",
        n
    );
}

#[test]
fn test_poisson_distribution_statistics() {
    // Test Poisson distribution
    let lambda = 5.0;

    set_seed(42);
    let samples = random::poisson::<u64>(lambda, &[SAMPLE_SIZE]).unwrap();

    // Convert to f64 for calculation
    let samples_f64 = Array::from_vec(samples.to_vec().iter().map(|&x| x as f64).collect());

    // Calculate statistics
    let sample_mean = calculate_mean(&samples_f64);
    let sample_variance = calculate_variance(&samples_f64, sample_mean);

    // Expected values for Poisson distribution (mean = variance = lambda)
    let expected_mean = lambda;
    let expected_variance = lambda;

    // Check if mean is within expected range
    assert!(
        (sample_mean - expected_mean).abs() < 0.1 * expected_mean,
        "Poisson distribution: Expected mean close to {}, got {}",
        expected_mean,
        sample_mean
    );

    // Check if variance is within expected range
    assert!(
        (sample_variance - expected_variance).abs() < 0.15 * expected_variance,
        "Poisson distribution: Expected variance close to {}, got {}",
        expected_variance,
        sample_variance
    );

    // Check if all values are non-negative integers
    let all_non_negative = samples.to_vec().iter().all(|_x| true);
    assert!(
        all_non_negative,
        "Poisson distribution: Some values are negative"
    );
}

#[test]
#[ignore] // This test may fail due to concurrent test execution affecting global state
fn test_seed_reproducibility() {
    // Test that setting the same seed produces the same results
    // NOTE: This test requires sequential execution as it uses global random state

    // First run with seed 42
    set_seed(42);
    let samples1 = random::normal(0.0, 1.0, &[100]).unwrap();

    // Second run with same seed
    set_seed(42);
    let samples2 = random::normal(0.0, 1.0, &[100]).unwrap();

    // Third run with different seed
    set_seed(43);
    let samples3 = random::normal(0.0, 1.0, &[100]).unwrap();

    // Same seed should produce exactly the same values
    let samples1_vec = samples1.to_vec();
    let samples2_vec = samples2.to_vec();
    assert_eq!(
        samples1_vec, samples2_vec,
        "Same seed should produce identical results"
    );

    // Different seed should produce different values
    let samples3_vec = samples3.to_vec();
    assert_ne!(
        samples1_vec, samples3_vec,
        "Different seeds should produce different results"
    );
}