%matplotlib inline pyplot.style.use(config.glyfish_style) # %% k = 5.0 λ = 1.0 nsample = 50000 samples = numpy.random.weibull(k, nsample) # %% target_pdf = stats.weibull(k, λ) x = numpy.linspace(0.001, 2.0, 500) figure, axis = pyplot.subplots(figsize=(12, 5)) axis.set_xlabel("X") axis.set_ylabel("PDF") axis.set_xlim([0.0, x[-1]]) axis.set_title(f"Weibull Distribution, k={k}, λ={λ}") axis.plot(x, [target_pdf(j) for j in x]) # %% figure, axis = pyplot.subplots(figsize=(12, 5)) axis.set_xlabel("Sample") axis.set_ylabel("PDF") axis.set_title(f"Weibull Distribution, k={k}, λ={λ}")
axis.set_xlabel(r"$X$") axis.set_title("Sampled Exponential Distribution") axis.set_prop_cycle(config.distribution_sample_cycler) axis.hist(samples, 60, density=True, rwidth=0.8, label=f"Samples", zorder=5) axis.plot(x, pdf(x), label=f"Target PDF", zorder=6, color="#003B6F") axis.legend(bbox_to_anchor=(0.9, 0.8)) config.save_post_asset(figure, "inverse_cdf_sampling", "exponetial_sampled_distribution") # %% # Inverse CDF sampling from weibull distribution k = 5.0 λ = 1.0 nsamples = 100000 pdf = stats.weibull(k, λ) cdf_inv = lambda u: λ * (numpy.log(1.0/(1.0 - u)))**(1.0/k) x = numpy.linspace(0.001, 1.6, 500) dx = 1.6/499.0 samples = [cdf_inv(u) for u in numpy.random.rand(nsamples)] # %% figure, axis = pyplot.subplots(figsize=(10, 6)) axis.set_ylim([0, 2.0]) axis.set_xlabel(r"$X$") axis.set_xlim([0.0, 1.6]) axis.set_yticks([0.2, 0.6, 1.0, 1.4, 1.8]) axis.set_title(f"Weibull Distribution, k={k}, λ={λ}") pdf_values = [pdf(v) for v in x]