Lets try to estimate both coalescent and migration rate in a two population model.
from phasic import (
Graph, with_ipv, StateIndexer, Property,
GaussPrior, HalfCauchyPrior, DataPrior,
SparseObservations, dense_to_sparse,
ConstantStepSize, ExpStepSize, AdaptiveStepSize, WarmupExpStepSize,
ConstantRegularization, ExpRegularization, ExponentialCDFRegularization,
Adam,
set_log_level, clear_caches,
)
import sys
import numpy as np
import jax.numpy as jnp
import matplotlib.pyplot as plt
import seaborn as sns
np.random.seed(4 )
import vscodenb
import svgd_tests
sns.set_palette('tab10' )
set_log_level('INFO' )
clear_caches()
[INFO] phasic.graph_cache: Cleared 0 cached graphs
Removed 6 file(s), preserved directory structure
nr_samples = 2
indexer = StateIndexer(
descendants= [
Property('pop1' , min_value= 0 , max_value= nr_samples),
Property('pop2' , min_value= 0 , max_value= nr_samples),
Property('in_pop' , min_value= 1 , max_value= 2 ),
])
initial = [0 ] * indexer.state_length
# set initial state with all lineages having one descendant at both loci
initial[indexer.descendants.props_to_index(pop1= 1 , pop2= 0 , in_pop= 1 )] = nr_samples
@with_ipv (initial)
def two_island(state):
transitions = []
if state[indexer.descendants.indices()].sum () <= 1 :
return transitions
for i in range (indexer.descendants.state_length):
if state[i] == 0 : continue
props_i = indexer.descendants.index_to_props(i)
for j in range (i, indexer.descendants.state_length):
if state[j] == 0 : continue
props_j = indexer.descendants.index_to_props(j)
if props_j.in_pop != props_i.in_pop:
continue
same = int (i == j)
if same and state[i] < 2 :
continue
if not same and (state[i] < 1 or state[j] < 1 ):
continue
child = state.copy()
child[i] -= 1
child[j] -= 1
des_pop1 = props_i.pop1 + props_j.pop1
des_pop2 = props_i.pop2 + props_j.pop2
if des_pop1 <= nr_samples and des_pop2 <= nr_samples:
k = indexer.descendants.props_to_index(
pop1= des_pop1,
pop2= des_pop2,
in_pop= props_i.in_pop
)
child[k] += 1
transitions.append([child, [state[i]* (state[j]- same)/ (1 + same), 0 ]])
if state[i] > 0 :
child = state.copy()
other_pop = 2 if props_i.in_pop == 1 else 1
child = state.copy()
child[i] -= 1
k = indexer.descendants.props_to_index(
pop1= props_i.pop1,
pop2= props_i.pop2,
in_pop= other_pop
)
child[k] += 1
transitions.append([child, [0 , state[i]]])
return transitions
graph = Graph(two_island)
true_theta = [10 , 2 ]
graph.update_weights(true_theta)
def label(state):
nr_pop1 = sum ([state[i] * bool (indexer.index_to_props(i).descendants.in_pop == 1 ) for i in indexer])
nr_pop2 = sum ([state[i] * bool (indexer.index_to_props(i).descendants.in_pop == 2 ) for i in indexer])
return f"pop1: { nr_pop1} \n pop2: { nr_pop2} "
graph.plot(rankdir= 'LR' , nodesep= 0.3 , ranksep= 2 ,
wrap= 10 ,
by_state= label)
[INFO] phasic.graph_cache: Saved graph to cache: 7a761bac1791755c... (6 vertices)
[INFO] phasic: Saved graph to cache: 6 vertices
observations = graph.sample(10000 )
observations.shape
with set_log_level('WARNING' ):
svgd = graph.svgd(observations,
prior= [
GaussPrior(ci= [2 , 10 ]),
GaussPrior(ci= [2 , 10 ]),
],
n_iterations= 100 ,
n_particles= 20 ,
optimizer= Adam(learning_rate= 0.3 ),
)
svgd.summary()
Parameter Fixed MAP Mean SD HPD 95% lo HPD 95% hi
0 No 9.911 9.909 0.07683 9.781 10.06
1 No 2.098 2.126 0.07213 2.004 2.164
Particles: 20, Iterations: 100
svgd.plot_ci(true_theta= true_theta)
svgd.plot_ci(true_theta= true_theta, ci= 0.5 )
/var/folders/s6/srs8qkh52w1_h32d65z95tth0000gn/T/ipykernel_88052/3416795907.py:1: DeprecationWarning: plot_ci(ci=...) is deprecated; use ci_level=... instead. It will be removed no earlier than phasic 0.34.0.
svgd.plot_ci(true_theta=true_theta, ci=0.5)
svgd.map_estimate_from_particles()
([9.911009811176747, 2.09810817661949], 7220.119223613634)
svgd.summary(ci_level= 0.5 )
Parameter Fixed MAP Mean SD HPD 50% lo HPD 50% hi
0 No 9.911 9.909 0.07683 9.902 9.936
1 No 2.098 2.126 0.07213 2.11 2.134
Particles: 20, Iterations: 100
Parameter Fixed MAP Mean SD HPD 95% lo HPD 95% hi
0 No 9.911 9.909 0.07683 9.781 10.06
1 No 2.098 2.126 0.07213 2.004 2.164
Particles: 20, Iterations: 100
with set_log_level('WARNING' ):
svgd.plot_pairwise_hdr(show_marginal_hpd= True )
svgd.plot_pairwise(true_theta= true_theta)
#svgd.animate_pairwise(true_theta=true_theta)
# mutation_rate = 1.2e-4
# joint_prob_graph = graph.joint_prob_graph(indexer,
# reward_only=['pop1', 'pop2'],
# reward_limit=1,
# tot_reward_limit=1,
# mutation_rate=mutation_rate)
# joint_prob_graph.plot(rankdir='LR', nodesep=0.3, ranksep=2,
# wrap=10, label_fmt=lambda vertex: str(vertex.index()), by_state=label)
# joint_prob_table = joint_prob_graph.joint_prob_table()
# joint_prob_table
# nr_samples = 2
# indexer = StateIndexer(
# descendants=[
# Property('pop1', min_value=0, max_value=nr_samples),
# Property('pop2', min_value=0, max_value=nr_samples),
# Property('in_pop', min_value=1, max_value=2),
# ])
# initial = [0] * indexer.state_length
# # set initial state with all lineages having one descendant at both loci
# initial[indexer.descendants.props_to_index(pop1=1, pop2=0, in_pop=1)] = nr_samples
# @with_ipv(initial)
# def two_island(state):
# transitions = []
# if state[indexer.descendants.indices()].sum() <= 1:
# return transitions
# for i in range(indexer.descendants.state_length):
# if state[i] == 0: continue
# props_i = indexer.descendants.index_to_props(i)
# for j in range(i, indexer.descendants.state_length):
# if state[j] == 0: continue
# props_j = indexer.descendants.index_to_props(j)
# if props_j.in_pop != props_i.in_pop:
# continue
# same = int(i == j)
# if same and state[i] < 2:
# continue
# if not same and (state[i] < 1 or state[j] < 1):
# continue
# child = state.copy()
# child[i] -= 1
# child[j] -= 1
# des_pop1 = props_i.pop1 + props_j.pop1
# des_pop2 = props_i.pop2 + props_j.pop2
# if des_pop1 <= nr_samples and des_pop2 <= nr_samples:
# k = indexer.descendants.props_to_index(
# pop1=des_pop1,
# pop2=des_pop2,
# in_pop=props_i.in_pop
# )
# child[k] += 1
# transitions.append([child, [state[i]*(state[j]-same)/(1+same), 0]])
# if state[i] > 0:
# child = state.copy()
# other_pop = 2 if props_i.in_pop == 1 else 1
# child = state.copy()
# child[i] -= 1
# k = indexer.descendants.props_to_index(
# pop1=props_i.pop1,
# pop2=props_i.pop2,
# in_pop=other_pop
# )
# child[k] += 1
# transitions.append([child, [0, state[i]]])
# return transitions
# graph = Graph(two_island)
# true_theta = [0.7, 0.3]
# graph.update_weights(true_theta)
# graph.plot(rankdir='LR', by_index=lambda i: f"Desc in pop1: {indexer.index_to_props(i).descendants.pop1}")
# graph.plot(rankdir='LR', by_state=lambda s: f"In pop1: {s[indexer.descendants.props_to_index(pop1=1)].sum()}")