December 19, 2013

Troubleshooting Jacobians of z and mu.

Analytical and numerical versions of z's Jacobian differ significantly in some entries. To troubleshoot, stepping backward through the derivations of J_z outlined in the reference post.

I will refer to the final equation from that post and count backward from the final line, describing the result of implementing each line. E.g. line 0 is the final result, the one used to compute z_ana; line 1 is the one labelled "(3D version)" .

Setup:

```
Track = detach(trash_Tracks_11(end), params_test);
K = get_K(Track);
S = Track.ll_S;
U = speye(size(K)) + S * K * S';
Ui = inv(U);
kern = get_model_kernel_derivative(params_test);
ind = get_curve_indices(Track);
views = Track.ll_views_flat;
Delta = eval_kernel(kern, ind, ind, views, views);
Delta3 = one_d_to_three_d(Delta);
i = 1;
i3 = 3*(i-1)+1;
I = i3:i3+2;
delta_i = Delta(i,:)';
delta_i3 = one_d_to_three_d(delta_i);
dK = zeros(size(K));
dK(I,:) = dK(I,:) + Delta3(I,:);
dK(:,I) = dK(:,I) + Delta3(I,:)';
A = A = S' * Ui * S;
z = z = A * y;
N = length(K);
```

Implementing line 1:

```
line_1 = -A(:,I) *delta_i3' * z - A * delta_i3 * z(i3:i3+2);
```

It actually looks pretty good. So why is line_0 wrong?

Let's isolate the first term.

```
line_1_a = -A(:,I) *delta_i3' * z;
line_0_a = -sum_1xN(A .* repmat((Delta3 * z)', N,1), 3);
plot(xx, line_1_a - line_0_a(:,i))
```

Error within 1e-12. Great! Error must be in the second term...

```
line_1_b = A * delta_i3 * z(i3:i3+2);
line_0_b = - A * (Delta3(1:3:end, :)' .* repmat(reshape(z,3,[]), N/3, 1));
plot(xx, line_1_b - line_0_b(:,i))
```

Yep. it's a mess.

...

Found the bug. Was incorrectly trying to implement Delta*{1x3} by taking Delta*{3x3}(1:3:end, :), which is totally wrong. Also, was mis-associating the matrix multiplication and elementwise multiplication. That is, I was taking (A * Delta_1x3) .* XXX instead of A * (Delta_1x3 .* XXX).

Those two fixes, and the dZ test now passes.

Still getting bad results for dmu.

...

Same bug as for dZ.

Developed new constant-width energy function, and derived its gradient. Writeup is here.

TODO:

- Derive Hessian of energy function
- implement and test gradient and hessian.