def ensemble_compute_acl(filename, start_index=None, end_index=None, min_nsamples=10): """Computes the autocorrleation length for a parallel tempered, ensemble MCMC. Parameter values are averaged over all walkers at each iteration and temperature. The ACL is then calculated over the averaged chain. Parameters ----------- filename : str Name of a samples file to compute ACLs for. start_index : int, optional The start index to compute the acl from. If None (the default), will try to use the number of burn-in iterations in the file; otherwise, will start at the first sample. end_index : int, optional The end index to compute the acl to. If None, will go to the end of the current iteration. min_nsamples : int, optional Require a minimum number of samples to compute an ACL. If the number of samples per walker is less than this, will just set to ``inf``. Default is 10. Returns ------- dict A dictionary of ntemps-long arrays of the ACLs of each parameter. """ acls = {} with loadfile(filename, 'r') as fp: if end_index is None: end_index = fp.niterations tidx = numpy.arange(fp.ntemps) for param in fp.variable_params: these_acls = numpy.zeros(fp.ntemps) for tk in tidx: samples = fp.read_raw_samples(param, thin_start=start_index, thin_interval=1, thin_end=end_index, temps=tk, flatten=False)[param] # contract the walker dimension using the mean, and flatten # the (length 1) temp dimension samples = samples.mean(axis=1)[0, :] if samples.size < min_nsamples: acl = numpy.inf else: acl = autocorrelation.calculate_acl(samples) if acl <= 0: acl = numpy.inf these_acls[tk] = acl acls[param] = these_acls maxacl = numpy.array(list(acls.values())).max() logging.info("ACT: %s", str(maxacl * fp.thinned_by)) return acls
def _getacl(si): # si: the samples loaded for a specific chain; may have nans in it si = si[~numpy.isnan(si)] if len(si) < min_nsamples: acl = numpy.inf else: acl = autocorrelation.calculate_acl(si) if acl <= 0: acl = numpy.inf return acl
def ensemble_compute_acl(filename, start_index=None, end_index=None, min_nsamples=10): """Computes the autocorrleation length for an ensemble MCMC. Parameter values are averaged over all walkers at each iteration. The ACL is then calculated over the averaged chain. If an ACL cannot be calculated because there are not enough samples, it will be set to ``inf``. Parameters ----------- filename : str Name of a samples file to compute ACLs for. start_index : int, optional The start index to compute the acl from. If None, will try to use the number of burn-in iterations in the file; otherwise, will start at the first sample. end_index : int, optional The end index to compute the acl to. If None, will go to the end of the current iteration. min_nsamples : int, optional Require a minimum number of samples to compute an ACL. If the number of samples per walker is less than this, will just set to ``inf``. Default is 10. Returns ------- dict A dictionary giving the ACL for each parameter. """ acls = {} with loadfile(filename, 'r') as fp: for param in fp.variable_params: samples = fp.read_raw_samples(param, thin_start=start_index, thin_interval=1, thin_end=end_index, flatten=False)[param] samples = samples.mean(axis=0) # if < min number of samples, just set to inf if samples.size < min_nsamples: acl = numpy.inf else: acl = autocorrelation.calculate_acl(samples) if acl <= 0: acl = numpy.inf acls[param] = acl maxacl = numpy.array(list(acls.values())).max() logging.info("ACT: %s", str(maxacl * fp.thin_interval)) return acls
def compute_acl(cls, filename, start_index=None, end_index=None, min_nsamples=10): """Computes the autocorrleation length for all model params and temperatures in the given file. Parameter values are averaged over all walkers at each iteration and temperature. The ACL is then calculated over the averaged chain. Parameters ----------- filename : str Name of a samples file to compute ACLs for. start_index : {None, int} The start index to compute the acl from. If None, will try to use the number of burn-in iterations in the file; otherwise, will start at the first sample. end_index : {None, int} The end index to compute the acl to. If None, will go to the end of the current iteration. min_nsamples : int, optional Require a minimum number of samples to compute an ACL. If the number of samples per walker is less than this, will just set to ``inf``. Default is 10. Returns ------- dict A dictionary of ntemps-long arrays of the ACLs of each parameter. """ acls = {} with cls._io(filename, 'r') as fp: if end_index is None: end_index = fp.niterations tidx = numpy.arange(fp.ntemps) for param in fp.variable_params: these_acls = numpy.zeros(fp.ntemps) for tk in tidx: samples = fp.read_raw_samples( param, thin_start=start_index, thin_interval=1, thin_end=end_index, temps=tk, flatten=False)[param] # contract the walker dimension using the mean, and flatten # the (length 1) temp dimension samples = samples.mean(axis=1)[0, :] if samples.size < min_nsamples: acl = numpy.inf else: acl = autocorrelation.calculate_acl(samples) if acl <= 0: acl = numpy.inf these_acls[tk] = acl acls[param] = these_acls return acls
def compute_acls(cls, fp, start_index=None, end_index=None): """Computes the autocorrleation length for all variable args and temperatures in the given file. Parameter values are averaged over all walkers at each iteration and temperature. The ACL is then calculated over the averaged chain. If the returned ACL is `inf`, will default to the number of current iterations. Parameters ----------- fp : InferenceFile An open file handler to read the samples from. start_index : {None, int} The start index to compute the acl from. If None, will try to use the number of burn-in iterations in the file; otherwise, will start at the first sample. end_index : {None, int} The end index to compute the acl to. If None, will go to the end of the current iteration. Returns ------- dict A dictionary of ntemps-long arrays of the ACLs of each parameter. """ acls = {} if end_index is None: end_index = fp.niterations tidx = numpy.arange(fp.ntemps) for param in fp.variable_args: these_acls = numpy.zeros(fp.ntemps, dtype=int) for tk in tidx: samples = cls.read_samples(fp, param, thin_start=start_index, thin_interval=1, thin_end=end_index, temps=tk, flatten=False)[param] # contract the walker dimension using the mean, and flatten # the (length 1) temp dimension samples = samples.mean(axis=1)[0, :] acl = autocorrelation.calculate_acl(samples) if numpy.isinf(acl): acl = samples.size these_acls[tk] = acl acls[param] = these_acls return acls
def compute_acl(cls, filename, start_index=None, end_index=None, min_nsamples=10): """Computes the autocorrleation length for all model params in the given file. Parameter values are averaged over all walkers at each iteration. The ACL is then calculated over the averaged chain. If an ACL cannot be calculated because there are not enough samples, it will be set to ``inf``. Parameters ----------- filename : str Name of a samples file to compute ACLs for. start_index : int, optional The start index to compute the acl from. If None, will try to use the number of burn-in iterations in the file; otherwise, will start at the first sample. end_index : int, optional The end index to compute the acl to. If None, will go to the end of the current iteration. min_nsamples : int, optional Require a minimum number of samples to compute an ACL. If the number of samples per walker is less than this, will just set to ``inf``. Default is 10. Returns ------- dict A dictionary giving the ACL for each parameter. """ acls = {} with cls._io(filename, 'r') as fp: for param in fp.variable_params: samples = fp.read_raw_samples( param, thin_start=start_index, thin_interval=1, thin_end=end_index, flatten=False)[param] samples = samples.mean(axis=0) # if < min number of samples, just set to inf if samples.size < min_nsamples: acl = numpy.inf else: acl = autocorrelation.calculate_acl(samples) if acl <= 0: acl = numpy.inf acls[param] = acl return acls
def compute_acls(cls, fp, start_index=None, end_index=None): """Computes the autocorrleation length for all variable args and temperatures in the given file. Parameter values are averaged over all walkers at each iteration and temperature. The ACL is then calculated over the averaged chain. If the returned ACL is `inf`, will default to the number of current iterations. Parameters ----------- fp : InferenceFile An open file handler to read the samples from. start_index : {None, int} The start index to compute the acl from. If None, will try to use the number of burn-in iterations in the file; otherwise, will start at the first sample. end_index : {None, int} The end index to compute the acl to. If None, will go to the end of the current iteration. Returns ------- FieldArray An ntemps-long `FieldArray` containing the ACL for each temperature and for each variable argument, with the variable arguments as fields. """ acls = {} if end_index is None: end_index = fp.niterations tidx = numpy.arange(fp.ntemps) for param in fp.variable_args: these_acls = numpy.zeros(fp.ntemps, dtype=int) for tk in tidx: samples = cls.read_samples(fp, param, thin_start=start_index, thin_interval=1, thin_end=end_index, temps=tk, flatten=False)[param] # contract the walker dimension using the mean, and flatten # the (length 1) temp dimension samples = samples.mean(axis=1)[0,:] acl = autocorrelation.calculate_acl(samples) if numpy.isinf(acl): acl = samples.size these_acls[tk] = acl acls[param] = these_acls return FieldArray.from_kwargs(**acls)
def compute_acls(cls, fp, start_index=None, end_index=None): """Computes the autocorrleation length for all variable args for all walkers for all temps in the given file. If the returned acl is inf, will default to the number of requested iterations. Parameters ----------- fp : InferenceFile An open file handler to read the samples from. start_index : {None, int} The start index to compute the acl from. If None, will try to use the number of burn-in iterations in the file; otherwise, will start at the first sample. end_index : {None, int} The end index to compute the acl to. If None, will go to the end of the current iteration. Returns ------- WaveformArray An ntemps x nwalkers `WaveformArray` containing the acl for each walker and temp for each variable argument, with the variable arguments as fields. """ acls = {} if end_index is None: end_index = fp.niterations tidx = numpy.arange(fp.ntemps) widx = numpy.arange(fp.nwalkers) for param in fp.variable_args: these_acls = numpy.zeros((fp.ntemps, fp.nwalkers), dtype=int) for tk in tidx: for wi in widx: samples = cls.read_samples(fp, param, thin_start=start_index, thin_interval=1, thin_end=end_index, walkers=wi, temps=tk)[param] acl = autocorrelation.calculate_acl(samples) these_acls[tk, wi] = int(min(acl, samples.size)) acls[param] = these_acls return WaveformArray.from_kwargs(**acls)
def compute_acls(cls, fp, start_index=None, end_index=None): """Computes the autocorrleation length for all variable args for all walkers for all temps in the given file. If the returned acl is inf, will default to the number of requested iterations. Parameters ----------- fp : InferenceFile An open file handler to read the samples from. start_index : {None, int} The start index to compute the acl from. If None, will try to use the number of burn-in iterations in the file; otherwise, will start at the first sample. end_index : {None, int} The end index to compute the acl to. If None, will go to the end of the current iteration. Returns ------- FieldArray An ntemps x nwalkers `FieldArray` containing the acl for each walker and temp for each variable argument, with the variable arguments as fields. """ acls = {} if end_index is None: end_index = fp.niterations tidx = numpy.arange(fp.ntemps) widx = numpy.arange(fp.nwalkers) for param in fp.variable_args: these_acls = numpy.zeros((fp.ntemps, fp.nwalkers), dtype=int) for tk in tidx: for wi in widx: samples = cls.read_samples( fp, param, thin_start=start_index, thin_interval=1, thin_end=end_index, walkers=wi, temps=tk)[param] acl = autocorrelation.calculate_acl(samples) these_acls[tk, wi] = int(min(acl, samples.size)) acls[param] = these_acls return FieldArray.from_kwargs(**acls)
def compute_acls(cls, fp, start_index=None, end_index=None): """Computes the autocorrleation length for all model params in the given file. Parameter values are averaged over all walkers at each iteration. The ACL is then calculated over the averaged chain. If the returned ACL is `inf`, will default to the number of current iterations. Parameters ----------- fp : InferenceFile An open file handler to read the samples from. start_index : {None, int} The start index to compute the acl from. If None, will try to use the number of burn-in iterations in the file; otherwise, will start at the first sample. end_index : {None, int} The end index to compute the acl to. If None, will go to the end of the current iteration. Returns ------- dict A dictionary giving the ACL for each parameter. """ acls = {} for param in fp.variable_params: samples = cls.read_samples(fp, param, thin_start=start_index, thin_interval=1, thin_end=end_index, flatten=False)[param] samples = samples.mean(axis=0) acl = autocorrelation.calculate_acl(samples) if numpy.isinf(acl): acl = samples.size acls[param] = acl return acls
def compute_acls(cls, fp, start_index=None, end_index=None): """Computes the autocorrleation length for all variable args in the given file. Parameter values are averaged over all walkers at each iteration. The ACL is then calculated over the averaged chain. If the returned ACL is `inf`, will default to the number of current iterations. Parameters ----------- fp : InferenceFile An open file handler to read the samples from. start_index : {None, int} The start index to compute the acl from. If None, will try to use the number of burn-in iterations in the file; otherwise, will start at the first sample. end_index : {None, int} The end index to compute the acl to. If None, will go to the end of the current iteration. Returns ------- dict A dictionary giving the ACL for each parameter. """ acls = {} for param in fp.variable_args: samples = cls.read_samples(fp, param, thin_start=start_index, thin_interval=1, thin_end=end_index, flatten=False)[param] samples = samples.mean(axis=0) acl = autocorrelation.calculate_acl(samples) if numpy.isinf(acl): acl = samples.size acls[param] = acl return acls