function results=wls(y,X,omega)
%PURPOSE: weighted least-squares regression 
%---------------------------------------------------
% USAGE: results = ols(y,x,omega)
% where: y = dependent variable vector    (nobs x 1)
%        x = independent variables matrix (nobs x nvar)
%        omega = diagonal matrix of weights (nobs x nobs)
%---------------------------------------------------
% RETURNS: a structure
%         results.meth  = 'wls'
%         results.beta  = bhat     (nvar x 1)
%         results.tstat = t-stats  (nvar x 1)
%         results.bstd  = std deviations for bhat (nvar x 1)
%         results.yhat  = yhat     (nobs x 1)
%         results.resid = residuals (nobs x 1)
%         results.sige  = e'*e/(n-k)   scalar
%         results.rsqr  = rsquared     scalar
%         results.rbar  = rbar-squared scalar
%         results.dw    = Durbin-Watson Statistic
%         results.nobs  = nobs
%         results.nvar  = nvars
%         results.y     = y data vector (nobs x 1)
%         results.bint  = (nvar x2 ) vector with 95% confidence intervals on beta


nIn = nargin;

if nIn < 3
  error('Not enough input arguments.');
elseif nIn > 3
  error('Too many input arguments.');
end

[nobs, nvar] = size(X);

w = diag(omega);
Py = y./sqrt(w);
W = kron(ones(1,nvar),w);
PX = X./sqrt(W);

results = ols(Py,PX);
results.meth = 'wls';
results.y = y;
results.nobs = nobs;
results.nvar = nvar;

xpxi = (X'*X)\eye(nvar);
results.yhat = X*results.beta;
results.resid = y - results.yhat;
sigu = results.resid'*results.resid;
results.sige = sigu/(nobs-nvar);
tmp = (results.sige)*(diag(xpxi));
sigb=sqrt(tmp);
results.bstd = sigb;
tcrit=-tdis_inv(.025,nobs);
results.bint=[results.beta-tcrit.*sigb, results.beta+tcrit.*sigb];
results.tstat = results.beta./(sqrt(tmp));
ym = y - mean(y);
rsqr1 = sigu;
rsqr2 = ym'*ym;
results.rsqr = 1.0 - rsqr1/rsqr2; 
rsqr1 = rsqr1/(nobs-nvar);
rsqr2 = rsqr2/(nobs-1.0);
if rsqr2 ~= 0
results.rbar = 1 - (rsqr1/rsqr2); 
else
    results.rbar = results.rsqr;
end;
ediff = results.resid(2:nobs) - results.resid(1:nobs-1);
results.dw = (ediff'*ediff)/sigu;

