Skip to content

Commit d228cd7

Browse files
authored
Merge pull request #18 from hadley/dev-waldo
Fix tolerance values
2 parents 013042b + b88ff04 commit d228cd7

File tree

2 files changed

+49
-49
lines changed

2 files changed

+49
-49
lines changed

tests/testthat/test-reconc_MixCond.R

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
test_that("reconc_MixCond simple example", {
2-
3-
# Simple example with
2+
3+
# Simple example with
44
# - 12 bottom
55
# - 10 upper: year, 6 bi-monthly, 3 quarterly
66
A <- matrix(data=c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
@@ -14,43 +14,43 @@ test_that("reconc_MixCond simple example", {
1414
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
1515
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1),
1616
nrow=10,byrow = TRUE)
17-
17+
1818
# Define means and vars for the forecasts
1919
means <- c(90,62,63,64,31,32,31,33,31,32,rep(15,12))
2020
vars <- c(20,8,8,8,4,4,4,4,4,4,rep(2,12))^2
21-
21+
2222
# create the lists for reconciliation
23-
## upper
23+
## upper
2424
fc_upper <- list(mu = means[1:10],
2525
Sigma = diag(vars[1:10]))
26-
26+
2727
## bottom
2828
fc_bottom <- list()
2929
for(i in seq(ncol(A))){
3030
fc_bottom[[i]] <-as.integer(.distr_sample(list(mean=means[i+10],sd = vars[i+10]), "gaussian", 2e4))
3131
fc_bottom[[i]][which(fc_bottom[[i]]<0)] <- 0 # set-negative-to-zero
3232
}
33-
34-
33+
34+
3535
res.MixCond <- reconc_MixCond(A,fc_bottom,fc_upper,bottom_in_type = "samples",seed=42)
36-
36+
3737
bott_rec_means <- unlist(lapply(res.MixCond$bottom_reconciled$pmf,PMF.get_mean))
3838
bott_rec_vars <- unlist(lapply(res.MixCond$bottom_reconciled$pmf,PMF.get_var))
39-
40-
39+
40+
4141
# Create PMF from samples
4242
fc_bottom_pmf <- list()
4343
for(i in seq(ncol(A))){
4444
fc_bottom_pmf[[i]] <-PMF.from_samples(fc_bottom[[i]])
4545
}
46-
46+
4747
# Reconcile from bottom PMF
4848
res.MixCond_pmf <- reconc_MixCond(A,fc_bottom_pmf,fc_upper,seed=42)
49-
49+
5050
bott_rec_means_pmf <- unlist(lapply(res.MixCond_pmf$bottom_reconciled$pmf,PMF.get_mean))
5151
bott_rec_vars_pmf <- unlist(lapply(res.MixCond_pmf$bottom_reconciled$pmf,PMF.get_var))
5252

53-
expect_equal(bott_rec_means,bott_rec_means_pmf,tolerance = "3e")
54-
expect_equal(bott_rec_vars,bott_rec_vars_pmf,tolerance = "3e")
53+
expect_equal(bott_rec_means,bott_rec_means_pmf, tolerance = 0.1)
54+
expect_equal(bott_rec_vars,bott_rec_vars_pmf, tolerance = 0.1)
5555

5656
})

tests/testthat/test-sample_funs.R

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,84 @@
11
test_that("sampling from univariate normal", {
2-
2+
33
# Generate 1e4 samples from univariate Gaussian
44
params <- list(mean=42, sd=1)
55
distr <- "gaussian"
66
n <- 1e4
77
samples <- .distr_sample(params, distr, n)
8-
9-
# Compute empirical mean and sd
8+
9+
# Compute empirical mean and sd
1010
sam_mean <- mean(samples)
1111
sam_sd <- sd(samples)
12-
12+
1313
# Check how close empirical values are to the truth
1414
m <- abs(sam_mean-42)/42
1515
s <- abs(sam_sd-1)
16-
16+
1717
expect_equal(m < 2e-3, TRUE)
1818
expect_equal(s < 4e-2, TRUE)
1919
})
2020

2121
test_that("sampling from univariate nbinom", {
22-
22+
2323
# Generate 1e4 samples from negative binomial (size, prob)
2424
params <- list(size=12,prob=0.8)
2525
distr <- "nbinom"
2626
n <- 1e4
2727
samples <- .distr_sample(params, distr, n)
28-
28+
2929
# Compute empirical mean
3030
sam_mean <- mean(samples)
3131
true_mean <- params$size*(1-params$prob)/params$prob
32-
32+
3333
# Check how close empirical values are to the truth
3434
m <- abs(sam_mean-true_mean)/true_mean
3535

3636
expect_equal(m < 3e-2, TRUE)
37-
37+
3838
# Generate 1e4 samples from negative binomial (size, mu)
3939
params <- list(size=12,mu=true_mean)
4040
distr <- "nbinom"
4141
n <- 1e4
4242
samples <- .distr_sample(params, distr, n)
43-
43+
4444
# Compute empirical mean
4545
sam_mean <- mean(samples)
46-
46+
4747
# Check how close empirical values are to the truth
4848
m <- abs(sam_mean-params$mu)/params$mu
49-
49+
5050
expect_equal(m < 3e-2, TRUE)
51-
51+
5252
# Check if it returns an error when all 3 parameters are specified
5353
params <- list(size=12,mu=true_mean,prob=0.8)
5454
distr <- "nbinom"
5555
n <- 1e4
5656
expect_error(.distr_sample(params, distr, n))
57-
57+
5858
# Check if it returns an error when size is not specified
5959
params <- list(mu=true_mean,prob=0.8)
6060
distr <- "nbinom"
6161
n <- 1e4
6262
expect_error(.distr_sample(params, distr, n))
63-
64-
65-
63+
64+
65+
6666
})
6767

6868
test_that("sampling from univariate poisson", {
69-
69+
7070
# Generate 1e4 samples from poisson
7171
params <- list(lambda=10)
7272
distr <- "poisson"
7373
n <- 1e4
7474
samples <- .distr_sample(params, distr, n)
75-
75+
7676
# Compute empirical mean
7777
sam_mean <- mean(samples)
78-
78+
7979
# Check how close empirical values are to the truth
8080
m <- abs(sam_mean-10)/10
81-
81+
8282
expect_equal(m < 3e-2, TRUE)
8383
})
8484

@@ -89,43 +89,43 @@ test_that("sampling from multivariate normal", {
8989
Sigma= matrix(c(1,0.7,0.7,1),nrow = 2)
9090
n <- 1e4
9191
samples <- .MVN_sample(n, mu, Sigma)
92-
92+
9393
# Compute empirical mean
9494
sam_mean <- colMeans(samples)
95-
95+
9696
# Check how close empirical values are to the truth
9797
m <- abs(sam_mean-10)/10
98-
98+
9999
expect_equal(all(m < 8e-3), TRUE)
100100
})
101101

102102
test_that("MVN density works", {
103-
103+
104104
# Create 3x3 covariance matrix
105105
L <- matrix(0,nrow=3,ncol=3)
106106
L[lower.tri(L,diag=TRUE)] <- c(0.9,0.8,0.5,0.9,0.2,0.6)
107107
Sigma <- L%*%t(L)
108-
108+
109109
# create mean vector
110110
mu <- c(0,1,-1)
111-
111+
112112
# matrix where to evaluate the MVN
113113
xx <- matrix(c(0,2,1,
114114
2,3,4,
115115
0.5,0.5,0.5,
116116
0,1,-1), ncol=3,byrow=TRUE)
117-
117+
118118
res <- .MVN_density(x=xx,mu=mu,Sigma=Sigma)
119-
119+
120120
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+
123123
# Check if block-evaluation works
124124
xx <- matrix(runif(3*1e4),ncol=3,byrow=TRUE)
125-
125+
126126
res_chuncks <- .MVN_density(x=xx,mu=mu,Sigma=Sigma)
127127
res_all <- .MVN_density(x=xx,mu=mu,Sigma=Sigma,max_size_x = 1e4)
128-
128+
129129
expect_equal(res_chuncks,res_all)
130-
130+
131131
})

0 commit comments

Comments
 (0)