function [mle, info, n] = varhetnr(y, x, mu0, TOL, Nmax)
% *************************************************************************
%
% FILE: varhetnr.m
%
% Date Created: Feb 10, 2004
% Author: Mark E. Irwin
%
% Contents: Finds MLE and information for variance heterogeneity model 
%           Y ~ N(mu, (mu*x)^2) by Newton-Raphson
%
% Revision History
% Date          Name            Changes/Reasons
%
% *************************************************************************
%
% Input:
%
%   y: response vector
%   x: variance covariate vector
%   mu0: initial guess for MLE
%   TOL: convergence tolerance
%   Nmax: number of functional iterations
%
% Output:
%
%   mle: mle
%   info: information,
%   n: number of iterations until convergence
%
% *************************************************************************

% Initialize loop

mui = mu0;
i = 0;
diff = 2 * TOL;  % guarentees at least one pass through loop

while (i < Nmax && diff > TOL)
  i = i + 1;
  muold = mui;
  mui = mui - hetscore(y, x, mui)/hethess(y, x, mui);
  diff = abs(muold - mui);
end

if (diff > TOL)
  warning('Method did not converage after Nmax steps');
end

mle = mui;
info = -hethess(y, x, mui);
n = i;
