Custom Probability Distribution Functions in R
Instruction: Write a function in R that defines a custom probability distribution, including density, distribution, quantile, and random generation functions.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
For X = lower + span × Z with Z following a Beta(a, b) distribution and span = upper − lower > 0, density needs the Jacobian 1/span; the CDF and quantile transform accordingly. The implementation below deliberately supports scalar finite positive shapes and bounded scalar endpoints. It propagates missing x/q/p values through R's distribution functions. It does not implement parameter recycling or log/tail options.
scaled_beta <- function(a, b, lower = 0, upper = 1) { stopifnot(is.numeric(a), is.numeric(b), length(a) == 1L, length(b) == 1L, is.finite(a), is.finite(b), a > 0, b > 0, is.numeric(lower), is.numeric(upper),...