<jupyter_start><jupyter_text>5. Resampling Methods – ConceptualExcercises from **Chapter 5* of [An Introduction to Statistical Learning](http://www-bcf.usc.edu/~gareth/ISL/) by <NAME>, <NAME>, <NAME> and <NAME>.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>1. Using basic statistical properties of the variance, as well as single- variable calculus, derive (5.6). In other words, prove that α given by (5.6) does indeed minimize Var(αX + (1 − α)Y ).![5_1.jpg](./images/5_1.jpg) 2. We will now derive the probability that a given observation is part of a bootstrap sample. Suppose that we obtain a bootstrap sample from a set of n observations.- (a) What is the probability that the first bootstrap observation is not the jth observation from the original sample? Justify your answer.- (b) What is the probability that the second bootstrap observation is not the jth observation from the original sample?- (c) Argue that the probability that the jth observation is not in the bootstrap sample is (1 − 1/n)n.- (d) When n = 5, what is the probability that the jth observation is in the bootstrap sample?- (e) When n = 100, what is the probability that the jth observation is in the bootstrap sample?- (f) When n = 10, 000, what is the probability that the jth observa- tion is in the bootstrap sample?![5_2.jpg](./images/5_2.jpg) 2(g) Create a plot that displays, for each integer value of n from 1 to 100,000, the probability that the jth observation is in the bootstrap sample. Comment on what you observe.<jupyter_code>def prob_j_in_sample(n):
    return 1 - (1 - 1/n)**n

x = np.arange(1, 100000)
y = np.array([prob_j_in_sample(n) for n in x])

ax = sns.lineplot(x=x, y=prob_j_in_sample(x))
plt.xlabel('n')
plt.ylabel('probability')<jupyter_output><empty_output><jupyter_text>2(h) We will now investigate numerically the probability that a bootstrap sample of size n = 100 contains the jth observation. Here j = 4. We repeatedly create bootstrap samples, and each time we record whether or not the fourth observation is contained in the bootstrap sample.<jupyter_code>store = [] 
for i in np.arange(1, 10000):
    store += [np.sum((np.random.randint(low=1, high=101, size=100) == 4)) > 0]

np.mean(store)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09702457956015524, 'fraction_numerical': 0.028460543337645538, 'mean_word_length': 4.81453634085213, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '601622', 'license': 'MIT', 'n_tokens_mistral': 768, 'n_tokens_neox': 700, 'n_words': 313, 'path': 'Notebooks/ch5_resampling_methods_conceptual.ipynb', 'repo_name': 'Joki79/ISL-python', 'size': '2351'}