sssj
sssj

Reputation: 11

Issues with fitting Switching Linear Dynamical System on my function

I made a function. Here is the code

import matplotlib.pyplot as plt

# length of the time series
time = 100

# two sets of dynamics
A1 = np.array([[0.9, 0], [0, 0.9]])  # a1 is a stable node
A2 = np.array([[0.9, 0.5], [-0.5, 0.9]])  # a2 is a stable spiral

# initialize state and observations
x = np.zeros((time, 2)) 
y = np.zeros((time, 2))

# generate data
for t in range(1, time):
    if t < 50:
        x[t] = A1 @ x[t-1] + np.random.normal(0, 0.1, 2) 
    else:
        x[t] = A2 @ x[t-1] + np.random.normal(0, 0.1, 2)
    y[t] = x[t] + np.random.normal(0, 0.1, 2)  # Observation with added noise

# Plot generated data
plt.plot(y[:,0], label='Dimension 1') #y[:,0] - selects all rows of y in the first column (0). in Python, : denotes a full slice from start to end, so y[:,0] means "take every row in the first column of y." 
plt.plot(y[:,1], label='Dimension 2')
plt.legend()
plt.show()

# Set the parameters of the SLDS
time_bins = 100    # number of time bins
n_disc_states = 2       # number of discrete states
latent_dim = 2       # number of latent dimensions
emissions_dim = 2      # number of observed dimensions

#initialize SLDS model
model = ssm.SLDS(emissions_dim, n_disc_states, latent_dim, emissions="gaussian") 

# BBVI with Mean-Field Posterior fitting method

elbos, posterior = model.fit(y_scaled, method="bbvi", variational_posterior="mf", num_iters=1000, init_method="kmeans")

The last code block is where I am encountering issues. I am getting

RuntimeWarning: divide by zero encountered in log
  return f_raw(*args, **kwargs)", "RuntimeWarning: divide by zero encountered in divide
  lls = -0.5 * np.log(2 * np.pi * etas) - 0.5 * (data[:, None, :] - mus)**2 / etas", and "RuntimeWarning: invalid value encountered in subtract
  lls = -0.5 * np.log(2 * np.pi * etas) - 0.5 * (data[:, None, :] - mus)**2 / etas" I am not sure where I am going wrong and how to fix these errors. 

Upvotes: 1

Views: 21

Answers (0)

Related Questions