date = 2023-04-28
tags = ["deno", "statistics"]
title = "Beta distribution script"

Beta distribution script

The Beta distribution is the distribution of the underlying probability `p` given the result of a binomial trial, coin tosses for an example .

Using the Beta distribution, the posterior distribution of `p` given your prior belief and observations can be calculated with the following formula:

"Beta"(alpha_"posterior", beta_"posterior") = "Beta"(alpha_"likelihood" + alpha_"prior", beta_"likelihood" + beta_"prior")

I uploaded a Deno script that plots the posterior distribution of `p` on the terminal.

% deno run https://jaeyoung.se/toys/beta_dist.js 
Beta Distribution Visualizer

Enter prior alpha and beta values.
alpha = 1 and beta = 1 means an uninformed prior.

alpha =  1
beta =  1
Prior probability distribution

       1.00 ┼─────────────────────────────────────────────────────────────────────────────────────────────────── 

Observed alpha = 0, beta = 0
Enter observed (+/-) alpha and (+/-) beta values.

alpha =  1
beta =  10
Posterior probability distribution

       0.04 ┼       ╭───╮                                                                                        
       0.03 ┤      ╭╯   ╰╮                                                                                       
       0.03 ┤     ╭╯     ╰─╮                                                                                     
       0.03 ┤    ╭╯        ╰╮                                                                                    
       0.03 ┤    │          ╰╮                                                                                   
       0.03 ┤   ╭╯           ╰╮                                                                                  
       0.02 ┤   │             ╰╮                                                                                 
       0.02 ┤  ╭╯              ╰╮                                                                                
       0.02 ┤  │                ╰╮                                                                               
       0.02 ┤  │                 ╰╮                                                                              
       0.02 ┤  │                  ╰─╮                                                                            
       0.02 ┤ ╭╯                    ╰╮                                                                           
       0.01 ┤ │                      ╰╮                                                                          
       0.01 ┤ │                       ╰─╮                                                                        
       0.01 ┤ │                         ╰╮                                                                       
       0.01 ┤╭╯                          ╰─╮                                                                     
       0.01 ┤│                             ╰─╮                                                                   
       0.01 ┤│                               ╰──╮                                                                
       0.00 ┤│                                  ╰───╮                                                            
       0.00 ┤│                                      ╰──────╮                                                     
       0.00 ┼╯                                             ╰──────────────────────────────────────────────────── 

Sources

https://learning.oreilly.com/library/view/bayesian-statistics-the/9781098122492

https://deno.land/x/chart