If has density and , the density of follows from the change of variables formula. When is not one-to-one, every branch of the inverse that maps back into the domain contributes:
Implement two functions:
transformed_pdf(px, branches, y): px is a vectorized density function, branches is a list of (g, g_prime) pairs of vectorized functions (one pair for a monotone map), and y is an array of points. Return the array . Branches are evaluated wherever they are given; each g is responsible for returning a point where px is zero when the branch does not apply.mc_density(sampler, f, edges, n, seed): create the generator rng as np.random.default_rng(seed), draw x = sampler(rng, n), compute y = f(x), count the samples in each histogram bin with np.histogram(y, bins=edges), and return the density estimate counts / (n * widths) as an array, where widths are the bin widths. Divide by the total number of draws n (samples outside the edges are simply lost mass).