Define a distribution with a user-supplied log-probability density function. The function must accept a torch tensor and return a scalar torch tensor. It must be differentiable via torch autograd.
Examples
if (FALSE) { # \dontrun{
# Laplace distribution (not built in)
x <- custom_distribution(
log_prob_fn = function(x) -torch_sum(torch_abs(x)),
name = "laplace"
)
# Truncated normal (positive only)
x <- custom_distribution(
log_prob_fn = function(x) {
torch_sum(-0.5 * x^2) # kernel of N(0,1)
},
constraint = list(lower = 0, upper = Inf),
name = "truncated_normal"
)
} # }
