function retW = CalcOptW(inEta,inFamily,inLink)
%function retW = CalcOptW(inEta,inFamily,inLink)
% 
% Calculate the optimal weight matrix for GLM, which is inversely
% proportional to the variance of the generated response in the IRLS
% algorithm. Note that
%                b'(theta)=g^{-1}(eta).
%
% Input:
%   inEta: eta;
%   inFamily: family of the distribution;
%   inLink: link function g().
%
% Output:
%   retW: nx1 weight vector for weight matrix W.
%
% Dabao Zhang    July 27, 2016
%
% Revised by:
%
% Copyright (c) 2016 by Dabao Zhang.

error(nargchk(3, 3, nargin));

retW = CalcIRLSResponseVar(inEta,inFamily,inLink);

% kappa = b'(theta) &  var(Y) is proportional to b''(theta)
if( sum(strcmp(inLink,{'bclogit','pologit','crlogit'})) )
    % Q: retW = mean(1./retW,2); which one is better?
    retW = 1./mean(retW,2);
else
    retW = 1./retW;
end
retW = retW./sum(retW);
