library (tidyverse)
library (purrrfect)
N <- 10000
coin <- c ('H' , 'T' )
one_omega <- \() sample (coin, size = 3 , replace = TRUE )
(many_omegas <- replicate (N, one_omega (), .as = flips)
%>% mutate (X = map_int (flips, \(x) sum (x== 'H' )))
%>% mutate (Y = map_int (flips, \(x) {
if (! "H" %in% x) {
- 1
} else if (x[1 ] == 'H' ) {
1
} else if (x[2 ] == 'H' ) {
2
} else {
3
}
}))
)
# A tibble: 10,000 × 4
.trial flips X Y
<dbl> <list> <int> <int>
1 1 <chr [3]> 1 1
2 2 <chr [3]> 2 1
3 3 <chr [3]> 3 1
4 4 <chr [3]> 2 1
5 5 <chr [3]> 2 1
6 6 <chr [3]> 0 -1
7 7 <chr [3]> 1 2
8 8 <chr [3]> 0 -1
9 9 <chr [3]> 2 1
10 10 <chr [3]> 1 3
# ℹ 9,990 more rows
(many_omegas
%>% summarize (cnt = n (),
.by = c (X,Y))
%>% mutate (joint_prob = cnt/ N,
prob_X_given_Y = cnt/ sum (cnt), .by = Y)
%>% pivot_wider (names_from = X,
values_from = prob_X_given_Y,
names_prefix = 'x=' ,
id_cols = Y)
)
# A tibble: 4 × 5
Y `x=1` `x=2` `x=3` `x=0`
<int> <dbl> <dbl> <dbl> <dbl>
1 1 0.263 0.493 0.244 NA
2 -1 NA NA NA 1
3 2 0.503 0.497 NA NA
4 3 1 NA NA NA
(many_omegas_update <- many_omegas
%>% summarize (cnt = n (),
.by = c (X,Y))
%>% mutate (joint_prob = cnt/ N,
prob_X_given_Y = cnt/ sum (cnt), .by = Y)
%>% filter (Y == 1 )
)
# A tibble: 3 × 5
X Y cnt joint_prob prob_X_given_Y
<int> <int> <int> <dbl> <dbl>
1 1 1 1301 0.130 0.263
2 2 1 2445 0.244 0.493
3 3 1 1210 0.121 0.244
library (tidyverse)
library (purrrfect)
N <- 10000
executives <- rep (c ('Married' , 'Never Married' , 'Divorced' ), times = c (4 ,3 ,2 ))
one_sample <- \() sample (executives, size = 3 , replace = FALSE )
(many_samples <- replicate (N, one_sample (), .as = employees)
%>% mutate (X = map_int (employees, \(x) sum (x == 'Married' )))
%>% mutate (Y = map_int (employees, \(x) sum (x == 'Never Married' )))
)
# A tibble: 10,000 × 4
.trial employees X Y
<dbl> <list> <int> <int>
1 1 <chr [3]> 2 1
2 2 <chr [3]> 2 1
3 3 <chr [3]> 0 1
4 4 <chr [3]> 1 1
5 5 <chr [3]> 2 1
6 6 <chr [3]> 1 1
7 7 <chr [3]> 3 0
8 8 <chr [3]> 1 2
9 9 <chr [3]> 1 2
10 10 <chr [3]> 2 1
# ℹ 9,990 more rows
(many_sample_updated <- many_samples
%>% summarize (cnt = n (),
.by = c (X,Y))
%>% mutate (joint_prob = cnt/ N,
prob_X_given_Y = cnt/ sum (cnt), .by = Y)
%>% pivot_wider (names_from = X,
values_from = prob_X_given_Y,
names_prefix = 'x=' ,
id_cols = Y)
)
# A tibble: 4 × 5
Y `x=2` `x=0` `x=1` `x=3`
<int> <dbl> <dbl> <dbl> <dbl>
1 1 0.399 0.0684 0.533 NA
2 0 0.603 NA 0.194 0.202
3 2 NA 0.350 0.650 NA
4 3 NA 1 NA NA
(many_samples
%>% summarize (cnt = n (),
.by = c (X,Y))
%>% mutate (joint_prob = cnt/ N,
prob_X_given_Y = cnt/ sum (cnt), .by = Y)
%>% filter (Y == 2 )
)
# A tibble: 2 × 5
X Y cnt joint_prob prob_X_given_Y
<int> <int> <int> <dbl> <dbl>
1 1 2 1358 0.136 0.650
2 0 2 730 0.073 0.350