<jupyter_start><jupyter_text>Rolling a Twenty-Sided DieSuppose we want to know whether a certain twenty-sided die tends to roll 1's more often. We roll the die 100 times and observe that we rolled a 1 a total of 9 times. We want to use a hypothesis test to determine whether this is unusually high if the dice were fair.Let's starting by stating the hypotheses. **Null hypothesis:** The probability of rolling a 1 is 1/20 = 0.05.**Alternative hypothesis:** The probability of rolling a 1 is greater than 0.05.<jupyter_code># These lines import the Numpy and Datascience modules.
import numpy as np
from datascience import *

# These lines do some fancy plotting magic
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
observed_value = 0.09<jupyter_output><empty_output><jupyter_text>First, we create our die.<jupyter_code>die = np.arange(1, 21)
die<jupyter_output><empty_output><jupyter_text>We do one trial of the simulation.<jupyter_code>nrolls = 100
rolls = np.random.choice(die, nrolls)
total_ones = np.count_nonzero(rolls == 1)
prop_ones = total_ones / nrolls
prop_ones<jupyter_output><empty_output><jupyter_text>We put this into a function so that we can simulate what would happen if we were to roll a fair die 100 times.<jupyter_code>def roll_simulation(n):
    die = np.arange(1, 21)
    rolls = np.random.choice(die, nrolls)
    total_ones = np.count_nonzero(rolls == 1)
    prop_ones = total_ones / n
    return prop_ones
roll_simulation(100)<jupyter_output><empty_output><jupyter_text>We do the simulation 1000 times within a loop and store the results inside `sim_props`.<jupyter_code>nsimulations = 1000
nrolls = 100
sim_props = make_array()

for i in np.arange(nsimulations):
    sim_props = np.append(sim_props, roll_simulation(nrolls))<jupyter_output><empty_output><jupyter_text>We can graph the output to visualize it with the observed value.<jupyter_code>Table().with_column('Simulated Proportions', sim_props).hist()
plt.scatter(observed_value, 0, color='red', s = 100)<jupyter_output><empty_output><jupyter_text>Finally, we can find the p-value.<jupyter_code>np.count_nonzero(sim_props >= observed_value) / nsimulations<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08730873087308731, 'fraction_numerical': 0.023402340234023402, 'mean_word_length': 5.946875, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '199934', 'license': 'CC0-1.0', 'n_tokens_mistral': 783, 'n_tokens_neox': 722, 'n_words': 230, 'path': 'labs/Lab-07-HypothesisTestingPartII/lab7-supplemental.ipynb', 'repo_name': 'kimbrianj/bsos233-sp22', 'size': '2222'}