from scipy import stats import rejectionSample # Use rejection sampling to estimate the Nakagami distribution with nu = 1.0. # Note that the distribution is only defined for x > 0. The reference # distributin used is a gaussian centered about x = 1 with stdev = 1. [samples, M, tAccept] = rejectionSample.rejectionSampler( \ '2 * 1**1 / gamma(1) * x**(2*1-1) * exp(-1.0*x**2)',\ 'norm(1,1)', 5000, gzOnly=True )
from scipy import stats import rejectionSample # Calculate the samples according to the student t dist. with 2 deg. of # freedom [samples, M, tAccept] = rejectionSample.rejectionSampler( \ '(1/2.0)*exp(-abs(x))', 't(2)', 1000 ) # Use the ks test to test the samples for goodness of fit with the true # laplacian distribution (s, p) = stats.kstest(samples, 'laplace') print '''The p-value from the k-s test of the rejection samples versus the laplace distribution was: %s ''' %(p) # Compare the acceptance rate to that of partb [samples, M, cauchyAccept] = rejectionSample.rejectionSampler( \ '(1/2.0)*exp(-abs(x))', 'cauchy', 1000, verbose=False ) print '''The proportion accepted using the cauchy reference distribution: %s\nThe proportion accepted using the t(2) reference distribution: %s\n%s higher portion of samples were accepted from the t(2) reference ''' \ %(cauchyAccept, tAccept, abs(tAccept - cauchyAccept))
from scipy import stats import rejectionSample # Generate samples using the rejection scheme with the cauchy as a reference [samples, M, proportion] = rejectionSample.rejectionSampler( \ '(1/2.0)*exp(-abs(x))', 'cauchy', 1000 ) # Perform the Kolmogorov-Smirinov test (s, p) = stats.kstest(samples, 'laplace') print '''The p-value from the k-s test of the rejection samples versus the laplace distribution was: %s ''' %(p)