Skip to content

Commit

Permalink
Process tutorial notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Aug 5, 2020
1 parent 4cb46a1 commit 167b38e
Show file tree
Hide file tree
Showing 37 changed files with 9,507 additions and 18,534 deletions.
3,089 changes: 1,375 additions & 1,714 deletions tutorials/W2D3_DecisionMaking/W2D3_Tutorial1.ipynb

Large diffs are not rendered by default.

4,974 changes: 2,148 additions & 2,826 deletions tutorials/W2D3_DecisionMaking/W2D3_Tutorial2.ipynb

Large diffs are not rendered by default.

3,476 changes: 1,533 additions & 1,943 deletions tutorials/W2D3_DecisionMaking/W2D3_Tutorial3.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def m_step(gamma, xi, dt):
gamma (): Number of epochs of EM to run
xi (numpy 3d array): Tensor of recordings, has shape (n_trials, T, C)
dt (float): Duration of a time bin
Returns:
psi_new (numpy vector): Updated initial probabilities for each state
A_new (numpy matrix): Updated transition matrix, A[i,j] represents the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
switch_prob = 0.1
log10_noise_level = -1

# Build model
# Build model
model = create_model(switch_prob=switch_prob,
noise_level=10.**log10_noise_level,
startprob=[0.5, 0.5])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def kalman_smooth(data, params):
system parameters.
Args:
data (ndarray): a sequence of osbervations of shape(n_timesteps, n_dim_obs)
data (ndarray): a sequence of osbervations of shape(n_timesteps, n_dim_obs)
params (dict): a dictionary of model paramters: (F, Q, H, R, mu_0, sigma_0)
Returns:
Expand All @@ -17,7 +17,7 @@ def kalman_smooth(data, params):

n_dim_state = F.shape[0]
n_dim_obs = H.shape[0]

# first run the forward pass to get the filtered means and covariances
mu, sigma = kalman_filter(data, params)

Expand All @@ -26,7 +26,7 @@ def kalman_smooth(data, params):
sigma_hat = np.zeros_like(sigma)
mu_hat[-1] = mu[-1]
sigma_hat[-1] = sigma[-1]

# smooth the data
for t in reversed(range(len(data)-1)):
sigma_pred = F @ sigma[t] @ F.T + Q # sigma_pred at t+1
Expand All @@ -36,7 +36,7 @@ def kalman_smooth(data, params):
# write the expression to compute the smoothed state mean estimate
mu_hat[t] = mu[t] + J @ (mu_hat[t+1] - F @ mu[t])
# write the expression to compute the smoothed state noise covariance estimate
sigma_hat[t] = sigma[t] + J @ (sigma_hat[t+1] - sigma_pred) @ J.T
sigma_hat[t] = sigma[t] + J @ (sigma_hat[t+1] - sigma_pred) @ J.T

return mu_hat, sigma_hat

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def sample_lds(n_timesteps, params, seed=0):
Returns:
ndarray, ndarray: the generated state and observation data
"""
"""
n_dim_state = params['F'].shape[0]
n_dim_obs = params['H'].shape[0]

Expand All @@ -32,7 +32,7 @@ def sample_lds(n_timesteps, params, seed=0):
state[t] = params['mu_0']
else:
state[t] = params['F'] @ state[t-1] + zi[t]

# write the expression for computing the observation
obs[t] = params['H'] @ state[t] + eta[t]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def kalman_filter(data, params):
system parameters.
Args:
data (ndarray): a sequence of osbervations of shape(n_timesteps, n_dim_obs)
data (ndarray): a sequence of osbervations of shape(n_timesteps, n_dim_obs)
params (dict): a dictionary of model paramters: (F, Q, H, R, mu_0, sigma_0)
Returns:
Expand All @@ -19,7 +19,7 @@ def kalman_filter(data, params):
n_dim_obs = H.shape[0]
I = np.eye(n_dim_state) # identity matrix

# state tracking arrays
# state tracking arrays
mu = np.zeros((len(data), n_dim_state))
sigma = np.zeros((len(data), n_dim_state, n_dim_state))

Expand All @@ -34,15 +34,15 @@ def kalman_filter(data, params):

# write the expression for computing the Kalman gain
K = sigma_pred @ H.T @ np.linalg.inv(H @ sigma_pred @ H.T + R)
# write the expression for computing the filtered state mean
# write the expression for computing the filtered state mean
mu[t] = mu_pred + K @ (y - H @ mu_pred)
# write the expression for computing the filtered state noise covariance
sigma[t] = (I - K @ H) @ sigma_pred

return mu, sigma


filtered_state_means, filtered_state_covariances = kalman_filter(obs, params)
with plt.xkcd():
with plt.xkcd():
plot_kalman(state, obs, filtered_state_means, title="my kf-filter",
color='r', label='my kf-filter')
1,532 changes: 72 additions & 1,460 deletions tutorials/W2D3_DecisionMaking/student/W2D3_Tutorial1.ipynb

Large diffs are not rendered by default.

2,303 changes: 140 additions & 2,163 deletions tutorials/W2D3_DecisionMaking/student/W2D3_Tutorial2.ipynb

Large diffs are not rendered by default.

1,494 changes: 116 additions & 1,378 deletions tutorials/W2D3_DecisionMaking/student/W2D3_Tutorial3.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit 167b38e

Please sign in to comment.