function k = compute_mean_lifetime_rate(x_t, x_ddag, dt)
% Compute rate estimate from mean lifetime.

% Get trajectory length.
T = length(x_t);

% Compute indicator functions for A and B.
h_A = (x_t <= x_ddag);
h_B = (x_t > x_ddag);

% Compute timepoints before transitions.
h_AB = h_A(1:end-1) & h_B(2:end);
h_BA = h_B(1:end-1) & h_A(2:end);
indices = [0 find(h_AB | h_BA)' (T+1)];

% Compute lifetimes from transitions.
lifetimes = (indices(2:end) - indices(1:end-1)) * dt;

% Compute mean lifetime.
mean_lifetime = mean(lifetimes);

% Estimate rate.
k = 1.0 / mean_lifetime;

return
