<jupyter_start><jupyter_text>**CENTRAL LIMIT THEOREM**<jupyter_code># Started at page 124

# Bernoulli distribution
# Binomial(n, p) - sum of n independent bernoulli(p) random variables

def bernoulli_trial(p):
    # return 1 with probability p
    # return 0 with probability 1-p
    return 1 if random.random() < p else 0

#random.random()

def binomial(n, p):
    # n --> is the number of independent bernoulli trials
    return sum(bernoulli_trial(p) for _ in range(n))
binomial(1000000000, 0.3)
def make_hist(p, n, num_points):
    data = [binomial(n, p) for _ in range(num_points)]

    # use a bar chart to show the actual binomial samples
    histogram = Counter(data)
    plt.bar([x for x in histogram.keys()], [v / num_points for v in histogram.values()], color='0.75')

    mu = p*n
    sigma = math.sqrt(n*p*(1-p))
    

    # use a line chart to show the normal approximation
    xs = range(min(data), max(data) + 1)
    ys = [normal_cdf(i + 0.5, mu, sigma) - normal_cdf(i - 0.5, mu, sigma) for i in xs]
    plt.plot(xs, ys)
    plt.title("Binomial Distribution vs. Normal Approximation")

    plt.show()
make_hist(0.75, 100, 10000)

# The moral of this approximation is that if you want to know the probability
#    that (say) a fair coin turns up more than 60 heads in 100 flips, you can estimate
#    it as the probability that a Normal(50, 5) is greater than 60
#    which is easier than computing the Binomial(100, 0.5)cdf
#    (Although in most applications you'd probably be using statistical software
#    that would gladly compute whatever probabilities that you want.)<jupyter_output><empty_output><jupyter_text>**For Further Exploration***   [scipy.stats](http://bit.ly/1L2H0Lj) contains pdf and cdf functions for most of the popular probabilitydistributions.*   Remember how, at the end of Chapter 5, I said that it would be a good idea to study astatistics textbook? It would also be a good idea to study a probability textbook. Thebest one I know that’s available online is Introduction to Probability.<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0955137481910275, 'fraction_numerical': 0.028461167390255667, 'mean_word_length': 3.914691943127962, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '229008', 'license': 'MIT', 'n_tokens_mistral': 728, 'n_tokens_neox': 653, 'n_words': 259, 'path': 'patch/Probability.py.ipynb', 'repo_name': 'patchikoooo/data-science-from-scratch', 'size': '2073'}