value abs_value
1 -2 2
2 -1 1
3 0 0
4 1 1
5 2 2
October 28, 2026
Based on https://www.effectivedatastorytelling.com/post/a-deeper-dive-into-lego-bricks-and-data-stories, original source: https://www.linkedin.com/learning/instructors/bill-shander
if_else()case_when()# A tibble: 20 × 5
carat cut color price price_cat
<dbl> <ord> <ord> <int> <chr>
1 1.23 Very Good F 10276 expensive
2 0.35 Premium H 706 inexpensive
3 0.7 Good E 2782 medium
4 0.4 Ideal D 1637 medium
5 0.53 Ideal G 1255 inexpensive
6 2.22 Ideal G 14637 expensive
7 0.3 Ideal G 878 inexpensive
8 1.05 Ideal H 4223 medium
9 0.53 Premium E 1654 medium
10 1.7 Ideal H 7068 medium
11 0.31 Good E 698 inexpensive
12 0.31 Ideal F 840 inexpensive
13 1.03 Ideal H 4900 medium
14 0.31 Premium G 698 inexpensive
15 1.56 Premium G 8858 medium
16 1.71 Premium G 11032 expensive
17 1 Good E 5345 medium
18 1.86 Ideal J 10312 expensive
19 1.08 Very Good E 3726 medium
20 0.31 Premium E 698 inexpensive
sample()sampling, shuffling, and resampling: sample()
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
[1] "i" "b" "g" "d" "a"
[1] "j" "g" "f" "i" "f"
[1] "f" "h" "i" "e" "g" "d" "c" "j" "b" "a"
[1] "e" "j" "e" "b" "e" "c" "f" "a" "e" "a"
set.seed()What if we want to be able to generate the same random numbers (here on the interval from 0 to 1) over and over?
Consider a situation where Alex and Jordan plan to meet to study in their college campus center (Mosteller 1987; Baumer et al. 2021). They are both impatient people who will wait only 10 minutes for the other before leaving.
But their planning was incomplete. Alex said, “Meet me between 7 and 8 tonight at the student center.” When should Jordan plan to arrive at the campus center? And what is the probability that they actually meet?
Assume that Alex and Jordan are both equally likely to arrive at the campus center anywhere between 7pm and 8pm.
The results themselves are equivalent. Differing values due to randomness in the simulation.
10 people are at a party, and all of them are wearing hats. They each place their hat in a pile; when they leave, they choose a hat at random. What is the probability at least one person selected the correct hat?
[1] TRUE
10[1] 0.6329
match_prob num_hats
1 1.0000 1
2 0.4964 2
3 0.6695 3
4 0.6241 4
5 0.6376 5
6 0.6290 6
7 0.6299 7
8 0.6299 8
9 0.6373 9
10 0.6273 10
11 0.6319 11
12 0.6348 12
13 0.6393 13
14 0.6300 14
15 0.6280 15
16 0.6396 16
17 0.6355 17
18 0.6283 18
19 0.6304 19
20 0.6461 20
What is the probability that in a room of 28 people, at least 2 of them have the same birthday?
[1] 0 1 0 1 1 1 1 0 1 0
set.seed(47)
num_stud_upper <- 28
num_class <- 100
birth_match_prob <- function(num_stud, reps) {
prob <- map_dbl(c(1:reps), \(w) class_duplicates(class_size = num_stud)) |>
mean()
return(data.frame(match_prob = prob, num_stud = num_stud))
}
map(c(1:num_stud_upper), birth_match_prob, reps = num_class) |>
list_rbind() match_prob num_stud
1 0.00 1
2 0.00 2
3 0.00 3
4 0.00 4
5 0.03 5
6 0.03 6
7 0.04 7
8 0.06 8
9 0.11 9
10 0.13 10
11 0.14 11
12 0.18 12
13 0.18 13
14 0.25 14
15 0.25 15
16 0.32 16
17 0.37 17
18 0.43 18
19 0.44 19
20 0.40 20
21 0.43 21
22 0.47 22
23 0.48 23
24 0.47 24
25 0.61 25
26 0.67 26
27 0.63 27
28 0.66 28
The goal of simulating a complicated model is not only to create a program which will provide the desired results. We also hope to be able to write code such that:
Population:
talent ~ Normal (100, 15)
grades ~ Normal (talent, 15)
SAT ~ Normal (talent, 15)
The example is taken directly (and mostly verbatim) from a blog by Aaron Roth Algorithmic Unfairness Without Any Bias Baked In.
College wants to admit students with
talent > 115
… but they only have access to grades and SAT which are noisy estimates of talent.
talent is known for existing students)talent based on grades and SATtalent is above 115But there is one difference: the Blues have more money than the Reds, so they each take the SAT twice, and report only the highest of the two scores to the college. This results in a small but noticeable bump in their average SAT scores, compared to the Reds.
The value of
SATmeans something different for the Reds versus the Blues
(They have different feature distributions.)
Red model (SAT taken once):
# A tibble: 3 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 33.2 0.152 218. 0
2 SAT 0.334 0.00149 224. 0
3 grades 0.334 0.00149 225. 0
Blue model (SAT is max score of two):
# A tibble: 3 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 25.3 1.60 15.8 2.04e- 50
2 SAT 0.432 0.0161 26.7 3.35e-119
3 grades 0.277 0.0154 18.0 6.83e- 63
Generate new data, use the two models separately.
Can the models predict if a student has talent > 115?
# A tibble: 2 × 5
color tpr fpr fnr error
<chr> <dbl> <dbl> <dbl> <dbl>
1 blue 0.503 0.0379 0.497 0.109
2 red 0.509 0.0378 0.491 0.109
What if we fit only one model to the entire dataset?
After all, there are laws against using protected classes to make decisions (housing, jobs, money loans, college, etc.)
# A tibble: 3 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 33.1 0.151 219. 0
2 SAT 0.334 0.00148 225. 0
3 grades 0.334 0.00148 226. 0
(The coefficients kinda look like the red model…)
One model:
# A tibble: 2 × 5
color tpr fpr fnr error
<chr> <dbl> <dbl> <dbl> <dbl>
1 blue 0.619 0.0627 0.381 0.112
2 red 0.507 0.0375 0.493 0.109
Two separate models:
# A tibble: 2 × 5
color tpr fpr fnr error
<chr> <dbl> <dbl> <dbl> <dbl>
1 blue 0.503 0.0379 0.497 0.109
2 red 0.509 0.0378 0.491 0.109
with two populations that have different feature distributions, learning a single classifier (that is prohibited from discriminating based on population) will fit the bigger of the two populations
depending on the nature of the distribution difference, it can be either to the benefit or the detriment of the minority population
no explicit human bias, either on the part of the algorithm designer or the data gathering process
the problem is exacerbated if we artificially force the algorithm to be group blind
well-intentioned “fairness” regulations prohibiting decision makers form taking sensitive attributes into account can actually make things less fair and less accurate at the same time
Taken from Risk Analysis Using Monte Carlo Simulations in R.
rnorm()rnorm() calculates random normal values
Task: to model an asset allocation problem where you decide what portion of wealth should be allocated to risk-free investment or high-risk investment
half risky, half risk free; each year modeled independently
step return_rate
1 1 1.0588529
2 2 0.9581425
3 3 1.0323918
4 4 1.0864976
5 5 1.0138117
6 6 0.9795349
7 7 1.0692285
8 8 0.9948075
9 9 1.0863899
10 10 1.0277911
11 11 1.0840087
12 12 1.0314113
lots of years, lots of different people
step return_rate run_number
1 1 1.004515 happy
2 2 1.110764 happy
3 3 1.036621 happy
4 4 1.071153 happy
5 5 1.094853 happy
6 6 1.065613 happy
7 7 1.027749 happy
8 8 1.066103 happy
9 9 1.064003 happy
10 10 1.109124 happy
11 11 1.106714 happy
12 12 1.027800 happy
step return_rate run_number
1 1 1.0174664 1
2 2 1.0710106 1
3 3 1.0454731 1
4 4 0.9803695 1
5 5 1.0500943 1
6 6 0.9655029 1
7 7 1.0378193 1
8 8 1.0888919 1
9 9 1.0723313 1
10 10 1.0322651 1
11 11 1.1300284 1
12 12 0.9159055 1
13 1 1.1123850 2
14 2 1.0477103 2
15 3 1.1192795 2
16 4 1.0672017 2
17 5 1.0280624 2
18 6 1.0332460 2
19 7 1.0065015 2
20 8 1.0895105 2
21 9 0.9596906 2
22 10 1.0089257 2
23 11 1.0751935 2
24 12 1.0630326 2
25 1 0.9792322 3
26 2 1.0313252 3
27 3 1.0270956 3
28 4 1.0606295 3
29 5 0.9859917 3
30 6 1.1622055 3
31 7 1.0290213 3
32 8 1.0935939 3
33 9 1.0698857 3
34 10 1.1066740 3
35 11 1.0179271 3
36 12 1.0454501 3
37 1 1.0015805 4
38 2 1.0992049 4
39 3 1.0403266 4
40 4 1.0516253 4
41 5 1.0377984 4
42 6 1.0583199 4
43 7 0.9373106 4
44 8 1.0761941 4
45 9 1.1514711 4
46 10 1.0782322 4
47 11 1.0252572 4
48 12 1.0974029 4
49 1 1.0432885 5
50 2 1.0948252 5
51 3 0.9989295 5
52 4 0.9439107 5
53 5 1.0405949 5
54 6 1.0390544 5
55 7 1.0844358 5
56 8 1.0915834 5
57 9 0.9722081 5
58 10 1.0526213 5
59 11 1.0025348 5
60 12 1.1136654 5
61 1 0.9708767 6
62 2 1.0727966 6
63 3 1.1012203 6
64 4 1.0139564 6
65 5 0.9797545 6
66 6 1.0433068 6
67 7 1.0776056 6
68 8 1.0544697 6
69 9 0.9705515 6
70 10 1.0413634 6
71 11 1.0949313 6
72 12 1.1251401 6
73 1 1.1600289 7
74 2 1.0216195 7
75 3 0.9954956 7
76 4 1.0118888 7
77 5 1.0210465 7
78 6 1.1247748 7
79 7 0.9250424 7
80 8 1.0015825 7
81 9 1.1887665 7
82 10 1.0370884 7
83 11 1.0404991 7
84 12 1.0762785 7
85 1 1.0708723 8
86 2 1.0323054 8
87 3 1.0254874 8
88 4 1.0163928 8
89 5 1.1114064 8
90 6 0.9526653 8
91 7 1.0302109 8
92 8 1.1212008 8
93 9 1.0083360 8
94 10 1.0565572 8
95 11 1.0464400 8
96 12 1.0074085 8
97 1 1.0601184 9
98 2 1.0700111 9
99 3 0.9881909 9
100 4 1.0890347 9
101 5 1.0395824 9
102 6 1.0586875 9
103 7 1.0028382 9
104 8 1.1664203 9
105 9 0.9592491 9
106 10 1.0488079 9
107 11 1.0867003 9
108 12 1.1116641 9
109 1 1.1226097 10
110 2 0.9800166 10
111 3 1.0890325 10
112 4 0.9522060 10
113 5 1.0486859 10
114 6 1.0618611 10
115 7 0.9878234 10
116 8 1.0936126 10
117 9 1.0211715 10
118 10 1.0411440 10
119 11 1.0800186 10
120 12 0.9932459 10
# A tibble: 120 × 4
# Groups: run_number [10]
step return_rate run_number compound_return
<int> <dbl> <fct> <dbl>
1 1 0.979 1 0.979
2 2 1.02 1 1.00
3 3 1.09 1 1.09
4 4 0.984 1 1.07
5 5 1.08 1 1.16
6 6 1.09 1 1.26
7 7 0.998 1 1.26
8 8 1.03 1 1.30
9 9 1.04 1 1.35
10 10 0.998 1 1.35
# ℹ 110 more rows
runs <- 20
map(c(1:runs), run_func, steps = decision_steps, alpha = 0.5) |>
list_rbind() |>
group_by(run_number) |>
mutate(compound_return = cumprod(return_rate)) |>
ggplot(aes(x = step, y = compound_return)) +
geom_line(aes(color = run_number)) +
theme_minimal() +
theme(legend.position = "none") +
scale_x_continuous(breaks = seq(1, 12)) +
labs(title = "Simulations of returns from asset allocation, alpha = 0.5")runs <- 20
map(c(1:runs), run_func, steps = decision_steps, alpha = 0.1) |>
list_rbind() |>
group_by(run_number) |>
mutate(compound_return = cumprod(return_rate)) |>
ggplot(aes(x = step, y = compound_return)) +
geom_line(aes(color = run_number)) +
theme_minimal() +
theme(legend.position = "none") +
scale_x_continuous(breaks = seq(1, 12)) +
labs(title = "Simulations of returns from asset allocation, alpha = 0.1")runs <- 20
map(c(1:runs), run_func, steps = decision_steps, alpha = 0.9) |>
list_rbind() |>
group_by(run_number) |>
mutate(compound_return = cumprod(return_rate)) |>
ggplot(aes(x = step, y = compound_return)) +
geom_line(aes(color = run_number)) +
theme_minimal() +
theme(legend.position = "none") +
scale_x_continuous(breaks = seq(1, 12)) +
labs(title = "Simulations of returns from asset allocation, alpha = 0.9")The standard deviation is now three times bigger.
runs <- 20
map(c(1:runs), run_func_sd10, steps = decision_steps, alpha = 0.5) |>
list_rbind() |>
group_by(run_number) |>
mutate(compound_return = cumprod(return_rate)) |>
ggplot(aes(x = step, y = compound_return)) +
geom_line(aes(color = run_number)) +
theme_minimal() +
theme(legend.position = "none") +
scale_x_continuous(breaks = seq(1, 12)) +
labs(title = "Simulations of returns from asset allocation, alpha = 0.5")