BayesicFitting

Model Fitting and Evidence Calculation

View project on GitHub



class ErrorsInXandYProblem( Problem )Source

A ErrorsInXandYProblem is an optimization of parameters which involves the fitting of data to a Model, where both the ydata and the xdata contain errors.

It entails that the xdata are not at the exact locations. They need to be optimized too. Consequently the parameters of the model are appended with a set of parameters, the length of xdata. These extra parameters will contain the target locations of the xdata. The 2-dimensional distance between data (xdata,ydata) and (target, model(target)) is minimised. The target are nuisance parameters which are not part of the modeling solution.

Define
     xd = xdata
     yd = ydata
     u = target
     F(u:P) = model( target )

Then the mismathes in both directions are
     X = u - xd
     Y = F(u:p) - yd

Both distances need to be minimized, possibly in the presence of a correlation between the mismatches X and Y

As the targets need to be optimised they need a Prior. In the present implementation there is the same Prior for all targets, which the centered on each of the xdata values. S.Gull argues to use a GaussPrior with a scale similar to the errors in both X and Y.

Attributes

  • prior : Prior
         Priors for the x-axis nuisance parameters.
  • varxx : float or ndarray of shape (ndata,)
         Variance in the xdata errors
  • varyy : float or ndarray of shape (ndata,)
         Variance in the ydata errors
  • varxy : float or ndarray of shape (ndata,)
         Covariance in the xdata and ydata errors

Attributes from Problem

model, xdata, ydata, weights, partype

  • Author : Do Kester

ErrorsInXandYProblem( model=None, xdata=None, ydata=None, weights=None, prior=None, covar=None, accuracy=None, copy=None )

Problem Constructor.

Parameters

  • model : Model
         the model to be solved
  • xdata : array_like or None
         independent variable
  • ydata : array_like or None
         dependent variable
  • weights : array_like or None
         weights associated with data
  • covar : ndarray of shape (2,2) or (ndata,2,2)
         covariance matrix of the errors in x and y
         (2,2) : valid for all datapoints
         (ndata,2,2): one for each datpoint
         Default is [[1,0],[0,1]]
  • accuracy : ndarray of shape (2,) or (3,) or (ndata,2) or (ndata,3)
         accuracy scale for the datapoints
         (2,) scale for resp. y and x, valid for all datapoints
         (3,) scale for y and y, and correlation coefficient between y and x, valid for all
         (ndata,2) or (ndata,3) one set of values for each datapoint
         Alternative for covarince matrix.
         covar = [[ acc[0]^2, 0], [0, acc[1]^2]] no correlation
               = [[ acc[0]^2, r], [r, acc[1]^2]] where r = acc[0] * acc[1] * acc[2]
         accuracy is converted to covar; default is covar.
  • prior : Prior
         prior for the x-axis nuisance parameters. All centered on each of the xdata
  • copy : Problem
         to be copied

copy( )
Copy.

setAccuracy( accuracy=None, covar=None )
Store 3 items from the covar matrix :

     | varyy, varxy |
     | varxy, varxx |

  When the accuracy is given, convert it to these items by
   varyy = acc[0] * acc[0]
   varxx = acc[1] * acc[1]
   varxy = acc[0] * acc[1] * acc[2]

Store also the determinant of the covariance matrix.

  When both accuracy and covar are None
   varyy = 0
   varxx = 1
   varxy = 0

Raises

AttributeError. When both accuracy and covar are not None.

Parameters

  • accuracy : ndarray of shape (2,) or (3,) or (ndata,2) or (ndata,3)
         accuracy scale for the datapoints
         (2,) scale for resp. y and x, valid for all datapoints
         (3,) scale for y and y, and correlation coefficient between y and x, valid for all
         (ndata,2) or (ndata,3) one set of values for each datapoint
         Alternative for covarince matrix.
         vxy = 0 if no correlation else acc[0] * acc[1] * acc[2]
         covar = [[ acc[0]^2, vxy ],
                  [ vxy , acc[1]^2 ]]
         accuracy is converted to covar; default is covar.
  • covar : ndarray of shape (2,2) or (ndata,2,2) or None
         covariance matrix of the errors in x and y

hasWeights( )

Return whether it has weights.

result( param )
Returns the result calculated at the xdatas.

Parameters

  • param : array_like
         values for the parameters + nuisance params.

splitParam( param )
Split the parameters into Model parameters and targets.

Parameters

  • param : array_like
         values for the parameters + nuisance params.

Return

tuple of ( targets, model parameters )

partial( param )
Return the partials as a matrix [2*nx,np+nx], where nx is the number of datapoints and np the number of parameters in the model.

     The upper left submatrix (nx,np) contains dM/dp
     the upper right submatrix (nx,nx) contains dM/dx on the diagonal
     the lower left submatrix (nx,np) contains zeros
     the lower right submatrix (nx,nx) contains the identity matrix

Parameters

  • param : array_like
         values for the parameters + nuisance params.

derivative( param )
Return the derivative to the Model.

Parameters

  • params : array_like
         list of problem parameters

domain2Unit( dval, kpar )
Return value in [0,1] for the selected parameter.

Parameters

  • dval : float
         domain value for the selected parameter
  • kpar : int
         selected parameter index, where kp is index in [parameters, hyperparams]

unit2Domain( uval, kpar )
Return domain value for the selected parameter.

Parameters

  • uval : array_like
         unit value for the selected parameter
  • kpar : int
         selected parameter indices, where kp is index in [parameters, hyperparams]

getXYresiduals( param )
Return residuals in y-direction and x-direction.

Parameters

  • param : array_like
         model parameters and xdata parameters

Returns

tuple of (y residuals, x residuals)

weightedResSq( allpars, mockdata=None, extra=False )
Return the (weighted) squared distance between (xdata,ydata) and (xtry,ytry) where xtry are the trial values for xdata and ytry = model.result( xtry, param )

Parameters

  • allpars : array_like
         model parameters, xdata parameters, and noise scale
  • mockdata : array_like
         model fit data model.result( xtry, param )
  • extra : bool (False)
         true : return ( wgt * res^2, wgt * [yres,xres] )
         false : return wgt * res2

myEngines( )
Return a default list of preferred engines

myStartEngine( )
Return a default preferred start engines: "start"

myDistribution( )
Return a default preferred ErrorDistribution: "gauss2d"

baseName( )

Returns a string representation of the model.

Methods inherited from Problem