Chapter 7.2

Question 7

library(tidyverse)
library(purrrfect)

k <- 3 #kth order statistic
(unif_order_stat_sim<- parameters(~n, c(4,18,12,16)
                                  )
  %>% add_trials(10000)
  %>% mutate(y_sample = map(n, .f = \(n) runif(n, min = 0, max = 1)))
  %>% mutate(y_sorted = map(y_sample, sort))
  %>% mutate(y1 = map_dbl(y_sample, .f = min),
             yk = map_dbl(y_sorted, pluck(k)),
             yn = map_dbl(y_sample, .f = max))
  %>% mutate(f_1 = dbeta(y1, 1, n-1+1),
             f_k = dbeta(yk, k, n-k+1),
             f_n = dbeta(yn, n, n-n+1))
  )
# A tibble: 40,000 × 10
       n .trial y_sample  y_sorted      y1    yk    yn    f_1   f_k   f_n
   <dbl>  <dbl> <list>    <list>     <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl>
 1     4      1 <dbl [4]> <dbl [4]> 0.296  0.557 0.910 1.40   1.65  3.01 
 2     4      2 <dbl [4]> <dbl [4]> 0.0405 0.627 0.812 3.53   1.76  2.14 
 3     4      3 <dbl [4]> <dbl [4]> 0.189  0.626 0.687 2.13   1.76  1.30 
 4     4      4 <dbl [4]> <dbl [4]> 0.722  0.764 0.766 0.0858 1.65  1.80 
 5     4      5 <dbl [4]> <dbl [4]> 0.0502 0.546 0.971 3.43   1.62  3.66 
 6     4      6 <dbl [4]> <dbl [4]> 0.0851 0.114 0.314 3.06   0.138 0.124
 7     4      7 <dbl [4]> <dbl [4]> 0.240  0.499 0.973 1.75   1.50  3.68 
 8     4      8 <dbl [4]> <dbl [4]> 0.168  0.634 0.779 2.30   1.77  1.89 
 9     4      9 <dbl [4]> <dbl [4]> 0.310  0.668 0.971 1.31   1.78  3.66 
10     4     10 <dbl [4]> <dbl [4]> 0.200  0.361 0.532 2.04   0.999 0.603
# ℹ 39,990 more rows
(ggplot(data = unif_order_stat_sim, aes(x = y1))
 + geom_histogram(aes(y = after_stat(density)),
                  fill = 'goldenrod',
                  center = 0.005, binwidth = 0.01)
 + geom_line(aes(y = f_1), col = 'cornflowerblue')
 + facet_wrap(~n, labeller = label_both)
 + theme_classic()
 + labs(title = 'Simulated and analytic densities for Y(1)')
)

(ggplot(data = unif_order_stat_sim, aes(x = yk))
 + geom_histogram(aes(y = after_stat(density)),
                  fill = 'goldenrod',
                  center = 0.005, binwidth = 0.01)
 + geom_line(aes(y = f_k), col = 'cornflowerblue')
 + facet_wrap(~n, labeller = label_both)
 + theme_classic()
 + labs(title = 'Simulated and analytic densities for Y(k = 3)')
)

(ggplot(data = unif_order_stat_sim, aes(x = yn))
 + geom_histogram(aes(y = after_stat(density)),
                  fill = 'goldenrod',
                  center = 0.005, binwidth = 0.01)
 + geom_line(aes(y = f_n), col = 'cornflowerblue')
 + facet_wrap(~n, labeller = label_both)
 + theme_classic()
 + labs(title = 'Simulated and analytic densities for Y(n)')
)