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.

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

          prior = grid.each_with_object({}) do |x, hash|
            hash[x] = 1.to_f / grid_size.to_f
          end

          # In contrast to the original notebook,
          # we're using Highcharts here:
          chart = LazyHighCharts::HighChart.new('2m-1') do |f|
            f.title(text: "Prior distribution")
            f.yAxis(min: 0, max: 1)
            f.series(name: "Prior probability of parameter value", yAxis: 0, data: prior.values)
            f.chart({defaultSeriesType: "line"})
          end
          = high_chart("2m1-1", chart)
        
      
    
    
      
        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

        posterior = ->(w, l) do
          unstandardized_posterior = grid.each_with_object({}) do |x, hash|
            hash[x] = prior[x] * likelihood[w, l, x]
          end

          unstandardized_posterior.map do |x, y|
            standardized = (y.to_f / unstandardized_posterior.values.sum.to_f).round(6)
            [x, standardized]
          end.to_h
        end

        chart = LazyHighCharts::HighChart.new('graph') do |f|
          f.title(text: "Posterior distribution")
          f.yAxis(min: 0, max: 1)
          f.series(name: "Posterior probability of parameter value", yAxis: 0, data: posterior[w, l].values)
          f.chart({defaultSeriesType: "line"})
        end

        = high_chart("2m1-2", chart)
      
    
  
    
      
        # For case (2)
        w = 3
        l = 1
      
    
  
    
      
        # For case (3)
        w = 5
        l = 2