Spaces:
Sleeping
Sleeping
added different versions
Browse files- Final/Home.py +11 -0
- Final/__pycache__/utils.cpython-39.pyc +0 -0
- Final/config/__init__.py +0 -0
- Final/config/__pycache__/__init__.cpython-39.pyc +0 -0
- Final/config/__pycache__/config_continuous.cpython-39.pyc +0 -0
- Final/config/__pycache__/config_discrete.cpython-39.pyc +0 -0
- Final/config/config_continuous.py +29 -0
- Final/config/config_discrete.py +20 -0
- Final/pages/Continuous.py +20 -0
- Final/pages/Discrete.py +24 -0
- Final/utils.py +15 -0
- app-v1.py +20 -0
- app-v2.py +28 -0
- app-v3.py +40 -0
- app-v4.py +49 -0
- app-v5.py +37 -0
- app-v6.py +54 -0
- multipage-v1/Home.py +11 -0
- multipage-v1/pages/Continuous.py +49 -0
- multipage-v1/pages/Discrete.py +48 -0
Final/Home.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="PyTorch Distributions")
|
| 6 |
+
st.title('PyTorch Distributions')
|
| 7 |
+
|
| 8 |
+
st.markdown("This is a Streamlit app that demonstrates the probability density functions of various PyTorch distributions. "
|
| 9 |
+
"Use the sidebar to select between continuous and discrete distributions, and set the parameters of the selected distribution.")
|
| 10 |
+
|
| 11 |
+
st.markdown("---")
|
Final/__pycache__/utils.cpython-39.pyc
ADDED
|
Binary file (508 Bytes). View file
|
|
|
Final/config/__init__.py
ADDED
|
File without changes
|
Final/config/__pycache__/__init__.cpython-39.pyc
ADDED
|
Binary file (147 Bytes). View file
|
|
|
Final/config/__pycache__/config_continuous.cpython-39.pyc
ADDED
|
Binary file (1.52 kB). View file
|
|
|
Final/config/__pycache__/config_discrete.cpython-39.pyc
ADDED
|
Binary file (1.13 kB). View file
|
|
|
Final/config/config_continuous.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
CONTINUOUS_DISTRIBUTIONS = {
|
| 5 |
+
"Uniform": {
|
| 6 |
+
"params": lambda: {
|
| 7 |
+
"a": st.sidebar.slider("Lower Bound (a)", -10.0, 10.0, -5.0),
|
| 8 |
+
"b": st.sidebar.slider("Upper Bound (b)", -10.0, 10.0, 5.0),
|
| 9 |
+
},
|
| 10 |
+
"dist": lambda p: torch.distributions.Uniform(p["a"], p["b"]),
|
| 11 |
+
"support": lambda p: (p["a"], p["b"]),
|
| 12 |
+
},
|
| 13 |
+
"Normal": {
|
| 14 |
+
"params": lambda: {
|
| 15 |
+
"mu": st.sidebar.slider("Mean (渭)", -10.0, 10.0, 0.0),
|
| 16 |
+
"sigma": st.sidebar.slider("Standard Deviation (蟽)", 0.1, 10.0, 1.0),
|
| 17 |
+
},
|
| 18 |
+
"dist": lambda p: torch.distributions.Normal(p["mu"], p["sigma"]),
|
| 19 |
+
"support": None,
|
| 20 |
+
},
|
| 21 |
+
"LogNormal": {
|
| 22 |
+
"params": lambda: {
|
| 23 |
+
"mu": st.sidebar.slider("Log Mean (渭)", -5.0, 5.0, 0.0),
|
| 24 |
+
"sigma": st.sidebar.slider("Log Std Dev (蟽)", 0.1, 3.0, 1.0),
|
| 25 |
+
},
|
| 26 |
+
"dist": lambda p: torch.distributions.LogNormal(p["mu"], p["sigma"]),
|
| 27 |
+
"support": lambda p: (0, None),
|
| 28 |
+
}
|
| 29 |
+
}
|
Final/config/config_discrete.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
DISCRETE_DISTRIBUTIONS = {
|
| 5 |
+
"Binomial": {
|
| 6 |
+
"params": lambda: {
|
| 7 |
+
"n": st.sidebar.slider("Total Count (n)", 1, 20, 10),
|
| 8 |
+
"p": st.sidebar.slider("Success Probability (p)", 0.0, 1.0, 0.5),
|
| 9 |
+
},
|
| 10 |
+
"dist": lambda p: torch.distributions.Binomial(p["n"], p["p"]),
|
| 11 |
+
"support": lambda p: (0, p["n"]),
|
| 12 |
+
},
|
| 13 |
+
"Poisson": {
|
| 14 |
+
"params": lambda: {
|
| 15 |
+
"lambda": st.sidebar.slider("Rate (位)", 0.1, 10.0, 3.0),
|
| 16 |
+
},
|
| 17 |
+
"dist": lambda p: torch.distributions.Poisson(p["lambda"]),
|
| 18 |
+
"support": lambda p: (0, None),
|
| 19 |
+
}
|
| 20 |
+
}
|
Final/pages/Continuous.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
from config.config_continuous import CONTINUOUS_DISTRIBUTIONS
|
| 5 |
+
from utils import compute_pdf
|
| 6 |
+
|
| 7 |
+
st.title("Continuous Distributions")
|
| 8 |
+
|
| 9 |
+
selected_dist = st.sidebar.selectbox("Choose a Distribution", list(CONTINUOUS_DISTRIBUTIONS.keys()))
|
| 10 |
+
params = CONTINUOUS_DISTRIBUTIONS[selected_dist]["params"]()
|
| 11 |
+
dist = CONTINUOUS_DISTRIBUTIONS[selected_dist]["dist"](params)
|
| 12 |
+
support = CONTINUOUS_DISTRIBUTIONS[selected_dist].get("support")
|
| 13 |
+
support = support(params) if callable(support) else None
|
| 14 |
+
|
| 15 |
+
x_range = torch.linspace(-10, 10, 1000)
|
| 16 |
+
pdf = compute_pdf(dist, x_range, support)
|
| 17 |
+
|
| 18 |
+
fig = go.Figure()
|
| 19 |
+
fig.add_trace(go.Scatter(x=x_range.numpy(), y=pdf.numpy(), mode="lines", name=selected_dist))
|
| 20 |
+
st.plotly_chart(fig)
|
Final/pages/Discrete.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
from config.config_discrete import DISCRETE_DISTRIBUTIONS
|
| 5 |
+
|
| 6 |
+
st.title("Discrete Distributions")
|
| 7 |
+
|
| 8 |
+
selected_dist = st.sidebar.selectbox("Choose a Distribution", list(DISCRETE_DISTRIBUTIONS.keys()))
|
| 9 |
+
params = DISCRETE_DISTRIBUTIONS[selected_dist]["params"]()
|
| 10 |
+
dist = DISCRETE_DISTRIBUTIONS[selected_dist]["dist"](params)
|
| 11 |
+
support = DISCRETE_DISTRIBUTIONS[selected_dist].get("support")
|
| 12 |
+
support = support(params) if callable(support) else None
|
| 13 |
+
|
| 14 |
+
if support:
|
| 15 |
+
min_val, max_val = support
|
| 16 |
+
x_range = torch.arange(min_val, max_val + 1 if max_val is not None else 20)
|
| 17 |
+
else:
|
| 18 |
+
x_range = torch.arange(0, 20)
|
| 19 |
+
|
| 20 |
+
pmf = dist.log_prob(x_range).exp()
|
| 21 |
+
|
| 22 |
+
fig = go.Figure()
|
| 23 |
+
fig.add_trace(go.Bar(x=x_range.numpy(), y=pmf.numpy(), name=selected_dist))
|
| 24 |
+
st.plotly_chart(fig)
|
Final/utils.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
def compute_pdf(dist, x_range, support):
|
| 4 |
+
pdf = torch.zeros_like(x_range, dtype=torch.float)
|
| 5 |
+
|
| 6 |
+
if support:
|
| 7 |
+
min_val, max_val = support
|
| 8 |
+
mask = (x_range >= min_val) if min_val is not None else torch.ones_like(x_range, dtype=torch.bool)
|
| 9 |
+
if max_val is not None:
|
| 10 |
+
mask &= (x_range <= max_val)
|
| 11 |
+
else:
|
| 12 |
+
mask = torch.ones_like(x_range, dtype=torch.bool)
|
| 13 |
+
|
| 14 |
+
pdf[mask] = dist.log_prob(x_range[mask]).exp()
|
| 15 |
+
return pdf
|
app-v1.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
|
| 5 |
+
st.title('PyTorch Distributions')
|
| 6 |
+
|
| 7 |
+
mu = st.slider('Mean', -10.0, 10.0, 0.0)
|
| 8 |
+
sigma = st.slider('Standard Deviation', 0.1, 10.0, 1.0)
|
| 9 |
+
|
| 10 |
+
normal_dist = torch.distributions.Normal(mu, sigma)
|
| 11 |
+
|
| 12 |
+
x_range = torch.linspace(-10, 10, 1000)
|
| 13 |
+
pdf = normal_dist.log_prob(x_range).exp()
|
| 14 |
+
|
| 15 |
+
plt.plot(x_range.numpy(), pdf.numpy())
|
| 16 |
+
plt.xlabel('x')
|
| 17 |
+
plt.ylabel('Probability Density')
|
| 18 |
+
st.pyplot(plt)
|
| 19 |
+
|
| 20 |
+
|
app-v2.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
v2: In this version, beyond v1, we will add some LaTeX and Markdown to the app.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import torch
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
|
| 9 |
+
st.title('PyTorch Distributions')
|
| 10 |
+
|
| 11 |
+
mu = st.slider('Mean', -10.0, 10.0, 0.0)
|
| 12 |
+
sigma = st.slider('Standard Deviation', 0.1, 10.0, 1.0)
|
| 13 |
+
|
| 14 |
+
normal_dist = torch.distributions.Normal(mu, sigma)
|
| 15 |
+
|
| 16 |
+
x_range = torch.linspace(-10, 10, 1000)
|
| 17 |
+
pdf = normal_dist.log_prob(x_range).exp()
|
| 18 |
+
|
| 19 |
+
st.markdown('The **probability density function** of a normal distribution is given by:')
|
| 20 |
+
st.latex(r'f(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right)')
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
plt.plot(x_range.numpy(), pdf.numpy())
|
| 24 |
+
plt.xlabel('x')
|
| 25 |
+
plt.ylabel('Probability Density')
|
| 26 |
+
st.pyplot(plt)
|
| 27 |
+
|
| 28 |
+
|
app-v3.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
v3: In this version, beyond v2, we will add a table to mention some properties of the normal distribution.
|
| 3 |
+
We wcan do this via the st.table() function.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import streamlit as st
|
| 7 |
+
import torch
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
import pandas as pd
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
st.title('PyTorch Distributions')
|
| 13 |
+
|
| 14 |
+
mu = st.slider('Mean', -10.0, 10.0, 0.0)
|
| 15 |
+
sigma = st.slider('Standard Deviation', 0.1, 10.0, 1.0)
|
| 16 |
+
|
| 17 |
+
normal_dist = torch.distributions.Normal(mu, sigma)
|
| 18 |
+
|
| 19 |
+
x_range = torch.linspace(-10, 10, 1000)
|
| 20 |
+
pdf = normal_dist.log_prob(x_range).exp()
|
| 21 |
+
|
| 22 |
+
st.markdown('The **probability density function** of a normal distribution is given by:')
|
| 23 |
+
st.latex(r'f(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right)')
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
data = {
|
| 27 |
+
'Property': ['Mean', 'Standard Deviation', 'Variance', 'Entropy'],
|
| 28 |
+
'Value': [mu, sigma, sigma**2, normal_dist.entropy().item()]
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
df = pd.DataFrame(data)
|
| 32 |
+
st.table(df)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
plt.plot(x_range.numpy(), pdf.numpy())
|
| 36 |
+
plt.xlabel('x')
|
| 37 |
+
plt.ylabel('Probability Density')
|
| 38 |
+
st.pyplot(plt)
|
| 39 |
+
|
| 40 |
+
|
app-v4.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
v4: In this version, beyond v3, we will make the graph interactive using st.LineChart and plotly.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import torch
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import plotly.graph_objects as go
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
st.title('PyTorch Distributions')
|
| 14 |
+
|
| 15 |
+
mu = st.slider('Mean', -10.0, 10.0, 0.0)
|
| 16 |
+
sigma = st.slider('Standard Deviation', 0.1, 10.0, 1.0)
|
| 17 |
+
|
| 18 |
+
normal_dist = torch.distributions.Normal(mu, sigma)
|
| 19 |
+
|
| 20 |
+
x_range = torch.linspace(-10, 10, 1000)
|
| 21 |
+
pdf = normal_dist.log_prob(x_range).exp()
|
| 22 |
+
|
| 23 |
+
st.markdown('The **probability density function** of a normal distribution is given by:')
|
| 24 |
+
st.latex(r'f(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right)')
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
data = {
|
| 28 |
+
'Property': ['Mean', 'Standard Deviation', 'Variance', 'Entropy'],
|
| 29 |
+
'Value': [mu, sigma, sigma**2, normal_dist.entropy().item()]
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
df = pd.DataFrame(data)
|
| 33 |
+
st.table(df)
|
| 34 |
+
|
| 35 |
+
chart_data = pd.DataFrame({"x": x_range, "pdf": pdf})
|
| 36 |
+
st.markdown('#### Probability Density Function (made using `st.line_chart`)')
|
| 37 |
+
st.line_chart(chart_data, x='x', y='pdf', x_label='x', y_label='f(x)', use_container_width=True)
|
| 38 |
+
|
| 39 |
+
st.markdown('#### Probability Density Function (made using `plotly`)')
|
| 40 |
+
fig = go.Figure()
|
| 41 |
+
fig.add_trace(go.Scatter(x=x_range, y=pdf, mode='lines', name='',
|
| 42 |
+
hovertemplate='x: %{x:.2f}<br>f(x): %{y:.2f}'))
|
| 43 |
+
fig.update_layout(xaxis_title='x', yaxis_title='f(x)')
|
| 44 |
+
|
| 45 |
+
st.plotly_chart(fig)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
app-v5.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
v5: In this version, beyond v4, we will:
|
| 3 |
+
- only be using plotly
|
| 4 |
+
- add a sidebar for capturing user input and right side for displaying the plot
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import streamlit as st
|
| 8 |
+
import torch
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import plotly.graph_objects as go
|
| 11 |
+
|
| 12 |
+
st.title('PyTorch Distributions')
|
| 13 |
+
|
| 14 |
+
# Sidebar for user input
|
| 15 |
+
st.sidebar.header("Distribution Parameters")
|
| 16 |
+
mu = st.sidebar.slider('Mean (\u03BC)', -10.0, 10.0, 0.0)
|
| 17 |
+
sigma = st.sidebar.slider('Standard Deviation (\u03C3)', 0.1, 10.0, 1.0)
|
| 18 |
+
|
| 19 |
+
normal_dist = torch.distributions.Normal(mu, sigma)
|
| 20 |
+
|
| 21 |
+
x_range = torch.linspace(-10, 10, 1000)
|
| 22 |
+
pdf = normal_dist.log_prob(x_range).exp()
|
| 23 |
+
|
| 24 |
+
st.sidebar.markdown("### Selected Parameters")
|
| 25 |
+
st.sidebar.write(f"**Mean (\u03BC):** {mu:.2f}")
|
| 26 |
+
st.sidebar.write(f"**Standard Deviation (\u03C3):** {sigma:.2f}")
|
| 27 |
+
st.sidebar.write(f"**Variance:** {sigma**2:.4f}")
|
| 28 |
+
st.sidebar.write(f"**Entropy:** {normal_dist.entropy().item():.4f}")
|
| 29 |
+
|
| 30 |
+
# Main plot area
|
| 31 |
+
st.markdown('#### Probability Density Function')
|
| 32 |
+
fig = go.Figure()
|
| 33 |
+
fig.add_trace(go.Scatter(x=x_range, y=pdf, mode='lines',
|
| 34 |
+
name='', hovertemplate='x: %{x:.4f}<br>f(x): %{y:.4f}'))
|
| 35 |
+
fig.update_layout(xaxis_title='x', yaxis_title='f(x)', showlegend=False)
|
| 36 |
+
|
| 37 |
+
st.plotly_chart(fig)
|
app-v6.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
v6: In this version, beyond v5, we will:
|
| 3 |
+
- use a selectbox to choose the distribution type amongst: Normal and Uniform
|
| 4 |
+
- use a slider to set the parameters of the selected distribution
|
| 5 |
+
- display the selected parameters in the sidebar
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import streamlit as st
|
| 9 |
+
import torch
|
| 10 |
+
import pandas as pd
|
| 11 |
+
import plotly.graph_objects as go
|
| 12 |
+
|
| 13 |
+
st.title('PyTorch Distributions')
|
| 14 |
+
|
| 15 |
+
# Sidebar for user input
|
| 16 |
+
st.sidebar.header("Distribution Parameters")
|
| 17 |
+
|
| 18 |
+
dist_type = st.sidebar.selectbox("Select Distribution", ["Normal", "Uniform"])
|
| 19 |
+
x_range = torch.linspace(-10, 10, 1000)
|
| 20 |
+
|
| 21 |
+
if dist_type == "Normal":
|
| 22 |
+
mu = st.sidebar.slider('Mean (渭)', -10.0, 10.0, 0.0)
|
| 23 |
+
sigma = st.sidebar.slider('Standard Deviation (蟽)', 0.1, 10.0, 1.0)
|
| 24 |
+
dist = torch.distributions.Normal(mu, sigma)
|
| 25 |
+
pdf = dist.log_prob(x_range).exp()
|
| 26 |
+
st.sidebar.markdown("### Selected Parameters")
|
| 27 |
+
st.sidebar.write(f"**Mean (渭):** {mu:.2f}")
|
| 28 |
+
st.sidebar.write(f"**Standard Deviation (蟽):** {sigma:.2f}")
|
| 29 |
+
st.sidebar.write(f"**Entropy:** {dist.entropy().item():.2f}")
|
| 30 |
+
|
| 31 |
+
elif dist_type == "Uniform":
|
| 32 |
+
a = st.sidebar.slider('Lower Bound (a)', -10.0, 10.0, -5.0)
|
| 33 |
+
b = st.sidebar.slider('Upper Bound (b)', -10.0, 10.0, 5.0)
|
| 34 |
+
if a >= b:
|
| 35 |
+
st.sidebar.error("Lower bound must be less than upper bound")
|
| 36 |
+
else:
|
| 37 |
+
dist = torch.distributions.Uniform(a, b)
|
| 38 |
+
x_mask = (x_range >= a) & (x_range <= b)
|
| 39 |
+
pdf = torch.zeros_like(x_range)
|
| 40 |
+
pdf[x_mask] = torch.distributions.Uniform(a, b).log_prob(x_range[x_mask]).exp()
|
| 41 |
+
|
| 42 |
+
st.sidebar.markdown("### Selected Parameters")
|
| 43 |
+
st.sidebar.write(f"**Lower Bound (a):** {a:.2f}")
|
| 44 |
+
st.sidebar.write(f"**Upper Bound (b):** {b:.2f}")
|
| 45 |
+
st.sidebar.write(f"**Entropy:** {dist.entropy().item():.2f}")
|
| 46 |
+
|
| 47 |
+
# Main plot area
|
| 48 |
+
st.markdown(f'#### Probability Density Function for {dist_type} Distribution')
|
| 49 |
+
fig = go.Figure()
|
| 50 |
+
fig.add_trace(go.Scatter(x=x_range, y=pdf, mode='lines',
|
| 51 |
+
name='', hovertemplate='x: %{x:.2f}<br>f(x): %{y:.2f}'))
|
| 52 |
+
fig.update_layout(xaxis_title='x', yaxis_title='f(x)', showlegend=False)
|
| 53 |
+
|
| 54 |
+
st.plotly_chart(fig)
|
multipage-v1/Home.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
|
| 5 |
+
st.set_page_config(page_title="PyTorch Distributions")
|
| 6 |
+
st.title('PyTorch Distributions')
|
| 7 |
+
|
| 8 |
+
st.markdown("This is a Streamlit app that demonstrates the probability density functions of various PyTorch distributions. "
|
| 9 |
+
"Use the sidebar to select between continuous and discrete distributions, and set the parameters of the selected distribution.")
|
| 10 |
+
|
| 11 |
+
st.markdown("---")
|
multipage-v1/pages/Continuous.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
|
| 5 |
+
st.title("Continuous Distributions")
|
| 6 |
+
|
| 7 |
+
# Sidebar selection
|
| 8 |
+
dist_type = st.sidebar.radio("Select Distribution", ["Normal", "Uniform"])
|
| 9 |
+
|
| 10 |
+
# Common x range
|
| 11 |
+
x_range = torch.linspace(-10, 10, 1000)
|
| 12 |
+
pdf = torch.zeros_like(x_range)
|
| 13 |
+
|
| 14 |
+
if dist_type == "Normal":
|
| 15 |
+
st.sidebar.header("Normal Distribution Parameters")
|
| 16 |
+
mu = st.sidebar.slider("Mean (渭)", -10.0, 10.0, 0.0)
|
| 17 |
+
sigma = st.sidebar.slider("Standard Deviation (蟽)", 0.1, 10.0, 1.0)
|
| 18 |
+
|
| 19 |
+
dist = torch.distributions.Normal(mu, sigma)
|
| 20 |
+
pdf = dist.log_prob(x_range).exp()
|
| 21 |
+
|
| 22 |
+
st.sidebar.markdown("### Selected Parameters")
|
| 23 |
+
st.sidebar.write(f"**Mean (渭):** {mu:.2f}")
|
| 24 |
+
st.sidebar.write(f"**Standard Deviation (蟽):** {sigma:.2f}")
|
| 25 |
+
st.sidebar.write(f"**Entropy:** {dist.entropy().item():.2f}")
|
| 26 |
+
|
| 27 |
+
elif dist_type == "Uniform":
|
| 28 |
+
st.sidebar.header("Uniform Distribution Parameters")
|
| 29 |
+
a = st.sidebar.slider("Lower Bound (a)", -10.0, 10.0, -5.0)
|
| 30 |
+
b = st.sidebar.slider("Upper Bound (b)", -10.0, 10.0, 5.0)
|
| 31 |
+
|
| 32 |
+
if a >= b:
|
| 33 |
+
st.sidebar.error("Lower bound must be less than upper bound")
|
| 34 |
+
else:
|
| 35 |
+
dist = torch.distributions.Uniform(a, b)
|
| 36 |
+
mask = (x_range >= a) & (x_range <= b)
|
| 37 |
+
pdf[mask] = dist.log_prob(x_range[mask]).exp()
|
| 38 |
+
|
| 39 |
+
st.sidebar.markdown("### Selected Parameters")
|
| 40 |
+
st.sidebar.write(f"**Lower Bound (a):** {a:.2f}")
|
| 41 |
+
st.sidebar.write(f"**Upper Bound (b):** {b:.2f}")
|
| 42 |
+
st.sidebar.write(f"**Entropy:** {dist.entropy().item():.2f}")
|
| 43 |
+
|
| 44 |
+
# Plot
|
| 45 |
+
st.markdown(f'#### Probability Density Function for {dist_type} Distribution')
|
| 46 |
+
fig = go.Figure()
|
| 47 |
+
fig.add_trace(go.Scatter(x=x_range, y=pdf, mode='lines'))
|
| 48 |
+
fig.update_layout(xaxis_title='x', yaxis_title='f(x)', showlegend=False)
|
| 49 |
+
st.plotly_chart(fig)
|
multipage-v1/pages/Discrete.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
|
| 5 |
+
st.title("Discrete Distributions")
|
| 6 |
+
|
| 7 |
+
# Sidebar selection
|
| 8 |
+
dist_type = st.sidebar.radio("Select Distribution", ["Poisson", "Binomial"])
|
| 9 |
+
|
| 10 |
+
# Common x range for discrete distributions
|
| 11 |
+
x_range = torch.arange(0, 20, 1)
|
| 12 |
+
pmf = torch.zeros_like(x_range, dtype=torch.float)
|
| 13 |
+
|
| 14 |
+
if dist_type == "Poisson":
|
| 15 |
+
st.sidebar.header("Poisson Distribution Parameters")
|
| 16 |
+
lambda_ = st.sidebar.slider("Rate (位)", 0.1, 10.0, 3.0)
|
| 17 |
+
|
| 18 |
+
dist = torch.distributions.Poisson(lambda_)
|
| 19 |
+
pmf = torch.exp(dist.log_prob(x_range))
|
| 20 |
+
|
| 21 |
+
st.sidebar.markdown("### Selected Parameters")
|
| 22 |
+
st.sidebar.write(f"**Rate (位):** {lambda_:.2f}")
|
| 23 |
+
st.sidebar.write(f"**Mean:** {dist.mean.item():.2f}")
|
| 24 |
+
st.sidebar.write(f"**Variance:** {dist.variance.item():.2f}")
|
| 25 |
+
|
| 26 |
+
elif dist_type == "Binomial":
|
| 27 |
+
st.sidebar.header("Binomial Distribution Parameters")
|
| 28 |
+
n = st.sidebar.slider("Number of Trials (n)", 1, 20, 10)
|
| 29 |
+
p = st.sidebar.slider("Success Probability (p)", 0.01, 1.0, 0.5)
|
| 30 |
+
|
| 31 |
+
dist = torch.distributions.Binomial(n, torch.tensor(p))
|
| 32 |
+
|
| 33 |
+
# Adjust x_range to be within [0, n]
|
| 34 |
+
x_range = torch.arange(0, n + 1)
|
| 35 |
+
pmf = torch.exp(dist.log_prob(x_range))
|
| 36 |
+
|
| 37 |
+
st.sidebar.markdown("### Selected Parameters")
|
| 38 |
+
st.sidebar.write(f"**Trials (n):** {n}")
|
| 39 |
+
st.sidebar.write(f"**Probability (p):** {p:.2f}")
|
| 40 |
+
st.sidebar.write(f"**Mean:** {dist.mean.item():.2f}")
|
| 41 |
+
st.sidebar.write(f"**Variance:** {dist.variance.item():.2f}")
|
| 42 |
+
|
| 43 |
+
# Plot
|
| 44 |
+
st.markdown(f'#### Probability Mass Function for {dist_type} Distribution')
|
| 45 |
+
fig = go.Figure()
|
| 46 |
+
fig.add_trace(go.Bar(x=x_range.tolist(), y=pmf.tolist()))
|
| 47 |
+
fig.update_layout(xaxis_title='x', yaxis_title='P(x)', showlegend=False)
|
| 48 |
+
st.plotly_chart(fig)
|