Download src/streamlit_app.py from MSivasandeep/Hypothesis_Testing_Using_Z-Test: direct link, hf CLI and curl.
- Browser
- Download file 2.8 kB
-
https://huggingface.co/spaces/MSivasandeep/Hypothesis_Testing_Using_Z-Test/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/MSivasandeep/Hypothesis_Testing_Using_Z-Test/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/MSivasandeep/Hypothesis_Testing_Using_Z-Test/resolve/main/src/streamlit_app.py
2.8 kB
| import streamlit as st | |
| import numpy as np | |
| from scipy import stats | |
| import matplotlib.pyplot as plt | |
| # Streamlit App Title | |
| st.title("π Z-Test for Website Conversion Rate Comparison") | |
| st.write("This app compares the conversion rate of an **old vs new website design** using a two-sample Z-test.") | |
| # --- Input Section --- | |
| st.header("π’ Input Conversion Data") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| n1 = st.number_input("Visitors (Old Design):", min_value=1, value=10000) | |
| x1 = st.number_input("Conversions (Old Design):", min_value=0, value=500) | |
| with col2: | |
| n2 = st.number_input("Visitors (New Design):", min_value=1, value=10000) | |
| x2 = st.number_input("Conversions (New Design):", min_value=0, value=650) | |
| # Button to Run Z-Test | |
| if st.button("Run Z-Test"): | |
| # Calculate conversion rates | |
| p1 = x1 / n1 | |
| p2 = x2 / n2 | |
| # Pooled proportion | |
| p = (x1 + x2) / (n1 + n2) | |
| # Z-statistic | |
| z = (p2 - p1) / np.sqrt(p * (1 - p) * (1/n1 + 1/n2)) | |
| # p-value (one-tailed) | |
| p_value_one_tailed = 1 - stats.norm.cdf(z) | |
| # Display Results | |
| st.header("π Z-Test Results") | |
| st.write(f"**Old Design Conversion Rate:** {p1 * 100:.2f}%") | |
| st.write(f"**New Design Conversion Rate:** {p2 * 100:.2f}%") | |
| st.write(f"**Pooled Proportion:** {p:.4f}") | |
| st.write(f"**Z-Score:** {z:.3f}") | |
| st.write(f"**One-tailed p-value:** {p_value_one_tailed:.4f}") | |
| alpha = 0.05 | |
| st.write(f"**Significance Level (Ξ±):** {alpha}") | |
| # Hypothesis decision | |
| if p_value_one_tailed < alpha: | |
| st.success("π **Result: Reject Null Hypothesis**") | |
| st.success("The new website design significantly improves conversions.") | |
| else: | |
| st.error("β **Result: Fail to Reject Null Hypothesis**") | |
| st.error("No statistically significant improvement observed.") | |
| # --- Charts Section --- | |
| st.header("π Visualizations") | |
| # Bar Chart | |
| fig1, ax1 = plt.subplots() | |
| versions = ["Old Design", "New Design"] | |
| conversion_rates = [p1 * 100, p2 * 100] | |
| ax1.bar(versions, conversion_rates) | |
| ax1.set_title("Comparison of Conversion Rates") | |
| ax1.set_ylabel("Conversion Rate (%)") | |
| for i, v in enumerate(conversion_rates): | |
| ax1.text(i, v + 0.2, f"{v:.2f}%", ha="center") | |
| st.pyplot(fig1) | |
| # Pie Charts | |
| fig2, (ax2, ax3) = plt.subplots(1, 2, figsize=(10, 4)) | |
| ax2.pie([x1, n1 - x1], labels=["Converted", "Not Converted"], | |
| autopct="%1.1f%%", startangle=90) | |
| ax2.set_title("Old Website Design") | |
| ax3.pie([x2, n2 - x2], labels=["Converted", "Not Converted"], | |
| autopct="%1.1f%%", startangle=90) | |
| ax3.set_title("New Website Design") | |
| st.pyplot(fig2) | |
| # Final note | |
| st.info("This Z-test helps determine if the new website design truly results in higher conversions.") | |