function [hme] = hmeInitRand(hme)
% [hme] = hmeInitRand(hme)
%
% Initialize an HME model with random (but sensible) parameter
% values. 
%
% See also hmeCreate, hmeInitLR.
%
% David Martin <dmartin@eecs.berkeley.edu>
% May 2002

% Copyright (C) 2002 David R. Martin <dmartin@eecs.berkeley.edu>
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License as
% published by the Free Software Foundation; either version 2 of the
% License, or (at your option) any later version.
% 
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
% General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
% 02111-1307, USA, or see http://www.gnu.org/copyleft/gpl.html.

if hme.leaf,

  % Initialize with random parameters of small magnitude so that
  % splits start out very soft.
  [d,k] = size(hme.param);
  hme.param = 1e-3 * rand(d,k);
  hme.param(:,k) = 0;

else
  
  % Initialize with a gating function that will divide the postive
  % unit orthant into approximately equal regions with soft
  % transitions.
  [d,b] = size(hme.param);
  hme.param = zeros(d,b);
  for i = 1:b-1,
    % create vector at random orientation
    while 1,
      v = 2*rand(d-1,1) - 1;
      if sum(v.^2) < 1, break, end
    end
    v = v ./ norm(v);
    % pick random location in middle of orthant
    x0 = 0.4 + 0.2*rand(d-1,1);
    % compute line intercept (line eqn: (x-x0).v=0)
    icpt = -x0'*v;
    hme.param(:,i) = [v;icpt] * 3;
  end
  hme.param(:,b) = 0;

end