from rusampling import Ru.Ru class, passing
the log-pdf logf, the dimension d (default 1), and any extra arguments that the log-pdf takes.rvs method, passing the number of samples n to generate.So, if f(x) is a pdf in 1 dimension, take 100 samples, by passing the log-pdf logf(x):
t = Ru(log_f) samples = t.rvs(n=100)
An example: sampling from the gamma distribution, passing args that gamma.logpdf take to Ru:
from scipy.stats import gamma t = Ru(gamma.logpdf, a=2) samples = t.rvs(n=50)
An example: parameter sampling from the normal distribution with prior \(1/\sigma\), using some data:
from scipy.stats import norm
import numpy as np
def log_posterior(params, x):
mu = params[0]
sigma = params[1]
log_f = np.sum(norm.logpdf(x, loc=mu, scale=sigma))
log_prior = -np.log(sigma)
return log_f + log_prior
x_normal = [0.95368014, -0.65628003, -0.63509242,
1.34746566, 2.0896044, 0.97076896]
t = Ru(log_posterior, x=x_normal, d=2)
parameter_samples = t.rvs(n=200)