Rubythinking


Chapter 2 - Excercise M1

Recall the globe tossing model from the chapter. Compute and plot the grid approximate posterior distribution for each of the following set of observations. In each case, assume a uniform prior for p.

  • (1) W, W, W
  • (2) W, W, W, L
  • (3) L, W, W, L, W, W, W

We want to estimate the probability of Water $p$ as parameter of the model. The parameter is at least 0 and at most 1.

      
        grid_size = 10
        step_size = 1.0 / grid_size.to_f
        grid = 0.step(by: step_size, to: 1).to_a

        # Let's first define and plot a uniform prior.
        prior_data = grid.map do |x| 
          [x, 1]
        end

        prior = ->(x) do
          prior_data.detect{ |point| point.first == x }.last
        end

        line_chart(prior_data)
      
    
Loading...
      
        
          factorial = ->(n) do
            return 1 if n < 1
              n.to_i.downto(1).inject(:*)
            end

            likelihood = ->(w, l, p) do
              (factorial[w+l].to_f / (factorial[w] * factorial[l])).to_f * (p**w) * ((1-p)**l)
            end
          
      
    

Now, let's compute the grid aprroximation of the posterior for each of the cases. The difference is only the data input we give in terms of "count of Water" versus "count of Land" of our tossing result given in the exercise.

    
      
        # For case (1)
        w = 3
        l = 0

        u_posterior = grid.map do |x, u_posterior|
          {x: x, y: prior[x] * likelihood[w, l, x]}
        end

        posterior = u_posterior.map do |item| 
          standardized = (item[:y].to_f / u_posterior.map{ |p| p[:y] }.sum.to_f)
          [item[:x], standardized]
        end.to_h

        line_chart(posterior, min: 0, max: 1)
      
    
  
Loading...
    
      
        # For case (2)
        w = 3
        l = 1
      
    
  
Loading...
    
      
        # For case (3)
        w = 5
        l = 2
      
    
  
Loading...