1
1
test_that(" sampling from univariate normal" , {
2
-
2
+
3
3
# Generate 1e4 samples from univariate Gaussian
4
4
params <- list (mean = 42 , sd = 1 )
5
5
distr <- " gaussian"
6
6
n <- 1e4
7
7
samples <- .distr_sample(params , distr , n )
8
-
9
- # Compute empirical mean and sd
8
+
9
+ # Compute empirical mean and sd
10
10
sam_mean <- mean(samples )
11
11
sam_sd <- sd(samples )
12
-
12
+
13
13
# Check how close empirical values are to the truth
14
14
m <- abs(sam_mean - 42 )/ 42
15
15
s <- abs(sam_sd - 1 )
16
-
16
+
17
17
expect_equal(m < 2e-3 , TRUE )
18
18
expect_equal(s < 4e-2 , TRUE )
19
19
})
20
20
21
21
test_that(" sampling from univariate nbinom" , {
22
-
22
+
23
23
# Generate 1e4 samples from negative binomial (size, prob)
24
24
params <- list (size = 12 ,prob = 0.8 )
25
25
distr <- " nbinom"
26
26
n <- 1e4
27
27
samples <- .distr_sample(params , distr , n )
28
-
28
+
29
29
# Compute empirical mean
30
30
sam_mean <- mean(samples )
31
31
true_mean <- params $ size * (1 - params $ prob )/ params $ prob
32
-
32
+
33
33
# Check how close empirical values are to the truth
34
34
m <- abs(sam_mean - true_mean )/ true_mean
35
35
36
36
expect_equal(m < 3e-2 , TRUE )
37
-
37
+
38
38
# Generate 1e4 samples from negative binomial (size, mu)
39
39
params <- list (size = 12 ,mu = true_mean )
40
40
distr <- " nbinom"
41
41
n <- 1e4
42
42
samples <- .distr_sample(params , distr , n )
43
-
43
+
44
44
# Compute empirical mean
45
45
sam_mean <- mean(samples )
46
-
46
+
47
47
# Check how close empirical values are to the truth
48
48
m <- abs(sam_mean - params $ mu )/ params $ mu
49
-
49
+
50
50
expect_equal(m < 3e-2 , TRUE )
51
-
51
+
52
52
# Check if it returns an error when all 3 parameters are specified
53
53
params <- list (size = 12 ,mu = true_mean ,prob = 0.8 )
54
54
distr <- " nbinom"
55
55
n <- 1e4
56
56
expect_error(.distr_sample(params , distr , n ))
57
-
57
+
58
58
# Check if it returns an error when size is not specified
59
59
params <- list (mu = true_mean ,prob = 0.8 )
60
60
distr <- " nbinom"
61
61
n <- 1e4
62
62
expect_error(.distr_sample(params , distr , n ))
63
-
64
-
65
-
63
+
64
+
65
+
66
66
})
67
67
68
68
test_that(" sampling from univariate poisson" , {
69
-
69
+
70
70
# Generate 1e4 samples from poisson
71
71
params <- list (lambda = 10 )
72
72
distr <- " poisson"
73
73
n <- 1e4
74
74
samples <- .distr_sample(params , distr , n )
75
-
75
+
76
76
# Compute empirical mean
77
77
sam_mean <- mean(samples )
78
-
78
+
79
79
# Check how close empirical values are to the truth
80
80
m <- abs(sam_mean - 10 )/ 10
81
-
81
+
82
82
expect_equal(m < 3e-2 , TRUE )
83
83
})
84
84
@@ -89,43 +89,43 @@ test_that("sampling from multivariate normal", {
89
89
Sigma = matrix (c(1 ,0.7 ,0.7 ,1 ),nrow = 2 )
90
90
n <- 1e4
91
91
samples <- .MVN_sample(n , mu , Sigma )
92
-
92
+
93
93
# Compute empirical mean
94
94
sam_mean <- colMeans(samples )
95
-
95
+
96
96
# Check how close empirical values are to the truth
97
97
m <- abs(sam_mean - 10 )/ 10
98
-
98
+
99
99
expect_equal(all(m < 8e-3 ), TRUE )
100
100
})
101
101
102
102
test_that(" MVN density works" , {
103
-
103
+
104
104
# Create 3x3 covariance matrix
105
105
L <- matrix (0 ,nrow = 3 ,ncol = 3 )
106
106
L [lower.tri(L ,diag = TRUE )] <- c(0.9 ,0.8 ,0.5 ,0.9 ,0.2 ,0.6 )
107
107
Sigma <- L %*% t(L )
108
-
108
+
109
109
# create mean vector
110
110
mu <- c(0 ,1 ,- 1 )
111
-
111
+
112
112
# matrix where to evaluate the MVN
113
113
xx <- matrix (c(0 ,2 ,1 ,
114
114
2 ,3 ,4 ,
115
115
0.5 ,0.5 ,0.5 ,
116
116
0 ,1 ,- 1 ), ncol = 3 ,byrow = TRUE )
117
-
117
+
118
118
res <- .MVN_density(x = xx ,mu = mu ,Sigma = Sigma )
119
-
119
+
120
120
true_val <- c(8.742644e-04 , 1.375497e-11 , 3.739985e-03 , 1.306453e-01 )
121
- expect_equal(res ,true_val ,tolerance = " 3e " )
122
-
121
+ expect_equal(res ,true_val ,tolerance = 0.1 )
122
+
123
123
# Check if block-evaluation works
124
124
xx <- matrix (runif(3 * 1e4 ),ncol = 3 ,byrow = TRUE )
125
-
125
+
126
126
res_chuncks <- .MVN_density(x = xx ,mu = mu ,Sigma = Sigma )
127
127
res_all <- .MVN_density(x = xx ,mu = mu ,Sigma = Sigma ,max_size_x = 1e4 )
128
-
128
+
129
129
expect_equal(res_chuncks ,res_all )
130
-
130
+
131
131
})
0 commit comments