Skip to content

Commit 7fb0009

Browse files
add basic_demo.ipynb
1 parent 9c2b521 commit 7fb0009

File tree

1 file changed

+223
-0
lines changed

1 file changed

+223
-0
lines changed

notebooks/basic_demo.ipynb

+223
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "6460f518",
6+
"metadata": {},
7+
"source": [
8+
"# Basic BVAS demo using simulated data"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 3,
14+
"id": "8137976b",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from bvas import simulate_data, BVASSelector"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"id": "44e25a88",
24+
"metadata": {},
25+
"source": [
26+
"### Simulate data"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 5,
32+
"id": "054dd652",
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"data = simulate_data(num_alleles=100, duration=26, num_variants=100, num_regions=10,\n",
37+
" k=0.1, seed=0, sampling_rate=10, strategy='global-median')"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 35,
43+
"id": "8362f2f2",
44+
"metadata": {},
45+
"outputs": [
46+
{
47+
"name": "stdout",
48+
"output_type": "stream",
49+
"text": [
50+
"Y torch.Size([100])\n",
51+
"Gamma torch.Size([100, 100])\n",
52+
"estimated_nu_eff (1,)\n",
53+
"true_betas torch.Size([100])\n",
54+
"\n",
55+
"Estimated effective population size: 490.3\n"
56+
]
57+
}
58+
],
59+
"source": [
60+
"# inspect simulated data\n",
61+
"for k, v in data.items():\n",
62+
" print(k, v.shape)\n",
63+
" \n",
64+
"print(\"\\nEstimated effective population size: {:.1f}\".format(data['estimated_nu_eff'].item()))"
65+
]
66+
},
67+
{
68+
"cell_type": "markdown",
69+
"id": "6bee4bdc",
70+
"metadata": {},
71+
"source": [
72+
"### Instantiate BVASSelector object"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": 18,
78+
"id": "bdc84a1f",
79+
"metadata": {},
80+
"outputs": [],
81+
"source": [
82+
"# create names for our 100 alleles (the first 10 alleles are non-neutral in the simulation)\n",
83+
"mutations = [\"Causal{}\".format(k) for k in range(1, 11)] \n",
84+
"mutations += [\"Spurious{}\".format(k) for k in range(11, 101)] \n",
85+
"\n",
86+
"selector = BVASSelector(data['Y'], \n",
87+
" data['Gamma'], \n",
88+
" mutations, \n",
89+
" S=5.0,\n",
90+
" tau=100.0)"
91+
]
92+
},
93+
{
94+
"cell_type": "markdown",
95+
"id": "ccca6ebd",
96+
"metadata": {},
97+
"source": [
98+
"### Run BVAS MCMC-based inference"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": 19,
104+
"id": "08f1f267",
105+
"metadata": {},
106+
"outputs": [
107+
{
108+
"data": {
109+
"application/vnd.jupyter.widget-view+json": {
110+
"model_id": "b7a31fde7b0d46e4ad1a9a6ef8090a5c",
111+
"version_major": 2,
112+
"version_minor": 0
113+
},
114+
"text/plain": [
115+
" 0%| | 0/1500 [00:00<?, ?it/s]"
116+
]
117+
},
118+
"metadata": {},
119+
"output_type": "display_data"
120+
}
121+
],
122+
"source": [
123+
"selector.run(T=1000, T_burnin=500)"
124+
]
125+
},
126+
{
127+
"cell_type": "markdown",
128+
"id": "0c6ada0e",
129+
"metadata": {},
130+
"source": [
131+
"### Inspect results\n",
132+
"\n",
133+
"- We find that 8 of the 10 true causal alleles are assigned large PIPs\n",
134+
"- We find that 2 of the 10 true causal alleles are missed (i.e. those with small effect sizes, namely Causal1 and Causal6)\n",
135+
"- We find that no spurious alleles are assigned large PIPs\n",
136+
"- We see that the beta estimates are regularized somewhat towards zero"
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": 36,
142+
"id": "68e34a56",
143+
"metadata": {},
144+
"outputs": [
145+
{
146+
"name": "stdout",
147+
"output_type": "stream",
148+
"text": [
149+
" PIP Beta BetaStd Rank\n",
150+
"Causal4 0.999999 0.051166 0.006216 1\n",
151+
"Causal5 0.999999 0.066024 0.006334 2\n",
152+
"Causal10 0.999999 -0.068597 0.008873 3\n",
153+
"Causal9 0.999999 -0.065844 0.010435 4\n",
154+
"Causal3 0.999903 0.039212 0.006989 5\n",
155+
"Causal8 0.881331 -0.026670 0.013342 6\n",
156+
"Causal7 0.250416 -0.005248 0.010489 7\n",
157+
"Causal2 0.133008 0.003241 0.008441 8\n",
158+
"Spurious89 0.023355 -0.000385 0.002696 9\n",
159+
"Spurious24 0.018575 0.000173 0.001668 10\n",
160+
"Spurious70 0.016120 0.000370 0.002402 11\n",
161+
"Spurious80 0.013704 0.000187 0.001622 12\n",
162+
"Spurious21 0.011855 0.000109 0.001287 13\n",
163+
"Spurious16 0.009838 -0.000098 0.001316 14\n",
164+
"Spurious44 0.009189 -0.000078 0.001053 15\n"
165+
]
166+
}
167+
],
168+
"source": [
169+
"print(selector.summary.iloc[:15][['PIP', 'Beta', 'BetaStd', 'Rank']])"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": 32,
175+
"id": "3753108b",
176+
"metadata": {},
177+
"outputs": [
178+
{
179+
"name": "stdout",
180+
"output_type": "stream",
181+
"text": [
182+
"[Causal1]\t0.01\n",
183+
"[Causal2]\t0.02\n",
184+
"[Causal3]\t0.04\n",
185+
"[Causal4]\t0.06\n",
186+
"[Causal5]\t0.08\n",
187+
"[Causal6]\t-0.01\n",
188+
"[Causal7]\t-0.02\n",
189+
"[Causal8]\t-0.04\n",
190+
"[Causal9]\t-0.06\n",
191+
"[Causal10]\t-0.08\n"
192+
]
193+
}
194+
],
195+
"source": [
196+
"# print true betas for the causal coefficients\n",
197+
"for mutation, beta in zip(mutations[:10], data['true_betas'][:10]):\n",
198+
" print(\"[{}]\\t{:.2f}\".format(mutation, beta.item()))"
199+
]
200+
}
201+
],
202+
"metadata": {
203+
"kernelspec": {
204+
"display_name": "Python 3",
205+
"language": "python",
206+
"name": "python3"
207+
},
208+
"language_info": {
209+
"codemirror_mode": {
210+
"name": "ipython",
211+
"version": 3
212+
},
213+
"file_extension": ".py",
214+
"mimetype": "text/x-python",
215+
"name": "python",
216+
"nbconvert_exporter": "python",
217+
"pygments_lexer": "ipython3",
218+
"version": "3.8.2"
219+
}
220+
},
221+
"nbformat": 4,
222+
"nbformat_minor": 5
223+
}

0 commit comments

Comments
 (0)