<jupyter_start><jupyter_text>Binomial Distribution<jupyter_code>import numpy as np
from scipy import stats
import matplotlib
import matplotlib.pyplot as plt
import importlib
from scipy.stats import skew, kurtosis, chi2
import pandas as pd
from scipy import stats # importando scipy.stats
import seaborn as sns # importando seaborn

# importanto la api de statsmodels
import statsmodels.formula.api as smf
import statsmodels.api as sm
import scipy
import seaborn as sns

stats.binom.rvs(size=10,n=20,p=0.8)
# distribucion binomial
#import scipy, scipy.stats
x = scipy.linspace(0,10,11)  # def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
pmf = scipy.stats.binom.pmf(x, 10, 0.1) # pmf(k, n, p, loc=0) Probability mass function.
import pylab
pylab.plot(x,pmf)<jupyter_output>/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:3: DeprecationWarning: scipy.linspace is deprecated and will be removed in SciPy 2.0.0, use numpy.linspace instead
  This is separate from the ipykernel package so we can avoid doing imports until<jupyter_text>n - number of trials.p - probability of occurence of each trial (e.g. for toss of a coin 0.5 each).size - The shape of the returned array. La forma de la matriz devuelta<jupyter_code># Given 10 trials for coin toss generate 10 data points:
from numpy import random

x = random.binomial(n=15, p=0.4, size=10)
print(x)
scipy.stats.binom.rvs(n=15, p=0.4, size=10)<jupyter_output>[7 4 5 8 7 4 6 4 2 8]<jupyter_text>Visualization of Binomial Distribution<jupyter_code>from numpy import random
#import matplotlib.pyplot as plt
#import seaborn as sns

sns.distplot(random.binomial(n=15, p=0.4, size=1000), hist=True, kde=False)
plt.show()<jupyter_output>/usr/local/lib/python3.6/dist-packages/seaborn/distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)<jupyter_text>Difference Between Normal and Binomial DistributionThe main difference is that normal distribution is continous whereas binomial is discrete, but if there are enough data points it will be quite similar to normal distribution with certain loc and scale.<jupyter_code>#from numpy import random
#import matplotlib.pyplot as plt
#import seaborn as sns

sns.distplot(random.normal(loc=50, scale=5, size=1000), hist=False, label='normal')
sns.distplot(random.binomial(n=100, p=0.5, size=1000), hist=False, label='binomial')

plt.show()<jupyter_output>/usr/local/lib/python3.6/dist-packages/seaborn/distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `kdeplot` (an axes-level function for kernel density plots).
  warnings.warn(msg, FutureWarning)
/usr/local/lib/python3.6/dist-packages/seaborn/distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `kdeplot` (an axes-level function for kernel density plots).
  warnings.warn(msg, FutureWarning)<jupyter_text>Ejemplo 1 2) La probabilidad de que una cierta clase de componente pase con éxito una determinada prueba de impacto es de 3/4. Encuentre la probabilidad de que exactamente dos de los siguientes cuatrocomponentes que se prueben pasen la prueba<jupyter_code># probabilidad distribicion binomial
# scipy.stats.binom.stats(n, p)
# scipy.stats.binom.pmf(r, n, p)

from scipy.stats import binom 
# setting the values 
# of n and p 
n = 4
p = 3/4
# defining the list of r values 
r_values = list(range(n + 1)) 
# obtaining the mean and variance  
mean, var = binom.stats(n, p) 
# list of pmf values 
dist = [binom.pmf(r, n, p) for r in r_values ] 
# printing the table 
print("r\tp(r)") 
for i in range(n + 1): 
    print(str(r_values[i]) + "\t" + str(dist[i])) 
# printing mean and variance 
print("mean = "+str(mean)) 
print("variance = "+str(var))<jupyter_output>r	p(r)
0	0.003906250000000001
1	0.046875000000000035
2	0.21093750000000006
3	0.4218750000000001
4	0.31640625000000006
mean = 3.0
variance = 0.75<jupyter_text>La probabilidad de que exactamente dos de las siguientes piezas cuatro componentes que se prueben pasen la prueba es de 0,2109 .<jupyter_code># grafica ejemplo 1
from scipy.stats import binom 
import matplotlib.pyplot as plt 
# setting the values 
# of n and p 
n = 4
p = 3/4
# defining list of r values 
r_values = list(range(n + 1)) 
# list of pmf values 
dist = [binom.pmf(r, n, p) for r in r_values ] 
# plotting the graph  
plt.bar(r_values, dist) 
plt.show()<jupyter_output><empty_output><jupyter_text>Distribución Binomialhttps://relopezbriega.github.io/blog/2016/06/29/distribuciones-de-probabilidad-con-python/<jupyter_code># Graficando Binomial
N, p = 30, 0.4 # parametros de forma 
binomial = stats.binom(N, p) # Distribución
x = np.arange(binomial.ppf(0.01), binomial.ppf(0.99))
fmp = binomial.pmf(x) # Función de Masa de Probabilidad
plt.plot(x, fmp, '--')
plt.vlines(x, 0, fmp, colors='b', lw=5, alpha=0.5)
plt.title('Distribución Binomial')
plt.ylabel('probabilidad')
plt.xlabel('valores')
plt.show()
# histograma
aleatorios = binomial.rvs(1000)  # genera aleatorios
cuenta, cajas, ignorar = plt.hist(aleatorios, 20)
plt.ylabel('frequencia')
plt.xlabel('valores')
plt.title('Histograma Binomial')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10324640766365088, 'fraction_numerical': 0.04328543551534504, 'mean_word_length': 5.306487695749441, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 5, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '465755', 'license': 'MIT', 'n_tokens_mistral': 2193, 'n_tokens_neox': 1986, 'n_words': 619, 'path': 'Binomial_Distribution.ipynb', 'repo_name': 'WuilsonEstacio/github-para-estadistica', 'size': '5637'}