-
Notifications
You must be signed in to change notification settings - Fork 0
/
noise.r
361 lines (297 loc) · 15.7 KB
/
noise.r
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#_____________________________________________________________________________________________________#
#___ Noise induced order and fitting stochastic models _______________________________________________#
#___ https://royalsocietypublishing.org/doi/10.1098/rstb.2019.0381 ___________________________________#
#___ January 2024 - Marco Fele _______________________________________________________________________#
#_____________________________________________________________________________________________________#
library("tidyverse")
rm(list = ls())
# Functions __________________________________________________________________________________________________________________________________________________________________________________________________________----
# I tried to make this function general
gillespie_algorithm <- function(states, # vector of size equal to the number of possible states, and entries equal to the number of individuals in that state
parameters, # vector of size equal to the number of reactions, and entries equal to the reaction rate
omega, # parameter indicating the spatial scale of the system, conventionally equal to the group size for collective decision-making, adjustable for population dynamics
stoichiometry_reagents, # matrix of size (rows * columns) number of reactions * number of possible states, with each entry the stoichiometry of the reagents for a given reaction
stoichiometry_products) { # matrix of size (rows * columns) number of reactions * number of possible states, with each entry the stoichiometry of the products for a given reaction
# Useful variables
n_reactions <- nrow(stoichiometry_reagents)
n_reagents <- ncol(stoichiometry_reagents)
# Find reaction velocities (or propensity) through the stochastic law of mass action for small population sizes (from: Approximation and inference methods for stochastic biochemical kinetics—a tutorial review)
velocities <- sapply(1:n_reactions, # return a vector of length equal to the number of reactions, with each entry the reaction velocity
function(reaction_number) {
reagents_needed <- stoichiometry_reagents[reaction_number, ]
velocity <- 0
if(all(states - reagents_needed >= 0)) { # reactions happen (change the value of velocity) only if there are enough reagents (which mean they arrive at zero if you account the reaction happening)
velocity <- parameters[reaction_number] * # all this shit comes from me simplifying the stochastic law of mass action, if you understand this kudos
1/omega^(sum(reagents_needed) - 1) * # include the resolution of the system
prod(c(sapply(1:n_reagents, function(reagent) { # for every reagent find the simplification of the factorial
prod(states[reagent] - 0:(reagents_needed[reagent] - 1))
}))[reagents_needed > 0]) # this is a vector with one entry per reagent, you multiply for every reagent which is involved in the reaction
}
return(velocity)
})
if(all(velocities < 0.00001 & velocities > -0.00001)) return(c(NA, NA, states)) # in case nothing can happen anymore
# Draw waiting time
waiting_time <- rexp(n = 1, rate = sum(velocities))
# Draw event
event <- sample(x = n_reactions, size = 1, prob = velocities)
# Update states
new_states <- states - stoichiometry_reagents[event, ] + stoichiometry_products[event, ]
# Returning event and states is redundant but lets keep it
return(c(waiting_time, event, new_states))
}
# Parameters ________________________________________________________________________________________________________________________________________________________________________________________________________________----
# Simulation parameters
set.seed(666)
max_transitions <- 100000
initial_states <- c(25, 25)
n_replicates <- 20
group_size <- sum(initial_states)
n_species <- length(initial_states)
# Reactions parameters for voter model
parameters <- c(1, 1, 0.01, 0.01, 0.00, 0.00)
names(parameters) <- c("r_a", "r_b", "sigma_a", "sigma_b", "ternary_1", "ternary_2")
# Reactions parameters for higher order interaction model
parameters_ternary <- c(1, 1, 0.01, 0.01, 0.08, 0.08)
names(parameters_ternary) <- c("r_a", "r_b", "sigma_a", "sigma_b", "ternary_1", "ternary_2")
# Stoichiometry matrices
stoichiometry_reagents <- matrix(c(1, 1, # each row is a reaction, each column is a species
1, 1,
1, 0,
0, 1,
2, 1,
1, 2),
ncol = n_species,
byrow = T)
stoichiometry_products <- matrix(c(2, 0, # each row is a reaction, each column is a species
0, 2,
0, 1,
1, 0,
3, 0,
0, 3),
ncol = n_species,
byrow = T)
# Voter model ______________________________________________________________________________________________________________________________________________________________________________________----
## Simulation ----
# Create containers to save results
data <- matrix(NA, ncol = 3 + n_species,
nrow = max_transitions * n_replicates)
colnames(data) <- c("replicate", "waiting_time", "event", paste("species", 1:n_species, sep = "_"))
state_columns <- 4:ncol(data)
# Stochastic simulation
for(replicate in 1:n_replicates) {
print(replicate)
replicate_to_index <- max_transitions * (replicate - 1) # just the way I use to save results when there are multiple replicates
# Set initial conditions
states <- initial_states # initial conditions
data[1 + replicate_to_index, ] <- c(replicate, 0, NA, states) # record initial conditions
# Simulate for one replicate
for(transition in 2:max_transitions) {
# Update based on mr Gillespie
data[transition + replicate_to_index, ] <- c(replicate,
gillespie_algorithm(states, parameters, omega = group_size,
stoichiometry_reagents, stoichiometry_products))
states <- data[transition + replicate_to_index, state_columns]
}
}
## Visualize results ----
# Modify data
data_m <- data |>
as.data.frame() |>
group_by(replicate) |>
mutate(time = cumsum(waiting_time),
duration = c(tail(waiting_time, -1), NA))
data_l <- data_m |>
pivot_longer(cols = contains("species"),
names_to = "species",
values_to = "number") |>
group_by(replicate, species) |>
mutate(proportion = number / group_size)
# Plot
ggplot(data_l |>
filter(replicate %in% 1:20, species == "species_1")) +
geom_step(aes(time, number, color = species,
group = interaction(species, replicate))) +
xlim(c(0, 3000)) +
facet_wrap(~replicate)
ggplot(data_l |>
filter(!is.na(duration))) +
geom_density(aes(number, color = species, weight = duration,
group = interaction(species, replicate)),
fill = NA)
## Manually calculate autocorrelation function (ACF) (just for fun, ACF is a bit tricky to calculate manually) ----
# Create a time series at one second resolution (I am not sure this is necessary but this is what I did)
data_resolution <- data_m |>
mutate(order = species_1,
time = ceiling(time)) |> # to find state at one second resolution
select(replicate, time, order) |>
group_by(replicate, time) |>
filter(row_number() == n()) |> # also to find state at one second resolution
group_by(replicate) |>
mutate(time_difference = c(diff(time, lag = 1), 1)) |>
uncount(time_difference) |> # fill in the blanks
mutate(time = 0:max(time))
# Calculate ACF
ACF_data <- data_resolution |>
group_by(replicate) |>
mutate(avg_order = mean(order),
deviation = order - avg_order,
avg_squared_deviation = mean(deviation ^ 2)) |>
merge(data.frame(tau = seq(0, 300, by = 3))) |>
mutate(other_time = time + tau) |>
left_join(data_resolution |>
rename(other_order = order),
by = join_by(replicate == replicate,
other_time == time)) |>
mutate(other_deviation = other_order - avg_order,
correlation = deviation * other_deviation / avg_squared_deviation) |>
group_by(replicate, tau) |>
summarise(ACF = mean(correlation, na.rm = T),
sample_size = n())
# Plot results
ggplot(ACF_data) +
geom_line(aes(tau, ACF, color = replicate, group = replicate)) +
geom_smooth(aes(tau, ACF), color = "red", linewidth = 2) +
geom_hline(aes(yintercept = 0)) +
geom_hline(aes(yintercept = 0.025), lty = "dashed") +
geom_hline(aes(yintercept = -0.025), lty = "dashed") +
xlim(c(0, 200))
## Fit SDE ----
# Calculate deterministic and stochastic component
data_det_sto <- data_resolution |>
group_by(replicate) |>
mutate(order = order / group_size,
dt_1 = c(tail(order, -1), NA)) |>
group_by(replicate, order) |>
mutate(diff = dt_1 - order,
size = n(),
first_moment = mean(diff / 1, na.rm = T),
residual = diff - first_moment * 1) |>
reframe(first_moment = unique(first_moment, na.rm = T),
second_moment = mean(residual^2/1, na.rm = T),
size = unique(size)) |>
pivot_longer(cols = c("first_moment", "second_moment"),
names_to = "effect",
values_to = "value")|>
group_by(order, effect) |>
mutate(avg_moment = mean(value, na.rm = T))
# Plot result
ggplot(data_det_sto) +
geom_point(aes(order, value, color = replicate, group = replicate)) +
#geom_line(aes(order, avg_moment), color = "red", linewidth = 2) +
geom_smooth(aes(order, value, weight = size),
formula = y ~ splines::bs(x, 4),
color = "red") +
facet_wrap(~effect)
# Compare to figure 2: the scale of the plot is different because he uses polarization
# Higher-order interactions model ______________________________________________________________________________________________________________________________________________________________________________________----
## Simulation ----
# Create containers to save results
data_ternary <- matrix(NA, ncol = 3 + n_species,
nrow = max_transitions * n_replicates)
colnames(data_ternary) <- c("replicate", "waiting_time", "event", paste("species", 1:n_species, sep = "_"))
state_columns_ternary <- 4:ncol(data_ternary)
# Stochastic simulation
for(replicate in 1:n_replicates) {
print(replicate)
replicate_to_index <- max_transitions * (replicate - 1)
# Set initial conditions
states <- initial_states # initial conditions
data_ternary[1 + replicate_to_index, ] <- c(replicate, 0, NA, states) # record initial conditions
# Simulate replicate
for(transition in 2:max_transitions) {
data_ternary[transition + replicate_to_index, ] <- c(replicate,
gillespie_algorithm(states, parameters_ternary, omega = group_size,
stoichiometry_reagents, stoichiometry_products))
states <- data_ternary[transition + replicate_to_index, state_columns]
}
}
## Visualize results ----
# Modify data
data_m_ternary <- data_ternary |>
as.data.frame() |>
group_by(replicate) |>
mutate(time = cumsum(waiting_time),
duration = c(tail(waiting_time, -1), NA))
data_l_ternary <- data_m_ternary |>
pivot_longer(cols = contains("species"),
names_to = "species",
values_to = "number") |>
group_by(replicate, species) |>
mutate(proportion = number / group_size)
# Plot
ggplot(data_l_ternary |>
filter(replicate %in% 1:20, species == "species_1")) +
geom_step(aes(time, number, color = species,
group = interaction(species, replicate))) +
xlim(c(0, 3000)) +
facet_wrap(~replicate)
ggplot(data_l_ternary |>
filter(!is.na(duration))) +
geom_density(aes(number, color = species, weight = duration,
group = interaction(species, replicate)),
fill = NA)
## Manually calculate autocorrelation function (ACF) ----
# Create a time series at one second resolution
data_resolution_ternary <- data_m_ternary |>
mutate(order = species_1,
time = ceiling(time)) |> # to find state at one second resolution
select(replicate, time, order) |>
group_by(replicate, time) |>
filter(row_number() == n()) |> # also to find state at one second resolution
group_by(replicate) |>
mutate(time_difference = c(diff(time, lag = 1), 1)) |>
uncount(time_difference) |> # fill in the blanks
mutate(time = 0:max(time))
# Calculate ACF
ACF_data_ternary <- data_resolution_ternary |>
group_by(replicate) |>
mutate(avg_order = mean(order),
deviation = order - avg_order,
avg_squared_deviation = mean(deviation ^ 2)) |>
merge(data.frame(tau = seq(0, 300, by = 3))) |>
mutate(other_time = time + tau) |>
left_join(data_resolution_ternary |>
rename(other_order = order),
by = join_by(replicate == replicate,
other_time == time)) |>
mutate(other_deviation = other_order - avg_order,
correlation = deviation * other_deviation / avg_squared_deviation) |>
group_by(replicate, tau) |>
summarise(ACF = mean(correlation, na.rm = T),
sample_size = n()) # no autocorrelation when the time series ends
# Plot results
ggplot(ACF_data_ternary) +
geom_line(aes(tau, ACF, color = replicate, group = replicate)) +
geom_smooth(aes(tau, ACF), color = "red", linewidth = 2) +
geom_hline(aes(yintercept = 0)) +
geom_hline(aes(yintercept = 0.025), lty = "dashed") +
geom_hline(aes(yintercept = -0.025), lty = "dashed") +
xlim(c(0, 200))
## Fit SDE ----
# Calculate deterministic and stochastic component
data_det_sto_ternary <- data_resolution_ternary |>
group_by(replicate) |>
mutate(order = order / group_size,
dt_1 = c(tail(order, -1), NA)) |>
group_by(replicate, order) |>
mutate(diff = dt_1 - order,
size = n(),
first_moment = mean(diff / 1, na.rm = T),
residual = diff - first_moment * 1) |>
reframe(first_moment = unique(first_moment, na.rm = T),
second_moment = mean(residual^2/1, na.rm = T),
size = unique(size)) |>
pivot_longer(cols = c("first_moment", "second_moment"),
names_to = "effect",
values_to = "value")|>
group_by(order, effect) |>
mutate(avg_moment = mean(value, na.rm = T))
# Plot result
ggplot(data_det_sto_ternary) +
geom_point(aes(order, value, color = replicate, group = replicate)) +
#geom_line(aes(order, avg_moment), color = "red", linewidth = 2) +
geom_smooth(aes(order, value, weight = size),
formula = y ~ splines::bs(x, 4),
color = "red") +
facet_wrap(~effect)
# Compare to figure 2: the scale of the plot is different because he uses polarization