-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathDistributionDecorator_FunctionImputation.R
204 lines (183 loc) · 6.29 KB
/
DistributionDecorator_FunctionImputation.R
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
#' @title Imputed Pdf/Cdf/Quantile/Rand Functions Decorator
#'
#' @description This decorator imputes missing pdf/cdf/quantile/rand methods from R6 Distributions
#' by using strategies dependent on which methods are already present in the distribution. Unlike
#' other decorators, private methods are added to the [Distribution], not public methods.
#' Therefore the underlying public `[Distribution]$pdf`, `[Distribution]$pdf`,
#' `[Distribution]$quantile`, and `[Distribution]$rand` functions stay the same.
#'
#' @template class_decorator
#' @template field_packages
#' @template param_log
#' @template param_logp
#' @template param_simplify
#' @template param_data
#' @template param_lowertail
#' @template param_n
#' @template method_decorate
#'
#' @examples
#' if (requireNamespace("GoFKernel", quietly = TRUE) &&
#' requireNamespace("pracma", quietly = TRUE)) {
#' pdf <- function(x) ifelse(x < 1 | x > 10, 0, 1 / 10)
#'
#' x <- Distribution$new("Test",
#' pdf = pdf,
#' support = set6::Interval$new(1, 10, class = "integer"),
#' type = set6::Naturals$new()
#' )
#' decorate(x, "FunctionImputation", n = 1000)
#'
#' x <- Distribution$new("Test",
#' pdf = pdf,
#' support = set6::Interval$new(1, 10, class = "integer"),
#' type = set6::Naturals$new(),
#' decorators = "FunctionImputation"
#' )
#'
#' x <- Distribution$new("Test",
#' pdf = pdf,
#' support = set6::Interval$new(1, 10, class = "integer"),
#' type = set6::Naturals$new()
#' )
#' FunctionImputation$new()$decorate(x, n = 1000)
#'
#' x$pdf(1:10)
#' x$cdf(1:10)
#' x$quantile(0.42)
#' x$rand(4)
#' }
#' @export
FunctionImputation <- R6Class("FunctionImputation",
inherit = DistributionDecorator,
public = list(
packages = c("pracma", "GoFKernel"),
#' @description
#' Decorates the given distribution with the methods available in this decorator.
#' @param n `(integer(1))`\cr
#' Grid size for imputing functions, cannot be changed after decorating.
#' Generally larger `n` means better accuracy but slower computation, and smaller `n`
#' means worse accuracy and faster computation.
decorate = function(distribution, n = 1000) {
assert_pkgload(self$packages)
if (!testUnivariate(distribution)) {
stop("FunctionImputation is currently only supported for univariate distributions.")
}
if ("FunctionImputation" %in% distribution$decorators) {
message(paste(distribution$name, "is already decorated with FunctionImputation."))
invisible(self)
} else {
pdist <- distribution$.__enclos_env__$private
pdist$.log <- TRUE
pdist$n_grid <- checkmate::assertIntegerish(n)
if (!isPdf(distribution)) {
pdf <- FunctionImputation$private_methods$.pdf
formals(pdf) <- c(formals(pdf), list(self = distribution, private = pdist))
pdist$.pdf <- pdf
pdist$.isPdf <- -1L
}
if (!isCdf(distribution)) {
cdf <- FunctionImputation$private_methods$.cdf
formals(cdf) <- c(formals(cdf), list(self = distribution, private = pdist))
pdist$.cdf <- cdf
pdist$.isCdf <- -1L
}
if (!isQuantile(distribution)) {
quantile <- FunctionImputation$private_methods$.quantile
formals(quantile) <- c(formals(quantile), list(self = distribution, private = pdist))
pdist$.quantile <- quantile
pdist$.isQuantile <- -1L
}
if (!isRand(distribution)) {
rand <- FunctionImputation$private_methods$.rand
formals(rand) <- c(formals(rand), list(self = distribution, private = pdist))
pdist$.rand <- rand
pdist$.isRand <- -1L
}
message(paste(distribution$name, "is now decorated with FunctionImputation."))
pdist$.updateDecorators(c(distribution$decorators, "FunctionImputation"))
invisible(self)
}
}
),
active = list(
#' @field methods
#' Returns the names of the available methods in this decorator.
methods = function() {
names(private)
}
),
private = list(
.pdf = function(x, log = FALSE) {
if (testDiscrete(self)) {
data <- matrix(x, ncol = 1)
pdf <- self$cdf(data = data) - self$cdf(data = data - 1)
} else if (testContinuous(self)) {
message(.distr6$message_numeric)
pdf <- pracma::fderiv(self$cdf, x)
}
if (log) {
pdf <- log(pdf)
}
return(pdf)
},
.cdf = function(x, lower.tail = TRUE, log.p = FALSE) {
message(.distr6$message_numeric)
if (testDiscrete(self)) {
grid_x <- impute_genx(self, private$n_grid)
cdf <- C_NumericCdf_Discrete(
q = x,
x = grid_x,
pdf = self$pdf(grid_x),
lower = lower.tail,
logp = log.p
)
} else {
cdf <- numeric(length(x))
for (i in seq_along(x)) {
cdf[i] <- integrate(self$pdf, self$properties$support$lower, x[i])$value
}
}
return(cdf)
},
.quantile = function(p, lower.tail = TRUE, log.p = FALSE) {
message(.distr6$message_numeric)
data <- p
if (testContinuous(self)) {
x <- self$workingSupport()
lower <- x$lower
upper <- x$upper
quantile <- numeric(length(data))
for (i in seq_along(data)) {
quantile[i] <- suppressMessages(GoFKernel::inverse(self$cdf,
lower = lower, upper = upper
)(data[i]))
}
} else {
x <- impute_genx(self, private$n_grid)
if (isCdf(self) == 1L) {
quantile <- suppressMessages(C_NumericQuantile(data, x, self$cdf(x), lower.tail, log.p))
} else {
cdf <- suppressMessages(C_NumericCdf_Discrete(x, x, self$pdf(x),
lower = TRUE,
logp = FALSE
))
quantile <- C_NumericQuantile(data, x, cdf, lower.tail, log.p)
}
}
return(quantile)
},
.rand = function(n) {
message(.distr6$message_numeric)
if (isQuantile(self) == 1L) {
return(self$quantile(runif(n)))
} else if (isPdf(self) == 1L) {
x <- impute_genx(self, private$n_grid)
return(sample(x, n, TRUE, self$pdf(x)))
} else {
return(self$quantile(runif(n)))
}
}
)
)
.distr6$decorators <- append(.distr6$decorators, list(FunctionImputation = FunctionImputation))