-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_the_anneal.Rmd
155 lines (101 loc) · 2.67 KB
/
sim_the_anneal.Rmd
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
---
title: "simulate_some_annealz"
author: "Andrew"
date: "20/08/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
`DEFINE function to optimize h(X)`
```{r}
fake_xs <- rnorm(42, 19, 4)
loglike_numbers <- function(numbers, mu) sum(dnorm(x = numbers, mean = mu, sd = 4, log = TRUE))
loglike_numbers(fake_xs, 7)
library(tidyverse)
tibble(possible_mus = -3:30) %>%
mutate(ll = map_dbl(possible_mus, loglike_numbers, numbers = fake_xs)) %>%
ggplot(aes(x = possible_mus, y = ll)) + geom_point() +
geom_vline(xintercept = 19)
```
`DEFINE the sampling function c(X)`
```{r}
see_of_x <- function(x, sigma = 1) { x + rnorm(1, sd = sigma) }
xx <- numeric(40)
xx[1] <- 30
## walk like a drunk: test my see of x function
for(i in 2:40){
xx[i] <- see_of_x(xx[i-1], sigma = .001)
}
plot(xx, type = "l")
```
`DEFINE temperature sequence`
```{r}
temp_line <- function(runs, tstart, tcool){
exp(log(tstart) + log(tcool)*0:(runs-1))
}
temp_line_str <- function(runs, tstart) {
seq(tstart, 0, length.out = runs)
}
tibble(time = 1:300,
temp = temp_line(300, 8, 0.9)) %>%
ggplot(aes(x = time, y = temp)) +
geom_point()
tibble(time = 1:300,
prob = exp(-13/temp_line(300, 70, 0.97))) %>%
ggplot(aes(x = time, y = prob)) +
geom_point()
```
```
REPEAT
DRAW sample X from c(x)
COMPUTE difference diff = h(X) - h(X_0)
IF diff > 0 ACCEPT X
ELSE
COMPUTE acceptance probability p = exp(diff/T)
DRAW value P from random uniform on (0,1)
IF P < p
ACCEPT X
ELSE
REJECT X
UPDATE temperature
UNTIL nsteps is reached
```
```{r}
# function to take temperature, diff, and give back TRUE or FALSE (will a negative be kept)
keep_neg_diff <- function(diff, Tmp){
stopifnot(diff<0)
runif(1, min = 0, max = 1) < exp(diff/Tmp)
}
keep_neg_diff(-30, Tmp = 40)
keep_neg_diff(10, Tmp = 0)
```
```{r}
## function to take x0 and x1 and a function and temp and return either x0 (reject) or x1 ( accept)
keep_or_reject <- function(x0, x1, fun, temp){
diff <- fun(x1) - fun(x0)
# browser()
if (diff > 0) {
return(x1)
} else if (keep_neg_diff(diff, Tmp = temp)){
return(x1)
} else {
return(x0)
}
}
```
```{r}
# evaluate for these only
ll_fakenums_f_x <- function(x) loglike_numbers(fake_xs, mu = x)
# make temperatures
runs <- 299
temps <- temp_line(runs, 3, 0.98)
xx <- numeric(runs + 1)
startval <- 10
xx[1] <- startval
for (i in 2:runs){
maybe <- see_of_x(xx[i-1])
xx[i] <- keep_or_reject(x0 = xx[i-1], x1 = maybe, fun = ll_fakenums_f_x, temp = temps[i-1])
}
plot(xx, type = "l")
```