BayesicFitting

Model Fitting and Evidence Calculation

View project on GitHub



class Prior( object )Source

Base class defining prior distributions.

Two methods need to be defined in specific priors which map the values between [0,1] on to the domain, and vice versa unit2Domain and domain2Unit.

     u = domain2Unit( d )
     d = unit2Domain( u )

d is a value in the domain of the prior and u is a vlue in [0,1]

The handling of limits is relegated to this Prior class.

  Define
     umin = domain2Unit( lowLimit )
     urange = domain2Unit( highLimit ) - umin

     u = ( domain2Unit( d ) - umin ) / urange
     d = unit2Domain( u * urange + umin )

Symmetric priors can be used in a circular variant; i.e. the low and high limits are folded on each other, provided that the limit values are the same (hence symmetric)

     u = limitedDomain2Unit( d ) + 1 ) / 3
     d = limitedUnit2Domain( ( u * 3 ) % 1 )

The copy method is also necessary.

Attributes

  • lowLimit : float
         low limit on the Prior
  • highLimit : float
         high limit on the Prior
  • deltaP : float
         width of numerical partial derivative calculation
  • circular : bool or float
         whether circular

Hidden Attributes

  • _lowDomain : float
         lower limit of the Priors possible values
  • _highDomain : float
         upper limit of the Priors possible values
  • _umin : float
         umin lowLimit in unit
  • _urng : float
         urange (hi-lo) in unit

Prior( limits=None, circular=False, domain=[-math.inf,math.inf], prior=None )

Default constructor.

Parameters

  • limits : None or list of 2 floats
         2 limits resp. low and high
  • circular : bool or float
         False not circular
         True circular with period from limits[0] to limits[1]
         period of circularity
  • domain : 2 floats
         over which the prior is defined
  • prior : Prior
         prior to copy (with new limits if applicable)

copy( )

Return a copy

limitedIntegral( center=0, circular=False, limits=None )
Calculate the Integral of the prior where tails are cut off due to limits or circularity.

Parameters

  • center : float
         of the prior
  • circular : bool or float
         bool : y|n circular with period from limits[0] to limits[1]
         float :period of circularity
  • limits : None or list of 2 float/None
         None : no limits.
         2 limits, resp low and high

setLimits( limits=None )
Set limits. It is asserted that lowLimit is smaller than highLimit.

Parameters

  • limits : None or list of any combination of [None, float]
         None : no limit (for both or one)
         float : [low,high] limit

Raises

ValueError when low limit is larger than high limit or out of Domain

setPriorAttributes( limits, circular )
Set circular attributes.

Parameters

  • limits : None or array of 2 floats
         defining the period
  • circular : bool or float
         False pass
         True Calculate period and center from limits
         float period

isCircular( )
Whether circular

limitedDomain2Unit( dval )
Shrink domain to value in [0,1]

limitedUnit2Domain( uval )
Expand value in [0,1] to domain

circularDomain2Unit( dval )
Make domain circular in unit by shrinking: domain ==> unit ==> [1/3,2/3]

circularUnit2Domain( uval )
Unpack circular unit to domain: [1/3,2/3] ==> unit ==> domain

unsetLimits( )

Remove all limits.

setAttributes( limits=None, scale=None )
Set possible attributes for a Prior.

Parameters

  • limits : float or None
         [low,high] limit
  • scale : float or None
         scale factor

isOutOfLimits( par )
True if the parameter is out of limits

Parameters

  • par : float or array_like
         the parameter to check

checkLimit( par )
Check whether the parameter is within limits.

Parameters

  • par : float
         the parameter to check

Raises

     ValueError when outside limits.

stayInLimits( par )
Return lower limit or upper limit when parameter is outside.

Parameters

  • par : float
         the parameter to check

hasLowLimit( )

Return true if the prior has its low limits set.

hasHighLimit( )

Return true if the prior has its high limits set.

hasLimits( )

Return true if it has any limits.

getLimits( )

Return the limits.

getIntegral( )
Return the integral of the prior over the valid range.

Default: 1.0 (for bound priors)

getRange( )

Return the range.

domain2Unit( dval )
Return a value in [0,1] given a value within the valid domain of a parameter for a distribution.

Parameters

  • dval : float
         value within the domain of a parameter

unit2Domain( uval )
Return a value within the valid domain of the parameter given a value between [0,1] for a distribution.

Parameters

  • uval : float
         value within [0,1]

result( p )
Return value of the Prior at a given value.

If result is not defined, fall back to numerical derivative of Domain2Unit.

Parameters

  • p : float
         the value

partialDomain2Unit( p )
Return the derivative of Domain2Unit, aka the result of the distribution at p

Parameters

  • p : float
         the value

logResult( p )
Return the log of the result; -inf when p == 0.

Parameters

  • p : float
         the value

numPartialDomain2Unit( dval )
Return a the numeric derivate of the domain2Unit function to dval.

Parameters

  • dval : float
         value within the domain of a parameter

partialLog( p )
Return partial derivative of log( Prior ) wrt parameter. default numPartialLog

Parameters

  • p : float
         the value

numPartialLog( p )
Return the numeric partial derivative of log( Prior ) wrt parameter. Parameters
  • p : float
         the value

isBound( )

Return true if the integral over the prior is bound.