0% found this document useful (0 votes)
5 views3 pages

Exp 7

The document contains a Python script that generates two random samples from Gaussian distributions, calculates their means and variances, and performs a t-test to determine if there is a significant difference between the samples. It outputs the sample data, calculated statistics, and the result of the hypothesis test based on a t-critical value. The results are saved to a CSV file for further analysis.

Uploaded by

Avender Sharma
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)
5 views3 pages

Exp 7

The document contains a Python script that generates two random samples from Gaussian distributions, calculates their means and variances, and performs a t-test to determine if there is a significant difference between the samples. It outputs the sample data, calculated statistics, and the result of the hypothesis test based on a t-critical value. The results are saved to a CSV file for further analysis.

Uploaded by

Avender Sharma
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
You are on page 1/ 3

NAME:Avender Sharma

Roll :12212079
import random
import csv

# Parameters
pop_std_dev = 5 # Population standard deviation
n1, n2 = 6, 6 # Sample sizes

# Generating two random samples using Gaussian distribution


sample_1 = [int(random.gauss(110, pop_std_dev)) for _ in
range(n1)]
sample_2 = [int(random.gauss(100, pop_std_dev)) for _ in
range(n2)]

# Calculate means
mean_1 = sum(sample_1) / n1
mean_2 = sum(sample_2) / n2

# Function to calculate sample variance (biased variance


formula)
def sample_variance(sample, sample_mean):
return sum((x - sample_mean) ** 2 for x in sample) /
(len(sample) - 1)

# Calculate variances
var_1 = sample_variance(sample_1, mean_1)
var_2 = sample_variance(sample_2, mean_2)

# Calculate pooled standard error


pooled_se = ((var_1 / n1) + (var_2 / n2)) ** 0.5

# Compute t-score
t_statistic = (mean_1 - mean_2) / pooled_se
df = n1 + n2 - 2 # Degrees of freedom

# Given t-critical for df = 10 and alpha = 0.05


(two-tailed)
t_critical_value = 2.571

# Save sample data to CSV file


with open("sample_data.csv", mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Sample 1", "Sample 2"])
for val_1, val_2 in zip(sample_1, sample_2):
writer.writerow([val_1, val_2])

# Output results
print(f"Sample 1 Data: {sample_1}")
print(f"Sample 2 Data: {sample_2}")
print(f"Mean of Sample 1: {mean_1}")
print(f"Mean of Sample 2: {mean_2}")
print(f"Variance of Sample 1: {var_1}")
print(f"Variance of Sample 2: {var_2}")
print(f"Pooled Standard Error: {pooled_se}")
print(f"T-Statistic: {t_statistic}")
print(f"Degrees of Freedom: {df}")
print(f"T-Critical (α = 0.05): {t_critical_value}")

# Hypothesis testing
if abs(t_statistic) > t_critical_value:
print("Result: Significant difference between the two
samples (Reject H₀).")
else:
print("Result: No significant difference between the
two samples (Fail to reject H₀).")

You might also like