Update src/streamlit_app.py
Browse files- src/streamlit_app.py +83 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,85 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from scipy import stats
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
# Streamlit App Title
|
| 7 |
+
st.title("π Z-Test for Website Conversion Rate Comparison")
|
| 8 |
+
st.write("This app compares the conversion rate of an **old vs new website design** using a two-sample Z-test.")
|
| 9 |
+
|
| 10 |
+
# --- Input Section ---
|
| 11 |
+
st.header("π’ Input Conversion Data")
|
| 12 |
+
|
| 13 |
+
col1, col2 = st.columns(2)
|
| 14 |
+
|
| 15 |
+
with col1:
|
| 16 |
+
n1 = st.number_input("Visitors (Old Design):", min_value=1, value=10000)
|
| 17 |
+
x1 = st.number_input("Conversions (Old Design):", min_value=0, value=500)
|
| 18 |
+
|
| 19 |
+
with col2:
|
| 20 |
+
n2 = st.number_input("Visitors (New Design):", min_value=1, value=10000)
|
| 21 |
+
x2 = st.number_input("Conversions (New Design):", min_value=0, value=650)
|
| 22 |
+
|
| 23 |
+
# Button to Run Z-Test
|
| 24 |
+
if st.button("Run Z-Test"):
|
| 25 |
+
# Calculate conversion rates
|
| 26 |
+
p1 = x1 / n1
|
| 27 |
+
p2 = x2 / n2
|
| 28 |
+
|
| 29 |
+
# Pooled proportion
|
| 30 |
+
p = (x1 + x2) / (n1 + n2)
|
| 31 |
+
|
| 32 |
+
# Z-statistic
|
| 33 |
+
z = (p2 - p1) / np.sqrt(p * (1 - p) * (1/n1 + 1/n2))
|
| 34 |
+
|
| 35 |
+
# p-value (one-tailed)
|
| 36 |
+
p_value_one_tailed = 1 - stats.norm.cdf(z)
|
| 37 |
+
|
| 38 |
+
# Display Results
|
| 39 |
+
st.header("π Z-Test Results")
|
| 40 |
+
st.write(f"**Old Design Conversion Rate:** {p1 * 100:.2f}%")
|
| 41 |
+
st.write(f"**New Design Conversion Rate:** {p2 * 100:.2f}%")
|
| 42 |
+
st.write(f"**Pooled Proportion:** {p:.4f}")
|
| 43 |
+
st.write(f"**Z-Score:** {z:.3f}")
|
| 44 |
+
st.write(f"**One-tailed p-value:** {p_value_one_tailed:.4f}")
|
| 45 |
+
|
| 46 |
+
alpha = 0.05
|
| 47 |
+
st.write(f"**Significance Level (Ξ±):** {alpha}")
|
| 48 |
+
|
| 49 |
+
# Hypothesis decision
|
| 50 |
+
if p_value_one_tailed < alpha:
|
| 51 |
+
st.success("π **Result: Reject Null Hypothesis**")
|
| 52 |
+
st.success("The new website design significantly improves conversions.")
|
| 53 |
+
else:
|
| 54 |
+
st.error("β **Result: Fail to Reject Null Hypothesis**")
|
| 55 |
+
st.error("No statistically significant improvement observed.")
|
| 56 |
+
|
| 57 |
+
# --- Charts Section ---
|
| 58 |
+
st.header("π Visualizations")
|
| 59 |
+
|
| 60 |
+
# Bar Chart
|
| 61 |
+
fig1, ax1 = plt.subplots()
|
| 62 |
+
versions = ["Old Design", "New Design"]
|
| 63 |
+
conversion_rates = [p1 * 100, p2 * 100]
|
| 64 |
+
ax1.bar(versions, conversion_rates)
|
| 65 |
+
ax1.set_title("Comparison of Conversion Rates")
|
| 66 |
+
ax1.set_ylabel("Conversion Rate (%)")
|
| 67 |
+
for i, v in enumerate(conversion_rates):
|
| 68 |
+
ax1.text(i, v + 0.2, f"{v:.2f}%", ha="center")
|
| 69 |
+
st.pyplot(fig1)
|
| 70 |
+
|
| 71 |
+
# Pie Charts
|
| 72 |
+
fig2, (ax2, ax3) = plt.subplots(1, 2, figsize=(10, 4))
|
| 73 |
+
|
| 74 |
+
ax2.pie([x1, n1 - x1], labels=["Converted", "Not Converted"],
|
| 75 |
+
autopct="%1.1f%%", startangle=90)
|
| 76 |
+
ax2.set_title("Old Website Design")
|
| 77 |
+
|
| 78 |
+
ax3.pie([x2, n2 - x2], labels=["Converted", "Not Converted"],
|
| 79 |
+
autopct="%1.1f%%", startangle=90)
|
| 80 |
+
ax3.set_title("New Website Design")
|
| 81 |
+
|
| 82 |
+
st.pyplot(fig2)
|
| 83 |
|
| 84 |
+
# Final note
|
| 85 |
+
st.info("This Z-test helps determine if the new website design truly results in higher conversions.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|