refellips.objectiveSE
- class refellips.objectiveSE.ObjectiveSE(model, data, lnsigma=None, use_weights=True, transform=None, logp_extra=None, name=None)
Bases:
BaseObjectiveObjective function for using with curvefitters such as refnx.analysis.curvefitter.CurveFitter.
- Parameters:
model (refnx.analysis.Model) – the residuals model function. One can also provide an object that inherits refnx.analysis.Model.
data (refnx.dataset.Data1D) – data to be analysed.
lnsigma (float or refnx.analysis.Parameter, optional) –
Used if the experimental uncertainty (data.y_err) underestimated by a constant fractional amount. The experimental uncertainty is modified as:
s_n**2 = y_err**2 + exp(lnsigma * 2) * model**2
See Objective.logl for more details.
use_weights (bool) – use experimental uncertainty in calculation of residuals and logl, if available. If this is set to False, then you should also set self.lnsigma.vary = False, it will have no effect on the fit.
transform (callable, optional) – the model, data and data uncertainty are transformed by this function before calculating the likelihood/residuals. Has the signature transform(data.x, y, y_err=None), returning the tuple (transformed_y, transformed_y_err).
logp_extra (callable, optional) – user specifiable log-probability term. This contribution is in addition to the log-prior term of the model parameters, and model.logp, as well as the log-likelihood of the data. Has signature: logp_extra(model, data). The model will already possess updated parameters. Beware of including the same log-probability terms more than once.
name (str) – Name for the objective.
Notes
For parallelisation logp_extra needs to be picklable.
- chisqr(pvals=None)
Calculates the chi-squared value for a given fitting system.
- Parameters:
pvals (array-like or refnx.analysis.Parameters) – values for the varying or entire set of parameters
- Returns:
chisqr – Chi-squared value, np.sum(residuals**2).
- Return type:
np.ndarray
- covar()
Estimates the covariance matrix of the curvefitting system.
- Returns:
covar – Covariance matrix
- Return type:
np.ndarray
- logl(pvals=None)
Calculate the log-likelhood of the system.
- Parameters:
pvals (array-like or refnx.analysis.Parameters) – values for the varying or entire set of parameters
- Returns:
logl – log-likelihood probability
- Return type:
float
Notes
The log-likelihood is calculated as:
logl = -0.5 * np.sum(((y - model) / s_n)**2 + np.log(2 * pi * s_n**2)) logp += self.model.logp() logp += self.logp_extra(self.model, self.data)
where
s_n**2 = y_err**2 + exp(2 * lnsigma) * model**2
At the moment s_n**2, the variance of the measurement uncertainties, is assumed to be unity. A future release may implement those uncertainties
- logp(pvals=None)
Calculate the log-prior of the system.
- Parameters:
pvals (array-like or refnx.analysis.Parameters) – values for the varying or entire set of parameters
- Returns:
logp – log-prior probability
- Return type:
float
Notes
The log-prior is calculated as:
logp = np.sum(param.logp() for param in self.varying_parameters())
- logpost(pvals=None)
Calculate the log-probability of the curvefitting system
- Parameters:
pvals (array-like or refnx.analysis.Parameters) – values for the varying or entire set of parameters
- Returns:
logpost – log-probability
- Return type:
float
Notes
The overall log-probability is the sum of the log-prior and log-likelihood. The log-likelihood is not calculated if the log-prior is impossible (logp == -np.inf).
- property npoints
int the number of points in the dataset.
- property parameters
refnx.analysis.Parameters, all the Parameters contained in the fitting system.
- pgen(ngen=1000, nburn=0, nthin=1)
Yield random parameter vectors from the MCMC samples. The objective state is not altered.
- Parameters:
ngen (int, optional) – the number of samples to yield. The actual number of samples yielded is min(ngen, chain.size)
nburn (int, optional) – discard this many steps from the start of the chain
nthin (int, optional) – only accept every nthin samples from the chain
- Yields:
pvec (np.ndarray) – A randomly chosen parameter vector
- plot(xaxis=None, plot_labels=True, fig=None)
Plot the data/model.
Requires matplotlib be installed.
- Parameters:
xaxis (String, optional) – Either ‘aoi’ or ‘wavelength’. If none specified, ‘wavelength’ will be chosen unless there is more than 1 unique aoi.
plot_labels (Bool, optional) – Whether to plot axis labels. The default is True.
fig (Figure instance, optional) – If fig is not supplied then a new figure is created. Otherwise the graph is created on the current axes on the supplied figure.
- Returns:
fig, ax – matplotlib figure and axes objects.
- Return type:
matplotlib.Figure,matplotlib.Axes
- prior_transform(u)
Calculate the prior transform of the system.
Transforms uniform random variates in the unit hypercube, u ~ uniform[0.0, 1.0), to the parameter space of interest, according to the priors on the varying parameters.
- Parameters:
u (array-like) – Size of the varying parameters
- Returns:
pvals – Scaled parameter values
- Return type:
array-like
Notes
If a parameter has bounds, x ~ Unif[-10, 10) then the scaling from u to x is done as follows:
x = 2. * u - 1. # scale and shift to [-1., 1.) x *= 10. # scale to [-10., 10.)
- residuals(pvals=None)
Calculates the residuals for a given fitting system.
- Parameters:
pvals (array-like or refnx.analysis.Parameters) – values for the varying or entire set of parameters
- Returns:
residuals – Residuals, (data.y - model) / y_err.
- Return type:
np.ndarray
- setp(pvals)
Set the parameters from pvals.
- Parameters:
pvals (array-like or refnx.analysis.Parameters) – values for the varying or entire set of parameters
- varying_parameters()
- Returns:
varying_parameters – The varying Parameter objects allowed to vary during the fit.
- Return type:
refnx.analysis.Parameters
- property weighted
bool Does the data have weights (data.y_err), and is the objective using them?
- refellips.objectiveSE.circular_distance(angle1, angle2, period=360)
Calculates the circular distance between two angles. Units (rad or deg) do not matter as long as they are consistent.
- Parameters:
angle1 (float, np.ndarray) – First angle
angle2 (float, np.ndarray) – Second angle
period (float) – The period of the circular domain (e.g.,360 for full circle).
Returns – float or np.ndarray: The shortest circular distance between the angles.