<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!   SLN: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![](https://wikimedia.org/api/rest_v1/media/math/render/svg/95c6524a3736c43e4bae139713f3df2392e6eda9)* true positives that are drunk is 100% (positive|drunk)* 1/100 are drunk * 8% are False Positive (positive| not drunk)Formula:P(Drunk|Positive) = P(Positive|Drunk) * P(Drunk) / P(Positive)P(Drunk|Positive) = P(Positive|Drunk) * P(Drunk) /                  (P(Positive|Drunk) * P(Drunk))  +  (P(Positive|Not Drunk) * P(Not Drunk)<jupyter_code># TODO - code!
import pandas as pd


prior= 1/1000
positive_drunk=1
positive_not_drunk= 0.08
def prob_drunk_positive(drunk,positive_drunk, positive_not_drunk):
  probabilities=[]
  iterations= range(1,4)
  for x in iterations:
    if x>1:
      prior= probabilities[-1]
    else :
      prior= drunk
    not_drunk= 1-drunk
    prob_positive = (positive_drunk*prior) + (positive_not_drunk*not_drunk)
    posterior_prob= (positive_drunk*prior)/prob_positive
    probabilities.append(posterior_prob)
  return (probabilities)

prob_drunk_positive(1/1000,1,0.08)<jupyter_output><empty_output><jupyter_text>SLN 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 pandas as pd
import numpy as np
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
url='https://archive.ics.uci.edu/ml/machine-learning-databases/voting-records/house-votes-84.data'
df = pd.read_csv(url, 
                 header=None,
                 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'])
print(df.shape)
df = df.replace({'?':np.NaN, 'n':0, 'y':1})
df.head()
republican = df[df.party == "republican"]#split data by party
print(republican.shape)
republican.head()
democrat = df[df.party == "democrat"]
print(democrat.shape)
democrat.head()
 namescolumns=['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']
data= df['handicapped-infants'].dropna()
data_mean, data_var, data_std = stats.bayes_mvs(data, alpha=0.95)
data_std
import matplotlib.pyplot as plt
import seaborn as sns
fig = plt.figure()
ax = fig.add_subplot(111)
sns.kdeplot(data, shade='blue');
ax.vlines(data_mean.statistic, 0, 1.5, colors='r', label='Estimated mean')
ax.axvspan(data_mean.minmax[0],data_mean.minmax[1], facecolor='r',alpha=0.2, label=r'Estimated mean (95% limits)')
ax.vlines(data_std.statistic, 0, 1.5, colors='g', label='Estimated scale')
ax.axvspan(data_std.minmax[0],data_std.minmax[1], facecolor='g', alpha=0.2,label=r'Estimated scale (95% limits)');
ax.legend(fontsize=10)
ax.set_xlim([-.5, 1.5])
ax.set_ylim([0, 1.5])
plt.show();<jupyter_output><empty_output><jupyter_text>**ALL my DataFrame<jupyter_code># data= df['education'].dropna()
# data_mean, data_var, data_std = stats.bayes_mvs(data, alpha=0.95)
bayes_result=[]
def calculate_bayes(data):
  for x in data:
    data_mean, data_var, data_std = stats.bayes_mvs(df[x].dropna(), alpha=0.95)
    bayes_result.append([x,data_mean.statistic, data_mean.minmax[0],data_mean.minmax[1],data_var.statistic,data_var.minmax[0],data_var.minmax[1], data_std.statistic,data_std.minmax[0],data_std.minmax[1]])
  return bayes_result

calculate_bayes(namescolumns)

df_result = pd.DataFrame(bayes_result, columns=['Project','Mean','Mean_max_low','Mean_max_upper','Variance','Var_max_low','Var_max_upper','Standard Deviation','Std_max_low','Std_max_upper']) #create data frame with my list
df_result
df_result.set_index('Project',inplace=True) #make project as an idex
df_result=df_result.T #traspose to see better my data
df_result
a=df_result['education']
a[0]

for x in namescolumns:  
  fig = plt.figure(figsize=(9,5))
  ax = fig.add_subplot(111)
  sns.kdeplot(df[x].dropna(), shade='blue');
  ax.vlines(df_result[x][0], 0, 1.5, colors='r', label='Estimated mean')
  ax.axvspan(df_result[x][1],df_result[x][2], facecolor='r',alpha=0.2, label=r'Estimated mean (95% limits)')
  ax.vlines(df_result[x][6], 0, 1.5, colors='g', label='Estimated scale')
  ax.axvspan(df_result[x][7],df_result[x][8], facecolor='g', alpha=0.2,label=r'Estimated scale (95% limits)');
  ax.legend(fontsize=10, loc='upper right')
  ax.set_xlim([-.5, 1.5])
  ax.set_ylim([0, 1.5])
  plt.show();<jupyter_output><empty_output><jupyter_text>In your own words, summarize the difference between Bayesian and Frequentist statistics Bayesian inference* uses probabilities and depends on the prior and likelihood of observed data.* I use my prior beliefs to make inferences about events that I observe.Frequentist inference (NHST)* depends on the likelihood for the observed and unobserved data( not need a prior)* I think that there is a single true value for the unknowns that we seek and it’s the data that is random and incomplete.  Resources impor- [Worked example of Bayes rule calculation](https://en.wikipedia.org/wiki/Bayes'_theoremExamples) (helpful as it fully breaks out the denominator)- [Source code for mvsdist in scipy](https://github.com/scipy/scipy/blob/90534919e139d2a81c24bf08341734ff41a3db12/scipy/stats/morestats.pyL139)<jupyter_code>import scipy 



a= [1184, 1203, 1219, 1238, 1243, 1204, 1269, 1256, 1156, 1248] 
b=[1136, 1178, 1212, 1193, 1226, 1154, 1230, 1222, 1161, 1148] 
meana=1222
meanb=1186

scipy.stats.ttest_ind(a, b, axis=0, equal_var=True, nan_policy='propagate')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12384140061791966, 'fraction_numerical': 0.03565911431513903, 'mean_word_length': 4.845748683220466, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 6, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '206831', 'license': 'MIT', 'n_tokens_mistral': 2840, 'n_tokens_neox': 2581, 'n_words': 665, 'path': 'module4-real-world-experiment-design/Johana_LS_DS7_133_Introduction_to_Bayesian_Inference_Assignment.ipynb', 'repo_name': 'johanaluna/AB-Demo', 'size': '7768'}