BayesicFitting

Model Fitting and Evidence Calculation

View project on GitHub



class DecisionTreeModel( Modifiable,Dynamic,LinearModel )Source

A DecisionTree Model (DTM) is mostly defined on multiple input dimensions (axes). It splits the data in 2 parts, according low and high values on a certain input axis. The splitting can continue along other axes.

The axes can contain float values, categorials (int) or booleans. The float axes can have Nans when data are unkown. Each category set one bit in an integer. The unknown category is a category by itself. Booleans that contain unknowns should be coded as categorial.

     f( x:p ) = DTM
                  |-> left => DTM (or None)
                  |-> rite => DTM (or None)
                  |-> dimension
                  |-> split or mask

The tree is searched left to right.

The parameters are all initialized at 0.0

Examples

dtm = DecisionTreeModel( )
print( dtm )
DecisionTree: with 0 components and 1 parameters

Attributes

  • left : None or DTM
         a None the tree stops otherwise there is a new split.
  • rite : None or DTM
         a None the tree stops otherwise there is a new split.
  • dimension : int
         split according to data values on this axes
  • itype : char. Either 'f' (float), 'i' (integer), or 'b' (boolean)
         characterizes the input dimension as float, integer or boolean
  • split : float between 0 and 1 (for float dimension)
         to the left normalized values < split; to the rite normvalues > split.
         Unknown values (NaNs to to the smallest faction).
  • nsplit : int
         number of calls to random.rand() to be averaged. Prior on "split"
  • mask : float between 0 and 1 (for float dimension)
         to the left normalized values < split; to the rite normvalues > split.
         Unknown values (NaNs to to the smallest faction.

Attributes from Modifiable

     modifiable

Attributes from Dynamic

     dynamic

Attributes from Model

     npchain, parameters, stdevs, xUnit, yUnit

Attributes from FixedModel

     npmax, fixed, parlist, mlist

Attributes from BaseModel

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

DecisionTreeModel( ndim=1, depth=0, split=0.5, kdim=0, itypes=[0], modifiable=True, dynamic=True, code=None, growPrior=None, copy=None, **kwargs )

DecisionTree model.

The DTM standardly has a UniformPrior for all parameters, with limits [0,1]

Parameters

  • ndim : int
         number of input dimensions
  • depth : int
         depth of the tree
  • kdim : None or list of ints
         input channel to be splitted.
  • itypes : [list of] int
         indicating the type of input: 0 for float, 1 for boolean, >1 for categorial
         The last number is repeated for remaining inputs
  • split : None or float or list of floats
         fraction (0<s<1) of the input kdim that falls on either side.
  • growPrior : None or Prior
         Governing the growth
  • modifiable : bool (True)
         Will the model modify dimension and/or split/mask
  • dynamic : bool (True)
         Will the model grow/shrink

copy( )

Copy method.

setSplitOrMask( itype, split )

For internal use only

isLeaf( )
Return true if self is a leaf

partitionList( xdata, plist )
Partition the xdata in plist over 2 new lists according to the DTM-branch.

Paramaters

  • xdata : array-like
         the xdata
  • plist : array of ints
         indices in xdata

Return

  • pl1, pl2 : 2 lists
         together containing all indices in plist

baseResult( xdata, params )
Returns the result of the model function.

Parameters

  • xdata : array_like
         values at which to calculate the result
  • params : array_like
         values for the parameters.

recursiveResult( xdata, params, kpar, plist, res )

For internal use only

basePartial( xdata, params, parlist=None )
Returns the partials at the input value.

Parameters

  • xdata : array_like
         values at which to calculate the partials
  • params : array_like
         values for the parameters.
  • parlist : array_like
         list of indices active parameters (or None for all)

recursivePartial( xdata, kpar, plist, part )

For internal use only

sortXdata( xdata )
Reorder the xdata according to the parameter ordering

Parameters

  • xdata : array_like
         values at which to calculate the partials

recursiveOrder( xdata, plist )

For internal use only

baseDerivative( xdata, params )
Return the derivative df/dx at each xdata (=x).

Parameters

  • xdata : array_like
         values at which to calculate the result
  • params : array_like
         values for the parameters.

baseName( )
Returns a string representation of the model.

fullName( ids=False )
Returns a string representation of the model.

Parameters

  • ids : bool
         if True give the pointers of the links too.

recursiveName( name, indent, kpar, ids=False )

For internal use only

baseParameterUnit( k )
Return the unit of the indicated parameter.

Parameters

  • k : int
         parameter number.

walk( )
Iterate tree in pre-order depth-first search order

Found this piece of code on the internet. Fairly obscure.

encode( )
Make a code tuple to be used by the constructor to resurrect the DTM The tuple consists of code : list of (list or 1 or 2), encoding the structure of DTM dims : list of dimensions at the branches splim : list of split/mask values at the branches

decode( code, kdim, splim, kbr )
Resurrect the DTM from the code generated by encode(). For internal use in the Constructor.

Parameters

  • code : list
         of (list or 1 or 2), encoding the structure of DTM
  • dims : list
         of dimensions at the branches
  • splim : list
         of split/mask values at the branches
  • kbr : int
         counter (start at 0)

findLeaf( kleaf )
Find a leaf in the tree, returning the leaf.

Parameter

  • kleaf : int
         index of the leaf to be found

findBranch( kbranch )
Find a branch in the tree, returning the branch.

Parameter

  • kbranch : int
         index of the branch to be found

check( )
Find a branch in the tree, returning the branch.

Parameter

  • kbranch : int
         index of the branch to be found

findRoot( )
Return the root of the tree.

count( )
Return number of leafs and branches.

countLeaf( )
Return number of leafs.

countBranch( )
Return number of leafs.

grow( offset=0, rng=None, location=0, split=0.5, kdim=0 )
Increase the the number of components by 1 (if allowed by maxComp)

Parameters

  • offset : int
         offset in the parameter list (pertaining to earlier models in the chain)
  • rng : Random Number Generator
         to obtain random values for items below.
  • location : int
         location where the new dtm-leaf should be inserted
  • kdim : int (<self.ndim)
         dimension to split
  • split : float (0<split<1)
         relative cut on this dimension

Return

  • bool : succes

shrink( offset=0, rng=None, location=0 )
Decrease the the number of componenets by 1 (if allowed by minComp) Remove an arbitrary item.

Parameters

  • offset : int
         location where the new params should be inserted

Return

  • bool : succes

vary( rng=None, location=0, split=0.5, kdim=0 )
Vary the model structure by changing kdim and/or split at location

Parameters

  • rng : Random Number Generator
         to obtain random values for items below.
  • location : int
         location where the dtm-branch should be changed
  • kdim : int (<self.ndim)
         dimension to split
  • split : float (0<split<1)
         relative cut on this dimension

Return

  • bool : succes
Methods inherited from Modifiable,
Methods inherited from Dynamic,
Methods inherited from LinearModel
Methods inherited from Model
Methods inherited from FixedModel
Methods inherited from BaseModel