form_trans {pdqr} | R Documentation |
Transform pdqr-function
Description
Perform a transformation of pdqr-function(s) (which assumed to be independent).
Usage
form_trans(f_list, trans, ..., method = "random", n_sample = 10000,
args_new = list())
form_trans_self(f, trans, ..., method = "random", args_new = list())
Arguments
f_list |
A list consisting from pdqr-function(s) and/or single number(s). Should have at least one pdqr-function (see Details). |
trans |
Transformation function. Should take as many (vectorized)
arguments as there are elements in |
... |
Extra arguments to |
method |
Transformation method. One of "random" or "bruteforce". |
n_sample |
Number of elements to sample. |
args_new |
|
f |
A pdqr-function. |
Details
form_trans_self()
is a thin wrapper for form_trans()
that
accepts a single pdqr-function instead of a list of them.
Class of output is chosen as class of first pdqr-function in
f_list
. Type of output is chosen to be "discrete" in case
all input pdqr-functions have "discrete" type, and "continuous" otherwise.
Method "random" performs transformation using random generation of samples:
-
Generates a sample of size
n_sample
from every element off_list
(if element is single number, it is repeatedn_sample
times). -
Calls
trans
with all generated samples (in order aligned withf_list
). Note that output should be either numeric or logical and haven_sample
elements (one for each combination of input values in "vectorized" fashion). So, for example, usingsum
directly is not possible as it returns only single number. -
Creates output pdqr-function. If output is logical, probability of being true is estimated as share of
TRUE
in output, and boolean pdqr-function is created (type "discrete" with "x" values equal to 0 and 1, and probabilities of being false and true respectively). If output is numeric, one ofnew_*()
(suitable for output class) is called with arguments fromargs_new
list.
Method "bruteforce":
-
Retypes input pdqr-function to "discrete" type (using "piecelin" method).
-
Computes output for every combination of "x" values (probability of which will be a product of corresponding probabilities).
-
Creates pdqr-function of type "discrete" with suitable
new_*()
function. -
Possibly retypes to "continuous" type if output should have it (also with "piecelin" method).
Notes about "bruteforce" method:
Its main advantage is that it is not random.
It may start to be very memory consuming very quickly.
It is usually useful when type of output function is "discrete". In case of "continuous" type, retyping from "discrete" to "continuous" might introduce big errors.
Used "discrete" probabilities shouldn't be very small because they will be directly multiplied, which might cause numerical accuracy issues.
Value
A pdqr-function for transformed random variable.
See Also
Pdqr methods for S3 group generic functions
for more accurate implementations of most commonly used functions. Some of
them are direct (without randomness) and some of them use form_trans()
with "random" method.
form_regrid()
to increase/decrease granularity of pdqr-functions for method
"bruteforce".
Other form functions:
form_estimate()
,
form_mix()
,
form_regrid()
,
form_resupport()
,
form_retype()
,
form_smooth()
,
form_tails()
Examples
# Default "random" transformation
d_norm <- as_d(dnorm)
## More accurate result would give use of `+` directly with: d_norm + d_norm
d_norm_2 <- form_trans(list(d_norm, d_norm), trans = `+`)
plot(d_norm_2)
lines(as_d(dnorm, sd = sqrt(2)), col = "red")
## Input list can have single numbers
form_trans(list(d_norm, 100), trans = `+`)
## Output of `trans` can be logical. Next example is random version of
## `d_norm >= 0`.
form_trans(list(d_norm, 0), trans = `>=`)
# Transformation with "bruteforce" method
power <- function(x, n = 1) {
x^n
}
p_dis <- new_p(
data.frame(x = 1:3, prob = c(0.1, 0.2, 0.7)),
type = "discrete"
)
p_dis_sq <- form_trans_self(
p_dis, trans = power, n = 2, method = "bruteforce"
)
meta_x_tbl(p_dis_sq)
## Compare with "random" method
p_dis_sq_rand <- form_trans_self(p_dis, trans = power, n = 2)
meta_x_tbl(p_dis_sq_rand)
# `form_trans_self()` is a wrapper for `form_trans()`
form_trans_self(d_norm, trans = function(x) {
2 * x
})