-
Notifications
You must be signed in to change notification settings - Fork 1
/
sde_small.jl
99 lines (75 loc) · 2.54 KB
/
sde_small.jl
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
#
# Generates sample paths to compare with ind-based simulation
#
using Parameters, Statistics, Random, LinearAlgebra, Distributions,
DifferentialEquations, StatsBase, StatsPlots, Plots, DataFrames, CSV, Optim
include("/home/bb/Gits/branching.brownian.motion.and.spde/sde_functions.jl")
j=1
k=1
# strengths of competition
cc = [1e-7,5e-6]
# for S = 1000
cc = [1e-8,5e-7]
# durations
TT = [1e3,5e3]
tspan = (0.0,TT[j])
# background parameters
S = 1000
w = fill(0.1, S) # niche breadths
U = fill(1.0, S) # total niche use
c = fill(cc[k],S) # strengths of competition
Ω = sum(U) # niche use scaling
η = fill(1.0, S) # segregation variances
μ = fill(1e-7, S) # mutation rates
V = fill(5.0, S) # magnitudes of drift
R = fill(1.0, S) # innate rate of growth
a = fill(1e-2,S) # strengths of abiotic selection
θ = fill(0.0, S) # phenotypic optima
pars = ModelParameters(S=S, w=w, U=U, η=η, c=c, a=a, μ=μ, V=V, R=R, θ=θ, Ω=Ω)
#
# find deterministic equilibrium
#
# approximation for equilibrium abundance
C = c.*.√( U.^2 ./ .√(4*π.*w) )
N₀ = Int64.(floor.( (R.-0.5*.√(μ.*a))./C ) )
# initial condition
u₀ = cat(θ,fill(10.0, S),fill(1000.0, S),dims=1)
# numerically solve SDE
prob = SDEProblem(f,g,u₀,tspan,pars)
sol = solve(prob)
# extract state variables
x̄_sol = copy(.-sol[(0*S+1):(1*S),:])
G_sol = copy(sol[(1*S+1):(2*S),:])
N_sol = copy(sol[(2*S+1):(3*S),:])
# build dataframes
spp = string("Species", 1)
df = DataFrame(spp = spp, x = x̄_sol[1,:], G = G_sol[1,:], N = N_sol[1,:], time = sol.t)
for i in 2:S
spp = string("Species", i)
df_dummy = DataFrame(spp = spp, x = x̄_sol[i,:], G = G_sol[i,:],
N = N_sol[i,:], time = sol.t)
global df = append!(df,df_dummy)
end
# weak competition
#CSV.write("/home/bob/Research/Branching Brownian Motion/sample_path_wc.csv", df)
# moderate competition
#CSV.write("/home/bob/Research/Branching Brownian Motion/sample_path_mc.csv", df)
# for S = 1000
LT = length(sol)
# extract state variables
x̄_sol = copy(.-sol[(0*S+1):(1*S),LT])
G_sol = copy(sol[(1*S+1):(2*S),LT])
N_sol = copy(sol[(2*S+1):(3*S),LT])
# build dataframes
spp = string("Species", 1)
df = DataFrame(spp = spp, x = x̄_sol[1], G = G_sol[1], N = N_sol[1])
for i in 2:S
spp = string("Species", i)
df_dummy = DataFrame(spp = spp, x = x̄_sol[i], G = G_sol[i],
N = N_sol[i])
global df = append!(df,df_dummy)
end
# weak competition
CSV.write("/home/bb/Gits/branching.brownian.motion.and.spde/sample_path_wc1e3.csv", df)
# moderate competition
#CSV.write("/home/bb/Gits/branching.brownian.motion.and.spde/sample_path_mc1e3.csv", df)