I'm trying to sample values from a Cauchy distribution, using what is called (I think) inverse transform sampling. My program is as follows:
from numpy import*
from matplotlib.pyplot import*
from scipy.interpolate import interp1d
def f(x):
return 1 / (pi*(1+x**2)) # Cauchy
x = linspace(-6,+6,100)
dx = x[1]-x[0]
cdf = cumsum(f(x))*dx
plt.plot(x,cdf)
plt.xlabel('X')
plt.ylabel('CDF')
plt.show()
from_cdf_to_x = interp1d(cdf, x)
cdf_new = np.random.uniform(0,1,100)
x_sampled = from_cdf_to_x(cdf_new)
The plot looks fine, but I get the following error in the 2nd part which I don't understand: A value in x_new is above the interpolation range.
What does it mean, in my case, and what would be the proper way to correct it? No matter how much I search, I don't see what is not correct in the program I made...
