function [wrms,chi2]=wstat2(resid,sigma,dof)

%    [wrms,chi2]=wstat2(resid,sigma,dof)
%
%    Calculate weighted statistics for residuals and goodness-of-fit to model
%
%    resid           vector of residuals (= data-model)
%    sigma           vector of data uncertainties
%    dof             degrees of freedom in model

%    wrms            weighted root mean square of residuals
%    chi2            chi-squared, goodness-of-fit to model

%    Rosanne Nikolaidis, 21-November-2000

n = length(resid);
weight = 1./sigma.^2;
stat1 = sum(resid.*weight);
stat2 = sum(weight);
stat3 = sum(resid.^2.*weight);
chi2 = stat3/(n-dof) ;
nrms = sqrt(chi2);
wrms = sqrt( n./stat2 ).*nrms;
