Fast arbitrary distribution random sampling (inverse transform sampling)

前端 未结 5 610
名媛妹妹
名媛妹妹 2020-11-28 07:00

The random module (http://docs.python.org/2/library/random.html) has several fixed functions to randomly sample from. For example random.gauss

5条回答
  •  北海茫月
    2020-11-28 07:10

    I was in a similar situation but I wanted to sample from a multivariate distribution, so, I implemented a rudimentary version of Metropolis-Hastings (which is an MCMC method).

    def metropolis_hastings(target_density, size=500000):
        burnin_size = 10000
        size += burnin_size
        x0 = np.array([[0, 0]])
        xt = x0
        samples = []
        for i in range(size):
            xt_candidate = np.array([np.random.multivariate_normal(xt[0], np.eye(2))])
            accept_prob = (target_density(xt_candidate))/(target_density(xt))
            if np.random.uniform(0, 1) < accept_prob:
                xt = xt_candidate
            samples.append(xt)
        samples = np.array(samples[burnin_size:])
        samples = np.reshape(samples, [samples.shape[0], 2])
        return samples
    

    This function requires a function target_density which takes in a data-point and computes its probability.

    For details check-out this detailed answer of mine.

提交回复
热议问题