function kim_t = compute_implied_rate(x_t, x_ddag, dt, tmax)
% Compute implied rate estimate as a function of observation time.
%
% kim_t = compute_implied_rate(x_t, x_ddag, tmax)
%
% ARGUMENTS
%   x_t - timeseries
%   x_ddag - dividing surface separating A and B states
%   dt - time interval between observations in x_t
%   tmax - maximum lag time to compute implied rate
%
% RETURNS
%   kim_t - implied rate as a function of lag time, up to time tmax

% Determine length of timeseries.
T = length(x_t);

% Quantize using dividing surface to define h_A and h_B functions.
h_A = (x_t <= x_ddag);
h_B = (x_t > x_ddag);

% Compute stationary probabilities.
PA = mean(h_A);
PB = mean(h_B);

% Compute normalized time-correlation function for dh_A.
Cdt = zeros(1,tmax);
dh_A = h_A - PA;
denom = mean(dh_A.*dh_A);
for t = 1:tmax
  Cdt(t) = mean(dh_A(1:T-t) .* dh_A(1+t:T)) / denom;
end
Cdt = [1 Cdt];

% Compute implied rate constant.
tval = [1:tmax] * dt;
kim_t = - tval.^(-1) .* log(Cdt(2:end));

return
