<jupyter_start><jupyter_text>2. ProbabilityHere we provide some examples of probability using scipy.Copyright:  1. Poisson distributionscipy.stats.poisson = A Poisson discrete random variable.As an instance of the rv_discrete class, poisson object inherits from it a collection of generic methods (see below for the full list), and completes them with details specific for this particular distribution.https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.poisson.html<jupyter_code>from scipy.stats import poisson
import matplotlib.pyplot as plt
import numpy as np

# prepare the figure canvas
fig, ax = plt.subplots(1, 1)

# display all the plots inline
%matplotlib inline 
mu = 0.6
mean, var, skew, kurt = poisson.stats(mu, moments='mvsk')
# display the probability mass function (pmf):
x = np.arange(poisson.ppf(0.01, mu), poisson.ppf(0.99, mu))
ax.plot(x, poisson.pmf(x, mu), 'bo', ms=8, label='poisson pmf')
ax.vlines(x, 0, poisson.pmf(x, mu), colors='b', lw=5, alpha=0.5)

# display all the plots inline
%matplotlib inline 
# Alternatively, the distribution object can be called (as a function) 
# to fix the shape and location. 
# This returns a “frozen” RV object holding the given parameters fixed.

# Freeze the distribution and display the frozen pmf:
rv = poisson(mu)
ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1, label='frozen pmf')
ax.legend(loc='best', frameon=False)

# display all the plots inline
%matplotlib inline 

plt.show()
# Check accuracy of cdf and ppf:
prob = poisson.cdf(x, mu)
np.allclose(x, poisson.ppf(prob, mu))
# Generate random numbers:
r = poisson.rvs(mu, size=1000)<jupyter_output><empty_output><jupyter_text>2. Binomial distributionscipy.stats.binom = A binomial discrete random variable.As an instance of the rv_discrete class, binom object inherits from it a collection of generic methods (see below for the full list), and completes them with details specific for this particular distribution.https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binom.html<jupyter_code>n, p = 5, 0.4
mean, var, skew, kurt = binom.stats(n, p, moments='mvsk')

# display the probability mass function (pmf):
x = np.arange(binom.ppf(0.01, n, p), binom.ppf(0.99, n, p))
ax.plot(x, binom.pmf(x, n, p), 'bo', ms=8, label='binom pmf')
ax.vlines(x, 0, binom.pmf(x, n, p), colors='b', lw=5, alpha=0.5)
# Freeze the distribution and display the frozen pmf:
rv = binom(n, p)
ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1, label='frozen pmf')
ax.legend(loc='best', frameon=False)
plt.show()
# Check accuracy of cdf and ppf:
prob = binom.cdf(x, n, p)
np.allclose(x, binom.ppf(prob, n, p))
# Generate random numbers:
r = binom.rvs(n, p, size=1000)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13179992698064988, 'fraction_numerical': 0.01606425702811245, 'mean_word_length': 5.284403669724771, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '423815', 'license': 'MIT', 'n_tokens_mistral': 1019, 'n_tokens_neox': 972, 'n_words': 310, 'path': '2. Probability.ipynb', 'repo_name': 'NilaBlueshirt/MAT494TeachingMaterial', 'size': '2739'}