def normal_cdfs_visualization(): xs = [x / 10.0 for x in range(-50, 50)] plt.plot(xs, [ds_probability.normal_cdf(x, sigma=1) for x in xs], '-', label='mu=0-sigma=1') plt.plot(xs, [ds_probability.normal_cdf(x, sigma=2) for x in xs], '--', label='mu=0-sigma=2') plt.plot(xs, [ds_probability.normal_cdf(x, sigma=0.5) for x in xs], ':', label='mu=0-sigma=0.5') plt.plot(xs, [ds_probability.normal_cdf(x, mu=-1) for x in xs], '-.', label='mu=-1-sigma=1') plt.legend() plt.title('Various Normals cdfs') plt.show()
def compare_binomial_dist_to_normal_approx(p, n, nb_points): data = [ds_probability.binomial(n, p) for _ in range(nb_points)] #showing actual binomial samples on bar chart histogram = Counter(data) plt.bar([x - 0.4 for x in histogram.keys()], [v / nb_points for v in histogram.values()], 0.8, color='0.7') mu_px = p * n sigma_px = math.sqrt(n * p * (1 - p)) #line chart that shows the normal approximation of the binomial variable xs = range(min(data), max(data) + 1) ys = [ ds_probability.normal_cdf(i + 0.5, mu_px, sigma_px) - ds_probability.normal_cdf(i - 0.5, mu_px, sigma_px) for i in xs ] plt.plot(xs, ys) plt.title('Binomial Dist vs Normal approximation') plt.show()
def normal_probability_between(lo, hi, mu=0, sigma=1): return ds_probability.normal_cdf( hi, mu, sigma) - ds_probability.normal_cdf(lo, mu, sigma)
def normal_probability_above(lo, mu=0, sigma=1): return 1 - ds_probability.normal_cdf(lo)
def normal_probability_below(x, mu=0, sigma=1): return ds_probability.normal_cdf(x)