Model

 
Model = class Model(FixedModel)
    Model(nparams=0, ndim=1, copy=None, params=None, **kwargs)
 
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
 
 
Method resolution order:
Model
FixedModel
BaseModel
builtins.object

Constructor:
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
Methods defined here:
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
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.
assignDF1( partial, i, dpi )
assignDF2( partial, i, dpi )
chainLength()
Return length of the chain.
copy()
Return a copy.
correctParameters( params )
Check parameters for non-zero and positivity
 
Parameters
----------
params : array_like
    parameters for the model.
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.
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
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
getIntegralUnit()
Return the unit of the integral of the model over x.
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)
getLinearIndex()
Return null.
getNumberOfParameters()
Returns the number of parameters of the ( compound ) model.
getParameterName( k )
Return the name of the indicated parameter.
 
Parameters
----------
k : int
    parameter number.
 
Raises
------
IndexError when k is larger than the number of parameters.
getParameterUnit( k )
Return the unit of the indicated parameter.
 
Parameters
----------
k : int
    parameter number.
 
Raise
-----
IndexError when k is > number of parameters
getPrior( k )
Return the prior of the indicated parameter.
 
Parameters
----------
k : int
    parameter number.
 
Raises
------
IndexError when k is larger than the number of parameters.
hasLimits( fitindex=None )
Return true if limits has been set for this model.
 
Parameters
----------
fitindex    list of indices to be fitted.
hasPriors( isBound=True )
Return True when the model has priors for all its parameters.
 
Parameters
----------
isBound : bool
    Also check if the prior is bound.
isDynamic()
Return whether the model can change the number of parameters dynamically.
isMixed()
Return false.
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.
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
nextPrior()
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
operate( res, pars, next )
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.
partialDomain2Unit( dvalue )
Return a the derivate of the domain2Unit function to dval.
 
Parameters
----------
dvalue : (list of) float
   parameter array
pipeModel( model )
Make a compound model by piping the result into the next.
 
Parameters
----------
model : Model
    model to pipe into
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
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
selectPipe( ndim, ninter, ndout )
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
setPrior( k, prior=None, **kwargs )
Set the prior for the indicated parameter.
 
Parameters
----------
k : int
    parameter number.
prior : Prior
    prior for parameter k
kwargs : keyword arguments
    attributes to be passed to the prior
 
Raises
------
IndexError when k is larger than the number of parameters.
shortName()
Return a short version the string representation: upto first non-letter.
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
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.
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
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.
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
__add__( model )
Method for making compound models using + operator.
 
>>> model1 = model2 + model3
 
Parameters
----------
model : Model
    a copy of model to add to a copy of self (the existing chain)
__call__( x )
Return the result of the model.
 
>>> y = model( x )
is equivalent to
>>> y = model.result( x, model.parameters )
 
Parameters
----------
x : (list of) float
    apply the model to x (as input)
__getitem__( i )
Return the i-th parameter.
 
>>> p_i = model[i]
 
Parameters
----------
i : int
    index for the parameter.
__iadd__( model )
Method for making compound models using += operator.
 
>>> model1 += model2
 
Parameters
----------
model : Model
    a model to add to self (the existing chain)
__imul__( model )
Method for making compound models using *= operator.
 
>>> model1 *= model2
 
Parameters
----------
model : Model
    a model to multiply with self (the existing chain)
__ior__( model )
Method for making compound models using |= operator.
 
>>> model1 |= model2
 
Use the results of model1 as input for model2.
 
Parameters
----------
model : Model
    a model to pipe the previous results through
__isub__( model )
Method for making compound models using -= operator.
 
>>> model1 -= model2
 
Parameters
----------
model : Model
    a model to subtract from self (the existing chain)
__itruediv__( model )
Method for making compound models using /= operator.
 
>>> model1 /= model2
 
Parameters
----------
model : Model
    a model to divide self with (the existing chain)
__mul__( model )
Method for making compound models using * operator.
 
>>> model1 = model2 * model3
 
Parameters
----------
model : Model
    a copy of model to multiply with a copy of self (the existing chain)
__or__( model )
Method for making compound models using | (pipe) operator.
 
>>> model1 = model2 | model3
 
Parameters
----------
model : Model
    a copy of model to pipe the results of a copy of self (the existing chain)
__sub__( model )
Method for making compound models using - operator.
 
>>> model1 = model2 - model3
 
Parameters
----------
model : Model
    a copy of model to subtract from a copy of self (the existing chain)
__truediv__( model )
Method for making compound models using / operator.
 
>>> model1 = model2 / model3
 
Parameters
----------
model : Model
    a copy of model to divide a copy of self with (the existing chain)

Methods inherited from FixedModel:
Methods inherited from BaseModel: