API
fresco_classes
model
This module is used to create Model objects for MCMC sampling. These functions and classes should provide all the tools to set up a Bayesian model for FRESCO by defining its log-prior and log-likelihood functions.
Caleb Marshall NCSU/TUNL 2019
- class pfunk.model.DPrior(means, widths)
Bases:
GenPriorHandles the priors for normalizing variables. Takes an array of values for the factor uncertainty and creates a “frozen” pdf. lnprior method returns the sum input values.
- class pfunk.model.DofPrior(means)
Bases:
GenPriorDefines an exponential prior intended for use with the t-distribution dof parameter, nu.
- class pfunk.model.FrescoEval(filename, remove=True)
Bases:
objectHandles the swapping of the new values, reading in of cross section, and the spline for the likelihood evaluation. Takes an fc.NamelistInput object and filename for the cross section.
- read_fresco()
- class pfunk.model.LnLikeElastic(filename, data, norm_index=False, scatter_index=False, remove=True, fixed_scatter_dof=False, hier_index=None, hier_stop=None)
Bases:
FrescoEvalClass that inherits the fresco processing features of FrescoEval, and then includes experimental data to form a simple Gaussian likelihood (which I have called chi^2 for no good reason).
Initialize with a fc.NamelistInput instance and fc.DataObject instance (or path to the data file)
normalization adjusts the likelihood function to include a factor_unc.
- fixed_chi(x)
- fresco_chi(x)
- norm_fresco_chi(x)
- norm_hier_chi(x)
- norm_scatter_chi(x)
- scatter_chi(x)
- class pfunk.model.LnLikeTransfer(filename, data, sf_index, scatter_index=None, norm_index=None, remove=True)
Bases:
LnLikeElasticTransfer likelihood functions that assume fresco has already been run (i.e elastic likelihood has already been called).
- fresco_chi(x)
Likelihood for transfer cross section. Always needs spectroscopic factor to be defined.
- norm_fresco_chi(x)
- norm_scatter_chi(x)
- scatter_chi(x)
Likelihood for transfer cross section. Always needs spectroscopic factor to be defined.
- class pfunk.model.LnLikeTransferTwoL(filename1, filename2, data, sf_index, percent_index, scatter_index=None, norm_index=None, remove=True)
Bases:
LnLikeElasticTransfer likelihood functions that assume fresco has already been run (i.e elastic likelihood has already been called). For the two l case we assume one fresco file still produced all of the cross sections.
- fresco_chi(x)
Likelihood that reads both cross sections and adds them together to compare to the data.
- norm_fresco_chi(x)
For this case we have a common factor, n, which is shared by both cross sections.
- norm_scatter_chi(x)
Error estimate, f, is multiplied by the total cross section.
- scatter_chi(x)
Scatter without the normalization.
- class pfunk.model.LnLikeTransferTwoL_Test(filename1, filename2, data, sf_index1, sf_index2, scatter_index=None, norm_index=None, remove=True)
Bases:
LnLikeElasticTransfer likelihood functions that assume fresco has already been run (i.e elastic likelihood has already been called). For the two l case we assume one fresco file still produced all of the cross sections.
- norm_scatter_chi(x)
Error estimate, f, is multiplied by the total cross section.
- class pfunk.model.Model(fresco_path, fresco_names, fresco_positions, **kwargs)
Bases:
objectThis class is used to specify the prior probabilities and likelihood function for a Bayesian model. It handles the input to the prior and likelihood instances.
- create_elastic_likelihood(filename, data, norm_index=None, scatter_index=None, remove=True, fixed_scatter_dof=False, hier_index=None)
- create_hier_prior(n_data_points)
- create_likelihood()
- create_norm_prior(means, widths)
- create_pot_prior(means, widths)
- create_prior()
- create_scatter_prior(widths=[1.0], flat=False, cauchy_width=None)
Create a prior for error adjustments
- Parameters
t_dof_mean – mean for the exponential prior
- Returns
appends to scatter prior list
- Return type
NA
- create_spec_prior(means, widths, gaus=False, mixing_percent=False)
- create_transfer_likelihood(filename, data, sf_index, scatter_index=None, norm_index=None, remove=True)
- create_two_l_transfer_likelihood(filename1, filename2, data, sf_index, percent_index, scatter_index=None, norm_index=None, remove=True)
- create_two_l_transfer_likelihood_test(filename1, filename2, data, sf_index1, sf_index2, scatter_index=None, norm_index=None, remove=True)
- create_vr_likelihood(v_index, r_index, c, percent=0.2, n=1.4)
- lnlikefunc(x)
- lnprob(x)
- run_fresco(x)
- class pfunk.model.PotPrior(means, widths)
Bases:
GenPriorHandles the priors for optical potential parameters. Takes two arrays for the Gaussian means and widths, and defines a frozen pdf. lnprior method returns the sum of input values.
- class pfunk.model.Priors(priors)
Bases:
objectHandles the evaluation of the prior probabilities. Right now there is just support for Gaussian priors and factor uncertainties. The assumed structure of the vector x to be evaluated is [f.u, Gaussian] with Gaussian being an array of means and widths. Instances of ScalePrior and PotPrior should be input as a list in the order expected from the likelihood.
- lnprior(x)
- make_slices()
- prior_sample()
- transform_prior(x)
For use with nested sampler. Returns a vector of transformed values from the uniform distribution input values.
model_fit
- class pfunk.model_fit.BasinBounds(xmax, xmin)
Bases:
object
- class pfunk.model_fit.ElasticStep(stepsize=0.5)
Bases:
object
- class pfunk.model_fit.MAPFit(model, percent_range=0.2, lnlike=False)
Bases:
object- lnprob(x)
- print_fun(x, f, accepted)
- run()
- run_anneal(max_iter=100)
- run_differential()
- set_basin()
- set_bounds(percent, bnds=True)
- single_run()
- special_sauce(iterations=50)
- pfunk.model_fit.cross_interpolate(cross)
- pfunk.model_fit.sf_chi_sq(theory, exper, err, sf)
- pfunk.model_fit.spec_chi(sf, cross, data)
- pfunk.model_fit.spec_factors(cross, data)
sampler
- class pfunk.sampler.Sampler(model, nwalker=50, nstep=100)
Bases:
object- ball_init(scatter=0.01)
Initialize a random ndim ball around model.x0 parameters according to ‘scatter’ parameter, i.e the larger scale is the larger the ball is (can be).
- check_p0()
Check the lnprob values of the starting values.
- Returns
lnprob values of the each p0
- Return type
np.array
- cont_init(lower=1.15, upper=1.55, scatter=0.01)
Ball initialization except that the real well depth, V, and radius parameter, r, are randomly scattered along the Vr**n = c relationship. V and r are assumed to be in the indices 0 and 1.
- free_sig_init(scatter=0.01)
Does a ball initialization on potential and sigma parameters.
- prior_sample_init()
- run_dynamic_nested(evidence=False, posterior=False, nlive=250, sample='slice')
- run_ensemble()
Run the sampler with the default stretch move.
- run_nested(nlive=250, dlogz=0.01, sample='slice', bound='multi')
- run_slice_ensemble()
utilities
- pfunk.utilities.confidence_bands(lines, percentiles=[16, 50, 84])
- pfunk.utilities.cross_section_residual(lines, levels=[68.0, 95.0], data=None, cs_true=None, credibility=68.0)
- pfunk.utilities.evidence_unc(results)
Copy and paste job of a function to estimate the statistical and sampling uncertainties of nested sampling evidence estimation.
- Parameters
results – object returned from dynesty sampler
- Returns
results object with well defined ‘logzerr’
- Return type
some sort of dictionary thingy
- pfunk.utilities.hpd_grid(sample, alpha=0.32, roundto=2)
Calculate highest posterior density (HPD) of array for given alpha. The HPD is the minimum width Bayesian credible interval (BCI). The function works for multimodal distributions, returning more than one mode Parameters ———-
- sampleNumpy array or python list
An array containing MCMC samples
- alphafloat
Desired probability of type I error (defaults to 0.05)
- roundto: integer
Number of digits after the decimal point for the results
hpd: array with the lower
written by Osvaldo Martin taken from https://github.com/aloctavodia/BAP/blob/master/first_edition/code/Chp1/hpd.py
- pfunk.utilities.log_norm_parameters(samples)
Estimate the lognormal parameters based on the given samples. Gaussian parameters are extracted from the log of the samples, and then translated into the appropriate lognormal parameters. There is no explicit fitting.
- Parameters
samples – numpy array of the samples
- Returns
median, factor_unc
- Return type
float64
- pfunk.utilities.make_samples_dynesty(results)
- Uses dynesty’s resampling function
to return equally weighted samples.
- Parameters
results – the results dictionary from dynesty.
- Returns
equally weighted samples
- Return type
numpy array
- pfunk.utilities.mixed_transition_sf(sf, alpha)
Function for convince that gives the two spectroscopic factors for a mixed l transition that is parameterized by a single spectroscopic factor and a mixing parameter, alpha. Returns the two spectroscopic factors.
- Parameters
sf – Combined spectroscopic factor samples.
alpha – Samples of alpha
- Returns
sf1, sf2
- Return type
numpy arrays
- pfunk.utilities.parameters_values(samples)
Given an array of samples returns each parameters 16, 50, 84 percentile values. Returns list of tuples.
- pfunk.utilities.plot_ci(lines, levels=[68.0, 95.0], data=None, colors=None, alpha=1.0, linestyle='-', linewidth=1.0, hatch=None, zorder=0, markersize=5.0)
Set up a plot with 68%, 95%, and 99% credibility bands. ‘#e0ecf4’,
- pfunk.utilities.plot_ci_comp(lines, colors, alpha=0.6)
Quickly generate multiple ci plots using just the 68% interval.
- Parameters
lines – cross section object
colors – list for each line
- Returns
plots
- Return type
plots?
- pfunk.utilities.spec_factor_correlations(samples, spec_parameter=1, n=0)
def spec_factor_correlations(samples, spec_parameter=1, n=0)
Given an array “samples” of shape (# of samples, # of parameters) select the parameter related to the spectroscopic factor and plot all correlations. If a non-zero n is given use the spearman-rank-order coefficient to find the top n most important parameters.