-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.4-Genetic_algorithm.qmd
227 lines (155 loc) · 4.19 KB
/
5.4-Genetic_algorithm.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
---
title: "Genetic algorithm"
author:
- Elizabeth King
- Kevin Middleton
format:
revealjs:
theme: [default, custom.scss]
standalone: true
self-contained: true
logo: QMLS_Logo.png
slide-number: true
show-slide-number: all
code-annotations: hover
bibliography: Randomization.bib
csl: evolution.csl
---
## Genetic algorithm
```{r}
#| label: setup
#| echo: false
#| warning: false
#| message: false
library(tidyverse)
library(cowplot)
library(rgenoud)
ggplot2::theme_set(theme_cowplot(font_size = 18))
set.seed(6452730)
```
- "Genetic" in name only
- Inspired by ideas from evolution: populations, selection, mutation, crossover
- General optimization approach
- "Stochastic search algorithm" [@Scrucca2013-jf]
- No likelihood necessary: optimize on MSE, MAE, *r*
## Genetic algorithm history
- Introduced / synthesized: [@Holland1975-nz; @Goldberg1989-zk]
- Modern implementation with derivatives: [@Sekhon1998-hs; @Mebane2011-vy]
## Benefits
- General solution for optimization
- Resists getting stuck in local optima
- Can be fast, even for large datasets
## GA schematic
{fig-align="center"}
## R packages for GA
1. `rgenoud` (GENetic Optimization Using Derivatives) [@Sekhon1998-hs]
- Combines derivative-based optimization with an evolutionary algorithm
2. `GA` [@Scrucca2013-jf; @Scrucca2017-yg]
## Simulate data: Gamma(3, 3)
```{r}
#| echo: false
set.seed(234798)
y <- rgamma(500, shape = 3, rate = 3)
ggplot(tibble(y), aes(y)) +
geom_histogram(bins = 30, fill = "darkorange3")
```
## Model statement and function
$$y \sim Gamma(shape,~rate)$$
```{r}
#| echo: true
gamma_sim <- function(p) {
log_lik <- sum(dgamma(y, p[1], p[2], log = TRUE))
return(log_lik)
}
gamma_sim(c(3, 3))
```
## Choosing the parameter "search" space
- What are the likely values for the parameters?
- What limits?
- Options to strictly stay in the bounds or allow broader search
## `genoud()` options: `P1` - `P9`
1. Cloning
2. Uniform mutation
3. Boundary mutation
4. Non-uniform mutation
5. Polytope crossover
6. Simple crossover
7. Whole non-uniform mutation
8. Heuristic crossover
9. Local-minimum crossover
## Fitting the model
```{r}
#| echo: true
#| output-location: slide
fm <- genoud(gamma_sim,
nvars = 2,
max = TRUE,
Domains = matrix(c(0, 10,
0, 10), nrow = 2, byrow = TRUE),
pop.size = 5000)
```
## Differences from ABC
- No prior distributions to draw from
- No samples to process
- Optima only
## Challenging models
- "Intractable" likelihoods
- Flat or oddly shaped surfaces
- Discontinuous likelihood
- Nonlinear models
- Multiple optima
## Non-linear model: Gompertz growth equation
$$y(t) = a e^{-b e^{-c t}}$$
- *a* is the asymptotic size
- *b* is the $x$ displacement of the entire curve
- *c* is growth rate
```{r}
#| echo: true
Gompertz <- function(t, a, b, c) {
a * exp(-b * exp(-c * t))
}
```
## Simulate data: a = 5, b = 1, c = 0.5
```{r}
#| echo: false
set.seed(436578)
GG <- tibble(t = seq(0, 25, length.out = 200),
y = Gompertz(t, a = 5, b = 1, c = 0.5))
GGsim <- tibble(t = runif(20, min = 0, max = 25),
y = Gompertz(t, a = 5, b = 1, c = 0.5) +
rnorm(20, 0, 0.25))
ggplot() +
geom_line(data = GG, aes(t, y), linewidth = 1) +
geom_point(data = GGsim, aes(t, y), size = 3) +
scale_y_continuous(limits = c(0, 6))
```
## Fitting the model
```{r}
#| echo: true
#| output-location: slide
Gomp_MSE <- function(p, GGsim) {
pred <- Gompertz(GGsim$t, p[1], p[2], p[3])
obs <- GGsim$y
return(sqrt(mean((obs - pred) ^ 2)))
}
gen <- genoud(fn = Gomp_MSE,
GGsim = GGsim,
nvars = 3,
pop.size = 5000)
```
## Visualizing the model
```{r}
ggplot() +
geom_line(data = GG, aes(t, y), linewidth = 1) +
geom_line(
data = tibble(t = seq(0, 25, length.out = 200),
y = Gompertz(t, a = gen$par[1], b = gen$par[2],
c = gen$par[3])),
aes(t, y),
color = "red", linewidth = 1) +
geom_point(data = GGsim, aes(t, y), size = 3) +
scale_y_continuous(limits = c(0, 6))
```
## References
::: {#refs}
:::