Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
:param log_likelihood: return the log-likelihood(s) for the entire
series. If matrix data is given, this will be a vector where each
element is the log-likelihood of a single row.
:type log_likelihood: boolean
:rtype: result object whose fields depend on of the above parameter
flags are True. The possible values are:
``smoothed`` (the return value of **smooth**, may contain ``smoothed.gains``),
``filtered`` (like ``smoothed``, may also contain ``filtered.gains``),
``predicted`` (the return value of **predict** if ``n_test > 0``)
``pairwise_covariances``, ``likelihoods`` and
``log_likelihood``.
"""
# pylint: disable=W0201
result = KalmanFilter.Result()
data = ensure_matrix(data)
single_sequence = len(data.shape) == 1
if single_sequence:
data = data[np.newaxis,:]
n_vars = data.shape[0]
n_measurements = data.shape[1]
n_states = self.state_transition.shape[0]
n_obs = self.observation_model.shape[-2]
def empty_gaussian(
n_states=n_states,
n_measurements=n_measurements,
cov=covariances):
return Gaussian.empty(n_states, n_vars, n_measurements, cov)
initial_value = ensure_matrix(initial_value)
assert(initial_value.shape[-2:] == (n_states, 1))
assert(initial_covariance.shape[-2:] == (n_states, n_states))
if len(initial_value.shape) == 2:
initial_value = np.vstack([initial_value[np.newaxis,...]]*n_vars)
if len(initial_covariance.shape) == 2:
initial_covariance = np.vstack([initial_covariance[np.newaxis,...]]*n_vars)
m = initial_value
P = initial_covariance
keep_filtered = filtered or smoothed
if filtered or gains:
result.filtered = KalmanFilter.Result()
if log_likelihood:
result.log_likelihood = np.zeros((n_vars,))
if likelihoods:
result.log_likelihoods = np.empty((n_vars, n_measurements))
if keep_filtered:
if observations:
filtered_observations = empty_gaussian(n_states=n_obs)
filtered_states = empty_gaussian(cov=True)
if gains:
result.filtered.gains = np.empty((n_vars, n_measurements, n_states, n_obs))
for j in range(n_measurements):
if verbose:
result.filtered.states.cov = filtered_states.cov
result.filtered.states = auto_flat_states(result.filtered.states)
if observations:
result.filtered.observations = auto_flat_observations(
filtered_observations)
if smoothed:
if observations:
result.smoothed.observations = auto_flat_observations(
result.smoothed.observations)
if states:
result.smoothed.states = auto_flat_states(
result.smoothed.states)
if n_test > 0:
result.predicted = KalmanFilter.Result()
if observations:
result.predicted.observations = empty_gaussian(
n_measurements=n_test,
n_states=n_obs)
if states:
result.predicted.states = empty_gaussian(n_measurements=n_test)
for j in range(n_test):
if verbose:
print('predicting %d/%d' % (j+1, n_test))
if states:
result.predicted.states.mean[:,j,:] = m[...,0]
if covariances:
result.predicted.states.cov[:,j,:,:] = P
if observations:
obs_mean, obs_cov = self.predict_observation(m, P)