import numpy as np from scipy import stats import matplotlib.pyplot as plt # Generate sample data np.random.seed(42) normal_data = np.random.normal(loc=0, scale=1, size=1000) skewed_data = np.random.exponential(scale=1, size=1000) # Perform normality tests def test_normality(data, name): shapiro_stat, shapiro_p = stats.shapiro(data) ks_stat, ks_p = stats.kstest(data, 'norm') anderson_result = stats.anderson(data, dist='norm') print(f"--- {name} ---") print(f"Shapiro-Wilk test: statistic = {shapiro_stat:.4f}, p-value = {shapiro_p:.4f}") print(f"Kolmogorov-Smirnov test: statistic = {ks_stat:.4f}, p-value = {ks_p:.4f}") print(f"Anderson-Darling test: statistic = {anderson_result.statistic:.4f}") print("Critical values:", anderson_result.critical_values) print() test_normality(normal_data, "Normal Distribution") test_normality(skewed_data, "Skewed Distribution") # Visualize the distributions fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5)) ax1.hist(normal_data, bins=30, density=True, alpha=0.7) ax1.set_title("Normal Distribution") ax1.set_xlabel("Value") ax1.set_ylabel("Density") ax2.hist(skewed_data, bins=30, density=True, alpha=0.7) ax2.set_title("Skewed Distribution") ax2.set_xlabel("Value") ax2.set_ylabel("Density") plt.tight_layout() plt.show() # Q-Q plots fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5)) stats.probplot(normal_data, dist="norm", plot=ax1) ax1.set_title("Q-Q Plot: Normal Distribution") stats.probplot(skewed_data, dist="norm", plot=ax2) ax2.set_title("Q-Q Plot: Skewed Distribution") plt.tight_layout() plt.show()