Commit 330a247a authored by Chris Jewell's avatar Chris Jewell
Browse files

Took unnecessary extension of lambda_* datasets out of posterior. Added...

Took unnecessary extension of lambda_* datasets out of posterior.  Added stop() to summary method to prevent barfing if posterior is empty.
parent 87f02d7a
......@@ -1408,11 +1408,11 @@ HaldDP <- R6::R6Class(
private$posterior$sample(private$DPModel_impl)
i = i + 1
}
# Do the updates
lapply(private$updaters, function(x)
x$update())
private$total_iters = private$total_iters + 1
setTxtProgressBar(pb, i)
}
......@@ -1480,7 +1480,7 @@ HaldDP <- R6::R6Class(
sources, types, iters, flatten)
assert_that(is.atomic(drop), is.logical(drop))
if(private$posterior$iters == 0) warning("Empty posterior")
params = params_checked$params
times = params_checked$times
locations = params_checked$locations
......@@ -1597,6 +1597,8 @@ HaldDP <- R6::R6Class(
CI_type = "chen-shao")
{
if(private$posterior$iters == 0) stop("Cannot summarise empty posterior.
Please call update() first.")
object <-
self$extract(params, times, locations, sources, types, iters, flatten, drop = F)
......
......@@ -27,22 +27,20 @@ Posterior_HaldDP = R6::R6Class(
)
)
self$q = to.tensor(NA,
dim=list(
'type' = namesTypes,
'iter' = 1:n_iter
))
dim = list('type' = namesTypes,
'iter' = 1:n_iter))
self$s = to.tensor(NA,
dim=list(
'type' = namesTypes,
'iter' = 1:n_iter
))
self$r = to.tensor(NA,
dim=list(
'type' = namesTypes,
'source' = namesSources,
'time' = namesTimes,
'iter' = 1:n_iter
))
dim = list('type' = namesTypes,
'iter' = 1:n_iter))
self$r = to.tensor(
NA,
dim = list(
'type' = namesTypes,
'source' = namesSources,
'time' = namesTimes,
'iter' = 1:n_iter
)
)
},
sample = function(model) {
# Extend the posterior if we need to
......@@ -69,13 +67,22 @@ Posterior_HaldDP = R6::R6Class(
extend = function(iters) {
# Extends the posterior file by iters iterations
rnames = list(1:(self$maxIters + iters))
self$alpha = arrayextend(self$alpha, along = 4, size = iters, newdimnames=rnames)
self$q = arrayextend(self$q, along = 2, size = iters, newdimnames=rnames)
self$s = arrayextend(self$s, along = 2, size = iters, newdimnames=rnames)
self$r = arrayextend(self$r, along = 4, size = iters, newdimnames=rnames)
self$lambda_j = arrayextend(self$lambda_j, along = 4, size = iters, newdimnames=rnames)
self$lambda_i = arrayextend(self$lambda_i, along = 4, size = iters, newdimnames=rnames)
self$lambda_j_prop = arrayextend(self$lambda_j_prop, along = 4, size = iters, newdimnames=rnames)
self$alpha = arrayextend(self$alpha,
along = 4,
size = iters,
newdimnames = rnames)
self$q = arrayextend(self$q,
along = 2,
size = iters,
newdimnames = rnames)
self$s = arrayextend(self$s,
along = 2,
size = iters,
newdimnames = rnames)
self$r = arrayextend(self$r,
along = 4,
size = iters,
newdimnames = rnames)
self$maxIters = self$maxIters + iters
},
calc_lambda_i = function(n_iter,
......@@ -88,9 +95,15 @@ Posterior_HaldDP = R6::R6Class(
namesIters,
k) {
k = as.tensor(k)
names(k) = c('source','time')
self$lambda_i = self$q * mul.tensor(self$r,i='source',self$alpha * k,j='source',by=c('time','iter'))
self$lambda_i = reorder.tensor(self$lambda_i, c('type','time','location','iter'))
names(k) = c('source', 'time')
self$lambda_i = self$q * mul.tensor(
self$r,
i = 'source',
self$alpha * k,
j = 'source',
by = c('time', 'iter')
)
self$lambda_i = reorder.tensor(self$lambda_i, c('type', 'time', 'location', 'iter'))
},
calc_lambda_j = function(n_iter,
nSources,
......@@ -103,8 +116,9 @@ Posterior_HaldDP = R6::R6Class(
k) {
# tensorA implementation
k = as.tensor(k)
names(k) = c('source','time')
self$lambda_j = self$alpha * mul.tensor(self$r, 'type', self$q, 'type', by='iter') * k
names(k) = c('source', 'time')
self$lambda_j = self$alpha * mul.tensor(self$r, 'type', self$q, 'type', by =
'iter') * k
},
calc_lambda_j_prop = function(n_iter,
nSources,
......@@ -127,7 +141,8 @@ Posterior_HaldDP = R6::R6Class(
namesIters,
k
)
self$lambda_j_prop = to.tensor(apply(self$lambda_j, c('time','location','iter'), function(x) x/sum(x)))
self$lambda_j_prop = to.tensor(apply(self$lambda_j, c('time', 'location', 'iter'), function(x)
x / sum(x)))
names(self$lambda_j_prop)[1] = 'source'
}
)
......
......@@ -78,6 +78,9 @@ test_that("HaldDP model construction", {
model$fit_params(n_iter = 100,
burn_in = 0,
thin = 1)
# Test model barfs if summary called on empty posterior
expect_error(model$summary())
model$update()
expect_equal_to_reference(model$summary(), "haldDPres1.rds")
model$update(n_iter=100)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment