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( ) |
---|
chainLength( ) |
---|
isNullModel( ) |
---|
isolateModel( k ) |
---|
Parameters
- k : int
the model number ( head = 0 )
Raises
IndexError when the chain is shorter than k.
addModel( model ) |
---|
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 ) |
---|
The final result is the difference of the models.
Parameters
- model : Model
model to be subtracted from
multiplyModel( model ) |
---|
The final result is the product of the models.
Parameters
- model : Model
model to be multiplied by
divideModel( model ) |
---|
The final result is the division of the models.
Parameters
- model : Model
model to be divided by
pipeModel( model ) |
---|
Parameters
- model : Model
model to pipe into
appendModel( model, 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 ) |
---|
Parameters
- params : array_like
parameters for the model.
result( xdata, param=None ) |
---|
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 ) |
---|
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 ) |
---|
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 ) |
---|
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 ) |
---|
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 ) |
---|
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 ) |
---|
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 ) |
---|
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 ) |
---|
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 ) |
---|
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 ) |
---|
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 ) |
---|
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 ) |
---|
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 ) |
---|
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( ) |
---|
getNumberOfParameters( ) |
---|
Returns the number of parameters of the ( compound ) model.
numDerivative( xdata, param ) |
---|
Parameters
- xdata : array_like
input data - param : array_like
a parameters vector
numPartial( xdata, param ) |
---|
Parameters
- xdata : array_like
input data - param : array_like
a parameters vector
isDynamic( ) |
---|
hasPriors( isBound=True ) |
---|
Parameters
- isBound : bool
Also check if the prior is bound.
getPrior( kpar ) |
---|
Parameters
- kpar : int
parameter number.
Raises
IndexError when kpar is larger than the number of parameters.
setPrior( kpar, prior=None, **kwargs ) |
---|
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 ) |
---|
Parameters
- kpsr : int
parameter number.
Raises
IndexError when kpar is larger than the number of parameters.
getParameterUnit( kpar ) |
---|
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 ) |
---|
- It is valid to insert for either parameter a None value indicating no lower/upper limits.
- 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( ) |
---|
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 ) |
---|
Parameters
fitindex list of indices to be fitted.
unit2Domain( uvalue, kpar=None ) |
---|
Parameters
- uvalue : (list of) float
value in [0,1] - kpar : int
index of the parameter
domain2Unit( dvalue, kpar=None ) |
---|
Parameters
- dvalue : (list of) float
value of parameter - kpar : int
index of the parameter
partialDomain2Unit( dvalue ) |
---|
Parameters
- dvalue : (list of) float
parameter array
nextPrior( ) |
---|
isMixed( ) |
---|
getLinearIndex( ) |
---|
testPartial( xdata, params, silent=True ) |
---|
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 ) |
---|
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 ) |
---|
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 |
---|
- select( params )
- selectNames( names )
- expand( xdata, param )
- basePartial( xdata, param, parlist=None )
Methods inherited from BaseModel |
---|