Commit bbaf74bc authored by jbleher's avatar jbleher
Browse files

distributions added

parent 568f1dcf
Loading
Loading
Loading
Loading
+148 −0
Original line number Diff line number Diff line
###################################################################
#                     Binomial Distribution
###################################################################

# The following script serves as a visualization of the binomial 
# distribution. For this purpose we use the shiny "library". If you
# run this code in your own R-environment make sure that the library
# is installed. When you run this on the jupyerhub the shiny app
# start in a separate window of your browser.

# Load the shiny library
library(shiny)

# Definition of the user interface (ui)
ui <- fluidPage(
  
  # Title of the shiny app
  titlePanel("Binomial Distribution"),
  
  # A panel on the left side of the page 
  sidebarLayout(
    
    # Elements within the panel on the left side
    sidebarPanel(
      
      # Dropdown menü
      selectInput("typ", "Functions:",
                  choices=c("Distribution function" = 0, 
                            "Probability mass function" = 1)),
      helpText("What function should be displayed?"),
      
      
      # Slider for the number of repetitions
      sliderInput(inputId = "repetitions",
                  label = "Number of repetitions:",
                  min = 1,
                  max = 150,
                  value = 75),
      
      # Slider of the probability of success
      sliderInput(inputId = "probability",
                  label = "Probability of success:",
                  min = 0,
                  max = 1,
                  value = 0.5)
      
    ),
    
    # Main panel on the right side of the page
    mainPanel(
      
      # Links to the functions defined in the code below
      # Display the distribution
      plotOutput(outputId = "binomial")
      
    )
  )
)

# Here comes the definition of the server function:
# The input to this function is always the function values of the above
# page elements. For example, if a user choses in the dropdown menu 
# the option "Distribution function" then the set value can be used
# within the server function below by using the inputId. The server function
# essentially is a endless loop that constantly checks the input elements
# from the UI for updates.

server <- function(input, output) {
  
  # Function for dynamic visualization
  output$binomial <- renderPlot({
    
    # Parameters
    n <- input$repetitions
    p <- input$probability
    
    # The probability of x successes given that one has observed a Bernoulli experiment n
    # times with probabiliy of successes p
    x <- seq(0, n, by = 1)
    y <- dbinom(x, n, p)
    
    # 10000 draws from the binomial distribution with n and p
    r <- rbinom(10000, n, p)

    if (input$typ == 0) {
      
      FFemp <- ecdf(r)
      xx <- knots(FFemp)
      xxlag <- c(xx)
      FFemplag <- head(c(0,FFemp(xx)),-1)
      par(mar=c(4, 4.5, 2, 4)+0.1)
      
      plot(FFemp,
           xlab="Number of successes",
           ylab="Cumulated relative frequency",
           main=sprintf("Distribution function with n=%s and p=%s", n, p),
           cex.axis=1.2,
           cex.lab=1.2,
           cex.main=1.2,
           lwd=2,
           cex=1.3,
           pch=16)
      
      points(xxlag,
             FFemplag,
             type="o",
             lt=0,
             cex=1.3,
             col="black")
      
      # Comparision of the distribution function with the normal distribution
      curve(pnorm(x, n*p, sqrt(n*p*(1-p))),
            col="coral3",
            lwd=2, 
            add=TRUE,
             yaxt="n")      
    }
    else {

      # Presentation of the probability mass function via a stick diagram
      plot(x, 
           y,
           type="h",
           lwd=2,
           ylim=c(0,max(y)),
           col="black",
           main=sprintf("Probability mass function with n=%s and p=%s", n, p),
           ylab="Frequency",
           xlab="Number of successes",
           cex.axis=1.2,
           cex.lab=1.2,
           cex.main=1.2) 
      
      # Density function of the normal distribution with mu = n*p and sigma = sqrt(n*p*(1-p))
      curve(dnorm(x, n*p, sqrt(n*p*(1-p))), 
            col="coral3",
            lwd=2, 
            add=TRUE,
            yaxt="n")
      
    }
    
  })
  
}

# Compile the elements in the shiny app
shinyApp(ui = ui, server = server)
 No newline at end of file
+105 −0
Original line number Diff line number Diff line
###################################################################
#                     Exponential Distribution
###################################################################

# The following script serves as a visualization of the exponential 
# distribution. For this purpose we use the shiny "library". If you
# run this code in your own R-environment make sure that the library
# is installed. When you run this on the jupyerhub the shiny app
# start in a separate window of your browser.

# Load the shiny library
library(shiny)

# Definition of the user interface (ui)
ui <- fluidPage(
  
  # Title of the shiny app
  titlePanel("Exponential Distribution"),
  
  # A panel on the left side of the page 
  sidebarLayout(
    
    # Elements within the panel on the left side
    sidebarPanel(
      
      # Dropdown menü
      selectInput("typ", "Functions:",
                  choices=c("Distribution function" = 0, 
                            "Probability mass function" = 1)),
      helpText("What function should be displayed?"),
      
      
      # Slider for the selection of the parameter lambda of the exponential distribution
      sliderInput(inputId = "lambda",
                  label = "lambda",
                  min = 0.00000001,
                  max = 0.5,
                  value = 0.25)
      ),
    
    # Main panel on the right side of the page
    mainPanel(
      
      # Links to the functions defined in the code below
      # Display the distribution
      plotOutput(outputId = "exponential")
      
    )
  )
)

# Here comes the definition of the server function:
# The input to this function is always the function values of the above
# page elements. For example, if a user choses in the dropdown menu 
# the option "Distribution function" then the set value can be used
# within the server function below by using the inputId. The server function
# essentially is a endless loop that constantly checks the input elements
# from the UI for updates.

server <- function(input, output) {
  
  # Function for dynamic visualization
  output$exponential <- renderPlot({
    
    # Parameter
    lambda <- input$lambda

    if (input$typ == 0) {
      
      # Comparison with the distribution function of the theoretical exponential distribution
      curve(pexp(x, lambda),
            from=0,
            to=50,
            xlab="x",
            ylab="Distribution function",
            main=sprintf("Distribution function with lambda=%s", lambda),
            cex.axis=1.2,
            cex.lab=1.2,
            cex.main=1.2,            
            col="coral3",
            lwd=3)      
    }
    else {
      
      # Comparison with the density function of the exponential distribution
      curve(dexp(x, lambda),
            from=0,
            to=50,
            xlab="x",
            ylab="Density function",
            main=sprintf("Density function with lambda=%s", lambda),
            cex.axis=1.2,
            cex.lab=1.2,
            cex.main=1.2,            
            col="coral3",
            lwd=3) 
      
    }
    
  })
  
}

# Compile the elements in the shiny app
shinyApp(ui = ui, server = server)
 No newline at end of file

02_code/R/normal_app.R

0 → 100644
+141 −0
Original line number Diff line number Diff line
###################################################################
#                     Normal distribution
###################################################################

# The following script serves as a visualization of the normal 
# distribution. For this purpose we use the shiny "library". If you
# run this code in your own R-environment make sure that the library
# is installed. When you run this on the jupyerhub the shiny app
# start in a separate window of your browser.

# Load the shiny library
library(shiny)

# Definition of the user interface (ui)
ui <- fluidPage(
  
  # Title of the shiny app
  titlePanel("Normal Distribution"),
  
  # A panel on the left side of the page 
  sidebarLayout(
    
    # Elements within the panel on the left side
    sidebarPanel(
      
      # Dropdown menü
      selectInput("typ", "Functions:",
                  choices=c("Distribution function" = 0, 
                            "Probability mass function" = 1)),
      helpText("What function should be displayed?"),
      
      # Slider for the selection of the expected value mu of the normal distribution
      sliderInput(inputId = "mu",
                  label = "mu",
                  min = -5,
                  max = 5,
                  value = 0,
                  step=0.01),
      
      #  Slider for the selection of the standard deviation sigma of the normal distribution
      sliderInput(inputId = "sigma",
                  label = "sigma",
                  min = 0.1,
                  max = 5,
                  value = 1,
                  step=0.01)
      
      ),
    
    
    # Main panel on the right side of the page
    mainPanel(
      
      # Links to the functions defined in the code below
      # Display the distribution
      plotOutput(outputId = "normal")
      
    )
  )
)

# Here comes the definition of the server function:
# The input to this function is always the function values of the above
# page elements. For example, if a user choses in the dropdown menu 
# the option "Distribution function" then the set value can be used
# within the server function below by using the inputId. The server function
# essentially is a endless loop that constantly checks the input elements
# from the UI for updates.
server <- function(input, output) {
  
  # Function for dynamic visualization
  output$normal <- renderPlot({
    
    # Distribution parameters
    mu <- input$mu
    sigma <- input$sigma
    
    if (input$typ == 0) {
      
      # Comparison with the distribution function of the normal distribution
      curve(pnorm(x, mean=mu, sd=sigma),
            from=-10,
            to=10,
            xlab="x",
            ylim=c(0,1),
            xlim=c(-10,10),
            ylab="Distribution function",
            main=sprintf("Distribution function with mu=%s and sigma=%s", mu, sigma),
            col="coral3",
            lwd=3)   
      
      # Comparison with the distribution function of the standard normal distribution
      curve(pnorm(x, mean=0, sd=1),
            ylim=c(0,1),
            xlim=c(-10,10),            
            from=-10,
            to=10,
            col="black",
            cex.axis=1.2,
            cex.lab=1.2,
            add=TRUE,
            cex.main=1.2,            
            lwd=3) 
      
    }
    else {
      
      # Comparison with the density function of the normal distribution
      curve(dnorm(x, mean=mu, sd=sigma),
            from=-10,
            to=10,
            xlab="x",
            xlim=c(-10,10),
            ylab="Dichtefunktion",
            main=sprintf("Density function with mu=%s and sigma=%s", mu, sigma),
            col="coral3",
            lwd=3) 
      
      # Comparison with the density function of the standard normal distribution
      curve(dnorm(x, mean=0, sd=1),
            xlim=c(-10,10),            
            from=-10,
            to=10,
            col="black",
            add=TRUE,
            cex.axis=1.2,
            cex.lab=1.2,
            cex.main=1.2,            
            lwd=3) 


      
            
    }
    
  })
  
}

# Compile the elements in the shiny app
shinyApp(ui = ui, server = server)
 No newline at end of file
+131 −0
Original line number Diff line number Diff line
###################################################################
#                     Poisson distribution
###################################################################

# The following script serves as a visualization of the Poisson 
# distribution. For this purpose we use the shiny "library". If you
# run this code in your own R-environment make sure that the library
# is installed. When you run this on the jupyerhub the shiny app
# start in a separate window of your browser.


# Load the shiny library
library(shiny)

# Definition of the user interface (ui)
ui <- fluidPage(
  
  # Title of the shiny app
  titlePanel("Poisson Distribution"),
  
  # A panel on the left side of the page 
  sidebarLayout(
    
    # Elements within the panel on the left side
    sidebarPanel(
      
      # Dropdown menü
      selectInput("typ", "Functions:",
                  choices=c("Distribution function" = 0, 
                            "Probability mass function" = 1)),
      helpText("What function should be displayed?"),
      
      
      # Slider for the selection of lambda
      sliderInput(inputId = "lambda",
                  label = "Parameter lambda:",
                  min = 1,
                  max = 50,
                  value = 25)

      ),
    
    # Main panel on the right side of the page
    mainPanel(
      
      # Links to the functions defined in the code below
      # Display the distribution
      plotOutput(outputId = "poisson")
      
    )
  )
)

# Here comes the definition of the server function:
# The input to this function is always the function values of the above
# page elements. For example, if a user choses in the dropdown menu 
# the option "Distribution function" then the set value can be used
# within the server function below by using the inputId. The server function
# essentially is a endless loop that constantly checks the input elements
# from the UI for updates.
server <- function(input, output) {
  
  # Function for dynamic visualization
  output$poisson <- renderPlot({
    
    # Poisson Parameter lambda
    lambda <- input$lambda
    
    # Sequence of events
    x <- seq(0, 100, by = 1)
    
    # Proability of x events with the given lambda
    y <- dpois(x, lambda)
    
    # 1000 draws from the poisson distribution
    r <- rpois(1000, lambda)
    

    if (input$typ == 0) {
      
      FFemp <- ecdf(r)
      xx <- knots(FFemp)
      xxlag <- c(xx)
      FFemplag <- head(c(0,FFemp(xx)),-1)
      par(mar=c(4, 4.5, 2, 4)+0.1)
      
      # Presentation of the empirical distribution function
      plot(FFemp,
           xlab="Number of events",
           ylab="Cumulated relative frequency",
           main=sprintf("Distribution function with lambda=%s", lambda),
           cex.axis=1.2,
           cex.lab=1.2,
           cex.main=1.2,
           lwd=2,
           cex=1.3,
           pch=16,
           xlim=c(1,100))
      
      # Points on the left end of the stairs of the empirical distribution function
      points(xxlag,
             FFemplag,
             type="o",
             lt=0,
             cex=1.3,
             col="black")
    }
    else {
      
      # Presentation of the probability function via a stick diagram
      plot(x, 
           y,
           type="h",
           lwd=2,
           ylim=c(0,max(y)),
           col="black",
           main=sprintf("Probability mass function with lambda=%s", lambda),
           ylab="Frequency",
           xlab="Number of events",
           cex.axis=1.2,
           cex.lab=1.2,
           cex.main=1.2) 
      
    }
    
  })
  
}

# Zusammenführen der Elemente zu einer shiny app
shinyApp(ui = ui, server = server)
 No newline at end of file
+135 −0
Original line number Diff line number Diff line
###################################################################
#                     Uniform (Rectangular) Distribution
###################################################################

# The following script serves as a visualization of the Uniform (rectengular) 
# distribution. For this purpose we use the shiny "library". If you
# run this code in your own R-environment make sure that the library
# is installed. When you run this on the jupyerhub the shiny app
# start in a separate window of your browser.

# Load the shiny library
library(shiny)

# Definition of the user interface (ui)
ui <- fluidPage(
  
  # Title of the shiny app
  titlePanel("Uniform Distribution"),
  
  # A panel on the left side of the page 
  sidebarLayout(
    
    # Elements within the panel on the left side
    sidebarPanel(
      
      
      # Dropdown menü
      selectInput("typ", "Functions:",
                  choices=c("Distribution function" = 0, 
                            "Probability mass function" = 1)),
      helpText("What function should be displayed?"),
      
      
      # Selection slider for the distribution parameter a of the uniform distribution
      sliderInput(inputId = "a",
                  label = "a",
                  min = 0,
                  max = 1,
                  value = 0.25),

      # Selection slider for the distribution parameter b of the uniform distribution
      sliderInput(inputId = "b",
                  label = "b",
                  min = 0,
                  max = 1,
                  value = 0.75)
      ), 
    
    # Main panel on the right side of the page
    mainPanel(
      
      # Links to the functions defined in the code below
      # Display the distribution
      plotOutput(outputId = "uniform")
      
    )
  )
)

# Here comes the definition of the server function:
# The input to this function is always the function values of the above
# page elements. For example, if a user choses in the dropdown menu 
# the option "Distribution function" then the set value can be used
# within the server function below by using the inputId. The server function
# essentially is a endless loop that constantly checks the input elements
# from the UI for updates.
server <- function(input, output, session) {
  
  # Function for dynamic visualization
  output$uniform <- renderPlot({
    
    # Distribution parameters
    a <- input$a
    b <- input$b
    
    updateSliderInput(session, "a", value = a,
                      min = 0, max = b-0.01, step=0.01)
    
    updateSliderInput(session, "b", value = b,
                      min = a+0.01, max = 1, step=0.01)    
    
    if (input$typ == 0) {
      
      # Chosen uniform distribution function
      curve(punif(x, min=a, max=b),
            from=0,
            to=1,
            xlab="x",
            ylab="Distribution Function",
            main=sprintf("Distribution Function with a=%s and b=%s", a, b),
            cex.axis=1.2,
            cex.lab=1.2,
            cex.main=1.2,            
            col="coral3",
            lwd=3)      
    }
    else {
      
      # Chosen uniform density function
      curve(dunif(x, min=a, max=b),
            from=a,
            to=b,
            xlab="x",
            ylab="Density Function",
            main=sprintf("Density Function with a=%s and b=%s", a, b),
            xlim=c(0,1),
            ylim=c(0,1/(b-a)+0.1/(b-a)),
            cex.axis=1.2,
            cex.lab=1.2,
            cex.main=1.2,            
            col="coral3",
            lwd=3) 
      
      curve(0*x,
            from=0,
            to=a,
            col="coral3",
            lwd=3, 
            add=TRUE)   
      
      curve(0*x,
            from=b,
            to=1,
            col="coral3",
            lwd=3, 
            add=TRUE)         
      
    }
    
  })
  
}

# Compile the elements to a shiny app
shinyApp(ui = ui, server = server)
 No newline at end of file