File size: 2,801 Bytes
978189f 20a6649 978189f 20a6649 | 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 | 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.")
|