def plot_histogram(size, bw_method=None):
x = jnp.arange(-0.5, 3, 0.01)
norm = distrax.Normal(loc=1.4, scale=0.5)
y = norm.prob(x)
bw_adjusted = ""
if bw_method:
bw_adjusted = "bw_adjusted"
norm_samples = norm.sample(seed=key, sample_shape=size)
fig, ax = plt.subplots()
ax.hist(norm_samples, density=True, stacked=True, rwidth=0.8, label="samples")
kde = gaussian_kde(norm_samples, bw_method=bw_method)
y_estimate = kde(x)
ax.plot(x, y, "g", label="true pdf")
ax.plot(x, y_estimate, "r--", label="estimated pdf")
ax.set_xlim(-0.5, 3)
ax.set_ylim(0, 2)
ax.set_title("n_samples = %d" % size)
ax.legend(loc=2, prop={"size": 7})
sns.despine()
pml.savefig(f"mcAccuracyDemo{size}{bw_adjusted}")
for size in [10, 100]:
plot_histogram(size)
plt.show()
for size in [10, 100]:
plot_histogram(size, bw_method=0.75)
plt.show()