# Hypothesis Testing Python

Null hypothesis and alternative hypothesis are the two different methods of hypothesis testing. The premise for a null hypothesis is an occurrence (also called the ground truth). An alternative hypothesis is a presumption that disputes the primary hypothesis. Imagine a woman in her seventies who has a noticeable tummy bump. Medical professionals could presume the bulge is a fibroid.

In this instance, our first finding (or the null hypothesis) is that this woman has a fibroid, and our alternative finding is that she does. We shall use terms null hypothesis (beginning assumption) and alternate hypothesis (countering assumption) to conduct hypothesis testing. The next step is gathering the data samples we can use to validate the null hypothesis.

Data on the hypothesis should be gathered and examined to determine whether or not H0 may be accepted. When doing that, there's a chance that the following things will occur:

• H0 is acknowledged as the ground truth since it is true.
• Since H0 is denied and H1 is accepted, the ground truth is incorrect.

The preferred scenarios are the two examples mentioned above. Our null hypothesis can be correct and accepted, or it can be incorrect and rejected.

The following options are the remaining ones:

• Although the null hypothesis (H0) was correct, we rejected it.
• Although the null hypothesis (H0) was incorrect, we did not dismiss it.

P-value:- The likelihood of discovering the recorded or more severe outcomes whenever the null hypothesis (H0) of a research question is true is known as the P value or computed probability; the meaning of "severe" relies upon how the hypothesis has been tested.

When your P value falls below the selected significance threshold, you dismiss the null hypothesis and agree that your sample contains solid proof that the alternative hypothesis is true. It Still does not Suggests a "significant" or "important" change; you must determine that while evaluating the applicability of your conclusion in the actual world.

## T- Test

When comparing the mean values of two samples that specific characteristics may connect, a t-test is performed to see if there exists a substantial difference. It is typically employed when data sets, such as those obtained from tossing a coin 100 times and stored as results, would exhibit a normal distribution. It could have unknown variances. The t-test is a method for evaluating hypotheses that allows you to assess a population-applicable assumption.

Assumptions

• Each sample's data is randomly and uniformly distributed (iid).
• Each sample's data have a normal distribution.
• Every sample's data share the same variance.

T-tests are of two types: 1. one-sampled t-test and 2. two-sampled t-test.

One sample t-test: The One Sample t-test ascertains if the sample average differs statistically from an actual or apposed population mean. A parametric testing technique is the One Sample t-test.

Example: You are determining if the average age of 10 people is 30 or otherwise. Check the Python script below for the implementation.

Code

Output

```[45, 89, 23, 46, 12, 69, 45, 24, 34, 67]
45.4
P-value is:  0.07179988272763554
We can accept the null hypothesis
```

Two sampled t-test:- To ascertain if there exists any statistical confirmation that the related population means are statistically substantially distinct, the Independent Samples T-Test, also known as the 2-sample T-test, analyses the mean values of two independent samples. The Independent Samples T-Test is also a parametric test. The Independent t Test is another name for this test.

For instance, can we check if there is a correlation between the two data groups defined in the code below?

Code

Output

```Data group 1 mean value: 16.25
Data group 2 mean value: 20.85
Data group 1 std value: 6.171507109288622
Data group 2 std value: 4.452808102759426
The P-value is:  0.012117171124028792
We can reject the null hypothesis
```

Paired sampled t-test:- The dependent samples t-test is another name for the paired samples t-test. A substantial difference between two related variables is tested using a univariate test. An illustration of this would be taking a person's blood pressure before and after a specific medication, condition, or period.

Code

Output

```The P-value of the test is:  0.15266056244408904
We can accept the null hypothesis
```

## Z-Test

In the z-test, which also functions as a hypothesis test, the z-statistic has a normal distribution. As the central limit theorem states, observations are assumed to be roughly normally distributed as sample size increases. Hence the z-test is most effective for samples bigger than 30.

The null and alternate hypotheses, alpha, and z-score values must all be specified when doing a z-test. The test statistic must then be computed, followed by a statement of the findings and conclusions. A z-statistic, also known as a z-score, measures how many standard deviations a score resulting from a z-test is either above or under the mean population.

One-sample location tests, two-sample location tests, paired difference tests, and maximum likelihood estimates are a few tests that may be performed as z-tests.

One-Sample Z-Test:- Assume a trader wants to determine if a stock's daily mean gain is more than 3%. The average is defined for a straightforward arbitrary sample of 50 results, which is 2%. Assume that the profits' standard deviation is 1.5 percent. Therefore, the null hypothesis in this situation is whenever the mean is equal to 3%.

On the other hand, the alternate hypothesis is if the average return is more or lower than 3%. We can assume a two-tailed z-test is used to choose an alpha value of 0.05%. As a result, the alpha has a significance level of 1.96 or -1.96, and 0.025% of the observations are distributed evenly across the two tails. We will disprove the null hypothesis if z is larger than 1.96 or smaller than -1.96.

The answer for z is determined by deducting the observed mean of the samples from the value of the mean daily return chosen for the test, in this instance, 1%. After that, multiply the result by the square root of the total number of observations in the sample divided by the standard deviation.

Since z is higher than 1.96, the person will reject the null hypothesis and concludes that the daily mean return is higher than 1%.

Code

Output

```2.417334226169332e-186
We can reject the null hypothesis
```

Two-sample Z-test- Equivalent to the t-test, the 2 data groups z-test examines two independent data sets to determine whether or not the sample means of the two groups are identical.

H0: average of the two data groups is 0

H1: average of the two data groups is not 0

Example: we are examining the two data groups in the code below.

Code

Output

```0.04813782199434202
We can reject the null hypothesis
```

## ANOVA (F-TEST)

The t-test is effective for comparing two groups; however, there are situations when we wish to examine more than two datasets at once. For instance, we would need to analyze the averages of each class or group to determine whether voter age varied depending on a categorical variable like race. We could do a separate t-test for each combination of groups, which raises the possibility of false positive results. Analysis of variance, or ANOVA, is a statistical inference technique that permits the parallel comparison of the distribution of several data groups.

The F-distribution cannot contain any negative numbers, contrary to the z-test and t-distributions, since the within and between-group variability are generally positive due to squaring each deviation value.

One-Way F-test(Anova):- Depending on the average similarity and f-score of two or more data groups, it may be determined if they are similar or not.

Code

Output

```p-value is:  0.04043792126789144
We can reject the null hypothesis
```

Two-Way F-test:- When there are two independent variables and two or more groups, we utilize the two-way F-test, a generalization of the one-way F-test. We cannot find the dominant factor using the 2-way F-test. If the personalized significance value needs to be validated, post-hoc testing is required.

Code

Output

```                                                  df      sum_sq     mean_sq  \
C(Frequency_fertilizers)                         1.0    1.531250    1.531250
C(Frequency_Watering)                            1.0  117.945205  117.945205
C(Frequency_fertilizers):C(Frequency_Watering)   1.0    1.643988    1.643988
Residual                                        30.0  802.437500   26.747917

F    PR(>F)
C(Frequency_fertilizers)                        0.057247  0.812528
C(Frequency_Watering)                           4.409510  0.044253
C(Frequency_fertilizers):C(Frequency_Watering)  0.061462  0.805889
Residual                                             NaN       NaN
```

## Chi-Square Test

This test is used when two categorized variables are from the same population. Its purpose is to decide if the two elements are significantly associated.

For example, we may group people in an election campaign survey based on their preferred method of voting and gender (male or female) (Democratic, Republican, or Independent). To determine if gender affects voting choice, we may apply a chi-square test evaluating independence.

Here is an example

Code

Output

```The p-value of our test is 1.4585823594475804e-06
We can reject the null hypothesis
```

## Mann-Whitney U Test

Tests whether the distributions of two independent samples are equal or not.

Assumptions

• Each sample's data are randomly and uniformly distributed (iid).
• Each sample's data can be rated.

Interpretation

• H0: Both sets' distributions are equivalent.
• H1: Neither of the sets' distributions is equal.

Code

Output

```P-value is:  0.520366020896531
We can accept the null hypothesis
```

## Wilcoxon Signed-Rank Test

Tests whether the distributions of two paired samples are equal or not.

Assumptions

• Every sample's observations are randomly and uniformly distributed (iid).
• Every sample's observations can be ranked.
• Every sample's observations should be coupled.

Interpretation

• H0: The samples' distributions are equal.
• H1: Neither of the samples' distributions is equal.

Code

Output

```P-value is:  0.625
We can accept the null hypothesis
```

## Kruskal-Wallis H Test

Tests whether the distributions of two or more independent samples are equal or not.

Assumptions

• Every sample's observations are randomly and uniformly distributed (iid).
• Every sample's observations can be ranked.

Interpretation

• H0: The samples' distributions are equal.
• H1: Neither of the samples' distributions is equal.

Code

Output

```P-value is:  0.49612971494281877
We can accept the null hypothesis
```

## Friedman Test

Tests whether the distributions of two or more paired samples are equal or not.

Assumptions

• Every sample's observations are randomly and uniformly distributed (iid).
• Every sample's observations can be ranked.
• Every sample's observations should be coupled.

Interpretation

• H0: The samples' distributions are equal.
• H1: Neither of the samples' distributions is equal.

Code

Output

```P-value is:  0.496585303791408
We can accept the null hypothesis
```