-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestJulia2.jl
executable file
·288 lines (227 loc) · 7.21 KB
/
testJulia2.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
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
println("hello world")
using Phylo, Random, Distributions
tree = rand(Ultrametric(2048, 10.0))
using Plots, Random
plot(tree)
@time rand!(BrownianTrait(tree, "Trait"), tree)
plot(tree, line_z="Trait")
using DiffEqFlux, DifferentialEquations, Plots
function BM!(du,u,t)
x = u
du = dx = 0.0
end
u0 = 0.0
tspan = (0.0,1.0)
function noise!(du,u,t)
x = u
du = 0.3
end
#= p = [1.5,1.0,3.0,1.0,0.3,0.3] =#
prob = SDEProblem(BM!, noise!,u0,tspan)
sol = solve(prob)
plot(sol)
#= ############################################################ =#
using StochasticDiffEq, StaticArrays
const μ = 0.5ones(4)
const σ = 0.1ones(4)
f(du,u,p,t) = du .= μ .* u
g(du,u,p,t) = du .= σ .* u
u0 = 0.1ones(4)
tspan = (0.0,1.0)
saveat = range(0.0,1.0,length=20)
prob = SDEProblem(f,g,u0,tspan)
@time for i in 1:100
sol = solve(prob,SRIW1(),adaptive=false,dt=saveat[2])
end
using StochasticDiffEq, StaticArrays
mu = 0.0
sigma = 0.1
alpha = 0.2
f(du,u,p,t) = du .= alpha * (mu .- u) + sigma .* u
u0 = 0.1
tspan = (0.0,1.0)
saveat = range(0.0,1.0,length=1000)
prob = SDEProblem(f,u0,tspan)
@time for i in 1:100
sol = solve(prob,SRIW1(),adaptive=false,dt=saveat[2])
end
#= ######################################################### =#
using DifferentialEquations, Plots
function drift(dx,x,p,t)
alpha, mu, sigma, tau = p;
dx = alpha * (mu - x)
end
function stoch(dx,x,p,t);
alpha, mu, sigma, tau = p;
dx = sqrt((2.0*sigma^2.0/tau))
end
x0 = 0.0
p = (0.3, 0.0, 1.5, 0.01);
tspan=(0.0, 50.0);
prob = SDEProblem(drift, stoch, x0, tspan, p)
sol = solve(prob); #= lots of errors here!! =#
#= ################################################################3 =#
using DifferentialEquations, Plots, DifferentialEquations.EnsembleAnalysis
function diff(x,p, t)
mu, sigma, alpha = p;
sigma
end
function drift(x, p, t)
mu, sigma, alpha = p;
alpha * (mu - x)
end
dt = 0.005
x0 = 0.0
p = (0.1, 1.0, 1.5);
tspan = (0.0, 1.0);
prob = SDEProblem(drift, diff, x0, tspan,p)
@time begin
sol = solve(prob,EM(), dt=dt)
end
#= 0.006506 seconds =#
plot(sol)
ensembleprob = EnsembleProblem(prob)
sol = solve(ensembleprob,EnsembleThreads(),trajectories=1000)
summ = EnsembleSummary(sol, 0.0:0.01:1.0)
plot(summ,labels="Middle 95%")
summ = EnsembleSummary(sol,0:0.01:1;quantiles=[0.25,0.75])
plot!(summ,labels="Middle 50%",legend=true)
#= ###############################################################33 =#
using Random, DifferentialEquations, Plots
mutable struct Node
name::Int64
parent::Union{Int64, Missing}
left::Union{Int64, String}
right::Union{Int64, String}
llength::Float64
rlength::Float64
lphenotype::Any
rphenotype::Any
rheight::Union{Float64, Missing}
lheight::Union{Float64, Missing}
end
tree = ["z", 1.0, [["a", 1.0, ["b", 1.0, "c", 1.0], 1.0], 1.0, ["d", 1.0, "e", 1.0], 1.0], 1.0]
tree = [["a", 1.0, "b", 1.0], 1.0, ["c", 1.0, "d", 1.0], 1.0]
tree = ["a", 1.0, "b", 1.0]
function unfold(tree)
treelist = []
index = 1
function recurse(tree, index)
if isa(tree[1], String) && isa(tree[3], String)
theNode = Node(index, missing, tree[1] , tree[3], tree[2], tree[4], [], [], missing, missing)
push!(treelist, theNode)
index+=1
elseif isa(tree[1], Vector) && isa(tree[3], String)
theNode = Node(index, missing, index+=1, tree[3], tree[2], tree[4], [], [], missing, missing)
push!(treelist, theNode)
recurse(tree[1], index)
elseif isa(tree[1], String) && isa(tree[3], Vector)
theNode = Node(index, missing, tree[1], index+=1, tree[2], tree[4], [], [], missing, missing)
push!(treelist, theNode)
recurse(tree[3], index)
elseif isa(tree[1], Vector) && isa(tree[3], Vector)
theNode = Node(index, missing, index+=1, index+=1, tree[2], tree[4], [], [], missing, missing)
push!(treelist, theNode)
recurse(tree[1], index)
recurse(tree[3], index)
end
end
recurse(tree, index)
treelist
end
treelist = unfold(tree)
##################################################################
using DifferentialEquations, Phylo, Plots; pyplot()
function mysim(tree, x0=0.0, t0 = 0.0)
## define the OU simulation
function OU(x0, tspan)
function drift(x, p, t)
mu, sigma, alpha = p;
alpha * (mu - x)
end
function diff(x,p, t)
mu, sigma, alpha = p;
sigma
end
dt = 0.005
p = (1, 0.3, 1.5);
prob = SDEProblem(drift, diff, x0, tspan,p)
sol = solve(prob, EM(), dt=dt)
end #OU
function Recurse!(tree, node)
if ismissing(node.inbound) ## the root node, to get started
for i in 1:length(node.other) ## 'other' means branches
node.other[i].data["1"] = ## "1" is a placeholder for the Dict
OU(x0, (t0, getheight(tree, node.other[i].inout[2])));
end
else
for i in 1:length(node.other) ## NB 'other' are branches here!
node.other[i].data["1"] =
OU(node.inbound.data["1"].u[end],
(getheight(tree, node), getheight(tree, node) +
node.other[i].length))
end
end
for i in 1:length(node.other)
if !isleaf(tree, node.other[i].inout[2])
Recurse!(tree, node.other[i].inout[2])
end
end
end# Recurse!
nodeInit = getroot(tree)
Recurse!(tree, nodeInit) # do the recursive simulations
tree
end # mysim
tr = Ultrametric(1024, 10.0);
tree = rand(tr);
plot(tree)
savefig("tree.png")
test = mysim(tree);
testbranches=getbranches(test);
plot(xlim=[0.0, 10.0], ylim= [-0.1, 2.0], legend=nothing)
for i in testbranches
plot!(i.data["1"].t, i.data["1"].u, legend= nothing)
end
current()
savefig("trace.png")
#####################################################################33
using Phylo, Plots
tree = parsenewick("((a:1.0, (b:1.0, c:1.0):1.0):1.0, d:1.0);")
tree = open(parsenewick, "tree.phy")
theRoot = getroot(tree)
theRoot.other[1].data["1"] = rand(10)
#######################################################################
## import Pkg; Pkg.add("PyPlot")
using DifferentialEquations, Phylo, Plots, Distributions, StatsPlots; pyplot()
alpha_vec = [5.0 5.0 3.0 4.0 2.0 1.0]
mu_vec = [0.0 0.0 0.0 0.0 0.0 0.0]
sigma_vec = [1.0 1.0 1.0 1.0 1.0 1.0]
function f(du, u, p, t)
du = alpha_vec .* (mu_vec .- u)
end # f
function g(du, u, p, t)
du= sigma_vec
end # g
rho = 0.0
mat = [1.0 rho rho rho rho rho;
rho 1.0 rho rho rho rho;
rho rho 1.0 rho rho rho;
rho rho rho 1.0 rho rho;
rho rho rho rho 1.0 rho;
rho rho rho rho rho 1.0]
time_tot = 1
tspan = (0.0, time_tot)
OU_noise = CorrelatedWienerProcess!(mat, tspan[1], zeros(6), zeros(6))
u0 = [0.0 0.0 0.0 0.0 0.0 0.0]
prob = SDEProblem(f,g,u0,tspan, noise=OU_noise)
dt = 1e-3
nums = Int(time_tot/dt) + 1
sol = solve(prob, dt=dt, adaptive=false);
plot(sol)
myt = sol.t
u1 = sol.u
uu1 = transpose(reshape(collect(Iterators.flatten(u1)), 2, nums))
plot(myt, uu1[1:nums,1], uu1[1:nums,2])
plot(uu1[1:nums,1], uu1[1:nums,2])
histogram(uu1[1:nums, 1])
plot(qqplot(Normal, uu1[1:nums, 1]))