0% found this document useful (0 votes)
6 views5 pages

Python Statistical Tests Implementation

The document outlines a Python program to implement various statistical tests including Z-test, one-sample t-test, two-sample t-test, paired sample t-test, chi-square tests, and one-way ANOVA. Each test is explained with its purpose and includes sample code for execution. The program allows users to select a statistical test and provides the necessary calculations and outputs.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Python Statistical Tests Implementation

The document outlines a Python program to implement various statistical tests including Z-test, one-sample t-test, two-sample t-test, paired sample t-test, chi-square tests, and one-way ANOVA. Each test is explained with its purpose and includes sample code for execution. The program allows users to select a statistical test and provides the necessary calculations and outputs.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Pratical- 11

Aim:- Write a program to implement z test, one sample t-test, two sample t-test ,Paired sample
ttest, chi-square ,goodness if fit and one way anova using python .

def run():

print("Select a statistical test:")

print("1. Z-test") print("2. One-

sample t-test") print("3. Two-

sample t-test") print("4. Paired

sample t-test") print("5. Chi-

square test") print("6.

Goodness-of-fit test") print("7.

One-way ANOVA")

1. Z-Test (One Sample)

The Z-test is used when the sample size is large (typically n > 30) and the population variance

import numpy as np from scipy import stats

def z_test(sample, population_mean, population_std):

sample_mean = [Link](sample) sample_size = len(sample) z_score =

(sample_mean - population_mean) / (population_std / [Link](sample_size)) p_value =

2 * (1 - [Link]([Link](z_score))) return z_score, p_value

#0utput

sample = [20, 21, 22, 23, 24, 25, 26] population_mean = 22

population_std = 2 z_score, p_value = z_test(sample,

population_mean, population_std) print("Z-Score:", z_score)

print("P-Value:", p_value)
2. One-Sample T-Test

This test is used when the sample size is small or the population variance from

scipy import stats

def one_sample_t_test(sample, population_mean):

t_statistic, p_value = stats.ttest_1samp(sample, population_mean)

return t_statistic, p_value

# Output

sample = [20, 21, 22, 23, 24, 25, 26] population_mean = 22

t_statistic, p_value = one_sample_t_test(sample, population_mean)

print("T-Statistic:", t_statistic) print("P-Value:",

p_value)

3. Two-Sample T-Test

Used to compare the means of two independent samples.

from scipy import stats

def two_sample_t_test(sample1, sample2):

t_statistic, p_value = stats.ttest_ind(sample1, sample2)

return t_statistic, p_value

# Output

sample1 = [20, 21, 22, 23, 24] sample2 = [25, 26, 27, 28, 29]

t_statistic, p_value = two_sample_t_test(sample1, sample2)

print("T-Statistic:", t_statistic) print("P-Value:", p_value)


4. Paired Sample T-Test

Used to compare means from the same group at different times.

from scipy import stats

def paired_sample_t_test(sample1, sample2):

t_statistic, p_value = stats.ttest_rel(sample1, sample2)

return t_statistic, p_value

#Output

sample1 = [20, 21, 22, 23, 24] sample2 = [22, 23, 24, 25, 26]

t_statistic, p_value = paired_sample_t_test(sample1, sample2)

print("T-Statistic:", t_statistic) print("P-Value:",

p_value)

5. Chi-Square Test (Goodness of Fit)

Used to determine if there is a significant difference between the observed and expected
frequencies.

from scipy import stats

def chi_square_test(observed, expected):

chi2_statistic, p_value = [Link](f_obs=observed, f_exp=expected)

return chi2_statistic, p_value

#Output

observed = [30, 10, 20, 40] expected = [25, 25, 25, 25]

chi2_statistic, p_value = chi_square_test(observed, expected)

print("Chi-Square Statistic:", chi2_statistic) print("P-Value:",

p_value)
6. Chi-Square Test (Independence)

Used to determine if there is a significant association between two categorical variables.

from scipy import stats

def chi_square_independence(observed):

chi2_statistic, p_value, _, _ = stats.chi2_contingency(observed)

return chi2_statistic, p_value

#Output

observed = [[10, 20, 30], [20, 15, 25]] chi2_statistic, p_value =

chi_square_independence(observed)

print("Chi-Square Statistic:", chi2_statistic) print("P-Value:",

p_value)

7. One-Way ANOVA

Used to determine if there are significant differences between the means of three or more
independent groups. from scipy import stats

def one_way_anova(*samples):

f_statistic, p_value = stats.f_oneway(*samples)

return f_statistic, p_value

#Output

sample1 = [20, 21, 22, 23, 24] sample2 = [25, 26, 27, 28, 29]

sample3 = [30, 31, 32, 33, 34] f_statistic, p_value =

one_way_anova(sample1, sample2, sample3)

print("F-Statistic:", f_statistic) print("P-Value:",

p_value)
Explanation of Tests:

1. Z-test (for proportion): Tests if the proportion of successes in a sample is different from a

hypothesized proportion. The function z_test_proportion computes the Z-statistic and p-

value.

2. One-sample t-test: Tests if the mean of a sample is different from a known value. The

stats.ttest_1samp function performs this test.

3. Two-sample t-test: Compares the means of two independent samples to see if they are

significantly different. The stats.ttest_ind function performs this test.

4. Paired sample t-test: Tests if the means of two related groups are different. The stats.ttest_rel

function performs this test.

5. Chi-square test for independence: Tests if two categorical variables are independent. The

stats.chi2_contingency function performs this test.

6. Chi-square goodness-of-fit test: Tests if the observed frequencies in categorical data match

the expected frequencies. The [Link] function performs this test.

7. One-way ANOVA: Tests if there are significant differences between the means of three or

more groups. We use statsmodels to perform ANOVA.

You might also like