<jupyter_start><jupyter_text>Section 5: Homework ExercisesThis material provides some hands-on experience using the methods learned from the third day's material.<jupyter_code>import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
import scipy.stats as st
import pymc3 as pm
import theano.tensor as tt
import arviz as az
import io<jupyter_output><empty_output><jupyter_text>Exercise 1Consider a data set from "Statistics: A Bayesian Perspective", by <NAME> (1995). The dataset describes the outcome of professional golfers putting from a number of distances:<jupyter_code># golf putting data from berry (1996)
golf_data = """distance tries successes
2 1443 1346
3 694 577
4 455 337
5 353 208
6 272 149
7 256 136
8 240 111
9 217 69
10 200 67
11 237 75
12 202 52
13 192 46
14 174 54
15 167 28
16 201 27
17 195 31
18 191 33
19 147 20
20 152 24"""


golf_data = pd.read_csv(io.StringIO(golf_data), sep=" ")
golf_data.assign(p=golf_data.successes/golf_data.tries).plot('distance', 'p');<jupyter_output><empty_output><jupyter_text>Use a latent GP to estimate the probability of success according to distance.<jupyter_code># Write your answer here<jupyter_output><empty_output><jupyter_text>Exercise 2: Nashville daily temperaturesThe file `TNNASHVI.txt` in your data directory contains daily temperature readings for Nashville, courtesy of the [Average Daily Temperature Archive](http://academic.udayton.edu/kissock/http/Weather/). This data, as one would expect, oscillates annually. Use a Gaussian process to fit a regression model to this data.<jupyter_code>daily_temps = pd.read_table("../data/TNNASHVI.txt", sep='\s+', 
                            names=['month','day','year','temp'], na_values=-99)
temps_2010 = daily_temps.temp[daily_temps.year>2010]
temps_2010.plot(style='b.', figsize=(10,6), grid=False)
# Write your answer here<jupyter_output><empty_output><jupyter_text>Exercise: Random effects meta-analysisCarlin (1992) considers a Bayesian approach to meta-analysis, and includes examples of 22 trials of beta-blockers to prevent mortality after myocardial infarction. In one possible random effects model we assume the true effect (on a log-odds scale) $d_i$ in a trial $i$ is drawn from some population distribution. Let $r^C_i$ denote number of events in the control group in trial $i$, and $r^T_i$ denote events under active treatment in trial $i$. Our model is:$$\begin{aligned}r^C_i &\sim \text{Binomial}\left(p^C_i, n^C_i\right) \\r^T_i &\sim \text{Binomial}\left(p^T_i, n^T_i\right) \\\text{logit}\left(p^C_i\right) &= \mu \\\text{logit}\left(p^T_i\right) &= \mu + \delta_i \\\delta_i &\sim f\end{aligned}$$Instead of assuming a Gaussian random effect $f$, experiment with Dirichlet process priors, and check whether it improves the resulting model.<jupyter_code>r_t_obs = [3, 7, 5, 102, 28, 4, 98, 60, 25, 138, 64, 45, 9, 57, 25, 33, 28, 8, 6, 32, 27, 22]
n_t_obs = [38, 114, 69, 1533, 355, 59, 945, 632, 278,1916, 873, 263, 291, 858, 154, 207, 251, 151, 174, 209, 391, 680]
r_c_obs = [3, 14, 11, 127, 27, 6, 152, 48, 37, 188, 52, 47, 16, 45, 31, 38, 12, 6, 3, 40, 43, 39]
n_c_obs = [39, 116, 93, 1520, 365, 52, 939, 471, 282, 1921, 583, 266, 293, 883, 147, 213, 122, 154, 134, 218, 364, 674]
N = len(n_c_obs)
# Write your answer here<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11506024096385542, 'fraction_numerical': 0.11475903614457832, 'mean_word_length': 5.049180327868853, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '146744', 'license': 'MIT', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1130, 'n_words': 428, 'path': 'notebooks/Section5-Homework.ipynb', 'repo_name': 'vishalbelsare/QB_Bayes_Course', 'size': '3323'}