def get_godambe(func_ex, all_boot, p0, data, eps, log=True): #assume that last element of p0 is theta, and the remaining elements are the demographic model parameters #log dictates whether parameters are regular are logarithmic #func_ex is dadi extrapolated function, all_boot is bootstrapped data, p0 is best_fit params for data/func_ex combination J = numpy.zeros((len(p0), len(p0))) func = lambda params: Inference.ll(params[-1]*func_ex(params[:-1], ns, grid_pts), data) hess = -get_hess(func, p0, eps) if log: func = lambda params: Inference.ll(numpy.exp(params[-1])*func_ex(numpy.exp(params[:-1]), ns, grid_pts), data) hess = -get_hess(func, numpy.log(p0), eps) for ii, boot in enumerate(all_boot): boot = Spectrum(boot) if not log: func = lambda params: Inference.ll(params[-1]*func_ex(params[:-1], ns, grid_pts), boot) grad_temp = get_grad(func, p0, eps) if log: func = lambda params: Inference.ll(numpy.exp(params[-1])*func_ex(numpy.exp(params[:-1]), ns, grid_pts), boot) grad_temp = get_grad(func, numpy.log(p0), eps) J_temp = numpy.outer(grad_temp, grad_temp) J = J + J_temp J = J/len(all_boot) J_inv = numpy.linalg.inv(J) # G = H*J^-1*H godambe = numpy.dot(numpy.dot(hess, J_inv), hess) return godambe, hess
def _object_func(params, data, model_func, sel_dist, theta, lower_bound=None, upper_bound=None, verbose=0, multinom=False, flush_delay=0, func_args=[], func_kwargs={}, fixed_params=None, ll_scale=1, output_stream=sys.stdout, store_thetas=False): """ Objective function for optimization. """ global _counter _counter += 1 # Deal with fixed parameters params_up = Inference._project_params_up(params, fixed_params) # Check our parameter bounds if lower_bound is not None: for pval, bound in zip(params_up, lower_bound): if bound is not None and pval < bound: return -_out_of_bounds_val / ll_scale if upper_bound is not None: for pval, bound in zip(params_up, upper_bound): if bound is not None and pval > bound: return -_out_of_bounds_val / ll_scale ns = data.sample_sizes all_args = [params_up, ns, sel_dist, theta] + list(func_args) sfs = model_func(*all_args, **func_kwargs) if multinom: result = Inference.ll_multinom(sfs, data) else: result = Inference.ll(sfs, data) if store_thetas: global _theta_store _theta_store[tuple(params)] = optimal_sfs_scaling(sfs, data) # Bad result if numpy.isnan(result): result = _out_of_bounds_val if (verbose > 0) and (_counter % verbose == 0): param_str = 'array([%s])' % (', '.join( ['%- 12g' % v for v in params_up])) output_stream.write('%-8i, %-12g, %s%s' % (_counter, result, param_str, os.linesep)) Misc.delayed_flush(delay=flush_delay) return -result / ll_scale
def func(params, data, theta_adjust=1): key = (tuple(params), tuple(ns), tuple(grid_pts)) if key not in cache: cache[key] = func_ex(params, ns, grid_pts) # theta_adjust deals with bootstraps that need different thetas fs = theta_adjust * cache[key] return Inference.ll(fs, data)
def func(params, data): key = (tuple(params), tuple(ns), tuple(grid_pts)) if key not in cache: cache[key] = func_ex(params, ns, grid_pts) fs = cache[key] return Inference.ll(fs, data)
def _object_func(params, data1, data2, cache1, cache2, model_func, sel_dist, scal_fac1, scal_fac2, theta1, theta2, lower_bound=None, upper_bound=None, verbose=0, multinom=False, flush_delay=0, func_args=[], func_kwargs={}, fixed_params1=None, fixed_params2=None, ll_scale=1, output_stream=sys.stdout, store_thetas=False): """ Objective function for optimization. """ global _counter _counter += 1 # Scaling factors scales sel_dist differently for species 1 and species 2 sel_dist1 = copy_func( sel_dist, defaults=scal_fac1) # scal_fac1 should be 2*Nea of pop 1 sel_dist2 = copy_func( sel_dist, defaults=scal_fac2) # scal_fac2 should be 4*Nea of pop 2 # Deal with fixed parameters params_up1 = Inference._project_params_up(params, fixed_params1) params_up2 = Inference._project_params_up(params, fixed_params2) # Check our parameter bounds if lower_bound is not None: for pval, bound in zip(params_up1, lower_bound): if bound is not None and pval < bound: return -_out_of_bounds_val / ll_scale if upper_bound is not None: for pval, bound in zip(params_up1, upper_bound): if bound is not None and pval > bound: return -_out_of_bounds_val / ll_scale ns1 = data1.sample_sizes ns2 = data2.sample_sizes all_args1 = [params_up1, ns1, sel_dist1, theta1, cache1] + list(func_args) all_args2 = [params_up2, ns2, sel_dist2, theta2, cache2] + list(func_args) # Pass the pts argument via keyword, but don't alter the passed-in # func_kwargs #func_kwargs = func_kwargs.copy() #func_kwargs['pts'] = pts sfs1 = model_func(*all_args1, **func_kwargs) sfs2 = model_func(*all_args2, **func_kwargs) if multinom: result = Inference.ll_multinom(sfs1, data1) + Inference.ll_multinom( sfs2, data2) else: result = Inference.ll(sfs1, data1) + Inference.ll(sfs2, data2) # Bad result if numpy.isnan(result): result = _out_of_bounds_val if (verbose > 0) and (_counter % verbose == 0): param_str = 'array([%s])' % (', '.join( ['%- 12g' % v for v in params_up1])) output_stream.write('%-8i, %-12g, %s%s' % (_counter, result, param_str, os.linesep)) Misc.delayed_flush(delay=flush_delay) return -result / ll_scale