Oluwalkemdown/SamplingDistribution
0
1import streamlit as st2import numpy as np3import matplotlib.pyplot as plt4 5# -------------------------------6# Page Configuration7# -------------------------------8st.set_page_config(9 page_title="Sampling Distribution Simulator",10 layout="wide"11)12 13st.title("๐ Sampling Distribution Simulator")14st.caption("Explore how the sampling distribution of the mean behaves (CLT in action).")15 16# -------------------------------17# Sidebar Controls18# -------------------------------19st.sidebar.header("Population Settings")20 21distribution = st.sidebar.selectbox(22 "Population Distribution",23 ["Normal", "Uniform", "Exponential", "Bimodal"]24)25 26population_size = st.sidebar.slider(27 "Population Size",28 min_value=1000,29 max_value=100000,30 step=1000,31 value=1000032)33 34st.sidebar.header("Sampling Settings")35 36sample_size = st.sidebar.slider(37 "Sample Size (n)",38 min_value=1,39 max_value=200,40 value=3041)42 43num_samples = st.sidebar.slider(44 "Number of Samples",45 min_value=10,46 max_value=5000,47 step=10,48 value=100049)50 51# -------------------------------52# Generate Population53# -------------------------------54np.random.seed(42)55 56if distribution == "Normal":57 mu = st.sidebar.slider("Mean (ฮผ)", -10.0, 10.0, 0.0)58 sigma = st.sidebar.slider("Std Dev (ฯ)", 0.5, 10.0, 2.0)59 population = np.random.normal(mu, sigma, population_size)60 61elif distribution == "Uniform":62 low = st.sidebar.slider("Lower Bound", -20.0, 0.0, -5.0)63 high = st.sidebar.slider("Upper Bound", 0.0, 20.0, 5.0)64 population = np.random.uniform(low, high, population_size)65 66elif distribution == "Exponential":67 scale = st.sidebar.slider("Scale (1/ฮป)", 0.5, 10.0, 2.0)68 population = np.random.exponential(scale, population_size)69 70elif distribution == "Bimodal":71 mu1 = st.sidebar.slider("Mean 1", -10.0, 0.0, -3.0)72 mu2 = st.sidebar.slider("Mean 2", 0.0, 10.0, 3.0)73 sigma = st.sidebar.slider("Std Dev", 0.5, 5.0, 1.5)74 population = np.concatenate([75 np.random.normal(mu1, sigma, population_size // 2),76 np.random.normal(mu2, sigma, population_size // 2)77 ])78 79# -------------------------------80# Sampling81# -------------------------------82sample_means = []83single_sample = np.random.choice(population, size=sample_size, replace=True)84 85for _ in range(num_samples):86 sample = np.random.choice(population, size=sample_size, replace=True)87 sample_means.append(np.mean(sample))88 89sample_means = np.array(sample_means)90 91# -------------------------------92# Layout93# -------------------------------94col1, col2, col3 = st.columns(3)95 96# -------------------------------97# Population Plot98# -------------------------------99with col1:100 st.subheader("Population Distribution")101 fig, ax = plt.subplots()102 ax.hist(population, bins=40, density=True)103 ax.set_xlabel("Value")104 ax.set_ylabel("Density")105 st.pyplot(fig)106 107# -------------------------------108# Single Sample Plot109# -------------------------------110with col2:111 st.subheader("One Random Sample")112 fig, ax = plt.subplots()113 ax.hist(single_sample, bins=20, density=True)114 ax.axvline(np.mean(single_sample), linestyle="--", label="Sample Mean")115 ax.legend()116 ax.set_xlabel("Value")117 st.pyplot(fig)118 119# -------------------------------120# Sampling Distribution Plot121# -------------------------------122with col3:123 st.subheader("Sampling Distribution of the Mean")124 fig, ax = plt.subplots()125 ax.hist(sample_means, bins=40, density=True)126 ax.axvline(np.mean(sample_means), linestyle="--", label="Mean of Sample Means")127 ax.set_xlabel("Sample Mean")128 ax.legend()129 st.pyplot(fig)130 131# -------------------------------132# Statistics Display133# -------------------------------134st.markdown("---")135st.subheader("๐ Summary Statistics")136 137colA, colB, colC = st.columns(3)138 139with colA:140 st.metric("Population Mean", f"{np.mean(population):.3f}")141 st.metric("Population Std Dev", f"{np.std(population):.3f}")142 143with colB:144 st.metric("Sample Mean", f"{np.mean(single_sample):.3f}")145 st.metric("Sample Std Dev", f"{np.std(single_sample):.3f}")146 147with colC:148 st.metric("Mean of Sample Means", f"{np.mean(sample_means):.3f}")149 st.metric("Std Dev of Sample Means", f"{np.std(sample_means):.3f}")150 151st.caption("As sample size increases, the sampling distribution becomes more normal and its spread shrinks (Central Limit Theorem).")152 