Some of this material can be found in the stan user guide and this is solely to serve as a reference in my own words.
To implement, you just need to provide a function to increment the total log-probability appropriately.
Note
When a function with the name ending in *_lpdf* or *_lpmf* is defined, the stan compiler automatically makes a *_lupdf* or lupmf version. Only normalised custom distributions are permitted.
Assume that we want to create a custom distribution per:
f_x <-function(x, a){if(a <0| a >1) stop("only defined for a in [0,1]")if(any(x <0| x >1)) stop("only defined for x in [0,1]") (1-a) * x ^-a}F_x <-function(x, a){if(a <0| a >1) stop("only defined for a in [0,1]")if(any(x <0| x >1)) stop("only defined for x in [0,1]") x^(1-a)}F_inv_x <-function(u, a){if(a <0| a >1) stop("only defined for a in [0,1]")if(any(u <0| u >1)) stop("only defined for x in [0,1]") u ^ (1/ (1-a))}a <-0.35x <-seq(0, 1, len =1000)d_fig <-data.table(x = x, y =f_x(x, a))d_sim <-data.table(y_sim =F_inv_x(runif(1e6), a))ggplot(d_fig, aes(x = x, y = y)) +geom_histogram(data = d_sim, aes(x = y_sim, y = ..density..),inherit.aes = F, fill =1, alpha =0.2,binwidth =density(d_sim$y_sim)$bw) +geom_line() +theme_bw()
Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
ℹ Please use `after_stat(density)` instead.