Introduction to Urn Problems

In this tutorial, we will be using Urn Problems to illustrate different probability distributions. The general idea of an urn problem is that there is an urn filled with balls of different colours (here only two colours), from which a certain number of balls is drawn. One colour, here blue, is seen as a success. The drawing process can be restricted by certain rules, such as whether balls are sampled with or without replacement, and when to stop drawing balls. Depending on the rules applied to the general problem different probability distributions will result. In this tutorial, we will briefly introduce the binomial, hypergeometric, negative binomial and negative hypergeometric distributions.

Example: Set the number of balls in the urn and then draw with replacement. You can draw either by clicking on the ball or by randomly drawing balls. Play around till you feel comfortable and then move on to the other tabs.










The Binomial Distribution

Drawing balls from the urn with replacement results in the so called binomial distribution. The number of drawn blue balls is counted. The probability of drawing k blue balls in n draws is given by

$$ P(\color{deepskyblue}{k}\,blue\,balls) \,=\,\binom{n}{ \color{deepskyblue}{k}} \cdot\left(\frac{\color{blue}{K}}{N} \right)^{\color{deepskyblue}{k}}\cdot \left(1-\frac{\color{blue}{K}}{N} \right)^{n-\color{deepskyblue}{k}}$$

where N represents the total number of balls (population size), K the total number of blue balls in the urn (successes in the population), n the number of balls drawn from the urn (sample size), and k the number of blue balls drawn from the urn (sample successes).














The Hypergeometric Distribution

Drawing balls from the urn without replacement results in the so called hypergeometric distribution. The number of drawn blue balls is counted. The probability of drawing k blue balls in n draws is given by

$$ P(\color{deepskyblue}{k}\,blue\,balls)\,= \,\frac{\binom{K}{ \color{deepskyblue}{k}}\cdot \binom{N-K}{n- \color{deepskyblue}{k}}}{\binom{N}{n}}$$

where N represents the total number of balls (population size), K the total number of blue balls in the urn (successes in the population), n the number of balls drawn from the urn (sample size), and k the number of blue balls drawn from the urn (sample successes).





The urn will automatically reset by changing the numbers of blue or red balls in the urn.









The Negative Binomial Distribution

Drawing balls from the urn with replacement until k blue balls (success balls) are drawn results in the so called negative binomial distribution. For simplicity we are using k = 1. This special case is also called geometric distribution. The probability of drawing n time until the first blue ball is drawn is given by

$$P(n\,draws\,until\,the\,first \,blue\,ball\,is\,drawn)\,= \,\binom{n-1}{\color{deepskyblue}{k}-1}\cdot \left(\frac{\color{blue}{K}}{N}\right)^ \color{deepskyblue}{k}\cdot \left( 1-\frac{\color{blue}{K}}{N} \right) ^{n-\color{deepskyblue}{k}}$$

where N represents the total number of balls (population size), K the total number of blue balls in the urn (successes in the population), n the number of balls drawn from the urn (sample size), and k the number of blue balls drawn from the urn (sample successes).












The Negative Hypergeometric Distribution

Drawing balls from the urn without replacement until k blue balls (success balls) are drawn results in the so called negative hypergeometric distribution. For simplicity we are using k = 1. The probability of drawing n time until the first blue ball is drawn is given by

$$P(n\,draws\,until\,the\,first \,blue\,ball\,is\,drawn)\,=\, \frac{\binom{N-n}{\color{blue}{K-k}}}{ \binom{N}{K}}$$

where N represents the total number of balls (population size), K the total number of blue balls in the urn (successes in the population), n the number of balls drawn from the urn (sample size), and k the number of blue balls drawn from the urn (sample successes).




The urn will automatically reset by changing the numbers of blue or red balls in the urn.








Do you think you got it? Try yourself!

Here you can check your knowledge about the distributions in this tutorial.










Please select the letter representing the following descriptions in the formula.











Please select the distributions belonging to the formula.























There are eight blue and four red balls in the urn. You draw ten balls.

Please select the distributions belonging to the plots.






There are seven blue and five red balls in the urn. You draw until the first blue ball is drawn.

Please select the distributions belonging to the plots.






There are three blue and thirteen red balls in the urn. You draw sixteen balls.

Please select the distributions belonging to the plots.






There are five blue and five red balls in the urn. You draw five balls.

Please select the distributions belonging to the plots.






There are four blue and twelve red balls in the urn. You draw until the first blue ball is drawn.

Please select the distributions belonging to the plots.









Richard has an urn and offers a game to Tom. If Tom draws five or more red balls, he wins. They won't replace balls drawn from the urn. He draws nine times. Tom only wants to play the game, if his chance to win is greater than 0.5.

Use the app to get the result.

Manuel has an urn with seven balls. Four are blue and three are red. Manuel draws without replacement.

Use the app to get the result.



Sarah has an urn with elven blue and nine red balls. She draws without replacement.

Use the app to get the result.

Lara has an urn with seven blue and eight red balls. Lara wants to know how often she has to draw to get at least one blue ball with a probability of 0.7

Use the app to get the result.

Samuel has an urn with six blue and five red balls. He draws five times.

Use the app to get the result.

Mia has three blue balls and two red balls. She puts four of them in a bag.

Use the app to get the result.
show with app
library(shiny)
library(shinydashboard)
library(ggplot2)

#=========================ui========================================
ui <- dashboardPage(skin = "blue",
  dashboardHeader(
    title = tagList(
      tags$span(
        class = "logo-mini", 
        HTML("<span style='font-size:8px'>Urn Problems</span>)")
      ),
      tags$span(
        class = "logo-lg", "Urn Problems"
      )
    )
  ),
                    
# ====================== Sidebar ========================
  dashboardSidebar( 
# create a sidebar
    sidebarMenu( 
# create sidebar menu items
      menuItem(
        "Introduction",
        tabName = "Introduction",
        icon = icon("info-circle") 
      ),
      menuItem(   
        HTML("Binomial <br> &nbsp &nbsp &nbsp &nbsp Distribution"),
        tabName = "Binomial",
        icon = icon("menu-right", lib = "glyphicon"),
        menuSubItem( HTML("The Binomial <br> &nbsp &nbsp &nbsp &nbsp 
                          Distribution"), tabName = "Binomial_dist"),
        menuSubItem("The urn", tabName = "Binomial_urn")
      ),
      menuItem(
        HTML("Hypergeometric <br> &nbsp &nbsp &nbsp &nbsp Distribution"),
          tabName = "Hypergeometric",
          icon = icon("menu-right", lib = "glyphicon"),
          menuSubItem(HTML("The Hypergeometric <br> &nbsp &nbsp &nbsp
            &nbsp Distribution"), tabName = "Hyper_dist"),
          menuSubItem("The urn", tabName = "Hyper_urn")
        ),
        menuItem(
          HTML("Negative Binomial <br> &nbsp &nbsp &nbsp &nbsp Distribution"),
          tabName = "Geometric",
          icon = icon("menu-right", lib = "glyphicon"),
          menuSubItem( HTML("The Negative Binomial<br> &nbsp &nbsp &nbsp &nbsp 
                            Distribution"), tabName = "Geo_dist"),
          menuSubItem("The urn", tabName = "Geo_urn")
        ),
        menuItem(
          HTML("Negative Hypergeometric <br> &nbsp &nbsp &nbsp 
               &nbsp Distribution"),
          tabName = "Negative_Hypergeometric",
          icon = icon("menu-right", lib = "glyphicon"),
          menuSubItem(HTML("The Negative Hypergeometric <br>
                           &nbsp &nbsp &nbsp &nbsp Distribution"),
                      tabName = "NegHyp_dist"),
          menuSubItem("The urn", tabName = "NegHyp_urn")
        ),
        menuItem("Your turn!", tabName = "Exercise", 
                 icon = icon("pencil-square-o"))
      )
    ),
                    
# =========================== tagStyle ==================================
    dashboardBody(
      includeScript("../../../Matomo-tquant.js"),
      tags$script(HTML("$('body').addClass('sidebar-mini');")),
      tags$head(tags$style(
          HTML(".main-header .logo {
               font-weight: bold;
               font-size: 22px;}")
        )
      ),
      tags$head(tags$style(
          HTML(".content-wrapper {
               background-color: #FFFFFF;}")
        )
      ),
# change colors of the slider
      tags$style( 
        HTML(".js-irs-0 .irs-single, 
             .js-irs-0 .irs-bar-edge
             {border: blue; 
             background: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-0 .irs-bar 
             {background: blue; 
             border-top: blue;
             border-bottom: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-1 .irs-single, 
             .js-irs-1 .irs-bar-edge
             {border: red; 
             background: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-1 .irs-bar 
             {background: red; 
             border-top: red;
             border-bottom: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-2 .irs-single, 
             .js-irs-2 .irs-bar-edge
             {border: blue; 
             background: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-2 .irs-bar 
             {background: blue; 
             border-top: blue;
             border-bottom: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-3 .irs-single, 
             .js-irs-3 .irs-bar-edge
             {border: red; 
             background: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-3 .irs-bar 
             {background: red; 
             border-top: red;
             border-bottom: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-4 .irs-single, 
             .js-irs-4 .irs-bar-edge
             {border: blue; 
             background: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-4 .irs-bar 
             {background: blue; 
             border-top: blue;
             border-bottom: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-5 .irs-single, 
             .js-irs-5 .irs-bar-edge
             {border: red; 
             background: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-5 .irs-bar 
             {background: red; 
             border-top: red;
             border-bottom: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-6 .irs-single, 
             .js-irs-6 .irs-bar-edge
             {border: blue; 
             background: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-6 .irs-bar 
             {background: blue; 
             border-top: blue; 
             border-bottom: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-7 .irs-single, 
             .js-irs-7 .irs-bar-edge
             {border: red; 
             background: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-7 .irs-bar 
             {background: red; 
             border-top: red; 
             border-bottom: red}"
        )
                      ),
      tags$style(
        HTML(".js-irs-8 .irs-single, 
             .js-irs-8 .irs-bar-edge
             {border: blue; 
             background: blue}"
        )
      ),
      tags$style(
        HTML( ".js-irs-8 .irs-bar 
             {background: blue; 
             border-top: blue;
             border-bottom: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-9 .irs-single, 
             .js-irs-9 .irs-bar-edge
             {border: red; 
             background: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-9 .irs-bar 
             {background: red; 
             border-top: red;
             border-bottom: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-10 .irs-single, 
             .js-irs-10 .irs-bar-edge 
             {border: blue; 
             background: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-10 .irs-bar 
             {background: blue; 
             border-top: blue;
             border-bottom: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-11 .irs-single, 
             .js-irs-11 .irs-bar-edge 
             {border: red; background: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-11 .irs-bar 
             {background: red;
             border-top: red;
             border-bottom: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-12 .irs-single, 
             .js-irs-12 .irs-bar-edge
             {border: blue; 
             background: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-12 .irs-bar 
             {background: blue; 
             border-top: blue;
             border-bottom: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-13 .irs-single, 
             .js-irs-13 .irs-bar-edge
             {border: red; 
             background: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-13 .irs-bar 
             {background: red; 
             border-top: red;
             border-bottom: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-14 .irs-single, 
             .js-irs-14 .irs-bar-edge
             {border: blue; 
             background: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-14 .irs-bar 
             {background: blue; 
             border-top: blue;
             border-bottom: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-15 .irs-single, 
             .js-irs-15 .irs-bar-edge
             {border: red; 
             background: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-15 .irs-bar 
             {background: red; 
             border-top: red; 
             border-bottom: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-16 .irs-single, 
             .js-irs-16 .irs-bar-edge
             {border: blue; 
             background: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-16 .irs-bar 
             {background: blue; 
             border-top: blue;
             border-bottom: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-17 .irs-single, 
             .js-irs-17 .irs-bar-edge
             {border: red; 
             background: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-17 .irs-bar 
             {background: red; 
             border-top: red;
             border-bottom: red}"
        )
      ),
      tags$style(
        HTML(".js-irs-18 .irs-single, 
             .js-irs-18 .irs-bar-edge
             {border: blue;
             background: blue}"
        )
      ),
      tags$style(
        HTML(".js-irs-18 .irs-bar 
             {background: blue; 
             border-top: blue;
             border-bottom: blue}"
        )
      ),
      tags$style(".fa-times {color:#c40505}"),
      tags$style(".fa-check {color:#50ea0e}"),
      tags$style(".fa-repeat {color:#0000c9}"),
      tags$style(".fa-reply {color:#0000c9}"),
      tags$head(tags$link(rel = "stylesheet", 
          type = "text/css", href = "style.css")
      ),
                      
# =========================== Introduction ============                
      tabItems(
        source(file.path("Layout", "Intro.R"), local = TRUE)$value,
                        
# ====================== Binomial ========================================      
        source(file.path("Layout", "Binom.R"), local = TRUE)$value,
        source(file.path("Layout", "Binom2.R"), local = TRUE)$value,

# ====================== Hypergeometric ===================================
        source(file.path("Layout", "Hyper.R"), local = TRUE)$value,
        source(file.path("Layout", "Hyper2.R"), local = TRUE)$value,
     
# ======================== Geometeric =====================================
        source(file.path("Layout", "Geo.R"), local = TRUE)$value,   
        source(file.path("Layout", "Geo2.R"), local = TRUE)$value,  

#==================================== Negative Hypergeometric =================
        source(file.path("Layout", "Neg_Hyp.R"), local = TRUE)$value,
        source(file.path("Layout", "Neg_Hyp2.R"), local = TRUE)$value,

#==================================== Exercise =================
        source(file.path("Layout", "Exercise.R"), local = TRUE)$value
      )
    )
)


#=============================================================================
#====================================== server ===============================
#=============================================================================

server <- function(input, output, session) {
  
# session$onSessionEnded(stopApp)        
# ==================================== Introduction ==========================
  source(file.path("Calculation", "Intro.R"), local = TRUE)$value
  
# ================================ The Binomial distribution ===================
  source(file.path("Calculation", "Binom.R"), local = TRUE)$value
  
# ==================================== Hypergeometric ==========================
  source(file.path("Calculation", "Hyper.R"), local = TRUE)$value
  
# ===================================== Geometric =============================
  source(file.path("Calculation", "Geo.R"), local = TRUE)$value
  
# ======================================= Negative Hypergeometric =============
  source(file.path("Calculation", "Neg_Hyp.R"), local = TRUE)$value 
  
#==================================== Exercise =================
  source(file.path("Calculation", "Exercise.R"), local = TRUE)$value
}

shinyApp(ui = ui, server = server)