BayesicFitting

Model Fitting and Evidence Calculation

View project on GitHub



class Model( FixedModel )Source

Model implements the common parts of (compound) models. It is the last common anchestor of all Models.

Models can be handled by the Fitter classes.

A model consists of one or more instantiations of (base) models which are concatenated in a chain of models using various operations (+-*/). A special operation is the pipe (|). It works like a unix pipe, i.e. the output of the left-hand process in used as input of the right-hand process.

Methods defined in BaseModel as eg. baseResult() are recursively called here as result(). They are the ones used in the fitters.

The Model is the place where model-related items are kept, like parameters, stdevs.

Model also implements a numerical derivation of partial to be used when partial is not given in the model definition itself. This same numerical derivation of partial is used in testPartial to indeed test whether the partial has been implemented properly.

Example:

x = numpy.arange( 10 )
poly = PolynomialModel( 2 )             # quadratic model
poly.parameters = [3,2,1]               # set the parameters for the model
y = poly( x )                           # evaluate the model at x
p0 = poly[0]                            # 3: the first parameter

# To make a compound model consisting of a gaussian and a constant background

gauss = GaussModel( )                   # gaussian model
gauss += PolynomialModel( 0 )           # gaussian on a constant background
print( gauss.getNumberOfParameters( ) )
4

# Set limits to this model

lolim = [0,-10,0,-5]                    # lower limits for the parameters
hilim = [10,10,2, 5]                    # high limits for parameters
gauss.setLimits( lolim, hilim )         # set limits. Does not work with all Fitters

# Pipe a model; The order of operation matters.
# m5 = ( m1 | m2 ) + m3

m1 = PolynomialModel( 1 )               # m1( x, p )
m2 = SineModel()                        # m2( x, q )
m3 = PolynomialModel( 0 )               # m3( x, r )
m4 = m1 | m2                            # m2( m1( x, p ), q )
m5 = m4 + m3                            # m2( m1( x, p ), q ) + m3( x, r )
print( m5.parameters )                  # [p, q, r]

# Implicit brackets
# m5 = m1 | ( m2 + m3 )

m1 = PolynomialModel( 1 )               # m1( x, p )
m2 = SineModel()                        # m2( x, q )
m3 = PolynomialModel( 0 )               # m3( x, r )
m4 = m2 + m3                            # m2( x, q ) + m3( x, r )
m5 = m1 | m4                            # m2( m1( x, p ), q ) + m3( m1( x, p ), r )
print( m5.parameters )                  # [p, q, r]

Attributes

  • parameters : array_like
         parameters of the model
  • stdevs : None or array_like
         standard deviations after a fit to the data
  • xUnit : astropy.units or list of
         unit of the x-values (list of in case of more dimensions)
  • yUnit : astropy.units
         unit of the y-values
  • npars : int (read only)
         number of parameters in this model
  • npchain : int (read only)
         identical to npars

Attributes from FixedModel

     npmax, fixed, parlist, mlist

Attributes from BaseModel

     npbase, ndim, priors, posIndex, nonZero, tiny, deltaP, parNames

Author Do Kester

Model( nparams=0, ndim=1, copy=None, params=None, **kwargs )

Initializes the Model with all attributes set to None, except for the parammeters which are all initialized to 0.

Parameters

  • nparams : int
         the number of parameters in this model
  • ndim : int
         the dimensionality of the xdatas (default: 1)
  • copy : Model
         model to be copied (default: None)
  • params : array_like
         initial parameters of the model
  • fixed : None or dictionary of {int:float|Model}
         int index of parameter to fix permanently.
         float|Model values for the fixed parameters.
         Attribute fixed can only be set in the constructor.
         See: FixedModel

copy( )

Return a copy.

chainLength( )

Return length of the chain.

isNullModel( )
Return True if the model has no parameters (a NullModel).

isolateModel( k )
Return a ( isolated ) copy of the k-th model in the chain. Fixed parameters and priors which might be present in the compound model will be lost in the isolated model.

Parameters

  • k : int
         the model number ( head = 0 )

Raises

IndexError when the chain is shorter than k.

addModel( model )
Make a compound model by concatinating/adding model to this.

The final result is the sum of the individual results.

The compound model is implemented as a chain of Models. Each of these base models contain the attributes ( parameters, limits etc. ) and when needed these attributes are taken from there, or stored there.

The operation (addition in this case) is always with the total result of the existing chain. For the use of "brackets" in a chain use BracketModel.

Parameters

  • model : Model
         model to be added to

subtractModel( model )
Make a compound model by concatinating/subtracting a model from this.

The final result is the difference of the models.

Parameters

  • model : Model
         model to be subtracted from

multiplyModel( model )
Make a compound model by concatinating/multiplying a model with this.

The final result is the product of the models.

Parameters

  • model : Model
         model to be multiplied by

divideModel( model )
Make a compound model by concatinating/dividing by a model.

The final result is the division of the models.

Parameters

  • model : Model
         model to be divided by

pipeModel( model )
Make a compound model by piping the result into the next.

Parameters

  • model : Model
         model to pipe into

appendModel( model, operation )
Append a model to the present chain using a operation.

Parameters

  • model : Model
         the model to be appended
  • operation : int
         operation index

Raises

ValueError when a model of a different dimensionality is offered.

correctParameters( params )
Check parameters for non-zero and positivity

Parameters

  • params : array_like
         parameters for the model.

result( xdata, param=None )
Return the result of the model as applied to an array of input data.

Parameters

  • xdata : array_like
         input data
  • param : array_like
         parameters for the model. Default parameters from the Model

operate( res, pars, next )

derivative( xdata, param, useNum=False )
Return the derivatives (df/dx) of the model at the inputs

Parameters

  • xdata : array_like
         an input vector or array
  • param : array_like
         parameters for the model
  • useNum : bool
         if true, numeric derivatives are used.

partial( xdata, param, useNum=False )
Return the partial derivatives of the model at the inputs

Parameters

  • xdata : array_like
         an input vector or array
  • param : array_like
         parameters for the model
  • useNum : bool
         if true, numeric partials are used.

selectPipe( ndim, ninter, ndout )
Select one of 9 pipe operations, depending on the dimensionality of the inputs and outputs of the model G and H

Model G has pars p and model H has pars q.

  F(x:pq) ==> H(G(x:p):q) G(x:p) | H(*:q)

  dF/dp ==> dH/dG * dG/dp H.derivative(G,p) * G.partial(x,q)
  dF/dq ==> dH(G(x:p):q) / dq G.partial(H,q)

  G.ndout mustbe H.ndim
  partial <== G
  dfdx <== H

Parameters

  • ndim : int
         input dimensions to G and thus to F
  • ninter : int
         output dim of G and input dim to H (must be same)
  • ndout : int
         output dimensions of H and thus of F

pipe_0( dGd, dHdG )
ninter == 1 and ndout == 1

Return partial in the form of [N,P]

Parameters

  • dGd : array of form [N,P]
         Either partial dGdp or derivative dGdx
  • dHdG : array of form [N]
         Derivative of H to G

pipe_1( dGd, dHdG )
ninter > 1 and ndout > 1

Return partial in the form [O][N,P]

Parameters

  • dGd : array of form [K][N,P]
         Either partial dGdp or derivative dGdx
  • dHdG : array of form [O][N,K]
         Derivative of H to G

pipe_2( dGd, dHdG )
ndim == 1 and ninter > 1 and ndout == 1

Return partial in the form of [N,P]

Parameters

  • dGd : array of form [K][N,P]
         Either partial dGdp or derivative dGdx
  • dHdG : array of form [N,K]
         Derivative of H to G

pipe_3( dGd, dHdG )
ndim == 1 and ninter = 1 and ndout > 1

Return partial in the form of [O][NP]

Parameters

  • dGd : array of form [N,P]
         Either partial dGdp or derivative dGdx
  • dHdG : array of form [N,0]
         Derivative of H to G

pipe_4( dGdx, dHdG )
ndim == 0 and ninter == 1 and ndout == 1

Return partial in the form of [N]

Parameters

  • dGdx : array of form [N]
         Derivative dGdx
  • dHdG : array of form [N]
         Derivative of H to G

pipe_5( dGdx, dHdG )
ndim == 1 and ninter > 1 and ndout > 1

Return derivative in the form of [N,O]

Parameters

  • dGdx : array of form [N,K]
         Either partial dGdp or derivative dGdx
  • dHdG : array of form [O][N,K]
         Derivative of H to G

pipe_6( dGdx, dHdG )
ndim == 1 and ninter > 1 and ndout == 1

Return derivative in the form of [N]

Parameters

  • dGdx : array of form [N,K]
         Either partial dGdp or derivative dGdx
  • dHdG : array of form [N,K]
         Derivative of H to G

pipe_7( dGdx, dHdG )
ndim == 1 and ninter = 1 and ndout > 1

Return derivative in the form of [N,O]

Parameters

  • dGdx : array of form [N,O]
         Either partial dGdp or derivative dGdx
  • dHdG : array of form [N]
         Derivative of H to G

pipe_8( dGdx, dHdG )
ndim > 1 and ninter > 1 and ndout == 1

Return derivative in the form of [N,I]

Parameters

  • dGdx : array of form [K][N,I]
         Either partial dGdp or derivative dGdx
  • dHdG : array of form [N,K]
         Derivative of H to G

pipe_9( dGdx, dHdG )
ndim > 1 and ninter == 1 and ndout > 1

Return derivative in the form of [O][N,I]

Parameters

  • dGdx : array of form [N,I]
         Either partial dGdp or derivative dGdx
  • dHdG : array of form [N,O]
         Derivative of H to G

shortName( )
Return a short version the string representation: upto first non-letter.

getNumberOfParameters( )

Returns the number of parameters of the ( compound ) model.

numDerivative( xdata, param )
Returns numerical derivatives (df/dx) of the model function.

Parameters

  • xdata : array_like
         input data
  • param : array_like
         a parameters vector

numPartial( xdata, param )
Returns numerical partial derivatives of the model function.

Parameters

  • xdata : array_like
         input data
  • param : array_like
         a parameters vector

isDynamic( )
Return whether the model can change the number of parameters dynamically.

hasPriors( isBound=True )
Return True when the model has priors for all its parameters.

Parameters

  • isBound : bool
         Also check if the prior is bound.

getPrior( kpar )
Return the prior of the indicated parameter.

Parameters

  • kpar : int
         parameter number.

Raises

IndexError when kpar is larger than the number of parameters.

setPrior( kpar, prior=None, **kwargs )
Set the prior for the indicated parameter.

Parameters

  • kpar : int
         parameter number.
  • prior : Prior
         prior for parameter kpar
  • kwargs : keyword arguments
         attributes to be passed to the prior

Raises

IndexError when kpar is larger than the number of parameters.

getParameterName( kpar )
Return the name of the indicated parameter.

Parameters

  • kpsr : int
         parameter number.

Raises

IndexError when kpar is larger than the number of parameters.

getParameterUnit( kpar )
Return the unit of the indicated parameter.

Parameters

  • kpar : int
         parameter number.

Raise

IndexError when kpar is > number of parameters

getIntegralUnit( )

Return the unit of the integral of the model over x.

setLimits( lowLimits=None, highLimits=None )
Sets the limits for the parameters of the compound model.
  1. It is valid to insert for either parameter a None value indicating no lower/upper limits.
  2. When a lowerlimit >= upperlimit no limits are enforced. It only works in *Fitter classes which support it.

Parameters

  • lowLimits : array_like
         lower limits on the parameters
  • highLimits : array_like
         upper limits on the parameters

getLimits( )
Return the limits stored in the priors

Returns

  • limits : tuple of 2 array-like or of 2 None (if self.priors is None)
         (lowlimits, highlimits)

lolim = [] hilim = [] mdl = self

  • while mdl is not None :
         if not super( Model, mdl ).hasLimits( )
             return [None,None]
         lolim += [p.lowLimit for p in mdl.priors]
         hilim += [p.highLimit for p in mdl.priors]

     mdl = mdl._next return (lolim, hilim)

hasLimits( fitindex=None )
Return true if limits has been set for this model.

Parameters

fitindex list of indices to be fitted.

unit2Domain( uvalue, kpar=None )
Convert a value in [0,1] to one inside the limits of the parameter.

Parameters

  • uvalue : (list of) float
         value in [0,1]
  • kpar : int
         index of the parameter

domain2Unit( dvalue, kpar=None )
Convert a value within the domain of the parameter to one in [0,1].

Parameters

  • dvalue : (list of) float
         value of parameter
  • kpar : int
         index of the parameter

partialDomain2Unit( dvalue )
Return a the derivate of the domain2Unit function to dval.

Parameters

  • dvalue : (list of) float
        parameter array

nextPrior( )

isMixed( )

Return false.

getLinearIndex( )

Return null.

testPartial( xdata, params, silent=True )
A test routine to check the calculation of the partial derivatives.

It is compared to a numerical calculation.

Parameters

  • xdata : (list of) float
         values of the independent variable
  • params : list of floats
         parameters for the model
  • silent : bool
         if false print outputs

Return

The number of large deviations.

strictNumericPartial( xdata, params, parlist=None )
Strictly numeric calculation of partials to params.

For compound models it is different from numPartial and numDerivative.

Parameters

  • xdata : float
         single xdata point (possibly multidimensional)
  • params : array-like
         parameters
  • kpar : None or int or list of int
         int : return derivative to parameter kpar.

assignDF1( partial, i, dpi )

assignDF2( partial, i, dpi )

strictNumericDerivative( xdata, param )
Strictly numeric calculation of derivative.

For compound models it is different from numPartial and numDerivative.

     ## More dimensions in x

Parameters

  • xdata : float
         single xdata point (possibly multidimensional)
  • param : array-like
         parameters
Methods inherited from FixedModel
Methods inherited from BaseModel