Skip to content

KalmanFilterTransformerPK

KalmanFilterTransformerPK

class KalmanFilterTransformerPK(state_dim, state_transition=None, transition_offsets=None, measurement_offsets=None, process_noise=None, measurement_noise=None, measurement_function=None, initial_state=None, initial_state_covariance=None, estimate_matrices=None, denoising=False)[source]

Kalman Filter, from pykalman (sktime native maintenance fork).

The Kalman Filter is an unsupervised algorithm, consisting of several mathematical equations which are used to create an estimate of the state of a process.

The Kalman Filter is typically used for denoising data, or inferring the hidden state of data.

This class is the adapter for the pykalman package into sktime. KalmanFilterTransformerPK implements hidden inferred states and denoising, depending on the boolean input parameter denoising. In addition, KalmanFilterTransformerPK provides parameter optimization via Expectation-Maximization (EM) algorithm [2], implemented by pykalman.

As the pykalman package is no longer maintained, sktime now contains an up-to-date maintenance fork of the pykalman package.

The maintenance fork can also be directly accessed in sktime.libs.pykalman.

Parameters:
state_dimint

System state feature dimension.

state_transitionnp.ndarray, optional (default=None)

of shape (state_dim, state_dim) or (time_steps, state_dim, state_dim). State transition matrix, also referred to as F, is a matrix which describes the way the underlying series moves through successive time periods.

transition_offsetsnp.ndarray, optional (default=None)

of shape (state_dim,) or (time_steps, state_dim). State offsets, also referred to as b, as described in pykalman.

measurement_offsetsnp.ndarray, optional (default=None)

of shape (measurement_dim,) or (time_steps, measurement_dim). Observation (measurement) offsets, also referred to as d, as described in pykalman.

process_noisenp.ndarray, optional (default=None)

of shape (state_dim, state_dim) or (time_steps, state_dim, state_dim). Process noise matrix, also referred to as Q, the uncertainty of the dynamic model.

measurement_noisenp.ndarray, optional (default=None)

of shape (measurement_dim, measurement_dim) or (time_steps, measurement_dim, measurement_dim). Measurement noise matrix, also referred to as R, represents the uncertainty of the measurements.

measurement_functionnp.ndarray, optional (default=None)

of shape (measurement_dim, state_dim) or (time_steps, measurement_dim, state_dim). Measurement equation matrix, also referred to as H, adjusts dimensions of measurements to match dimensions of state.

initial_statenp.ndarray, optional (default=None)

of shape (state_dim,). Initial estimated system state, also referred to as X0.

initial_state_covariancenp.ndarray, optional (default=None)

of shape (state_dim, state_dim). Initial estimated system state covariance, also referred to as P0.

estimate_matricesstr or list of str, optional (default=None).

Subset of [state_transition, measurement_function, process_noise, measurement_noise, initial_state, initial_state_covariance, transition_offsets, measurement_offsets] or - all. If estimate_matrices is an iterable of strings, only matrices in estimate_matrices will be estimated using EM algorithm, like described in pykalman. If estimate_matrices is all, then all matrices will be estimated using EM algorithm.

Note - parameters estimated by EM algorithm assumed to be constant.

denoisingbool, optional (default=False).

This parameter affects transform. If False, then transform will be inferring hidden state. If True, uses pykalman smooth for denoising.

Attributes:
is_fitted

Whether fit has been called.

See also

KalmanFilterTransformerFP

Kalman Filter transformer, adapter for the FilterPy package into sktime.

Notes

pykalman KalmanFilter documentation :

https://pykalman.github.io/#kalmanfilter

References

[1]

Greg Welch and Gary Bishop, “An Introduction to the Kalman Filter”, 2006 https://www.cs.unc.edu/~welch/media/pdf/kalman_intro.pdf

[2]

R.H.Shumway and D.S.Stoffer “An Approach to time Series Smoothing and Forecasting Using the EM Algorithm”, 1982 https://www.stat.pitt.edu/stoffer/dss_files/em.pdf

>>> import numpy as np
>>> import sktime.transformations.kalman_filter as kf
>>> time_steps, state_dim, measurement_dim = 10, 2, 3
>>>
>>> X = np.random.rand(time_steps, measurement_dim) * 10
>>> transformer = kf.KalmanFilterTransformerPK(state_dim=state_dim)
>>> X_transformed = transformer.fit_transform(X=X)

Example of - denoising, matrix estimation and missing values:

>>> import numpy as np
>>> import sktime.transformations.kalman_filter as kf
>>> time_steps, state_dim, measurement_dim = 10, 2, 2
>>>
>>> X = np.random.rand(time_steps, measurement_dim)
>>> # missing value
>>> X[0][0] = np.nan
>>>
>>> # If matrices estimation is required, elements of ``estimate_matrices``
>>> # are assumed to be constants.
>>> transformer = kf.KalmanFilterTransformerPK(
...     state_dim=state_dim,
...     measurement_noise=np.eye(measurement_dim),
...     denoising=True,
...     estimate_matrices=['measurement_noise']
...     )
>>>
>>> X_transformed = transformer.fit_transform(X=X)

Example of - dynamic inputs (matrix per time-step) and missing values:

>>> import numpy as np
>>> import sktime.transformations.kalman_filter as kf
>>> time_steps, state_dim, measurement_dim = 10, 4, 4
>>>
>>> X = np.random.rand(time_steps, measurement_dim)
>>> # missing values
>>> X[0] = [np.nan for i in range(measurement_dim)]
>>>
>>> # Dynamic input -
>>> # ``state_transition`` provide different matrix for each time step.
>>> transformer = kf.KalmanFilterTransformerPK(
...     state_dim=state_dim,
...     state_transition=np.random.rand(time_steps, state_dim, state_dim),
...     estimate_matrices=['initial_state', 'initial_state_covariance']
...     )
>>>
>>> X_transformed = transformer.fit_transform(X=X)

Methods

check_is_fitted([method_name])

Check if the estimator has been fitted.

clone()

Obtain a clone of the object with same hyper-parameters and config.

clone_tags(estimator[, tag_names])

Clone tags from another object as dynamic override.

create_test_instance([parameter_set])

Construct an instance of the class, using first test parameter set.

create_test_instances_and_names([parameter_set])

Create list of all test instances and a list of names for them.

fit(X[, y])

Fit transformer to X, optionally to y.

fit_transform(X[, y])

Fit to data, then transform it.

get_class_tag(tag_name[, tag_value_default])

Get class tag value from class, with tag level inheritance from parents.

get_class_tags()

Get class tags from class, with tag level inheritance from parent classes.

get_config()

Get config flags for self.

get_fitted_params([deep])

Get fitted parameters.

get_param_defaults()

Get object's parameter defaults.

get_param_names([sort])

Get object's parameter names.

get_params([deep])

Get a dict of parameters values for this object.

get_tag(tag_name[, tag_value_default, ...])

Get tag value from instance, with tag level inheritance and overrides.

get_tags()

Get tags from instance, with tag level inheritance and overrides.

get_test_params([parameter_set])

Return testing parameter settings for the estimator.

inverse_transform(X[, y])

Inverse transform X and return an inverse transformed version.

is_composite()

Check if the object is composed of other BaseObjects.

load_from_path(serial)

Load object from file location.

load_from_serial(serial)

Load object from serialized memory container.

reset()

Reset the object to a clean post-init state.

save([path, serialization_format])

Save serialized self to bytes-like object or to (.zip) file.

set_config(**config_dict)

Set config flags to given values.

set_params(**params)

Set the parameters of this object.

set_random_state([random_state, deep, ...])

Set random_state pseudo-random seed parameters for self.

set_tags(**tag_dict)

Set instance level tag overrides to given values.

transform(X[, y])

Transform X and return a transformed version.

update(X[, y, update_params])

Update transformer with X, optionally y.