hmmer-3.3/easel/esl_random.c
/* Function:  esl_rnd_Dirichlet()
 * Synopsis:  Sample a Dirichlet-distributed random probability vector 
 * Incept:    SRE, Wed Feb 17 12:20:53 2016 [H1/76]
 *
 * Purpose:   Using generator <rng>, sample a Dirichlet-distributed
 *            probabilty vector <p> of <K> elements, using Dirichlet
 *            parameters <alpha> (also of <K> elements). 
 *
 *            Caller provides the allocated space for <p>.
 * 
 *            <alpha> is optional. If it isn't provided (i.e. is
 *            <NULL>), sample <p> uniformly. (That is, use <alpha[i] =
 *            1.> for all i=0..K-1.)
 *
 *            This routine is redundant with <esl_dirichlet_DSample()>
 *            and <esl_dirichlet_DSampleUniform()> in the dirichlet
 *            module. Provided here because there's cases where we
 *            just want to sample a probability vector without
 *            introducing a dependency on all the stats/dirichlet code
 *            in Easel.
 *
 * Args:      rng   : random number generator
 *            alpha : OPTIONAL: Dirichlet parameters 0..K-1, or NULL to use alpha[i]=1 for all i
 *            K     : number of elements in alpha, p
 *            p     : RESULT: sampled probability vector
 *
 * Returns:   <eslOK> on success, and <p> is a sampled probability vector.
 */
esl_rnd_Dirichlet()
Synopsis:  Sample a Dirichlet-distributed random probability vector
Incept:    SRE, Wed Feb 17 12:20:53 2016 [H1/76]

Using generator , sample a Dirichlet-distributed
probabilty vector  of  elements, using Dirichlet
parameters  (also of  elements).

Caller provides the allocated space for .

 is optional. If it isn't provided , sample  uniformly.

This routine is redundant with 
and  in the dirichlet
module. Provided here because there's cases where we
just want to sample a probability vector without
introducing a dependency on all the stats/dirichlet code
in Easel.



 on success, and  is a sampled probability vector.
esl_rnd_Dirichlet () Synopsis : Sample a Dirichlet - distributed random probability vector Incept : SRE Wed Feb 17 12 : 20 : 53 2016 [ H1 / 76 ] Using generator sample a Dirichlet - distributed probabilty vector of elements using Dirichlet parameters ( also of elements ) . Caller provides the allocated space for . is optional . If it isn ' t provided sample uniformly . This routine is redundant with and in the dirichlet module . Provided here because there ' s cases where we just want to sample a probability vector without introducing a dependency on all the stats / dirichlet code in Easel . on success and is a sampled probability vector .
int
esl_rnd_Dirichlet(ESL_RANDOMNESS *rng, const double *alpha, int K, double *p)
{
  int    i;
  double norm = 0.;
  for (i = 0; i < K; i++) 
    {
      p[i] = esl_rnd_Gamma(rng, (alpha ? alpha[i] : 1.0));
      norm += p[i];
    }
  for (i = 0; i < K; i++)
    p[i] /= norm;
  return eslOK;
}
int esl_rnd_Dirichlet ( ESL_RANDOMNESS * rng , const double * alpha , int K , double * p ) { int i ; double norm = 0. ; for ( i = 0 ; i < K ; i ++ ) { p [ i ] = esl_rnd_Gamma ( rng , ( alpha ? alpha [ i ] : 1.0 ) ) ; norm += p [ i ] ; } for ( i = 0 ; i < K ; i ++ ) p [ i ] /= norm ; return eslOK ; }
Function:  esl_rnd_Dirichlet()
 Synopsis:  Sample a Dirichlet-distributed random probability vector
 Incept:    SRE, Wed Feb 17 12:20:53 2016 [H1/76]
Function : esl_rnd_Dirichlet () Synopsis : Sample a Dirichlet - distributed random probability vector Incept : SRE Wed Feb 17 12 : 20 : 53 2016 [ H1 / 76 ]