<jupyter_start><jupyter_text>Stats exercises for Acalonia.com Resource: https://saileshdev.github.io/stats-prob/<jupyter_code># Import libraries
from scipy.stats import binom
from scipy.stats import poisson
from scipy.stats import randint 

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.stats as stats
import seaborn as sns

import warnings
warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>Using Python, create a plot of the probability mass function (i.e. the probabilities of individual outcomes) for the following distributions. (a) Discrete uniform distribution with  (i.e. with outcomes  equally likely), with non-zero PMF values<jupyter_code>outcomes = [1,2,3,4,5,6]
df = pd.DataFrame(outcomes,columns=['Outcomes'])
probabilities = df['Outcomes'].value_counts(normalize=True)    
plt.bar(probabilities.index, probabilities.values)
plt.show()<jupyter_output><empty_output><jupyter_text>(b) Binomial distribution with p = 0.3, n = 10 with non-zero PMF values.<jupyter_code>n = 10
p = 0.3

# 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>c) Poisson distribution with lambda = 5, with non-zero PMF values.<jupyter_code># We assume that we are using n of previous item ... 
# We replace r_values with outcomes for size.
# r_values for probabilities.index. 
lmbda = 5

p_dist = poisson.rvs(mu=lmbda, size=len(r_values))
plt.bar(r_values, p_dist);<jupyter_output><empty_output><jupyter_text>Do the histograms match the shapes of the distribution?<jupyter_code># another test...
values = np.random.uniform(1, 6, 1000000)
# generates 1000000 points of unifrom distribution between -10 and 10
# 6 is the number of buckets for the histogram.
plt.hist(values, edgecolor='k', bins=6)
plt.show();<jupyter_output><empty_output><jupyter_text>This makes more sense.<jupyter_code>b_1 = stats.binom(n=10,p=0.3)
b_1.rvs(size=1000000,random_state=42)

# sample pmf for one value.
b_1.pmf(2)
# this is our range.
t=np.arange(0,11)
t

# plot
plt.scatter(t,b_1.pmf(t),label="PMF",color="Orange")
plt.title("Binomial Distribution")
plt.ylabel("Density")
plt.xticks(t)
plt.legend()
plt.show();<jupyter_output><empty_output><jupyter_text>but what we need is a hist so...<jupyter_code>b_1_sample=b_1.rvs(size=1000000,random_state=42)
# rwidth=0.75,
plt.hist(b_1_sample,bins=11,range=(0,11),density=True,align='left', edgecolor='k', label="Samples",alpha=0.75)
plt.title("Binomial Distribution (Hist)")
plt.ylabel("Density")
plt.legend()
plt.xticks(t)
plt.show()
poisson = stats.poisson(mu=5)
p_sample = poisson.rvs(size=1000000,random_state=42)
# end = np.max(p_sample)+1 
end = 11
t=np.arange(0,end)
plt.hist(p_sample, bins=end,range=(0,end), density=True,align='left', edgecolor='k',label="Samples", alpha=0.75)
plt.title("Poisson Distribution (Hist)")
plt.ylabel("Density")
plt.legend()
plt.xticks(t)
plt.show()<jupyter_output><empty_output><jupyter_text>Poisson of 1 mil values is the same with original distribution. What happens if x = 4...<jupyter_code># if x = 4
outcomes_ = [1,2,3,4]
df_ = pd.DataFrame(outcomes_,columns=['Outcomes'])
probabilities_ = df_['Outcomes'].value_counts(normalize=True)    
plt.bar(probabilities_.index, probabilities_.values)
plt.show()
# another test...
values_ = np.random.uniform(1, 4, 1000000)
# generates 1000000 points of unifrom distribution between -10 and 10
# 6 is the number of buckets for the histogram.
plt.hist(values_, edgecolor='k', bins=4)
plt.show();
# From a binom perspective.
n = 3
p = 0.3

# 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()
# version 2
b_4 = stats.binom(n=4,p=0.3)
b_4.rvs(size=1000000,random_state=42)

# sample pmf for one value.
b_4.pmf(2)

# this is our range.
t=np.arange(0,4)
t

# plot
plt.scatter(t,b_4.pmf(t),label="PMF",color="Orange")
plt.title("Binomial Distribution")
plt.ylabel("Density")
plt.xticks(t)
plt.legend()
plt.show();
b_4_sample=b_4.rvs(size=1000000,random_state=42)
# rwidth=0.75,
plt.hist(b_4_sample,bins=4,range=(0,4),density=True,align='left', edgecolor='k', label="Samples",alpha=0.75)
plt.title("Binomial Distribution (Hist)")
plt.ylabel("Density")
plt.legend()
plt.xticks(t)
plt.show()
# poisson at 4.
poisson = stats.poisson(mu=5)
p_sample = poisson.rvs(size=1000000,random_state=42) 
end = 4
t=np.arange(0,end)
plt.hist(p_sample, bins=end,range=(0,end), density=True,align='left', edgecolor='k',label="Samples", alpha=0.75)
plt.title("Poisson Distribution (Hist)")
plt.ylabel("Density")
plt.legend()
plt.xticks(t)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14852513502285003, 'fraction_numerical': 0.039883672621520566, 'mean_word_length': 6.465116279069767, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 1, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '216910', 'license': 'MIT', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1824, 'n_words': 410, 'path': 'stats/stats.ipynb', 'repo_name': 'verneh/DataSci', 'size': '4814'}