|
| 1 | +# Copyright 2024 The PyMC Labs Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# Import custom functions |
| 15 | +import prior_functions as pf |
| 16 | + |
| 17 | +import streamlit as st |
| 18 | + |
| 19 | +# Constants |
| 20 | +SEED = 42 |
| 21 | +N_DRAWS = 50_000 |
| 22 | +# Specify the possible distributions and their paramaters you want to visualise |
| 23 | +DISTRIBUTIONS_DICT = { |
| 24 | + "Beta": ["alpha", "beta"], |
| 25 | + "Bernoulli": ["p"], |
| 26 | + "Exponential": ["lam"], |
| 27 | + "Gamma": ["alpha", "beta"], |
| 28 | + "HalfNormal": ["sigma"], |
| 29 | + "LogNormal": ["mu", "sigma"], |
| 30 | + "Normal": ["mu", "sigma"], |
| 31 | + "Poisson": ["mu"], |
| 32 | + "StudentT": ["nu", "mu", "sigma"], |
| 33 | + "TruncatedNormal": ["mu", "sigma", "lower", "upper"], |
| 34 | + "Uniform": ["lower", "upper"], |
| 35 | + "Weibull": ["alpha", "beta"], |
| 36 | +} |
| 37 | +PLOT_HEIGHT = 500 |
| 38 | +PLOT_WIDTH = 1000 |
| 39 | + |
| 40 | +# -------------------------- TOP OF PAGE INFORMATION ------------------------- |
| 41 | + |
| 42 | +# Set browser / tab config |
| 43 | +st.set_page_config( |
| 44 | + page_title="MMM App - Prior Distributions Transformations", |
| 45 | + page_icon="💎", |
| 46 | +) |
| 47 | + |
| 48 | +# Give some context for what the page displays |
| 49 | +st.title("Bayesian Prior Distribution Demonstrator") |
| 50 | + |
| 51 | +# -------------------------- VISUALISE PRIOR ------------------------- |
| 52 | + |
| 53 | +# Select the distribution to visualise |
| 54 | +dist_name = st.selectbox( |
| 55 | + "Please select the distribution you would like to visualise:", |
| 56 | + options=DISTRIBUTIONS_DICT.keys(), |
| 57 | +) |
| 58 | +st.header(f":blue[{dist_name} Distribution]") # header |
| 59 | + |
| 60 | +# Variables need to be instantiated to avoid error where upper < lower |
| 61 | +lower = None |
| 62 | +upper = None |
| 63 | + |
| 64 | +# Initialize parameters with None |
| 65 | +params = {param: None for param in DISTRIBUTIONS_DICT[dist_name]} |
| 66 | + |
| 67 | +# User inputs for distribution parameters |
| 68 | +for param in params.keys(): |
| 69 | + if param == "lower": |
| 70 | + params[param] = st.number_input( |
| 71 | + f"Please enter the value for {param.title()}:", key=param, value=0.0 |
| 72 | + ) |
| 73 | + elif param == "upper": |
| 74 | + params[param] = st.number_input( |
| 75 | + f"Please enter the value for {param.title()}:", key=param, value=2.0 |
| 76 | + ) |
| 77 | + elif param == "alpha": |
| 78 | + params[param] = st.number_input( |
| 79 | + f"Please enter the value for {param.title()}:", |
| 80 | + key=param, |
| 81 | + value=1.0, |
| 82 | + min_value=0.01, |
| 83 | + ) |
| 84 | + elif param == "beta": |
| 85 | + params[param] = st.number_input( |
| 86 | + f"Please enter the value for {param.title()}:", |
| 87 | + key=param, |
| 88 | + value=1.0, |
| 89 | + min_value=0.01, |
| 90 | + ) |
| 91 | + elif param == "sigma": |
| 92 | + params[param] = st.number_input( |
| 93 | + f"Please enter the value for {param.title()}:", |
| 94 | + key=param, |
| 95 | + value=1.0, |
| 96 | + min_value=0.01, |
| 97 | + ) |
| 98 | + # Poisson mu must be > 0 |
| 99 | + elif param == "mu" and dist_name == "Poisson": |
| 100 | + params[param] = st.number_input( |
| 101 | + f"Please enter the value for {param.title()}:", |
| 102 | + key=param, |
| 103 | + value=1.0, |
| 104 | + min_value=0.01, |
| 105 | + ) |
| 106 | + elif param == "mu": |
| 107 | + params[param] = st.number_input( |
| 108 | + f"Please enter the value for {param.title()}:", key=param, value=0.0 |
| 109 | + ) |
| 110 | + elif param == "p": |
| 111 | + params[param] = st.number_input( |
| 112 | + f"Please enter the value for {param.title()}:", |
| 113 | + key=param, |
| 114 | + value=0.5, |
| 115 | + min_value=0.0, |
| 116 | + max_value=1.0, |
| 117 | + ) |
| 118 | + elif param == "lam": |
| 119 | + params[param] = st.number_input( |
| 120 | + f"Please enter the value for {param.title()}:", |
| 121 | + key=param, |
| 122 | + value=1.0, |
| 123 | + min_value=0.01, |
| 124 | + ) |
| 125 | + elif param == "nu": |
| 126 | + params[param] = st.number_input( |
| 127 | + f"Please enter the value for {param.title()}:", |
| 128 | + key=param, |
| 129 | + value=10.0, |
| 130 | + min_value=0.01, |
| 131 | + ) |
| 132 | + |
| 133 | + |
| 134 | +# Check to ensure lower < upper |
| 135 | +if lower and lower >= upper: |
| 136 | + st.error("Error: Lower bound must be less than upper bound.") |
| 137 | + |
| 138 | +## Create the selected distribution and sample from it |
| 139 | +dist = pf.get_distribution(dist_name, **params) |
| 140 | +draws = dist.rvs(N_DRAWS, random_state=SEED) |
| 141 | + |
| 142 | + |
| 143 | +# Plot distribution |
| 144 | +fig_root = pf.plot_prior_distribution(draws, title=f"{dist_name} Distribution Samples") |
| 145 | +fig_root.update_layout(height=PLOT_HEIGHT, width=PLOT_WIDTH) |
| 146 | +st.plotly_chart(fig_root, use_container_width=True) |
0 commit comments