Spaces:
Sleeping
Sleeping
File size: 14,692 Bytes
cf25a8a f10dca3 cf25a8a 4e9be24 cf25a8a fc7f7d3 cf25a8a 72979fc fc7f7d3 72979fc fc7f7d3 72979fc f10dca3 72979fc f10dca3 fc7f7d3 72979fc fc7f7d3 f10dca3 72979fc f10dca3 72979fc fc7f7d3 f10dca3 72979fc fc7f7d3 f10dca3 fc7f7d3 72979fc f10dca3 72979fc f10dca3 72979fc f10dca3 72979fc f10dca3 fc7f7d3 72979fc f10dca3 fc7f7d3 cf25a8a f10dca3 cf25a8a 72979fc cf25a8a 72979fc fc7f7d3 f10dca3 cf25a8a f10dca3 fc7f7d3 f10dca3 fc7f7d3 cf25a8a f10dca3 cf25a8a f10dca3 fc7f7d3 cf25a8a f10dca3 fc7f7d3 f10dca3 cf25a8a fc7f7d3 f10dca3 fc7f7d3 f10dca3 cf25a8a fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 72979fc fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 f10dca3 fc7f7d3 cf25a8a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | # mean_inference_app.py
# Streamlit ≥1.32 — Accessible, minimal color design
import streamlit as st
import pandas as pd
import numpy as np
from scipy.stats import norm, t
import io
# ---------- Page Config ----------
st.set_page_config(
page_title="Inference for Means",
page_icon="📈",
layout="centered",
initial_sidebar_state="collapsed"
)
# ---------- Accessible CSS - Compact for embedding ----------
st.markdown("""
<style>
/* Maximize vertical space usage */
.block-container {
padding-top: 0.5rem !important;
padding-bottom: 0.5rem !important;
max-width: 100% !important;
}
/* Tighter element spacing */
.element-container { margin-bottom: 0.3rem !important; }
.stRadio > div { margin-bottom: 0 !important; }
.stSelectSlider { padding-top: 0 !important; padding-bottom: 0 !important; }
h2 { margin-top: 0 !important; margin-bottom: 0.3rem !important; font-size: 1.4rem !important; }
h3 { margin-top: 0.3rem !important; margin-bottom: 0.2rem !important; font-size: 1.1rem !important; }
p { margin-bottom: 0.3rem !important; }
.stNumberInput { margin-bottom: 0 !important; }
/* Compact info boxes */
.stAlert { padding: 0.4rem 0.7rem !important; margin: 0.2rem 0 !important; }
/* Clean result box - compact */
.result-box {
background: #f8f9fa;
border: 2px solid #dee2e6;
padding: 0.6rem;
border-radius: 8px;
text-align: center;
margin: 0.2rem 0;
}
.result-box .label {
font-size: 0.75rem;
color: #6c757d;
margin-bottom: 0.1rem;
}
.result-box .value {
font-size: 1.2rem;
font-weight: 600;
color: #212529;
}
/* Decision boxes - compact */
.decision-box {
padding: 0.6rem;
border-radius: 8px;
text-align: center;
margin: 0.3rem 0;
font-weight: 600;
font-size: 1rem;
}
.decision-reject {
background: #fff;
border: 3px solid #212529;
}
.decision-accept {
background: #fff;
border: 3px dashed #6c757d;
}
/* Compact dividers */
hr { margin: 0.5rem 0 !important; border: none; border-top: 1px solid #dee2e6; }
/* Compact metrics */
[data-testid="stMetricValue"] { font-size: 1.1rem !important; }
[data-testid="stMetricLabel"] { font-size: 0.75rem !important; }
/* Compact expander */
.streamlit-expanderHeader { padding: 0.3rem 0 !important; font-size: 0.9rem !important; }
/* Hide Streamlit branding for cleaner embed */
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
header {visibility: hidden;}
</style>
""", unsafe_allow_html=True)
# ---------- Title ----------
st.markdown("## 📈 Inference for Means")
# ---------- INPUTS ----------
col1, col2 = st.columns(2)
with col1:
inf_type = st.radio("Inference type:", ["One-Sample Mean", "Two-Sample Mean (independent)"],
key="inf_type")
with col2:
analysis_type = st.radio("Analysis:", ["Confidence Interval", "Hypothesis Test"],
key="analysis_type")
# Distribution choice
dist_choice = st.radio(
"Distribution:",
["z (large sample)", "t (small sample, σ unknown)"],
key="dist_choice",
horizontal=True
)
dist_is_z = dist_choice.startswith("z")
st.markdown("---")
# ---------- Sample Data Inputs ----------
if inf_type == "One-Sample Mean":
col1, col2, col3 = st.columns(3)
with col1:
n = st.number_input("Sample size (n)", min_value=2, step=1, value=30, key="n")
with col2:
xbar = st.number_input("Sample mean (x̄)", format="%.4f", value=0.0, key="xbar")
with col3:
s = st.number_input("Sample std dev (s)", min_value=0.0001, format="%.4f", value=1.0, key="s")
se = s / np.sqrt(n)
st.info(f"Standard Error: **SE = s/√n = {se:.4f}**")
else: # Two-Sample
col1, col2 = st.columns(2)
with col1:
st.subheader("Group 1")
n1 = st.number_input("Sample size n₁", min_value=2, step=1, value=30, key="n1")
xbar1 = st.number_input("Sample mean x̄₁", format="%.4f", value=0.0, key="xbar1")
s1 = st.number_input("Sample std dev s₁", min_value=0.0001, format="%.4f", value=1.0, key="s1")
with col2:
st.subheader("Group 2")
n2 = st.number_input("Sample size n₂", min_value=2, step=1, value=30, key="n2")
xbar2 = st.number_input("Sample mean x̄₂", format="%.4f", value=0.0, key="xbar2")
s2 = st.number_input("Sample std dev s₂", min_value=0.0001, format="%.4f", value=1.0, key="s2")
st.markdown("---")
# ---------- CI / HT specific controls ----------
if analysis_type == "Confidence Interval":
conf_level = st.select_slider("Confidence level:",
options=[0.90, 0.95, 0.99], value=0.95,
format_func=lambda x: f"{x*100:.0f}%", key="conf_level")
else:
col1, col2 = st.columns(2)
with col1:
alpha = st.select_slider("Significance level (α):",
options=[0.01, 0.05, 0.10], value=0.05, key="alpha")
with col2:
if inf_type == "One-Sample Mean":
mu0 = st.number_input("Null mean (μ₀):", format="%.4f", value=0.0, key="mu0")
if inf_type == "One-Sample Mean":
alt = st.radio("Alternative hypothesis (H₁):",
["μ ≠ μ₀ (Two-sided)", "μ > μ₀ (Right-sided)", "μ < μ₀ (Left-sided)"],
horizontal=True, key="alt_one")
else:
alt = st.radio("Alternative hypothesis (H₁):",
["μ₁ ≠ μ₂ (Two-sided)", "μ₁ > μ₂ (Right-sided)", "μ₁ < μ₂ (Left-sided)"],
horizontal=True, key="alt_two")
# ---------- RUN ----------
run = st.button("▶ Run Analysis", type="primary", use_container_width=True)
# ---------- Helper function ----------
def result_box(label, value):
return f'<div class="result-box"><div class="label">{label}</div><div class="value">{value}</div></div>'
# ===== RESULTS =====
if run:
st.markdown("---")
# ===== ONE-SAMPLE =====
if inf_type == "One-Sample Mean" and n >= 2:
se = s / np.sqrt(n)
df = n - 1
if analysis_type == "Confidence Interval":
crit = norm.ppf(1 - (1 - conf_level)/2) if dist_is_z else t.ppf(1 - (1 - conf_level)/2, df)
margin = crit * se
lower, upper = xbar - margin, xbar + margin
cols = st.columns(2)
with cols[0]:
st.markdown(result_box("Sample Mean (x̄)", f"{xbar:.4f}"), unsafe_allow_html=True)
with cols[1]:
st.markdown(result_box(f"{conf_level*100:.0f}% Confidence Interval",
f"[{lower:.4f}, {upper:.4f}]"), unsafe_allow_html=True)
st.markdown("---")
st.subheader("Calculation Details")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Standard Error", f"{se:.4f}")
with col2:
label = "Critical Value (z)" if dist_is_z else f"Critical Value (t, df={df})"
st.metric(label, f"±{crit:.4f}")
with col3:
st.metric("Margin of Error", f"{margin:.4f}")
results = {"Analysis": ["CI"], "n": [n], "x̄": [xbar], "s": [s],
"SE": [se], "Lower": [lower], "Upper": [upper]}
else: # Hypothesis Test
stat = (xbar - mu0) / se
if "≠" in alt:
p_val = 2 * (1 - (norm.cdf(abs(stat)) if dist_is_z else t.cdf(abs(stat), df)))
crit = norm.ppf(1 - alpha/2) if dist_is_z else t.ppf(1 - alpha/2, df)
crit_display = f"±{crit:.4f}"
elif ">" in alt:
p_val = 1 - (norm.cdf(stat) if dist_is_z else t.cdf(stat, df))
crit = norm.ppf(1 - alpha) if dist_is_z else t.ppf(1 - alpha, df)
crit_display = f"{crit:.4f}"
else:
p_val = norm.cdf(stat) if dist_is_z else t.cdf(stat, df)
crit = -(norm.ppf(1 - alpha) if dist_is_z else t.ppf(1 - alpha, df))
crit_display = f"{crit:.4f}"
reject = p_val <= alpha
stat_name = "z" if dist_is_z else "t"
cols = st.columns(2)
with cols[0]:
st.markdown(result_box(f"{stat_name}-statistic", f"{stat:.4f}"), unsafe_allow_html=True)
with cols[1]:
st.markdown(result_box("p-value", f"{p_val:.4g}"), unsafe_allow_html=True)
if reject:
st.markdown(f'<div class="decision-box decision-reject">✗ REJECT H₀ at α = {alpha}</div>',
unsafe_allow_html=True)
else:
st.markdown(f'<div class="decision-box decision-accept">— Fail to reject H₀ at α = {alpha}</div>',
unsafe_allow_html=True)
st.markdown("---")
st.subheader("Calculation Details")
st.write(f"**Hypotheses:** H₀: μ = {mu0:.4f} vs H₁: {alt}")
st.write(f"**Sample mean:** x̄ = {xbar:.4f}")
st.write(f"**Standard Error:** {se:.4f}")
if not dist_is_z:
st.write(f"**Degrees of freedom:** df = {df}")
st.write(f"**Critical value(s):** {crit_display}")
results = {"Analysis": ["HT"], "n": [n], "x̄": [xbar], "μ₀": [mu0],
"stat": [stat], "p-value": [p_val], "Reject": [reject]}
# ===== TWO-SAMPLE =====
elif inf_type.startswith("Two-Sample") and n1 >= 2 and n2 >= 2:
se = np.sqrt(s1**2/n1 + s2**2/n2)
diff = xbar1 - xbar2
# Welch df
df_num = (s1**2/n1 + s2**2/n2)**2
df_den = (s1**2/n1)**2/(n1-1) + (s2**2/n2)**2/(n2-1)
df = df_num / df_den
if analysis_type == "Confidence Interval":
crit = norm.ppf(1 - (1 - conf_level)/2) if dist_is_z else t.ppf(1 - (1 - conf_level)/2, df)
margin = crit * se
lower, upper = diff - margin, diff + margin
cols = st.columns(2)
with cols[0]:
st.markdown(result_box("Difference (x̄₁ − x̄₂)", f"{diff:.4f}"), unsafe_allow_html=True)
with cols[1]:
st.markdown(result_box(f"{conf_level*100:.0f}% Confidence Interval",
f"[{lower:.4f}, {upper:.4f}]"), unsafe_allow_html=True)
st.markdown("---")
st.subheader("Calculation Details")
st.write(f"**x̄₁ = {xbar1:.4f}**, **x̄₂ = {xbar2:.4f}**")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("SE (Welch)", f"{se:.4f}")
with col2:
st.metric("df (Welch)", f"{df:.1f}")
with col3:
st.metric("Margin of Error", f"{margin:.4f}")
results = {"Analysis": ["CI-2"], "x̄₁": [xbar1], "x̄₂": [xbar2],
"Diff": [diff], "Lower": [lower], "Upper": [upper]}
else: # Hypothesis Test
stat = diff / se
if "≠" in alt:
p_val = 2 * (1 - (norm.cdf(abs(stat)) if dist_is_z else t.cdf(abs(stat), df)))
crit = norm.ppf(1 - alpha/2) if dist_is_z else t.ppf(1 - alpha/2, df)
crit_display = f"±{crit:.4f}"
elif ">" in alt:
p_val = 1 - (norm.cdf(stat) if dist_is_z else t.cdf(stat, df))
crit = norm.ppf(1 - alpha) if dist_is_z else t.ppf(1 - alpha, df)
crit_display = f"{crit:.4f}"
else:
p_val = norm.cdf(stat) if dist_is_z else t.cdf(stat, df)
crit = -(norm.ppf(1 - alpha) if dist_is_z else t.ppf(1 - alpha, df))
crit_display = f"{crit:.4f}"
reject = p_val <= alpha
stat_name = "z" if dist_is_z else "t"
cols = st.columns(2)
with cols[0]:
st.markdown(result_box(f"{stat_name}-statistic", f"{stat:.4f}"), unsafe_allow_html=True)
with cols[1]:
st.markdown(result_box("p-value", f"{p_val:.4g}"), unsafe_allow_html=True)
if reject:
st.markdown(f'<div class="decision-box decision-reject">✗ REJECT H₀ at α = {alpha}</div>',
unsafe_allow_html=True)
else:
st.markdown(f'<div class="decision-box decision-accept">— Fail to reject H₀ at α = {alpha}</div>',
unsafe_allow_html=True)
st.markdown("---")
st.subheader("Calculation Details")
st.write(f"**Hypotheses:** H₀: μ₁ = μ₂ vs H₁: {alt}")
st.write(f"**x̄₁ = {xbar1:.4f}**, **x̄₂ = {xbar2:.4f}**, **Difference = {diff:.4f}**")
st.write(f"**Standard Error (Welch):** {se:.4f}")
st.write(f"**Degrees of freedom (Welch):** {df:.1f}")
st.write(f"**Critical value(s):** {crit_display}")
results = {"Analysis": ["HT-2"], "x̄₁": [xbar1], "x̄₂": [xbar2],
"stat": [stat], "p-value": [p_val], "Reject": [reject]}
# Download
if 'results' in locals():
df_out = pd.DataFrame(results)
buff = io.BytesIO()
with pd.ExcelWriter(buff, engine="xlsxwriter") as writer:
df_out.to_excel(writer, index=False)
st.download_button("📥 Download Results", data=buff.getvalue(),
file_name="mean_inference.xlsx",
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
# ---------- Formulas (collapsed) ----------
with st.expander("📚 Formulas & Theory"):
st.markdown(r"""
**One-Sample Mean**
- SE: $s/\sqrt{n}$
- CI: $\bar x \pm t_{\alpha/2, df} \cdot SE$
- Test stat: $t = (\bar x - \mu_0)/SE$, $df = n-1$
**Two Independent Means (Welch)**
- SE: $\sqrt{s_1^2/n_1 + s_2^2/n_2}$
- Welch df: $\dfrac{(s_1^2/n_1 + s_2^2/n_2)^2}{(s_1^2/n_1)^2/(n_1-1) + (s_2^2/n_2)^2/(n_2-1)}$
- CI: $(\bar x_1 - \bar x_2) \pm t_{\alpha/2, df} \cdot SE$
- Test stat: $t = (\bar x_1 - \bar x_2)/SE$
**When to use z vs t:**
- **z**: Large sample (n ≥ 30) or σ known
- **t**: Small sample with σ unknown (assumes normal population)
""")
|