Sunday, October 19, 2025

Simple program for finding out the p-value for rejecting null hypothesis

 import numpy as np

import scipy.stats as stats


# energy expenditure (in mJ) and stature (0=obese, 1=lean)

energy = np.array([[9.21, 0],[7.53, 1],[7.48, 1],[8.08, 1],[8.09, 1],[10.15, 1],[8.40, 1],[0.88, 1],[1.13, 1],[2.90, 1],[11.51, 0],[2.79, 0],[7.05, 1],[1.85, 0],[19.97, 0],[7.48, 1],[8.79, 0],[9.69, 0],[2.68, 0],[3.58, 1],[9.19, 0],[4.11, 1]])


# Separating the data into 2 groups

group1 = energy[energy[:, 1] == 0] # elements of the array where obese == True

group1 = group1[:,0] # energy expenditure of obese

group2 = energy[energy[:, 1] == 1] # elements of the array where lean == True

group2 = group2[:,0] # energy expenditure of lean


# Perform t-test

t_statistic, p_value = stats.ttest_ind(group1, group2, equal_var=True)


print("T-TEST RESULTS: Obese (0) vs Lean (1) Energy Expenditure")

print("=" * 55)

print(f"Obese group (n={len(group1)}): Mean = {np.mean(group1):.2f} mJ, Std = {np.std(group1, ddof=1):.2f} mJ")

print(f"Lean group (n={len(group2)}): Mean = {np.mean(group2):.2f} mJ, Std = {np.std(group2, ddof=1):.2f} mJ")

print(f"\nT-statistic: {t_statistic:.4f}")

print(f"P-value: {p_value:.4f}")


# Interpretation

alpha = 0.05

print(f"\nINTERPRETATION (α = {alpha}):")

if p_value < alpha:

    print("✅ REJECT NULL HYPOTHESIS")

    print("   There is a statistically significant difference in energy expenditure")

    print("   between obese and lean individuals.")

else:

    print("❌ FAIL TO REJECT NULL HYPOTHESIS")

    print("   No statistically significant difference in energy expenditure")

    print("   between obese and lean individuals.")


# Show the actual data

print(f"\nOBESE GROUP ENERGY EXPENDITURE: {group1}")

print(f"LEAN GROUP ENERGY EXPENDITURE: {group2}")


No comments:

Post a Comment