% calib -- calibrate x' from y' given regression of y on x
function [xprime, xprimecdf] = calib(yprime, beta, Sigmabeta, s2, df, xord)
% [xprime, xprimecdf] = calib(yprime, beta, Sigmabeta, s2, df, xord, m)
% yprime ~ n x 1 = proxy values for reconstruction
% beta ~ 2 x 1 = regression coefficients of y on x from calibration: 
%   y = beta(1) + beta(2) * x + epsilon
% Sigmabeta ~ 2 x 2 = point estimate of Cov(beta), adjusted appropriately 
%   for serial correlation etc.
% s2 = estimated variance of regression errors epsilon
% df = DOF from calibration regression
% xord ~ 1 x h = x ordinates at which cdf of x' will be evaluated
% xprime ~ n x 1 = recon point estimates = (y - beta(1)) / beta(2)
% xprimecdf ~ n x h = posterior cdf of xprime. 

h = size(xord, 2);
n = size(yprime, 1);
xprime = (yprime - beta(1)) / beta(2);
xprimecdf = NaN(n, h);
Sigma = Sigmabeta;
Sigma(1, 1) = Sigma(1, 1) + s2;
Sigma(1, 2) = -Sigma(1, 2);
Sigma(2, 1) = Sigma(1, 2);
for i = 1:n
    mu = [yprime(i) - beta(1); beta(2)];
    xprimecdf(i, :) = r2tcdf(xord', mu, Sigma, df)';
end
    % end of calib

        