Skip to content

Commit 80201fd

Browse files
committed
[core] heuristic for setting t1, tK
1 parent dad595c commit 80201fd

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

smcpp/analysis.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,27 @@ def _normalize_data(self, length_cutoff, filter):
173173
ci += 1
174174
self._contigs = new_contigs
175175

176+
def _calculate_t1_tK(self, args):
177+
n = 2 + max(self._ns)
178+
p = (self._esfs.sum().astype('float') - self._esfs[0, 0]) / self._esfs.sum()
179+
Ne = p / self._theta / (2. * (1. / np.arange(1, n)).sum())
180+
logger.debug("Ne: %f", Ne)
181+
Ne *= 2 * self._N0
182+
if args.t1 is None:
183+
# distribution of first coalescent in sample ~ 1 - exp(-nC2 t / (Ne / N0)) ~= .1
184+
args.t1 = 200 + np.log(.9) / (-n * (n - 1) / 2) * Ne
185+
if args.t1 <= 0:
186+
logger.error("--t1 should be >0")
187+
sys.exit(1)
188+
logger.debug("setting t1=%f", args.t1)
189+
if args.tK is None:
190+
args.tK = (2 + 1.16 ** 0.5) * Ne
191+
if args.tK <= 0:
192+
logger.error("--tK should be >0")
193+
sys.exit(1)
194+
logger.debug("setting tK=%f", args.tK)
195+
196+
176197
def _init_inference_manager(self, polarization_error):
177198
## Create inference object which will be used for all further calculations.
178199
logger.debug("Creating inference manager...")
@@ -288,14 +309,9 @@ def __init__(self, files, args):
288309
# Thin the data
289310
self._perform_thinning(args.thinning)
290311

291-
# Set t1, tK
292-
n = min(200, max(self._ns.max(), 2))
293-
if args.t1 is None:
294-
args.t1 = np.exp(np.log(1000) * (200 - n) / 200 + np.log(100) * (n / 200))
295-
if args.t1 <= 0:
296-
logger.error("--t1 should be >0")
297-
sys.exit(1)
298-
logger.debug("setting t1=%f", args.t1)
312+
# Set t1, tK if not already set
313+
self._calculate_t1_tK(args)
314+
299315
self._init_knots(args.knots, args.t1, args.tK, args.offset)
300316
for x in smcpp.defaults.additional_knots:
301317
self._knots = np.append(self._knots, x * self._knots[-1])

smcpp/defaults.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
maximum = 1e4
99
M = 40
1010
t1 = None
11-
tK = 100000
11+
tK = None
1212
perplexity_threshold = .5
1313
regularization_degree = 2
1414
spline = "pchip"

0 commit comments

Comments
 (0)