Ejemplo n.º 1
0
def getSLDSFit(unit_trial, K, xDim):
    # K : number of discrete state of z
    # xDim : number of latent dimensions
    numTrial, numUnit, numTime = unit_trial.shape
    factor_unit_trial = unit_trial.transpose([0, 2, 1])
    factor_unit_trial = factor_unit_trial.reshape([-1, factor_unit_trial.shape[2]])
    yDim = numUnit
    inputDim = 1 # some constants
    inputs = np.ones((numTime, inputDim))

    # factor analysis as initialization
    # Use FA or PCA to initialize C_init and D_init
    estimator = factan(n_components=xDim, tol=0.000001, copy=True,
                       max_iter=1000, noise_variance_init=None,
                       svd_method='randomized', iterated_power=3,
                       random_state=None)

    estimator.fit(factor_unit_trial)
    C_init = estimator.components_.T
    D_init = estimator.mean_

    # To support different emission matrices for each discrete state, pass in
    Cs=[C_init.copy() for _ in range(numTrial)]
    Ds=[D_init.copy() for _ in range(numTrial)]

    # SLDS model fit
    test_model = DefaultSLDS(K, yDim, xDim, inputDim, Cs=Cs, Ds=Ds)
    for trial in range(numTrial):
        test_model.add_data(unit_trial[trial].T, inputs=inputs)

    print("Initializing with Gibbs")
Ejemplo n.º 2
0
plt.xlabel("$y_1$")
plt.ylabel("$y_2$")


#################
#  build model  #
#################
Kmax = 10                           # number of latent discrete states
D_latent = 2                        # latent linear dynamics' dimension
D_obs = 2                           # data dimension

Cs = np.eye(D_obs)                  # Shared emission matrix
sigma_obss = 0.05 * np.eye(D_obs)   # Emission noise covariance

model = DefaultSLDS(
    K=Kmax, D_obs=D_obs, D_latent=D_latent,
    Cs=Cs, sigma_obss=sigma_obss)

model.add_data(data)
model.resample_states()


##################
#  run sampling  #
##################
n_show = 50
samples = np.empty((n_show, data.shape[0]))
samples[:n_show] = model.stateseqs[0]

fig = plt.figure(figsize=(8,3))
gs = gridspec.GridSpec(6,1)
Ejemplo n.º 3
0
D_input = 1
T = 1000

# Make an LDS with known parameters
true_mu_inits = [np.ones(D_latent) for _ in range(K)]
true_sigma_inits = [np.eye(D_latent) for _ in range(K)]
true_As = [.9 * random_rotation(D_latent)
           for k in range(K)]
true_Bs = [3 * npr.randn(D_latent, D_input) for k in range(K)]
true_sigma_states = [np.eye(D_latent) for _ in range(K)]
true_C = np.random.randn(D_obs, D_latent)
true_Ds = np.zeros((D_obs, D_input))
true_sigma_obs = np.eye(D_obs)
true_model = DefaultSLDS(
    K, D_obs, D_latent, D_input=D_input,
    mu_inits=true_mu_inits, sigma_inits=true_sigma_inits,
    As=true_As, Bs=true_Bs, sigma_statess=true_sigma_states,
    Cs=true_C, Ds=true_Ds, sigma_obss=true_sigma_obs)

# Simulate some data with a given discrete state sequence
inputs = np.ones((T, D_input))
z = np.arange(K).repeat(T // K)
y, x, z = true_model.generate(T, inputs=inputs, stateseq=z)

# Fit with another LDS.  Give it twice as many states in
# order to have some flexibility during inference.
test_model = DefaultSLDS(2*K, D_obs, D_latent, D_input,
                         Cs=npr.randn(D_obs, D_latent),
                         Ds=npr.randn(D_obs, D_input))
test_model.add_data(y, inputs=inputs)
Ejemplo n.º 4
0
#################
#  build model  #
#################

Kmax = 10  # number of latent discrete states
D_latent = 2  # latent linear dynamics' dimension
D_obs = 2  # data dimension

Cs = [np.eye(D_obs) for _ in range(Kmax)]  # Shared emission matrices
sigma_obss = [0.05 * np.eye(D_obs)
              for _ in range(Kmax)]  # Emission noise covariances

model = DefaultSLDS(K=Kmax,
                    D_obs=D_obs,
                    D_latent=D_latent,
                    Cs=Cs,
                    sigma_obss=sigma_obss)

model.add_data(data)
model.resample_states()

for _ in progprint_xrange(10):
    model.resample_model()
model.states_list[0]._init_mf_from_gibbs()

####################
#  run mean field  #
####################

vlbs = []
Ejemplo n.º 5
0
# Make an LDS with known parameters
true_mu_inits = [np.ones(D_latent) for _ in range(K)]
true_sigma_inits = [np.eye(D_latent) for _ in range(K)]
true_As = [.9 * random_rotation(D_latent) for k in range(K)]
true_Bs = [3 * npr.randn(D_latent, D_input) for k in range(K)]
true_sigma_states = [np.eye(D_latent) for _ in range(K)]
true_C = np.random.randn(D_obs, D_latent)
true_Ds = np.zeros((D_obs, D_input))
true_sigma_obs = np.eye(D_obs)
true_model = DefaultSLDS(K,
                         D_obs,
                         D_latent,
                         D_input=D_input,
                         mu_inits=true_mu_inits,
                         sigma_inits=true_sigma_inits,
                         As=true_As,
                         Bs=true_Bs,
                         sigma_statess=true_sigma_states,
                         Cs=true_C,
                         Ds=true_Ds,
                         sigma_obss=true_sigma_obs)

# Simulate some data with a given discrete state sequence
inputs = np.ones((T, D_input))
z = np.arange(K).repeat(T // K)
y, x, z = true_model.generate(T, inputs=inputs, stateseq=z)

# Fit with another LDS.  Give it twice as many states in
# order to have some flexibility during inference.
test_model = DefaultSLDS(2 * K,
                         D_obs,
Ejemplo n.º 6
0
plt.ylabel("$y_2$")

#%%
#################
#  build model  #
#################
Kmax = 10  # number of latent discrete states
D_latent = 2  # latent linear dynamics' dimension
D_obs = 2  # data dimension

Cs = np.eye(D_obs)  # Shared emission matrix
sigma_obss = 0.05 * np.eye(D_obs)  # Emission noise covariance

model = DefaultSLDS(K=Kmax,
                    D_obs=D_obs,
                    D_latent=D_latent,
                    Cs=Cs,
                    sigma_obss=sigma_obss)

model.add_data(data)
model.resample_states()

#%%
##################
#  run sampling  #
##################
n_show = 50
samples = np.empty((n_show, data.shape[0]))
samples[:n_show] = model.stateseqs[0]

fig = plt.figure(figsize=(8, 3))
Ejemplo n.º 7
0
plt.plot(data[:,0],data[:,1],'bx-')


#################
#  build model  #
#################

Kmax = 10                           # number of latent discrete states
D_latent = 2                        # latent linear dynamics' dimension
D_obs = 2                           # data dimension

Cs = [np.eye(D_obs) for _ in range(Kmax)]                   # Shared emission matrices
sigma_obss = [0.05 * np.eye(D_obs) for _ in range(Kmax)]    # Emission noise covariances

model = DefaultSLDS(
    K=Kmax, D_obs=D_obs, D_latent=D_latent,
    Cs=Cs, sigma_obss=sigma_obss)

model.add_data(data)
model.resample_states()

for _ in progprint_xrange(10):
    model.resample_model()
model.states_list[0]._init_mf_from_gibbs()


####################
#  run mean field  #
####################

vlbs = []
Ejemplo n.º 8
0
D_latent = 2
D_input = 1
T = 1000

# Make an LDS with known parameters
true_mu_inits = [np.ones(D_latent) for _ in range(K)]
true_sigma_inits = [np.eye(D_latent) for _ in range(K)]
true_As = [.99 * random_rotation(D_latent, theta=np.pi/((k+1) * 4)) for k in range(K)]
true_Bs = [3 * npr.randn(D_latent, D_input) for k in range(K)]
true_sigma_states = [np.eye(D_latent) for _ in range(K)]
true_C = np.random.randn(D_obs, D_latent)
true_Ds = np.zeros((D_obs, D_input))
true_sigma_obs = 0.05 * np.eye(D_obs)
true_model = DefaultSLDS(
    K, D_obs, D_latent, D_input=D_input,
    mu_inits=true_mu_inits, sigma_inits=true_sigma_inits,
    As=true_As, Bs=true_Bs, sigma_statess=true_sigma_states,
    Cs=true_C, Ds=true_Ds, sigma_obss=true_sigma_obs)

# Simulate some data with a given discrete state sequence
inputs = np.ones((T, D_input))
z = np.arange(K).repeat(T // K)
y, x, _ = true_model.generate(T, inputs=inputs, stateseq=z)

# Fit with another LDS.  Give it twice as many states in
# order to have some flexibility during inference.
test_model = DefaultSLDS(2*K, D_obs, D_latent, D_input,
                         Cs=npr.randn(D_obs, D_latent),
                         Ds=npr.randn(D_obs, D_input))
test_model.add_data(y, inputs=inputs)