scieee Science in your language
[en] (orig)

Clade Size Distributions Under the Coalescent Diversification Model

Author: Song, Yexuan; Colijn, Caroline; MacPherson, Ailene
Publisher: Zenodo
DOI: 10.5281/zenodo.17741526
Source: https://zenodo.org/records/17741526/files/CoalescentYule_11_21.pdf
Clade
Size
Coalescen
and
Yule
Models
P elimina ies
and
No a ion
Plo ing
Op ions
P elimina ies
Coalescen
Model
Simula ions
Simula ing
he
(Kingman)
coalescen
up
o
he
cu -off
ime.
Simula ing
a
coalescen
genealogy
wi h
ni n
lineages
up
o
ime
T
.
The
a iable
in S
is
dummy
a i-
able
o
index
he
s ochas ic
eplica e.
In[144]:=
Clea
[simCoal]
simCoal
[nIn_, T_, in S_] :=simCoal[nIn, T, in S]=Block[{ou , , Δ , λ, l1, l2, n},
(*Ini ialize*)
n=nIn;
ou =Table[{1},{i, 1, n}];
(*P in [ou ];*)
=0;
λ=Binomial[n, 2];
Δ =RandomVa ia e[Exponen ialDis ibu ion[λ]];
While[ +Δ <T && n >1,
= +Δ ;
(*Choose lineages o coalesce*)
{l1, l2}=RandomSample[Table[i, {i, 1, n}], 2];
(*Coalescence, add joined lineages and dele e pa en al lineages*)
AppendTo[ou , Join[ou 〚l1〛, ou 〚l2〛]];
ou =Dele e[ou , {{l1},{l2}}];
(*P in [ou ];*)
(*Upda e numbe o lineages and choose nex coalescence ime*)
n=n-1;
I [n≥2,
λ=Binomial[n, 2];
Δ =RandomVa ia e[Exponen ialDis ibu ion[λ]];
]
];
ou
]
Simula ing
nSim
eplica e
coalescen
simula ions.
In[146]:=
Clea
[simsC]
simsC
[n_, T_, nSims_] :=
simsC[n, T, nSims]=Table[simCoal[n, T, in S],{in S, 1, nSims}];
The
size
o
he
clades.
In[364]:=
simsSubCN
[n_, T_, in S_] :=Map[Map[Leng h[#] &, #] &, {simCoal[n, T, in S]}]
In[148]:=
simsCN
[n_, T_, nSims_] :=Map[Map[Leng h[#] &, #] &, simsC[n, T, nSims]]
2
Coalescen Yule_11_21.nb
Case
1:
A
andomly
sampled
clade
In[149]:=
(*Randomly sample a clade*)
simCase1
[n_, T_, nSims_] :=Block{sims, samples},
sims =simsCN[n, T, nSims];
(*lis o sample clade sizes*)
samples =Fla en[Map[RandomSample[#, 1]&, sims]];
Tablem, Leng h[Selec [samples, #m &]]
nSims ,{m, 1, n}

In[151]:=
SimPlo 1
[n_, T_] :=Lis Plo [simCase1[n, T, 50 000], Plo Range All, Filling Axis,
Plo S yle Black, Plo Ma ke s {●, Medium}, F ameLabel {"Clade Size", "P ob."}]
In[152]:=
SimPlo 1
[6, 1.0]
Ou [152]=
●●●●●
●
123456
0.00
0.05
0.10
0.15
0.20
0.25
0.30
Clade Size
P ob.
Case
2:
A
andomly
sampled
lineage
Randomly
sampling
a
lineage
is
analogous
o
andomly
sampling
a
clade
p opo ional
o
i s
size.
In[153]:=
simCase2
[n_, T_, nSims_] :=Block

{sims, samples},
sims =simsCN[n, T, nSims];
(*lis o sample clade sizes*)
samples =Fla en[Map[RandomSample[# #, 1]&, sims]];
Tablem, Leng h[Selec [samples, #m &]]
nSims ,{m, 1, n}

Coalescen Yule_11_21.nb
3
In[154]:=
Lis Plo [simCase2[6, 1, 10 000], Plo Range All,
Filling Axis, F ameLabel {"Clade Size", "P ob."}]
Ou [154]=
123456
0.00
0.05
0.10
0.15
0.20
0.25
0.30
Clade Size
P ob.
Case
3:
Randomly
sampling c
clades
(wi hou
eplacemen ).
In[155]:=
simCase3
[n_, T_, c_, nSims_] :=Block

{sims, samples, i},
sims =Selec [simsCN[n, T, nSims], Leng h[#] ≥c &];
(*Selec he simula ions wi h he minimum necessa y numbe o clades*)
samples =Map[So [RandomSample[#, c]] &, sims];
(*Randomly sample cclades and so as o ding does no ma e *)
TableM, Leng h[Selec [samples, #M &]]
nSims // N,{M, βLis 3[n, c]}

4
Coalescen Yule_11_21.nb
Case
4:
Randomly
sampling c
lineages
(wi hou
eplacemen ).
In[468]:=
Clea
[simSubCase4]
simSubCase4
[n_, T_, c_, in S_] :=
simSubCase4[n, T, c, in S]=Block[{sim, samples, i, s, emp, index},
samples = {};
sim =simsSubCN[n, T, in S]〚1〛;
(*P in [sim];*)
index =Table[i, {i, 1, Leng h[sim]}];
(*P in [index];*)
Fo [i=1, i ≤c, i++,(*Sampling o lineages*)
emp =RandomSample[sim index, 1]〚1〛;
(*P in [ emp];*)
sim〚 emp〛--;
(*Remo e sampled lineage om clade o esampling*)
AppendTo[samples, simsSubCN[n, T, in S]〚1, emp〛]
];
So [samples]
]
In[470]:=
emp4
[c_] :=Table[simSubCase4[6, 0.5, c, in S],{in S, 1, 20 000}];
es 4
[c_] :=TableM, Leng h[Selec [ emp4[c],#M &]]
Leng h
[ emp4[c]] // N,{M, βLis 4[6, c]}
In[428]:=
Clea
[simCase4]
simCase4
[n_, T_, c_, nSims_] :=Block{sim, samples, i, s, emp, index},
samples = {};
Fo [s=1, s ≤nSims, s++,
AppendTo[samples, {}];
sim =simsCN[n, T, nSims]〚s〛;
index =Table[i, {i, 1, Leng h[sim]}];
Fo [i=1, i ≤c, i++,(*Sampling o lineages*)
emp =RandomSample[sim index, 1]〚1〛;
sim〚 emp〛--;
(*Remo e sampled lineage om clade o esampling*)
AppendTo[samples〚-1〛, simsCN[n, T, nSims]〚s, emp〛]
];
];
TableM, Leng h[Selec [samples, #M &]]
nSims // N,{M, βLis 4[n, c]}

Coalescen Yule_11_21.nb
5

Clade
Size
Dis ibu ions
S ep
1
and
2
The
p obabili y
o
obse ing
k
ances o s
a
ime
T
gi en
ha
he e
a e
n
samples.
In[157]:=
g
[k_, n_, T_] :=I

k1,
1-SumExp[-Binomial[i, 2]T] (2 i -1) (-1)idF[n, i]
aF[n, i],{i, 2, n},
SumExp[-Binomial[i, 2]T] (2 i -1) (-1)i-kaF[k, i -1] × dF[n, i]
k! (i-k)! aF[n, i],{i, k, n}

An
al e na i e
exp ession
o
he
p obabili y
abo e.
In[158]:=
g2
[k_, n_, T_] :=I

k1,
1-SumExp[-Binomial[i, 2]T]P oduc Binomial[j, 2]
Binomial[j, 2]-Binomial[i, 2],
{j, Dele eCases[Table[x, {x, 2, n}], i]},{i, 2, n}
,1
Binomial[k, 2]Sum
Binomial[i, 2]Exp[-Binomial[i, 2]T]P oduc Binomial[j, 2]
Binomial[j, 2]-Binomial[i, 2],
{j, Dele eCases[Table[x, {x, k, n}], i]}

,{i, k, n}


The
numbe
o
ances o s
as
a
unc ion
o
ime
in
he
coalescen
model
6
Coalescen Yule_11_21.nb
In[159]:=
NumAncC
=
Show[Plo [{g[6, 6, ], g[5, 6, ], g[4, 6, ], g[3, 6, ], g[2, 6, ], g[1, 6, ]},
{ , 0, 2}, Plo Range All, Plo S yle MyCol2],
(*Plo [To al[{g[6,6, ],g[5,6, ],g[4,6, ],g[3,6, ],g[2,6, ],g[1,6, ]}],
{ ,0,2},Plo S yleDi ec i e[Black,Dashed]],*)
ImageSize Medium, F ameLabel {"Time", "P ob"}]
Ou [159]=
0.0 0.5 1.0 1.5 2.0
0.0
0.2
0.4
0.6
0.8
1.0
Time
P ob
S ep
1:
Va ying
popula ion
size.
In[]:=
Le
N
(τ)
be
he
popula ion
size
a
ime
τ
whe e
ime
is
measu ed
in
uni s
o
N
e
gene a ions.
The
p obabili y
ha
n
lineages
ha e
k
pa en s
in
a
popula ion
o
changing
size
is
gi en
by:
Th ee
example
popula ion
dynamic
ajec o ies
ha
ha e
he
same
size
a
ime
τmax
in
he
pas .
Coalescen Yule_11_21.nb
7
In[160]:=
N e
=5;
Ndyn0
[τ_,Τmax_] :=5Exp[0(τ-Τmax)]
N e
Ndyn1
[τ_,Τmax_] :=5Exp[-0.2 (τ-Τmax)]
N e
Ndyn2
[τ_,Τmax_] :=5Exp[0.2 (τ-Τmax)]
N e
In[164]:=
Plo [{Ndyn0[τ2, 2.0], Ndyn1[τ2, 2.0], Ndyn2[τ2, 2.0]},
{τ2
,
0
,
2}
,
Plo S yle
{{Black}
,
{Black
,
Dashed}
,
{Black
,
Do ed}}]
Ou [164]=
0.0 0.5 1.0 1.5 2.0
0.8
1.0
1.2
1.4
Calcula ing
he
ha monic
mean
size
o e
ime
and
ep esen ing
he
esul
as
a
in e pola ing
unc ion.
In[165]:=
Clea
[Nh]
Nh
[Τmax_, Ndyn_] :=Nh[Τmax, Ndyn]=Block{ou , Δ , emp},
Δ =Τmax
100.
;
emp =TableΔ , 1
Ndyn[ , Τmax]Δ ,{ , 0, Τmax, Δ };
emp =P ependTo[ emp, {0, 0}];
In e pola ion[Accumula e[ emp]]

8
Coalescen Yule_11_21.nb
In[167]:=
Clea
[gTilde]
gTilde
[k_, n_, T_, Ndyn_, Tmax_] :=gTilde[k, n, T, Ndyn, Tmax]=Block{nRe },
I k1,
1-
SumExp[-Binomial[i, 2]Nh[Tmax, Ndyn][T]] (2 i -1) (-1)idF[n, i]
aF[n, i],{i, 2, n},
Sum
Exp[-Binomial[i, 2]Nh[Tmax, Ndyn][T]] (2 i -1) (-1)i-kaF[k, i -1] × dF[n, i]
k! (i-k)! aF[n, i],
{i, k, n}

In[]:=
Clea
[NumAncCDyn]
In[169]:=
NumAncCDyn
[Ndyn_, s y_] :=
Show[Plo [E alua e[Table[gTilde[k, 6, , Ndyn, 2.0],{k, {6, 5, 4, 3, 2, 1}}]],
{ , 0, 2}, Plo Range {0, 1}, Plo S yle Map[{#, s y}&, MyCol2]],
ImageSize
Medium
,
F ameLabel {"Time"
,
"P ob"}]
In[170]:=
Show
[NumAncCDyn[Ndyn0, Au oma ic],
NumAncCDyn
[
Ndyn1
,
Dashed]
,
NumAncCDyn
[
Ndyn2
,
Do ed]]
Ou [170]=
0.0 0.5 1.0 1.5 2.0
0.0
0.2
0.4
0.6
0.8
1.0
Time
P ob
S ep
1:
The
SIS
Model
Calcula ing
he
dynamics
o
an
SIRS
model
In[]:=
Clea
[pa s]
Pick
wo
se s
o
pa ame e s
ha
ha e
he
same
endemic
ou b eak
size
bu
e y
diffe en
ansmission
Coalescen Yule_11_21.nb
9
In[200]:=
leg2
=Swa chLegend[MyCol2, Table[m, {m, 1, 5}],
Backg ound Di ec i e[Whi e, Opaci y[0.7]],
LegendLabel S yle["Mu . Coun , m", Bold]];
Show
Plo E alua eTableTes 1[m, 10, ]
To [m, 10, 2.0]+Tes 1[10 -m, 10, ]
To [10 -m, 10, 2.0],m, 1, 10
2,
{ , 0, 2}, Plo Range All, Plo S yle MyCol2, ImageSize Medium,
F ameLabel  "Time, T", "P (T m)",(*Plo Label
"Coalescen Model (Case 1)",*)Epilog Inse [leg2, Scaled[{0.85, 0.5}]],
(*Lis LinePlo [Table[{{T,0},{T,1.0}},{T,{0.5,1.0,1.5}}],
Plo S yleLigh e [G ay]],*)Aspec Ra io 0.5
Expo [Di <> "Tes 2.
jpeg
"
,
%]
;
Ou [200]=
0.0 0.5 1.0 1.5 2.0
0.0
0.5
1.0
1.5
2.0
2.5
Time, T
P (T m)
Mu . Coun , m
1
2
3
4
5
Case
2:
A
andomly
sampled
lineage
The
p obabili y
o
andomly
sampling
a
lineage
om
a
clade
o
size
m
a
ime
T.
In[202]:=
P 2[m_, n_, T_] :=
SumSumm
n[x, n]〚m〛×g[n-kp, n, T] × P 0[x, n, kp],{x, XTilde[n-kp, n]},
{kp, 0, n -1}

16
Coalescen Yule_11_21.nb

Case
2
e sus
Case
1
In[203]:=
leg
=Swa chLegend[MyCol2, Table[m, {m, 1, 6}], Backg ound 
Di ec i e[Whi e, Opaci y[0.7]], LegendLabel S yle["Clade Size", Bold]];
leg2
=LineLegend[
{Di ec i e[Black, Thick, Opaci y[0.3]], Di ec i e[Black, Thick, Dashed]},
{"Case 1", "Case 2"}, Backg ound Di ec i e[Whi e, Opaci y[0.9]]
(*,LegendLabelS yle["Clade Size",Bold]*)]
Show
[
Plo [E alua e[Table[P 2[m, 6, ],{m, 1, 6}]],{ , 0, 2}, Plo Range All,
Plo S yle Map[Di ec i e[#, Dashed]&, MyCol2], ImageSize Medium],
Plo [E alua e[Table[P 1[m, 6, ],{m, 1, 6}]],{ , 0, 2}, Plo Range All,
Plo S yle Map[Di ec i e[#, Opaci y[0.3]] &, MyCol2], ImageSize Medium],
Lis LinePlo [{{{0.5, 0},{0.5, 1}},{{1.0, 0},{1.0, 1}}},
Plo S yle Ligh e [G ay]], Epilog Inse [leg2, Scaled[{0.8, 0.85}]]]
Expo [Di <> "CoalCase2_A.jpeg", %];
Ou [204]=
Case 1
Case 2
Ou [205]=
0.0 0.5 1.0 1.5 2.0
0.0
0.2
0.4
0.6
0.8
1.0
Case 1
Case 2
In[207]:=
Case12Ba
[T_, colx_, coly_] :=Show[Ba Cha [Table[P 1[m, 6, T],{m, 1, 6}],
Cha S yle Map[Di ec i e[#, Opaci y[0.3]] &, MyCol2],
Plo Range {0, 0.5}, Aspec Ra io 1, ImageSize Small],
Show[Lis Plo [Table[{{m, P 2[m, 6, T]}},{m, 1, 6}],
Plo S yle MyCol2, Plo Ma ke s {●, Medium}],
Lis LinePlo [Table[{{m, 0},{m, P 2[m, 6, T]}},{m, 1, 6}],
Plo S yle Map[Di ec i e[#, Dashed]&, MyCol2]]],
F ameTicksS yle {{coly(*Di ec i e[Whi e,Opaci y[0.0],12]*), False},
{colx (*Di ec i e[Black,12]*), False}}]
Coalescen Yule_11_21.nb
17
In[208]:=
Case12Ba
[0.5, Di ec i e[Whi e, Opaci y[0.0], 12], Di ec i e[Black, 12]]
Expo [Di <> "CoalCase2
_
B.jpeg", %];
Ou [208]=
●●
●● ●●
●●
●●
●●
0.0
0.1
0.2
0.3
0.4
0.5
In[210]:=
Case12Ba
[1.0, Di ec i e[Black, 12], Di ec i e[Black, 12]]
Expo [Di <> "CoalCase2_C.jpeg", %];
Ou [210]=
●●
●● ●● ●● ●●
●●
123456
0.0
0.1
0.2
0.3
0.4
0.5
Valida ing
he
analy ical
esul s
wi h
he
simula ions
In[212]:=
alid2
[n_, T_] :=Show[Ba Cha [Table[P 2[m, n, T],{m, 1, n}],
Cha S yle Map[Di ec i e[#, Opaci y[0.75]] &, MyCol2],
Plo Range {0, T}, Aspec Ra io 1, ImageSize Small],
Lis Plo [simCase2[n, T, 50 000], Plo Range All,
Filling Axis, Plo S yle Black, Plo Ma ke s {●, Medium}]]
18
Coalescen Yule_11_21.nb
In[213]:=
G aphicsRow
[{ alid2[6, 0.5], alid2[6, 1.0], alid2[6, 1.5]}]
Expo [Di <> "CoalCase2
_
Valid.jpeg", %];
Ou [213]=
●
●●
●
●
●
1 2 3 4 5 6
0.0
0.1
0.2
0.3
0.4
0.5
●●●●●
●
1 2 3 4 5 6
0.0
0.2
0.4
0.6
0.8
1.0
●●●●●
●
1 2 3 4 5 6
0.0
0.2
0.4
0.6
0.8
1.0
1.2
1.4
Case
3:
Randomly
sampling c
clades
wi hou
eplacemen
In[215]:=
3[x_, C_, n_, k_] :=
P oduc [dF[[x, n]〚i〛,ℬ[C, n]〚i〛],{i, 1, n}]
dF[k
,
Leng h
[C]]
Leng h[C]!
P oduc [ℬ[C
,
n]〚i〛!
,
{i
,
1
,
n}]
In[216]:=
P 3[C_, n_, T_] :=
Sum[Sum[ 3[x, C, n, n -kp] × g[n-kp, n, T] × P 0[x, n, kp],{x, XTilde[n-kp, n]}],
{
kp
,
0
,
n-
Leng h
[
C
]
}
]
Case
3
In[217]:=
Plo [{P 3[{1, 1, 2}, 6, ], P 3[{2, 2}, 6, ], P 3[{3, 3}, 6, ]},
{
,
0
,
2
}
,
Plo Range
All
,
Plo S yle

CompCol
]
Ou [217]=
0.0 0.5 1.0 1.5 2.0
0.00
0.05
0.10
0.15
0.20
0.25
0.30
0.35
Coalescen Yule_11_21.nb
19
Case
3
s.
Case
1
In[]:= ?
LineLegend
Ou [
] =
Symbol
LineLegend [{col1,…},{lbl1,…}] gene a es a legend ha associa es colo col iwi h label lbl i.
LineLegend [{col1,…}, Au oma ic ]gene a es a legend wi h placeholde labels o he colo s col i.
LineLegend [{lbl1,…}] ep esen s a legend wi h inhe i ed colo s wi hin isualiza ion unc ions.
In[218]:=
legC
=LineLegendFla en[
Map[{Di ec i e[#, Dashed], Di ec i e[#, Opaci y[0.5], Thick]} &, CompCol]],
"P ((2, 2)6, T)", "P (2 6, T)2", "P ((3, 3)6, T)",
"P (3 6, T)2", "P ((2, 2, 2)6, T)", "P (2 6, T)3",
"P ((2, 3)6, T)", "P (2 6, T)P (3 6, T)",
Backg ound Di ec i e[{Whi e, Opaci y[0.95]}], ImageSize {110, Au oma ic}
Expo
[
Di <> "CoalCase13
_
Leg
.
jpeg
"
,
%
]
;
Ou [218]=
P ((2, 2)6, T)
P (2 6, T)2
P ((3, 3)6, T)
P (3 6, T)2
P ((2, 2, 2)6, T)
P (2 6, T)3
P ((2, 3)6, T)
P (2 6, T)P (3 6, T)
20
Coalescen Yule_11_21.nb
In[220]:=
Case13
=Show
Plo [{P 3[{2, 2}, 6, ], P 3[{3, 3}, 6, ], P 3[{2, 2, 2}, 6, ], P 3[{2, 3}, 6, ]},
{ , 0, 2}, Plo Range All, Plo S yle Map[Di ec i e[{#, Dashed}] &, CompCol]],
Plo P 1[2, 6, ]2, P 1[3, 6, ]2, P 1[2, 6, ]3, P 1[2, 6, ] × P 1[3, 6, ],{ , 0,
2}, Plo Range All, Plo S yle Map[Di ec i e[{#, Opaci y[0.5]}] &, CompCol]
(*,EpilogInse [legC,Scaled[{0.8,0.65}]]*)
Expo [Di <> "CoalCase13.jpeg", %];
Ou [220]=
0.0 0.5 1.0 1.5 2.0
0.00
0.02
0.04
0.06
0.08
0.10
Coalescen Yule_11_21.nb
21

Valida ion
wi h
simula ions
In[222]:=
emp
=simCase3[6, 0.5, 2, 50 000]〚;; , 2〛;
Valid3a
=Show[Ba Cha [Map[P 3[#, 6, 0.5]&, βLis 3[6, 2]],
Cha S yle Di ec i e[CompCol〚1〛, Opaci y[0.5]]],
Lis Plo [Table[{j, emp〚j〛},{j, 1, Leng h[ emp]}],
Plo S yle CompCol〚1〛, Plo Ma ke s {●, Medium}],
Lis LinePlo [Table[{{j, 0},{j, emp〚j〛}},{j, 1, Leng h[ emp]}],
Plo S yle Di ec i e[CompCol〚1〛, Dashed], Plo Range All],
F ameTicks {{T ue, False},{ ikLis [βLis 3[6, 2]], False}}]
Expo [Di <> "CoalCase3_Valida.jpeg", %];
Ou [223]=
●
●
●
●
●
●
●
●
●
(1, 1)
(1, 2)
(1, 3)
(1, 4)
(1, 5)
(2, 2)
(2, 3)
(2, 4)
(3, 3)
0.00
0.05
0.10
0.15
22
Coalescen Yule_11_21.nb
In[225]:=
emp
=simCase3[6, 0.5, 3, 50 000]〚;; , 2〛;
Valid3b
=Show[Ba Cha [Map[P 3[#, 6, 0.5]&, βLis 3[6, 3]],
Cha S yle Di ec i e[CompCol〚2〛, Opaci y[0.5]]],
Lis Plo [Table[{j, emp〚j〛},{j, 1, Leng h[ emp]}],
Plo S yle CompCol〚2〛, Plo Ma ke s {●, Medium}],
Lis LinePlo [Table[{{j, 0},{j, emp〚j〛}},{j, 1, Leng h[ emp]}],
Plo S yle Di ec i e[CompCol〚2〛, Dashed], Plo Range All],
F ameTicks {{T ue, False},{ ikLis [βLis 3[6, 3]], False}}]
Expo [Di <> "CoalCase3_Validb.jpeg", %];
Ou [226]=
●
●●
●
●
●
●
(1, 1, 1)
(1, 1, 2)
(1, 1, 3)
(1, 1, 4)
(1, 2, 2)
(1, 2, 3)
(2, 2, 2)
0.00
0.05
0.10
0.15
0.20
0.25
Coalescen Yule_11_21.nb
23
In[228]:=
emp
=simCase3[6, 0.5, 4, 50 000]〚;; , 2〛;
Valid3c
=Show[Ba Cha [Map[P 3[#, 6, 0.5]&, βLis 3[6, 4]],
Cha S yle Di ec i e[CompCol〚3〛, Opaci y[0.5]]],
Lis Plo [Table[{j, emp〚j〛},{j, 1, Leng h[ emp]}],
Plo S yle CompCol〚3〛, Plo Ma ke s {●, Medium}],
Lis LinePlo [Table[{{j, 0},{j, emp〚j〛}},{j, 1, Leng h[ emp]}],
Plo S yle Di ec i e[CompCol〚3〛, Dashed], Plo Range All],
F ameTicks {{T ue, False},{ ikLis [βLis 3[6, 4]], False}}]
Expo [Di <> "CoalCase3_Validc.jpeg", %];
Ou [229]=
●
●
●
●
(1, 1, 1, 1)
(1, 1, 1, 2)
(1, 1, 1, 3)
(1, 1, 2, 2)
0.00
0.02
0.04
0.06
0.08
0.10
Case
4:
Randomly
sampling
c
lineages
wi hou
eplacemen
In[]:= (* 4[x_,C_,n_]:=Block[{iLis ,c},
c=Leng h[C];
iLis =Table[Leng h[Selec [C,#C〚i〛&]],{i,1,c}];
P oduc [Binomial[[x,n]〚i〛,ℬ[C,n]〚i〛],{i,1,n}]
P oduc [Binomial[C〚j〛,iLis 〚j〛],{j,1,c}]/Binomial[n,c]
]*)
In[231]:=
4[x_, C_, n_] :=Block{iLis , c},
c=Leng h[C];
P oduc [dF[i[x, n]〚i〛,ℬ[C, n]〚i〛],{i, 1, n}]
P oduc [ℬ[C, n]〚i〛!,{i, 1, n}]
Leng h[C]!
dF[n, Leng h[C]]

In[234]:=
P 4[C_, n_, T_] :=Sum[
Sum[ 4[x, C, n] × g[n-kp, n, T] × P 0[x, n, kp],{x, XTilde[n-kp, n]}],{kp, 0, n}]
24
Coalescen Yule_11_21.nb
In[235]:=
Show
[
Plo [{P 3[{1, 1, 2}, 6, ], P 3[{2, 2}, 6, ], P 3[{3, 3}, 6, ]},{ , 0, 2},
Plo Range All, Plo S yle Map[Di ec i e[#, Dashed]&, CompCol]],
Plo [{P 4[{1, 1, 2}, 6, ], P 4[{2, 2}, 6, ], P 4[{3, 3}, 6, ]},{ , 0, 2},
Plo Range All, Plo S yle Map[Di ec i e[#, Opaci y[0.3], Thick]&, CompCol]]]
Expo [Di <> "CoalCase34.jpeg", %];
Ou [235]=
0.0 0.5 1.0 1.5 2.0
0.00
0.05
0.10
0.15
0.20
0.25
0.30
0.35
In[237]:=
legD
=LineLegendFla en[Map[
{Di ec i e[#, Dashed], Di ec i e[#, Opaci y[0.5], Thick]} &, CompCol〚1 ;; 3〛]],
"P ((1, 1, 2)6, T)", "P 3((1, 1, 2)6, T)", "P ((2, 2)6, T)",
"P 2((2, 2)6, T)", "P ((3, 3)6, T)", "P 2((3, 3)6, T)",
Backg ound Di ec i e[{Whi e, Opaci y[0.95]}], ImageSize {110, Au oma ic}
Expo [Di <> "CoalCase34
_
Leg
.
jpeg
"
,
%]
;
Ou [237]=
P ((1, 1, 2)6, T)
P 3((1, 1, 2)6, T)
P ((2, 2)6, T)
P 2((2, 2)6, T)
P ((3, 3)6, T)
P 2((3, 3)6, T)
In[239]:=
CLis
= {{1, 1, 2},{2, 2},{3, 3}};
Coalescen Yule_11_21.nb
25
In[301]:=
leg3
=ShowPlo iDynτ,
Max
iEqu /. pa s1/. pa s1,
τ, 0, Max
iEqu /. pa s1, Plo Range All, Plo S yle Black,
Plo iDynτ, Max
iEqu /. pa s2/. pa s2, τ, 0, Max
iEqu /. pa s2,
Plo Range All, Plo S yle Di ec i e[Black, Dashed],
Backg ound Di ec i e[Whi e, Opaci y[0.8]], Axes False,
F ameTicks {{{0.5, 1.0}, False},{{0, 1.0, 2.0}, False}},
F ameTicksS yle Di ec i e[Black, 7],
F ameLabel {S yle["(Coalescen )Time", 8], S yle["(Scaled)P e alence", 8]},
ImageSize {150, 100}
Ou [301]=
0 1. 2.
0.5
1.
(Coalescen )Time
(Scaled)P e alence
Plo
o
he
a io
o
ha monic
mean
p e alence
In[332]:=
Lis LinePlo


Tableτ2,
1
NIn eg a e1
iDynτ, Max
iEqu /.pa s1/.pa s1
,{τ,0,τ2}
1
NIn eg a e1
iDynτ, Max
iEqu /.pa s2/.pa s2
,{τ,0,τ2}
,τ2, 0.1, Max
iEqu /. pa s1, 0.1,
F ameLabel {"Time", "Ra io o Ha monic Mean P e elances"}

Ou [332]=
0.0 0.5 1.0 1.5 2.0
1.05
1.10
1.15
1.20
1.25
1.30
Time
Ra io o Ha monic Mean
P e elances
32
Coalescen Yule_11_21.nb

In[302]:=
Show

(*La ge Ou b eak*)
Lis LinePlo TableTable{ , P 4SIR[m, 6, , pa s1]},
 , 0, Max
iEqu /. pa s1, Max
iEqu 50 /. pa s1,{m, 1, 6}, Plo Range All,
Plo S yle Map[{#, Au oma ic}&, MyCol2], ImageSize Medium,
(*Mild Ou b eak*)
Lis LinePlo TableTable{ , P 4SIR[m, 6, , pa s2]},
 , 0, Max
iEqu /. pa s2, Max
iEqu 50 /. pa s2,{m, 1, 6}, Plo Range All,
Plo S yle Map[{#, Dashed}&, MyCol2], ImageSize Medium,
(*Time poin s*)
Lis LinePlo [
{{{0.5, 0},{0.5, 1.0}},{{1.25, 0},{1.25, 1.0}}}, Plo S yle G ay],
Epilog Inse [leg3, Scaled[{0.38, 0.75}]]

Expo [Di <> "CoalCase5_SIR.jpeg", %];
Ou [302]=
0.0 0.5 1.0 1.5 2.0
0.0
0.2
0.4
0.6
0.8
1.0
0 1. 2.
0.5
1.
(Coalescen )Time
(Scaled)P e alence
Coalescen Yule_11_21.nb
33
In[304]:=
Case5SIRBa
[T_, pa sA_, pa sB_, colx_, coly_] :=
Show[Ba Cha [Table[P 4SIR[m, 6, T, pa sA],{m, 1, 6}],
Cha S yle Map[Di ec i e[#, Opaci y[0.3]] &, MyCol2],
Plo Range {0, 1.0}, Aspec Ra io 1, ImageSize Small],
Show[Lis Plo [Table[{{m, P 4SIR[m, 6, T, pa sB]}},{m, 1, 6}],
Plo S yle MyCol2, Plo Ma ke s {●, Medium}],
Lis LinePlo [Table[{{m, 0},{m, P 4SIR[m, 6, T, pa sB]}},{m, 1, 6}],
Plo S yle Map[Di ec i e[#, Dashed]&, MyCol2], Plo Range All]],
F ameTicksS yle {{coly(*Di ec i e[Whi e,Opaci y[0.0],12]*), False},
{colx (*Di ec i e[Black,12]*), False}}]
In[305]:=
Case5SIRBa
[0.5, pa s1, pa s2,
Di ec i e[Whi e, Opaci y[0.0], 12], Di ec i e[Black, 12]]
Expo
[
Di <> "CoalCase5
_
SIR
_
B.
jpeg
"
,
%
]
;
Ou [305]=
●● ●● ●● ●● ●● ●●
0.0
0.2
0.4
0.6
0.8
1.0
In[307]:=
Case5SIRBa
[1.25, pa s1, pa s2, Di ec i e[Black, 12], Di ec i e[Black, 12]]
Expo [Di <> "CoalCase5
_
SIR
_
C.
jpeg
", %];
Ou [307]=
●● ●● ●● ●● ●●
●●
123456
0.0
0.2
0.4
0.6
0.8
1.0
Yule
34
Coalescen Yule_11_21.nb
De i ing
The
Numbe
o
Ances o s
a
Time
T
P ob.
o
k
ances o s
a
ime
T
De i ing
Mas e
Equa ions
o
he
numbe
o
ances o s
In[]:= F[k_,τ_, n_] :=
I [kn
,
-λk P[k
,
τ]
,
I [k1
,
λ(k+1)P[k+1
,
τ]
,
-λk P[k
,
τ]+λ(k+1)P[k+1
,
τ]]]
In[]:=
equs
[n_] :=Join[Table[D[P[k, τ],τ]F[k, τ, n],{k, 1, n}],
Table[P[k, 0]I [kn, 1, 0],{k, 1, n}]]
In[]:=
a s
[n_] :=Table[P[k, τ],{k, 1, n}]
Semi-analy ical
solu ion
gi en
n
.
In[]:=
sol
[n_] :=DSol e[equs[n], a s[n],τ]
In[]:=
sol
[2]
Ou [
] =
P[1, τ] -2λ τ -1+2λ τ, P[2, τ] -2λ τ
In[]:=
sol
[3]
Ou [

] =


P[1, τ] -3λ τ

-1+λ τ

2

2+λ τ

, P[2, τ]3-3λ τ

-1+λ τ

, P[3, τ] -3λ τ


In[]:=
sol
[4]
Ou [
] =
P[1, τ] -4λ τ -1+λ τ
3
3+λ τ,
P[2, τ]6-4λ τ

-1+λ τ

2, P[3, τ]4-4λ τ

-1+λ τ

, P[4, τ] -4λ τ


In[]:=
sol
[6]
Ou [

] =
P[1, τ] -6λ τ -1+λ τ
5
5+λ τ,
P[2, τ]15 -6λ τ -1+λ τ4, P[3, τ]20 -6λ τ -1+λ τ3,
P[4, τ]15 -6λ τ

-1+λ τ

2, P[5, τ]6-6λ τ

-1+λ τ

, P[6, τ] -6λ τ


Nume ical
Solu ion
Coalescen Yule_11_21.nb
35
In[]:=
empN
=NDSol e[equs[6] /.λ1.0, a s[6],{τ, 0, 1.0}]〚1〛;
NumPlo
=Plo [{P[6, τ] /. empN, P[5, τ] /. empN,
P[4, τ] /. empN, P[3, τ] /. empN, P[2, τ] /. empN, P[1, τ] /. empN,
P[6, τ]+P[5, τ]+P[4, τ]+P[3, τ]+P[2, τ]+P[1, τ] /. empN},
{τ, 0, 1}, Plo S yle Join[Map[Di ec i e[#, Thickness[0.003]] &, MyCol],
{Di ec i e[Black, Dashed]}]]
Ou [

] =
0.0 0.2 0.4 0.6 0.8 1.0
0.0
0.2
0.4
0.6
0.8
1.0
Analy ical
(Sequen ial)
Solu ion
In[]:=
DSol e
[{D[P[n, τ],τ]-λn P[n, τ], P[n, 0]1},{P[n, τ]},τ]
Ou [
] =


P[n, τ] -nλ τ


In[]:=
DSol e
D[P[n-1, τ],τ]-λ(n-1)P[n-1, τ]+λn-nλ τ, P[n-1, 0]0,
{P[n-1, τ]},τ

// Simpli y
Ou [

] =


P[-1+n, τ] -nλ τ

-1+λ τ

n


In[]:=
DSol e
D[P[n-2, τ],τ]-λ(n-2)P[n-2, τ]+λ(n-1)-nλ τ -1+λ τn,
P[n-2, 0]0

,{P[n-2, τ]},τ

// Simpli y
Ou [

] =
P[-2+n, τ]1
2
-nλ τ -1+λ τ2(-1+n)n
Gene al
solu ion
o
k
>
n
In[]:= P Tes [n_, kp_,λ_,τ_] :=
1
kp
!dF[n, kp]Exp[-nλ τ] (Exp[λ τ]-1)kp
In[]:= P Tes [n, n -2, λ,τ]//Simpli y
Ou [
] =
-nλ τ -1+λ τ-
2
+nn!
2
(
-2+n
)
!
36
Coalescen Yule_11_21.nb
In[]:=
DSol e
D[P[1, τ],τ] λ 2-nλ τ -1+λ τ-
2
+nn!
2(-2+n)! /.n6, P[1, 0]0,
{P[1, τ]},τ

// Simpli y
Ou [

] =


P[1, τ] -6λ τ

-1+λ τ

5

5+λ τ



Gene al
solu ion
o
k
1
In[]:= P Tes 2[n_, kp_,λ_,τ_] :=I

kp n-1,
-nλ τ -1+λ τ(n-1)(n-1)+λ τ,1
kp
!dF[n, kp]Exp[-nλ τ] (Exp[λ τ]-1)kp
Nume ical
check
In[]:=
Show
[NumPlo ,
Plo [{P Tes 2[6, 0, 1.0, τ], P Tes 2[6, 1, 1.0, τ], P Tes 2[6, 2, 1.0, τ],
P Tes 2[6, 3, 1.0, τ], P Tes 2[6, 4, 1.0, τ], P Tes 2[6, 5, 1.0, τ]},{τ, 0, 1.0},
Plo S yle Map[Di ec i e[#, Dashed, Thick]&, MyCol], Plo Range All]]
Ou [

] =
0.0 0.2 0.4 0.6 0.8 1.0
0.0
0.2
0.4
0.6
0.8
1.0
Simula ion
Tes
Simula ing
he
Yule
(coalescen )
up
o
cu -off
ime
T
.
Case
1:
A
andomly
sampled
clade
Case
2:
A
andomly
sampled
lineage
Case
3:
Randomly
sampling
c
clades
(wi hou
eplacemen ).
Case
4:
Randomly
sampling c
lineages
(wi hou
eplacemen ).
Coalescen Yule_11_21.nb
37

Clade
Size
Dis ibu ions
S ep
1
and
2:
◼
P
(mλ,T):
The
p obabili y
o
a
clade
o
size
m
descending
om
an
ances o
a
ime
T
.
In[]:= P Y[m_,λ_, T_] :=Exp[-λT] (1-Exp[-λT])m-1
In[]:= Plo E alua e[Table[P Y[m, 1.0, ],{m, 1, 6}]],{ , 0, 2.0}, Plo Range All,
F ameLabel  "Time", "Clade Size (k1)", Plo S yle MyCol2
Ou [
] =
0.0 0.5 1.0 1.5 2.0
0.0
0.2
0.4
0.6
0.8
1.0
Time
Clade Size (k1)
P (m n,T)
x
P (m x,n,T)
k1
n
P (x k,n,T)P (k n,T)
◼P (k n,T):
The
p obabili y
ha
he
Yule
p ocess
wi h
n
lineages
a
he
p esen
day
has
k
ances o s
a
ime
T
be o e
he
p esen
day
is:
In[]:= P Yk[k_, n_,λ_, T_] :=I

k1, -nλT-1+λT(n-1)(n-1)+λT,
1
(
n-k
)
!dF[n, (n-k)] Exp[-nλT] (Exp[λT]-1)(n-k)
◼P (x k,n,T):
Nex
we
wish
o
calcula e
he
p obabili y
o
obse ing
a
gi en
pa i ion
x
o
he
n
axa
a
ime
T
gi en
ha
he e
a e
k
ances o s
a
ha
ime.
In[]:=
Clea
[P Y0]
In[]:= P Y0[x_, k_, n_] :=
k!
Binomial
[
n-1
,
k-1
]
P oduc
[

[
x
,
n
]
〚
i
〛
!
,
{
i
,
1
,
Leng h
[

[
x
,
n
]
]
}
]
Case
1:
A
andomly
sampled
clade
38
Coalescen Yule_11_21.nb
Case
2:
A
andomly
sampled
lineage
Case
3:
Randomly
sampling
mul iple
clades
(wi hou
eplacemen )
Case
4:
Randomly
sampling
mul iple
lineages
(wi hou
eplacemen )
Coalescen
Yule
Compa ison
Time
o
he
mos
ecen
common
ances o
Compa ing
he
imes
o
common
ances y
in
he
Yule
e sus
Coalescen
Models.
In[]:= Plo {g[1, 6, ], P Yk[1, 6, 1.0, ]},{ , 0, 2.0},
Plo S yle {G ay, Black}, F ameLabel  "Time, T", "P (TMRCA ≤T)",
Epilog Inse LineLegend{G ay, Black},

"Coalescen ", "Yule (λ1.0)"


, Scaled[{0.25, 0.75}]


Ou [

] =
0.0 0.5 1.0 1.5 2.0
0.0
0.2
0.4
0.6
0.8
Time, T
P (TMRCA ≤T)
Coalescen
Yule (λ  1.0)
Compa ison
o
Coalescen
and
Yule
Clade-Size
Dis ibu ions
Densi y-Dependence
In[]:=
λ
Comp =1.0;
Coalescen Yule_11_21.nb
39
In[]:=
G aphicsRow
[{Show[
Plo [{P 3[{1, 1, 2}, 6, ], P 3[{2, 2}, 6, ], P 3[{3, 3}, 6, ]},{ , 0, 2},
Plo Range All, Plo S yle Map[Di ec i e[#, Dashed]&, CompCol]],
Plo [{P 4[{1, 1, 2}, 6, ], P 4[{2, 2}, 6, ], P 4[{3, 3}, 6, ]},
{ , 0, 2}, Plo Range All,
Plo S yle Map[Di ec i e[#, Opaci y[0.3], Thick]&, CompCol]]], Show[
Plo [{P Y3[{1, 1, 2},6,λComp, ],
P Y3[{2, 2},6,λComp, ], P Y3[{3, 3},6,λComp, ]},{ , 0, 2},
Plo Range All, Plo S yle Map[Di ec i e[#, Dashed]&, CompCol]],
Plo [{P Y4[{1, 1, 2},6,λComp, ], P Y4[{2, 2},6,λComp, ],
P Y4[{3, 3},6,λComp, ]},{ , 0, 2}, Plo Range All,
Plo S yle Map[Di ec i e[#, Opaci y[0.3], Thick]&, CompCol]]]}]
Ou [

] =
0.0 0.5 1.0 1.5 2.0
0.00
0.05
0.10
0.15
0.20
0.25
0.30
0.35
0.0 0.5 1.0 1.5 2.0
0.00
0.05
0.10
0.15
0.20
0.25
0.30
40
Coalescen Yule_11_21.nb
In[]:=
Show
[
(*Coalescen Clade Sampling*)
Plo [{P 3[{1, 1, 2}, 6, ], P 3[{2, 2}, 6, ], P 3[{3, 3}, 6, ]},{ , 0, 2},
Plo Range All, Plo S yle Map[Di ec i e[#, Opaci y[0.3]] &, CompCol]],
(*Coalescen Lineage Sampling*)
Plo [{P 4[{1, 1, 2}, 6, ], P 4[{2, 2}, 6, ], P 4[{3, 3}, 6, ]},{ , 0, 2},
Plo Range All, Plo S yle Map[Di ec i e[#, Dashed, Thick]&, CompCol]],
(*Yule Lineage Sampling*)
Plo [{P Y4[{1, 1, 2},6,λComp, ], P Y4[{2, 2},6,λComp, ],
P Y4[{3, 3},6,λComp, ]},{ , 0, 2}, Plo Range All,
Plo S yle Map[Di ec i e[#(*
,
Opaci y[0.3]*)
,
Thick
,
Do ed]&
,
CompCol]]]
Ou [
] =
0.0 0.5 1.0 1.5 2.0
0.00
0.05
0.10
0.15
0.20
0.25
0.30
0.35
Simula ion
Tes
In[]:=
simYB
[6, 1.0, 1]
Ou [
] =
{{0, {1, 1, 1, 1, 1, 1}},{0.017626, {2, 1, 1, 1, 1}},{0.142699, {3, 1, 1, 1}},
{0.275207, {3, 2, 1}},{0.407731, {3, 3}},{0.45118, {6}},{3., {7}}}
Time
T
belongs
o
wha
in e al
o
he
piecewise
cons an
unc ion?
In[]:= pa [n_,λ_, T_, in S_] :=
Floo [In e pola ion[T anspose[{simYB[n, λ, in S]〚;; , 1〛, Table[i, {i, 0, n}]}],
In e pola ionO de
0
]
[
T
]
]
In[]:=
Clea
[ andClade]
andClade[n_,λ_, T_, in S_] := andClade[n, λ, T, in S] =
RandomSample[simYB[n, λ, in S]〚pa [n, λ, T, in S], 2〛, 1]〚1〛
Coalescen Yule_11_21.nb
41