<jupyter_start><jupyter_text>Week 06, Part 2 Topic 1. Intro to binomial distribution 1. BACK TO SLIDES FOR BINOMIAL FORMULA MEAN AND STDDEV<jupyter_code># resize
require(repr)
options(repr.plot.width=4, repr.plot.height=4, repr.plot.res=300)<jupyter_output><empty_output><jupyter_text>1. Intro to Binomial FormulaFirst lets look at an example we flip a coin and call a head a "SUCCESS" and tail a "FAILURE".Note: this is just to use the nomenclature of the binomial distribution - we could as easily call tails a success.We can use the function "sample" to simulate a fair coin toss:<jupyter_code>help(sample)
nTosses = 10
samples = sample(2,size=nTosses, replace=TRUE) # replace=TRUE just means we start again with equal prob of heads/tails
print(samples)<jupyter_output>[1] 1 1 2 1 1 1 1 2 2 2<jupyter_text>Now, lets re-map 1$\rightarrow$0 & 2$\rightarrow$1 just for consistency in using the binomial formula:<jupyter_code>samples[samples==1] = 0 # tails, failures
samples[samples==2] = 1 # heads, successes
print(samples)<jupyter_output>[1] 0 0 1 0 0 0 0 1 1 1<jupyter_text>What do we expect the  of successes & failures to be?  Well, for a fair coin it should be p=0.5What about for our sample above?<jupyter_code>prop_sample = sum(samples)/nTosses
print(prop_sample)<jupyter_output>[1] 0.4<jupyter_text>If we run this a few times, we see that we get widely varying answers for our *sample* proportion:<jupyter_code>for (i in 1:10){
    samples = sample(2,size=nTosses, replace=TRUE) 
    samples[samples==1] = 0 # tails, failures
    samples[samples==2] = 1 # heads, successes
    
    prop_sample = sum(samples)/nTosses
    print(prop_sample)
}<jupyter_output>[1] 0.7
[1] 0.4
[1] 0.7
[1] 0.6
[1] 0.7
[1] 0.5
[1] 0.6
[1] 0.4
[1] 0.6
[1] 0.6
{'fraction_non_alphanumeric': 0.10945558739255014, 'fraction_numerical': 0.0498567335243553, 'mean_word_length': 5.0, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '324145', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 708, 'n_tokens_neox': 630, 'n_words': 216, 'path': 'week06/.ipynb_checkpoints/prep_part2_introToBinomialDist-checkpoint.ipynb', 'repo_name': 'jnaiman/is507_fall2020', 'size': '1745'}