<jupyter_start><jupyter_text>t-test___There may be situations where the standard deviation of the population is unknown, and the sample size is small. In all such cases, we use the T-distribution. This distribution is also called *Student’s T distribution*.The following are the chief characteristics of the T-distribution:+ The T-distribution is similar in shape to a normal distribution, except that it is slightly flatter.+ The sample size is small, generally less than 30.+ The T-distribution uses the concept of degrees of freedom. The degrees of freedom are the number of observations in a statistical test that can be estimated independently.    Example: Suppose we have three numbers x, y and z. If we know the mean is 5. we can say the sum of numbers should be 5*3 = 15. Now we have freedom to choose any number as x and y, but not z. z should be choosen in a way such that the numbers add upto 15 so the mean turns to be 5. So even though we have 3 numbers we have freedom to choose only 2. i.e. we have 2 degrees of freedom.    + As the sample size decreases, the degrees of freedom reduce, or in other words, the certainty with which the population parameter can be predicted from the sample parameter reduces.The degrees of freedom (df) in the T-distribution is the number of samples (n) -1, or in other words, df = n - 1  ![](data/t_dist.png)The formula for the critical test statistic in a one-sample t-test is given by the followingequation: $$t = \frac{\overline x - \mu}{\frac{s}{\sqrt n}}$$where $\overline x$ is the sample mean, $\mu$ is the population mean, $s$ is the sample standard deviation and $n$ is the sample size. One-sample t-test A one-sample t-test is similar to a one-sample z-test, with the following differences:1. The size of the sample is small (<30).2. The population standard deviation is not known; we use the sample standard deviation(s) to calculate the standard error.3. The critical statistic here is the t-statistic, given by the following formula:$$t = \frac{\overline x - \mu}{\frac{s}{\sqrt n}}$$ A coaching institute, preparing students for an exam, has 200 students, and the average score of the students in the practice tests is 80. It takes a sample of nine students and records their scores; it seems that the average score has now increased. These are the scores of these ten students: 80, 87, 80, 75, 79, 78, 89, 84, 88. Conduct a hypothesis test at a 5% significance level to verify if there is a significant increase in the average score.$H_0:\mu = 80$  $H_1:\mu > 80$<jupyter_code>import numpy as np
import scipy.stats as stats

sample = np.array([80,87,80,75,79,78,89,84,88])

stats.ttest_1samp(sample,80)<jupyter_output><empty_output><jupyter_text>Since the p-value is greater than 0.05, we fail to reject the null hypothesis. Hence, we cannot conclude that the average score of students has changed.  Two-sample t-test A two-sample t-test is used when we take samples from two populations, where both the sample sizes are less than 30, and both the population standard deviations are unknown. Formula:$$t = \frac{\overline x_1 - \overline x_2}{\sqrt{S_p^2(\frac{1}{n_1}+\frac{1}{n_2})}}$$Where $x_1$ and $x_2$ are the sample means  The degrees of freedom: $df=n_1 + n_2 − 2$  The pooled variance $S_p^2 = \frac{(n_1 -1)S_1^2 + (n_2-1)S_2^2}{n_1+n_2-2}$  A coaching institute has centers in two different cities. It takes a sample of ten students from each center and records theirscores, which are as follows:  |Center A:| 80, 87, 80, 75, 79, 78, 89, 84, 88||---------|-----------------------------------||Center B:| 81, 74, 70, 73, 76, 73, 81, 82, 84|   Conduct a hypothesis test at a 5% significance level, and verify if there a significant difference in the average scores of thestudents in these two centers.$H_0:\mu_1 = \mu_2$  $H_1:\mu_1 != \mu_2$<jupyter_code>a = np.array([80,87,80,75,79,78,89,84,88])
b = np.array([81,74,70,73,76,73,81,82,84])

stats.ttest_ind(a,b)<jupyter_output><empty_output><jupyter_text>We can conclude that there is a significant difference in the average scores of students in the two centers of the coachinginstitute since the p-value is less than 0.05  Two-sample t-test for paired samples This test is used to compare population means from samples that are dependent on each other, that is, sample values are measured twice using the same test group.+ A measurement taken at two different times (e.g., pre-test and post-test score with an intervention administered between the two time points)+ A measurement taken under two different conditions (e.g., completing a test under a "control" condition and an "experimental" condition)This equation gives the critical value of the test statistic for a paired two-sample t-test:$$t = \frac{\overline d}{s/\sqrt{n}}$$Where $\overline d$ is the average of the difference between the elements of the two samples. Boththe samples have the same size, $n$.S = standard deviation of the differences between the elements of the two samples =$$\sqrt{\frac{\sum d^2 -((\sum d)^2/ n)}{n -1}}$$ The coaching institute is conducting a special program to improve the performance of the students. The scores of the same set of students are compared before and after the special program. Conduct a hypothesis test at a 5% significance level to verify if the scores have improved because of this program.<jupyter_code>a = np.array([80,87,80,75,79,78,89,84,88])
b = np.array([81,89,83,81,79,82,90,82,90])

stats.ttest_rel(a,b)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09414340448823208, 'fraction_numerical': 0.04050355774493705, 'mean_word_length': 4.390363815142576, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '32764', 'license': 'MIT', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1506, 'n_words': 802, 'path': '13. t-test.ipynb', 'repo_name': 'luckyrabbit85/Statistics-For-Data-Science', 'size': '5481'}