Repository navigation
Expand file tree
/
Copy pathlik.R
More file actions
235 lines (225 loc) · 8.16 KB
/
Copy pathlik.R
File metadata and controls
235 lines (225 loc) · 8.16 KB
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
#' Likelihood Estimation
#'
#' Compute the likelihood of input data, optionally conditioned on some event(s).
#'
#' @param params Circuit parameters learned via \code{\link{forde}}.
#' @param query Data frame of samples, optionally comprising just a subset of
#' training features. Likelihoods will be computed for each sample. Missing
#' features will be marginalized out. See Details.
#' @param evidence Optional set of conditioning events. This can take one of
#' three forms: (1) a partial sample, i.e. a single row of data with some but
#' not all columns; (2) a data frame of conditioning events, which allows for
#' inequalities; or (3) a posterior distribution over leaves. See Details.
#' @param arf Pre-trained \code{\link{adversarial_rf}} or other object of class
#' \code{ranger}. This is not required but speeds up computation considerably
#' for total evidence queries. (Ignored for partial evidence queries.)
#' @param oob Only use out-of-bag leaves for likelihood estimation? If
#' \code{TRUE}, \code{x} must be the same dataset used to train \code{arf}.
#' Only applicable for total evidence queries.
#' @param log Return likelihoods on log scale? Recommended to prevent underflow.
#' @param batch Batch size. The default computes densities for all queries in
#' one round. Splitting into batches lowers peak memory with large samples or
#' many trees, and is what \code{parallel} distributes across workers. This
#' has no impact on results.
#' @param parallel Compute in parallel? Batches are processed in parallel, so
#' this only takes effect when \code{batch} is smaller than
#' \code{nrow(query)}. Requires a registered \code{foreach} backend
#' (\code{doParallel}, \code{doFuture}) or active \code{mirai} daemons.
#' See \code{\link{arf-options}}.
#'
#'
#' @details
#' This function computes the likelihood of input data, optionally conditioned
#' on some event(s). Queries may be partial, i.e. covering some but not all
#' features, in which case excluded variables will be marginalized out.
#'
#' There are three methods for (optionally) encoding conditioning events via the
#' \code{evidence} argument. The first is to provide a partial sample, where
#' some but not all columns from the training data are present. The second is to
#' provide a data frame with three columns: \code{variable}, \code{relation},
#' and \code{value}. This supports inequalities via \code{relation}.
#' Alternatively, users may directly input a pre-calculated posterior
#' distribution over leaves, with columns \code{f_idx} and \code{wt}. This may
#' be preferable for complex constraints. See Examples.
#'
#'
#' @return
#' A vector of likelihoods, optionally on the log scale.
#'
#'
#' @references
#' Watson, D., Blesch, K., Kapar, J., & Wright, M. (2023). Adversarial random
#' forests for density estimation and generative modeling. In \emph{Proceedings
#' of the 26th International Conference on Artificial Intelligence and
#' Statistics}, pp. 5357-5375.
#'
#'
#' @examples
#' # Train ARF and estimate leaf parameters
#' arf <- adversarial_rf(iris)
#' psi <- forde(arf, iris)
#'
#' # Estimate average log-likelihood
#' ll <- lik(psi, iris, arf = arf, log = TRUE)
#' mean(ll)
#'
#' # Identical but slower
#' ll <- lik(psi, iris, log = TRUE)
#' mean(ll)
#'
#' # Partial evidence query
#' lik(psi, query = iris[1, 1:3])
#'
#' # Condition on Species = "setosa"
#' evi <- data.frame(Species = "setosa")
#' lik(psi, query = iris[1, 1:3], evidence = evi)
#'
#' # Condition on Species = "setosa" and Petal.Width > 0.3
#' evi <- data.frame(Species = "setosa",
#' Petal.Width = ">0.3")
#' lik(psi, query = iris[1, 1:3], evidence = evi)
#'
#' \dontrun{
#' # Parallelization with doParallel
#' doParallel::registerDoParallel(cores = 4)
#'
#' # ... or with doFuture
#' doFuture::registerDoFuture()
#' future::plan("multisession", workers = 4)
#'
#' # ... or with mirai (shares large read-only inputs across workers via mori)
#' mirai::daemons(4)
#' }
#'
#' @seealso
#' \code{\link{arf}}, \code{\link{adversarial_rf}}, \code{\link{forde}}, \code{\link{forge}}, \code{\link{expct}}
#'
#'
#' @export
#' @import ranger
#' @import data.table
#' @importFrom stats predict
#' @importFrom foreach foreach %do% %dopar%
#' @importFrom truncnorm dtruncnorm
#'
lik <- function(
params,
query,
evidence = NULL,
arf = NULL,
oob = FALSE,
log = TRUE,
batch = NULL,
parallel = TRUE
) {
# To avoid data.table check issues
tree <- cvg <- leaf <- variable <- mu <- sigma <- value <- obs <- prob <-
V1 <- relation <- f_idx <- wt <- val <- family <- fold <- f_idx_uncond <-
. <- NULL
# Check query
x <- as.data.frame(query)
colnames_x <- colnames(x)
n <- nrow(x)
d <- ncol(x)
if (d == params$meta[, .N] & is.null(arf)) {
warning('For total evidence queries, it is faster to include the ', 'pre-trained arf.')
}
if (!all(colnames(x) %in% params$meta$variable)) {
err <- setdiff(colnames(x), params$meta$variable)
stop('Unrecognized feature(s) among colnames: ', err)
}
x <- suppressWarnings(prep_x(x))
factor_cols <- sapply(x, is.factor)
pure <- all(factor_cols) | !any(factor_cols) # worker arg (was computed inside lik_fn)
# Prep evidence
conj <- !is.null(evidence) && !(ncol(evidence) == 2 && all(c("f_idx", "wt") %in% colnames(evidence)))
# Check ARF
preds <- NULL # set below iff arf-based leaf assignment is used
if (d == params$meta[, .N] & !is.null(arf)) {
num_trees <- arf$num.trees
preds <- stats::predict(arf, x, type = 'terminalNodes')$predictions + 1L
preds <- data.table(
'tree' = rep(seq_len(num_trees), each = n),
'leaf' = as.vector(preds),
'obs' = rep(seq_len(n), times = num_trees)
)
if (isTRUE(oob)) {
preds <- stats::na.omit(preds)
}
preds <- merge(preds, params$forest[, .(tree, leaf, f_idx)], by = c('tree', 'leaf'), sort = FALSE)
setnames(x, params$meta$variable)
} else {
arf <- NULL
setnames(x, colnames_x)
}
# PMF over leaves
if (is.null(evidence)) {
num_trees <- params$forest[, max(tree)]
omega <- params$forest[, .(f_idx, cvg)]
omega[, wt := cvg / num_trees]
omega[, cvg := NULL]
} else if (isTRUE(conj)) {
omega <- cforde(params, evidence, "or")$forest[, .(f_idx = f_idx_uncond, wt = cvg)]
} else {
omega <- evidence
}
omega <- omega[wt > 0]
leaves <- omega[, f_idx]
# Optional batching
if (is.null(batch)) {
batch <- n
}
k <- round(n / batch)
if (k < 1) {
k <- 1L
}
batch_idx <- suppressWarnings(split(seq_len(n), seq_len(k)))
# Per-fold work lives once in arf_lik_fold() (lik_workers.R). This closure
# adapts it to foreach; `arf` is used only via is.null() (preds carries the
# arf-derived leaf assignments), passed to the worker as a boolean.
lik_fn <- function(fold, arf) {
arf_lik_fold(fold, params, x, factor_cols, leaves, omega, preds, batch_idx, pure, !is.null(arf))
}
# Parallelism is across folds: a single fold is inherently serial. mirai only
# for k > 1.
use_mirai <- FALSE
if (k > 1) {
backend <- arf_select_backend(parallel)
use_mirai <- identical(backend, "mirai")
}
if (use_mirai) {
arf_load_on_daemons() # daemons need arf (worker uses bare data.table verbs)
out <- arf_mirai_tree_map(
k,
arf_lik_fold,
list(
params = mori::share(params),
x = mori::share(x),
factor_cols = factor_cols,
leaves = mori::share(leaves),
omega = mori::share(omega),
preds = if (!is.null(preds)) mori::share(preds) else NULL,
batch_idx = mori::share(batch_idx),
pure = pure,
has_arf = !is.null(arf)
)
)
} else if (isTRUE(parallel) && k > 1) {
out <- foreach(fold = seq_len(k), .combine = rbind) %dopar% lik_fn(fold, arf)
} else {
out <- foreach(fold = seq_len(k), .combine = rbind) %do% lik_fn(fold, arf)
}
# Folds return per-obs log-likelihoods, already reduced against omega inside
# the worker (folds cover disjoint obs); nothing left to aggregate here.
# Anybody missing?
zeros <- setdiff(seq_len(n), out[, obs])
if (length(zeros) > 0L) {
zero_dt <- data.table(obs = zeros, lik = -Inf)
out <- rbind(out, zero_dt)
}
# Export
if (!isTRUE(log)) {
out[, lik := exp(lik)]
}
return(out[order(obs), lik])
}