Rubythinking


Chapter 2 - Excercise M2

IRuby notebook

Like in the previous notebook for 2M1, we have the following globe tossing results:

  • (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.

The key point for this exercise is that we employ a different prior: $$ \text{prior}(p) = \begin{cases} 0, & \text{if}\ p < 0.5 \\ 1, & \text{otherwise} \end{cases} $$

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

        prior = grid.map do |x|
          y =
            if x < 0.5
              0
            else
              1
            end

          [x, y]
        end.to_h

        line_chart(prior, min: 0, max: 0.1)
      
    
  
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|
          {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: 0.1)

      
    
  
Loading...
    
      
        # For case (2)
        w = 3
        l = 1
      
    
  
Loading...
    
      
        # For case (3)
        w = 5
        l = 2
      
    
  
Loading...