6.3. pypfilt.model

All simulation models should derive the following base class:

class pypfilt.model.Model

The base class for simulation models, which defines the minimal set of methods that are required.

init(ctx, vec)

Initialise a matrix of state vectors.

Parameters:
  • ctx – The simulation context.
  • vec – An uninitialised \(P \times S\) matrix of state vectors, for \(P\) particles and state vectors of length \(S\) (as defined by field_types()).
field_types(ctx)

Return a list of (field_name, field_dtype, field_shape) tuples that define the state vector.

The third element, field_shape, is optional and contains the shape of this field if it forms an array of type field_dtype.

These tuples must be in the same order as the state vector itself.

Parameters:ctx – The simulation context.
field_names(ctx)

Return a list of the fields that define the state vector.

These tuples must be in the same order as the state vector itself.

Parameters:ctx – The simulation context.
can_smooth()

Return the set of field names in the state vector that can be smoothed by the post-regularised particle filter (see post_regularise()).

update(ctx, time, dt, is_fs, prev, curr)

Perform a single time-step, jumping forward to time and recording the updated particle states in curr.

Parameters:
  • ctx – The simulation context.
  • time – The (end) time of the current time-step.
  • dt – The time-step size.
  • is_fs – Indicates whether this is a forecasting simulation.
  • prev – The state before the time-step.
  • curr – The state after the time-step (destructively updated).
resume_from_cache(ctx)

Notify the model that a simulation will begin from a saved state.

The model does not need to initialise the state vectors, since these will have been loaded from a cache file, but it may need to update any internal variables (i.e., those not stored in the state vectors).

Note

Models should only implement this method if they need to prepare for the simulation.

stat_info()

Describe each statistic that can be calculated by this model as a (name, stat_fn) tuple, where name is a string that identifies the statistic and stat_fn is a function that calculates the value of the statistic.

Note

Models should only implement this method if they define one or more statistics.

is_valid(hist)

Identify particles whose state and parameters can be inspected. By default, this function returns True for all particles. Override this function to ensure that inchoate particles are correctly ignored.

Note

Models should only implement this method if there are conditions where some particles should be ignored.

6.3.1. Performing “mini-steps”

For simulation models that require very small time-steps, it may be desirable to avoid recording the particle states at each time-step. This can be achieved by using a large time-step (e.g., setting steps_per_unit = 1) and dividing each time-step into a number of “mini-steps” that will not be recorded. See Record only a subset of simulation model time-steps for several examples of using the ministeps() decorator.

pypfilt.model.ministeps(mini_steps=None)

Wrap a model’s update() method to perform multiple “mini-steps” for each time-step.

Parameters:mini_steps – The (optional) number of “mini-steps” to perform for each time-step. This can be overridden by providing a value for the "time.mini_steps_per_step" setting.