*
* @RanMixture  x  parms
* draws a vector from a mixture of Normals.
*
* Parameters:
*   x = (output) VECTOR of draws. This should be dimensioned before calling
*   parms = C x 3 RECTANGULAR, where C is the number of branches in the mixture.
*     parms(i,1) is the probability of the branch, parms(i,2) is the mean and
*     parms(i,3) is the standard deviation.
*
* Revision Schedule:
*   04/2003 Written by Tom Doan, Estima
*   03/2004 Revised, adding returns after error displays and correcting an error
*           with rows and columns switched in testing the parms array.
*   02/2018 Simplify by using %RANBRANCH.
*
procedure RanMixture x parms
type vect *x
type rect parms
*
local integer i j n c
local real z
*
if .not.%defined(parms) {
   display "Syntax: @RanMixture x parms"
   return
}
compute n=%size(x)
compute c=%rows(parms)
if %cols(parms)<>3 {
   display "Error: parms array must be Cx3"
   return
}
*
* For efficiency, we draw all the Normals in a batch, then
* scale and translate based upon the chosen branch.
*
compute x=%ran(1.0)
do i=1,n
   *
   * Draw the branch
   *
   compute j=%ranbranch(%xcol(parms,1))
   *
   *  Scale and translate the draw
   *
   compute x(i)=x(i)*parms(j,3)+parms(j,2)
end do i
end
