<jupyter_start><jupyter_text>Lambda School Data Science Module 133 Introduction to Bayesian Inference  Assignment - Code it up!Most of the above was pure math - now write Python code to reproduce the results! This is purposefully open ended - you'll have to think about how you should represent probabilities and events. You can and should look things up, and as a stretch goal - refactor your code into helpful reusable functions!Specific goals/targets:1. Write a function `def prob_drunk_given_positive(prob_drunk_prior, prob_positive, prob_positive_drunk)` that reproduces the example from lecture, and use it to calculate and visualize a range of situations2. Explore `scipy.stats.bayes_mvs` - read its documentation, and experiment with it on data you've tested in other ways earlier this week3. Create a visualization comparing the results of a Bayesian approach to a traditional/frequentist approach4. In your own words, summarize the difference between Bayesian and Frequentist statisticsIf you're unsure where to start, check out [this blog post of Bayes theorem with Python](https://dataconomy.com/2015/02/introduction-to-bayes-theorem-with-python/) - you could and should create something similar!Stretch goals:- Apply a Bayesian technique to a problem you previously worked (in an assignment or project work) on from a frequentist (standard) perspective- Check out [PyMC3](https://docs.pymc.io/) (note this goes beyond hypothesis tests into modeling) - read the guides and work through some examples- Take PyMC3 further - see if you can build something with it! 1. Write a function def prob_drunk_given_positive(prob_drunk_prior, prob_positive, prob_positive_drunk) that reproduces the example from lecture, and use it to calculate and visualize a range of situations<jupyter_code># Bayesian results
def prob_drunk_positive(prob_drunk_prior, prob_positive, prob_positive_drunk):
 for i in range(0,10): 
   prob_drunk_prior = (prob_positive_drunk*prob_drunk_prior)/(prob_positive_drunk*prob_drunk_prior + prob_positive*(1-prob_drunk_prior))
   print(f'Likelyhood that somebody is drunk given they tested positive on {i+1} tests: {round(prob_drunk_prior*100, 6)}%')
prob_drunk_positive(.001, .08, 1)<jupyter_output>Likelyhood that somebody is drunk given they tested positive on 1 tests: 1.235788%
Likelyhood that somebody is drunk given they tested positive on 2 tests: 13.525211%
Likelyhood that somebody is drunk given they tested positive on 3 tests: 66.15997%
Likelyhood that somebody is drunk given they tested positive on 4 tests: 96.068951%
Likelyhood that somebody is drunk given they tested positive on 5 tests: 99.673716%
Likelyhood that somebody is drunk given they tested positive on 6 tests: 99.973819%
Likelyhood that somebody is drunk given they tested positive on 7 tests: 99.997905%
Likelyhood that somebody is drunk given they tested positive on 8 tests: 99.999832%
Likelyhood that somebody is drunk given they tested positive on 9 tests: 99.999987%
Likelyhood that somebody is drunk given they tested positive on 10 tests: 99.999999%<jupyter_text>2. Explore scipy.stats.bayes_mvs - read its documentation, and experiment with it on data you've tested in other ways earlier this week<jupyter_code>import scipy.stats as stats
import pandas as pd
import numpy as np
# Load the data
!wget 'https://archive.ics.uci.edu/ml/machine-learning-databases/voting-records/house-votes-84.data'
# Read in data and set column names
df = pd.read_csv('house-votes-84.data', names=['party','handicapped-infants',
                          'water-project', 'budget', 'physician-fee-freeze', 
                          'el-salvador-aid', 'religious-groups', 
                          'anti-satellite-ban', 'aid-to-contras', 'mx-missile',
                          'immigration', 'synfuels', 'education', 
                          'right-to-sue','crime','duty-free', 'south-africa'])
# Clean data
df = df.replace({'y':1 , 'n':0, '?':np.nan})
# Seperate data by Democrat or Republicn
dem = df[df['party'] == 'democrat']
rep = df[df['party'] == 'republican']
# Confidence interval function
def confidence_interval(data, confidence=0.95):

  data = np.array(data)
  mean = np.mean(data)
  n = len(data)
  stderr = np.std(data, ddof=1) / np.sqrt(n)
  margin_of_error = stderr * stats.t.ppf((1 + confidence) / 2.0, n - 1)
  return (mean, mean - margin_of_error, mean + margin_of_error)
stats.bayes_mvs(dem['duty-free'].dropna(), alpha=.95)
confidence_interval(dem['duty-free'].dropna())
stats.bayes_mvs(rep['mx-missile'].dropna(), alpha=.95)
confidence_interval(rep['mx-missile'].dropna())<jupyter_output><empty_output><jupyter_text>3. Create a visualization comparing the results of a Bayesian approach to a traditional/frequentist approach<jupyter_code># Traditional/frequentist function
def traditional(prob_positive, prob_false_positive):
  for i in range(1,11):
      prob_positive1 = prob_positive - prob_false_positive**(i)
      print(f'Likelyhood that somebody is drunk given they tested positive on {i} tests: {prob_positive1*100}%')
traditional(1, .08)
# Let's rewrite the functions to fix the outputs for plotting
# Bayesian results
def prob_drunk_positive1(prob_drunk_prior, prob_positive, prob_positive_drunk):
  prob_df = []
  for i in range(0,10): 
   prob_drunk_prior = (prob_positive_drunk*prob_drunk_prior)/(prob_positive_drunk*prob_drunk_prior + prob_positive*(1-prob_drunk_prior))
   prob_df.append(prob_drunk_prior)
  return prob_df

# Traditional/frequentist
def traditional1(prob_positive, prob_false_positive):
  prob_df = []
  for i in range(1,11):
      prob_positive1 = prob_positive - prob_false_positive**(i)
      prob_df.append(prob_positive1)
  return prob_df
import matplotlib.pyplot as plt

bayesian = prob_drunk_positive1(.001, .08, 1)
traditional = traditional1(1, .08)
plt.style.use('seaborn-whitegrid')

fig, ax = plt.subplots(figsize=(8,6))

ax.plot(bayesian, lw=2, label='Bayesian')
ax.plot(traditional, '--', lw=2, label='Traditional')

ax.set_xticks([0, 1, 2, 3, 4, 5, 6, 7])
ax.set_xticklabels(['1', '2', '3', '4', '5', '6', '7', '8'])
ax.set_yticks([0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1])
ax.set_yticklabels(['0', '10', '20', '30', '40', '50','60', '70', '80', '90', '100%'])
ax.set_xlim(-.1,7)
ax.set_ylim(0,1.05)
ax.set_title('Likelyhood of Being Drunk While Driving Given (n) Positive Tests')
ax.set_ylabel('Percent Likelyhood')
ax.set_xlabel('Number of Tests')
ax.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10802039870190079, 'fraction_numerical': 0.03585226394683975, 'mean_word_length': 4.964976958525345, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '16840', 'license': 'MIT', 'n_tokens_mistral': 2276, 'n_tokens_neox': 2026, 'n_words': 664, 'path': 'module3-introduction-to-bayesian-inference/LS_DS_133_Introduction_to_Bayesian_Inference_Assignment.ipynb', 'repo_name': 'Nckflannery/DS-Unit-1-Sprint-3-Statistical-Tests-and-Experiments', 'size': '6471'}