<jupyter_start><jupyter_text>---author: <NAME> (<EMAIL>)--- We choose a value, $0 \le \alpha \le 1$, as the Type I Error rate, and in this casewe will set it to be 0.05.We're going to use fake fata here, but you can replace our fake data with your real data below.Because the data are matched pairs, the samples must be the same size.<jupyter_code># Replace the following example data with your real data
sample1 = [15, 10,  7, 22, 17, 14]
sample2 = [ 9,  1, 11, 13,  3,  6]<jupyter_output><empty_output><jupyter_text>Two-tailed testIn a two-sided hypothesis test, the null hypothesis states that the meandifference is equal to 0 (or some other hypothesized value), $H_0: \mu_D = 0$.<jupyter_code>from scipy import stats
stats.ttest_rel(sample1, sample2, alternative = "two-sided")<jupyter_output><empty_output><jupyter_text>Our $p$-value, 0.0355, is smaller than $\alpha$, so we have sufficient evidence to reject the null hypothesis and conclude that the mean difference between the two samples is significantly different from zero. Note that the function above specifically tests whether the mean of$x_i-x'_i$ is zero.  If we want instead to test whether it is some other value$d\neq0$, then that's equivalent to testing whether the mean of $(x_i-d)-x'_i$is zero.  We could do so with the code below, which uses an example value of $d$.The null hypothesis is now $H_0: \mu_D=d$.<jupyter_code>d = 6  # as an example
stats.ttest_rel([ x - d for x in sample1 ], sample2, alternative = "two-sided")<jupyter_output><empty_output><jupyter_text>The above $p$-value is greater than $\alpha=0.05$, so we could not conclude thatthe mean difference is significantly different from our chosen $d=6$. Right-tailed testIf instead we want to test whether the mean difference is less than or equal tozero, $H_0: \mu_D\le0$, we can use a right-tailed test, as follows.<jupyter_code>stats.ttest_rel(sample1, sample2, alternative = "greater")<jupyter_output><empty_output><jupyter_text>Our $p$-value, 0.01775, is smaller than $\alpha$, so we have sufficient evidenceto reject the null hypothesis and conclude that the mean difference between thetwo samples is significantly greater than zero. A similar change could be made to the code above to test $H_0:\mu_D\le d$,as in the example code further above that uses $d=6$. Left-tailed testIf instead we want to test whether the mean difference is greater than or equal tozero, $H_0: \mu_D\ge 0$, we can use a right-tailed test, as follows.<jupyter_code>stats.ttest_rel(sample1, sample2, alternative = "less")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09066147859922179, 'fraction_numerical': 0.023735408560311283, 'mean_word_length': 5.049411764705883, 'pattern_counts': {'":': 0, '<': 23, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '653326', 'license': 'MIT', 'n_tokens_mistral': 816, 'n_tokens_neox': 771, 'n_words': 348, 'path': 'database/tasks/How to do a hypothesis test for a mean difference (matched pairs)/Python, using SciPy.ipynb', 'repo_name': 'nathancarter/how2data', 'size': '2599'}