Madhav commited on
Commit
8737d2a
·
1 Parent(s): 3c22ce5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -20
app.py CHANGED
@@ -7,7 +7,6 @@ from numpyro.infer import MCMC, NUTS
7
  from sklearn.datasets import make_regression
8
  from jax import random
9
  import streamlit as st
10
- import sympy as sp
11
 
12
 
13
  # Define the model
@@ -32,6 +31,21 @@ def run_linear_regression(X, y, alpha_prior, beta_prior, sigma_prior):
32
  samples = mcmc.get_samples()
33
 
34
  # Plot the results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  fig, ax = plt.subplots(figsize=(8, 6))
36
  ax.scatter(X, y, color='blue', alpha=0.5, label='data')
37
  light_color = (1.0, 0.5, 0.5, 0.7)
@@ -49,48 +63,47 @@ def run_linear_regression(X, y, alpha_prior, beta_prior, sigma_prior):
49
  # User Input
50
  st.write("# Bayesian Linear Regression")
51
 
52
- st.latex(r'''\begin{aligned}
53
- \textbf{Prior:} \hspace{0.5 cm} p(\alpha) &= N(\mu_0, \Sigma_0),\hspace{0.2 cm} p(\beta) = N(\mu_1, \Sigma_1),\hspace{0.2 cm} p(\sigma) = N(\mu_2, \Sigma_2)\\
54
- \textbf{Likelihood:} \hspace{0.5 cm} p(y | X, \beta,\alpha, \sigma) &= N(X\beta + \alpha, \sigma)\\
55
- \textbf{Posterior:} \hspace{0.5 cm} p(\alpha | X, y) &\propto p(y | X, \beta, \sigma) p(\alpha),\hspace{0.2 cm} p(\beta | X, y) \propto p(y | X, \alpha, \sigma) p(\beta),\hspace{0.2 cm} p(\sigma | X, y) \propto p(y | X, \beta, \alpha) p(\sigma)
56
- \end{aligned}''')
57
 
58
 
59
 
60
  alpha_prior_option = st.selectbox("Choose an option for alpha prior:", ["Normal", "Laplace", "Cauchy"])
61
  if alpha_prior_option == "Normal":
62
- alpha_loc = st.slider("Select a mean value for alpha", -10.0, 10.0, 0.0, 0.1)
63
- alpha_scale = st.slider("Select a standard deviation value for alpha", 0.01, 10.0, 1.0, 0.1)
64
  alpha_prior = dist.Normal(alpha_loc, alpha_scale)
65
  elif alpha_prior_option == "Laplace":
66
- alpha_loc = st.slider("Select a mean value for alpha", -10.0, 10.0, 0.0, 0.1)
67
- alpha_scale = st.slider("Select a scale value for alpha", 0.01, 10.0, 1.0, 0.1)
68
  alpha_prior = dist.Laplace(alpha_loc, alpha_scale)
69
  elif alpha_prior_option == "Cauchy":
70
- alpha_loc = st.slider("Select a location value for alpha", -10.0, 10.0, 0.0, 0.1)
71
- alpha_scale = st.slider("Select a scale value for alpha", 0.01, 10.0, 1.0, 0.1)
72
  alpha_prior = dist.Cauchy(alpha_loc, alpha_scale)
73
 
74
  beta_prior_option = st.selectbox("Choose an option for beta prior:", ["Normal", "Laplace", "Cauchy"])
75
  if beta_prior_option == "Normal":
76
- beta_loc = st.slider("Select a mean value for beta", -10.0, 10.0, 0.0, 0.1)
77
- beta_scale = st.slider("Select a standard deviation value for beta", 0.01, 10.0, 1.0, 0.1)
78
  beta_prior = dist.Normal(beta_loc, beta_scale)
79
  elif beta_prior_option == "Laplace":
80
- beta_loc = st.slider("Select a mean value for beta", -10.0, 10.0, 0.0, 0.1)
81
- beta_scale = st.slider("Select a scale value for beta", 0.01, 10.0, 1.0, 0.1)
82
  beta_prior = dist.Laplace(beta_loc, beta_scale)
83
  elif beta_prior_option == "Cauchy":
84
- beta_loc = st.slider("Select a location value for beta", -10.0, 10.0, 0.0, 0.1)
85
- beta_scale = st.slider("Select a scale value for beta", 0.01, 10.0, 1.0, 0.1)
86
  beta_prior = dist.Cauchy(beta_loc, beta_scale)
87
 
88
  sigma_prior_option = st.selectbox("Choose an option for sigma prior:", ["HalfNormal", "HalfCauchy"])
89
  if sigma_prior_option == "HalfNormal":
90
- sigma_scale = st.slider("Select a scale value for sigma", 0.01, 10.0, 1.0, 0.1)
91
  sigma_prior = dist.HalfNormal(sigma_scale)
92
  elif sigma_prior_option == "HalfCauchy":
93
- sigma_scale = st.slider("Select a scale value for sigma", 0.01, 10.0, 1.0, 0.1)
94
  sigma_prior = dist.HalfCauchy(sigma_scale)
95
 
96
  rng_key = random.PRNGKey(0)
 
7
  from sklearn.datasets import make_regression
8
  from jax import random
9
  import streamlit as st
 
10
 
11
 
12
  # Define the model
 
31
  samples = mcmc.get_samples()
32
 
33
  # Plot the results
34
+ fig, ax = plt.subplots(1, 3, figsize=(12, 4))
35
+ ax[0].hist(samples['alpha'], bins=20, density=True)
36
+ ax[0].set_title('alpha')
37
+ ax[1].hist(samples['beta'], bins=20, density=True)
38
+ ax[1].set_title('beta')
39
+ ax[2].hist(samples['sigma'], bins=20, density=True)
40
+ ax[2].set_title('sigma')
41
+ st.pyplot(fig)
42
+ st.write("The mean of alpha is", np.mean(samples['alpha']))
43
+ st.write("The mean of beta is", np.mean(samples['beta']))
44
+ st.write("The mean of sigma is", np.mean(samples['sigma']))
45
+
46
+ st.write("The plot of predicted line is shown below using mean values of alpha and beta (Xβ+α):")
47
+
48
+
49
  fig, ax = plt.subplots(figsize=(8, 6))
50
  ax.scatter(X, y, color='blue', alpha=0.5, label='data')
51
  light_color = (1.0, 0.5, 0.5, 0.7)
 
63
  # User Input
64
  st.write("# Bayesian Linear Regression")
65
 
66
+
67
+ """ Prior: p(α) = N(μ0,Σ0), p(β) = N(μ1,Σ1), p(σ) = N(Σ2)"""
68
+ """ Likelihood: p(y|X,β,α,σ) = N(Xβ+α,σ)"""
69
+ """ Posterior: p(α|X,y) p(y|X,β,σ)p(α), p(β|X,y) p(y|X,α,σ)p(β), p(σ|X,y) p(y|X,β,α)p(σ)"""
 
70
 
71
 
72
 
73
  alpha_prior_option = st.selectbox("Choose an option for alpha prior:", ["Normal", "Laplace", "Cauchy"])
74
  if alpha_prior_option == "Normal":
75
+ alpha_loc = st.slider("Select a mean value for prior of alpha(α) (μ0)", -10.0, 10.0, 0.0, 0.1)
76
+ alpha_scale = st.slider("Select a standard deviation value for prior of alpha(α) (Σ0)", 0.01, 10.0, 1.0, 0.1)
77
  alpha_prior = dist.Normal(alpha_loc, alpha_scale)
78
  elif alpha_prior_option == "Laplace":
79
+ alpha_loc = st.slider("Select a mean value for prior of alpha(α) (μ0)", -10.0, 10.0, 0.0, 0.1)
80
+ alpha_scale = st.slider("Select a standard deviation value for prior of alpha(α) (Σ0)", 0.01, 10.0, 1.0, 0.1)
81
  alpha_prior = dist.Laplace(alpha_loc, alpha_scale)
82
  elif alpha_prior_option == "Cauchy":
83
+ alpha_loc = st.slider("Select a mean value for prior of alpha(α) (μ0)", -10.0, 10.0, 0.0, 0.1)
84
+ alpha_scale = st.slider("Select a standard deviation value for prior of alpha(α) (Σ0)", 0.01, 10.0, 1.0, 0.1)
85
  alpha_prior = dist.Cauchy(alpha_loc, alpha_scale)
86
 
87
  beta_prior_option = st.selectbox("Choose an option for beta prior:", ["Normal", "Laplace", "Cauchy"])
88
  if beta_prior_option == "Normal":
89
+ beta_loc = st.slider("Select a mean value for prior of beta(β) (μ1)", -10.0, 10.0, 0.0, 0.1)
90
+ beta_scale = st.slider("Select a standard deviation value for prior of beta(β) (Σ1)", 0.01, 10.0, 1.0, 0.1)
91
  beta_prior = dist.Normal(beta_loc, beta_scale)
92
  elif beta_prior_option == "Laplace":
93
+ beta_loc = st.slider("Select a mean value for prior of beta(β) (μ1)", -10.0, 10.0, 0.0, 0.1)
94
+ beta_scale = st.slider("Select a standard deviation value for prior of beta(β) (Σ1)", 0.01, 10.0, 1.0, 0.1)
95
  beta_prior = dist.Laplace(beta_loc, beta_scale)
96
  elif beta_prior_option == "Cauchy":
97
+ beta_loc = st.slider("Select a mean value for prior of beta(β) (μ1)", -10.0, 10.0, 0.0, 0.1)
98
+ beta_scale = st.slider("Select a standard deviation value for prior of beta(β) (Σ1)", 0.01, 10.0, 1.0, 0.1)
99
  beta_prior = dist.Cauchy(beta_loc, beta_scale)
100
 
101
  sigma_prior_option = st.selectbox("Choose an option for sigma prior:", ["HalfNormal", "HalfCauchy"])
102
  if sigma_prior_option == "HalfNormal":
103
+ sigma_scale = st.slider("Select a scale value for prior of sigma(σ) (Σ2)", 0.01, 10.0, 1.0, 0.1)
104
  sigma_prior = dist.HalfNormal(sigma_scale)
105
  elif sigma_prior_option == "HalfCauchy":
106
+ sigma_scale = st.slider("Select a scale value for prior of sigma(σ) (Σ2)", 0.01, 10.0, 1.0, 0.1)
107
  sigma_prior = dist.HalfCauchy(sigma_scale)
108
 
109
  rng_key = random.PRNGKey(0)