Probability / pages /1_probability.py
Phani1008's picture
Update pages/1_probability.py
14c55f0 verified
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from streamlit_lottie import st_lottie
import requests
# Function to load Lottie animations
def load_lottie_url(url: str):
response = requests.get(url)
if response.status_code != 200:
return None
return response.json()
# Title of the app
st.title("Probability and Statistics Explorer")
# Load and display Lottie animation
lottie_url = "https://assets4.lottiefiles.com/packages/lf20_4ueuj8wi.json" # Replace with your own Lottie URL
lottie_animation = load_lottie_url(lottie_url)
st_lottie(lottie_animation, speed=1, width=None, height=None, key="animation")
# Sidebar for user input
st.sidebar.header("User Input")
# Mean and standard deviation input
mean = st.sidebar.number_input("Mean (μ)", value=0.0)
std_dev = st.sidebar.number_input("Standard Deviation (σ)", value=1.0)
# Generate values for the normal distribution
x = np.linspace(mean - 4 * std_dev, mean + 4 * std_dev, 1000)
y = norm.pdf(x, mean, std_dev)
# Plot the normal distribution
plt.figure(figsize=(10, 5))
plt.plot(x, y, label='Normal Distribution', color='blue')
plt.title('Normal Distribution Curve')
plt.xlabel('Value')
plt.ylabel('Probability Density')
plt.grid()
plt.legend()
st.pyplot(plt)
# Probability calculations
st.sidebar.header("Probability Calculations")
# Input for cumulative probability
probability_input = st.sidebar.number_input("Calculate P(X ≤ x)", value=0.0)
# Calculate cumulative probability
cumulative_prob = norm.cdf(probability_input, mean, std_dev)
st.sidebar.write(f"P(X ≤ {probability_input}) = {cumulative_prob:.4f}")
# Input for percentile
percentile_input = st.sidebar.number_input("Calculate Percentile (x)", value=0.0)
# Calculate percentile
percentile_value = norm.ppf(percentile_input, mean, std_dev)
st.sidebar.write(f"{percentile_input * 100}th Percentile = {percentile_value:.4f}")
# Show the app
st.write("This app allows you to explore the normal distribution and perform basic probability calculations.")