<jupyter_start><jupyter_text>Hypothesis testing using M&M'S®<jupyter_code>import numpy as np
import statsmodels.stats.proportion as smp
import seaborn as sns

from scipy.stats import binom, binom_test

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Record here the number of orange and total M&M'S in your sample.<jupyter_code>n_orange = 
n =<jupyter_output><empty_output><jupyter_text>Proportion of orange M&M'S:<jupyter_code>n_orange / n<jupyter_output><empty_output><jupyter_text>95% confidence interval for the proportion<jupyter_code>smp.proportion_confint(n_orange, n, alpha=0.05)<jupyter_output><empty_output><jupyter_text>What happens if we require a 99% (instead of 95%) CI?<jupyter_code>smp.proportion_confint(n_orange, n, alpha=0.01)<jupyter_output><empty_output><jupyter_text>What would happen if we had 10 times as many M&M'S (but the same proportion)?<jupyter_code>smp.proportion_confint(n_orange * 10, n * 10, alpha=0.05)<jupyter_output><empty_output><jupyter_text>Assuming that the population-level proportion is 20%, what is the probability of observing `n_orange` out of `n` M&M'S?*Note*: `pmf` returns the 'probability mass function', i.e. the probability at each discrete value $0, \ldots, n$.<jupyter_code>binom.pmf(n_orange, n, 0.2)<jupyter_output><empty_output><jupyter_text>Let's visualise the probability at each discrete value $0, \ldots, n$.<jupyter_code>x = np.arange(n + 1)
y = binom.pmf(x, n, 0.2)
sns.pointplot(x, y)<jupyter_output><empty_output><jupyter_text>What is the sum of all these probabilities?<jupyter_code>binom.pmf(x, n, 0.2).sum()<jupyter_output><empty_output><jupyter_text>Test whether the proportion is 'significantly different' from 20%.*Note*: `binom_test` returns the $p$-value for the test.<jupyter_code>binom_test(n_orange, n, 0.2)<jupyter_output><empty_output><jupyter_text>What happens if we change the 'null' proportion we are testing against?<jupyter_code>ps = np.arange(0.01, 0.99, 0.01)
y = [ binom_test(n_orange, n, p) for p in ps ]
sns.pointplot(ps, y)
sns.pointplot(ps, -np.log10(y))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1221264367816092, 'fraction_numerical': 0.022509578544061302, 'mean_word_length': 7.927350427350428, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '334300', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 857, 'n_tokens_neox': 791, 'n_words': 181, 'path': '05_statistical_thinking/notebooks/01_hypothesis_testing.ipynb', 'repo_name': 'JoseHJBlanco/ga-data-science', 'size': '2088'}