I'm writing code to perform rejection sampling from a Cauchy distribution.
x = np.linspace(-4, 4, 100)
dist = scipy.stats.cauchy
global_max = dist.pdf(0)
This is straightforward. The default parameters of the Cauchy distribution in scipy.stats are (0, 1). In the code below, I generate a million random points, and only accept the points whose y-coordinate lies below the curve, i.e. less than the Cauchy PDF value at the corresponding x-coordinate.
num_samples=1000000
sample_y = np.random.uniform(0, global_max, size=num_samples)
sample_x = np.random.uniform(-4, 4, size=num_samples)
X = sample_x[sample_y <= dist.pdf(sample_x)]
params = scipy.stats.cauchy.fit(X)
Finally I compare the actual distribution to the estimated one:
print('Estimated parameters =', params)
plt.hist(X, bins=50, density=True, alpha=0.3)
plt.plot( x, scipy.stats.cauchy( params[0], params[1] ).pdf(x), color='red' )
plt.plot( x, dist.pdf(x), color='green' );
Output:
Actual parameters = (0, 1)
Estimated parameters = (-0.0030743926369336217, 0.7362620502669363)
I'm unable to understand why this is happening. The variance is significantly different. What am I missing here?


