<jupyter_start><jupyter_text>Two Sample Testing<jupyter_code>import numpy as np
from scipy.stats import ttest_1samp, ttest_ind,mannwhitneyu,levene,shapiro,wilcoxon
from statsmodels.stats.power import ttest_power
import pandas as pd<jupyter_output><empty_output><jupyter_text>Independent samples<jupyter_code>weight = np.array([
# sugar consumption in grams and stature (0=diabetic, 1=non diabetic)
[9.31, 0],
[7.76, 0],
[6.98, 1],
[7.88, 1],
[8.49, 1],
[10.05, 1],
[8.80, 1],
[10.88, 1],
[6.13, 1],
[7.90, 1],
[11.51, 0],
[12.59, 0],
[7.05, 1],
[11.85, 0],
[9.99, 0],
[7.48, 0],
[8.79, 0],
[8.69, 1],
[9.68, 0],
[8.58, 1],
[9.19, 0],
[8.11, 1]])<jupyter_output><empty_output><jupyter_text>The above dataset contains 2 samples from 2 different population - Diabetic and Non-Diabetic. And these 2 population are independent of each other. Hence we apply unpaired test on this dataset spliting it into 2 samples (Diabetic and Non-Diabetic)Diabetic - [9.31, 7.76, 11.51, 12.59, 11.85, 9.99, 7.48, 8.79, 9.68, 9.19]Non-Diabetic - [6.98, 7.88, 8.49, 10.05, 8.8, 10.88, 6.13, 7.9, 7.05, 8.69, 8.58, 8.11]<jupyter_code>diabetic = weight[weight[:,1]==0][:,0]
non_diabetic = weight[weight[:,1]==1][:,0]
diabetic, non_diabetic<jupyter_output><empty_output><jupyter_text>**Formulate the Hypothesis**H0 : &mu;1 = &mu;2 (&mu;1 - &mu;2 = 0)Ha : &mu;1 $\neq$ &mu;2 (&mu;1 2, &mu;1 > &mu;2)This test assumes the variances are equal among the two diff samples<jupyter_code># ttest_ind is used for parametric unpaired samples
ttest_ind(diabetic, non_diabetic)<jupyter_output><empty_output><jupyter_text>p_value < 0.05 (significance level). Hence we reject the null hypothesis. We have sufficient evidence to accept the alternative hypothesis<jupyter_code># mannwhitneyu is used for non-parametric unpaired samples
mannwhitneyu(diabetic, non_diabetic)<jupyter_output><empty_output><jupyter_text>p_value < 0.05 (significance level). Hence we reject the null hypothesis. We have sufficient evidence to accept the alternative hypothesis  Dependent Samples<jupyter_code># pre and post-Exercise food energy intake
intake = np.array([
[5460, 3980],
[5230, 4890],
[5640, 3885],
[6180, 5160],
[6390, 5645],
[6512, 4650],
[6765, 6109],
[7515, 5975],
[7515, 6790],
[8230, 6970],
[8770, 7335],
])<jupyter_output><empty_output><jupyter_text>The above intake dataset contains 2 samples (pre, post) derived from a single population. And these 2 samples are dependent on eachother. Hence we apply paired test on this dataset.<jupyter_code>pre = intake[:,0]
post = intake[:,1]<jupyter_output><empty_output><jupyter_text>**Formulate the Hypothesis**H0 : &mu;1 = &mu;2 (&mu;1 - &mu;2 = 0)Ha : &mu;1 $\neq$ &mu;2 (&mu;1 2, &mu;1 > &mu;2)This test assumes the variances are equal among the two diff samples<jupyter_code># For paired t-test of parametric samples
ttest_1samp(post-pre, popmean=0)

# we consider both the smaple means are equal. Hence difference in popmean is zero
# For paired t-test of non-parametric samples
wilcoxon(post-pre)<jupyter_output><empty_output><jupyter_text>Both the parametric and non-parametric tests for this paired sample suggests that we need to reject the Null Hypothesis and embrace the Alternate Hypothesis  Test for the variancesH0 = The variances of both the samples are equalHa = The variances are not equal<jupyter_code>levene(diabetic,non_diabetic)
levene(pre,post)<jupyter_output><empty_output><jupyter_text>The greater p_value suggests that we have to accept the null hypothesis which says that both the samples have equal variances  Test for the Shape of the populationH0 = The sample comes from a normal populationHa = The sample doesn't come from a normal distribution<jupyter_code>t_statistic, p_value = shapiro(diabetic)
t_statistic, p_value
t_statistic, p_value = shapiro(non_diabetic)
t_statistic, p_value
t_statistic, p_value = shapiro(pre)
t_statistic, p_value
t_statistic, p_value = shapiro(post)
t_statistic, p_value<jupyter_output><empty_output><jupyter_text>The greater p_value in both the cases suggests that we have to accept the null hypothesis which says that all the samples from both the dataset comes from a normal population  Power of the test (1-&beta;)To calculate the power of the test we need to calculate the delta value, which requires Spooled (Pooled Standard Deviation) to be calculated for two sampe testWhere<jupyter_code>delta = (np.mean(pre) - np.mean(post)) / np.sqrt(((11-1)*np.var(pre) + (11-1)*np.var(post)) / 11-1+11-1)
delta
ttest_power(delta, nobs=11, alpha=0.05, alternative="two-sided")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12384918895221395, 'fraction_numerical': 0.06992547128452434, 'mean_word_length': 6.052550231839258, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '493995', 'license': 'Apache-2.0', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1633, 'n_words': 509, 'path': 'CourseContent/04-Advanced.Statistics/Week3/SM4 - Two Sample Testing.ipynb', 'repo_name': 'averma111/AIML-PGP', 'size': '4562'}