1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
/* ------------------------------------------------------------- */
/* File: example_mixt_inv.c */
/* ------------------------------------------------------------- */
/* Include UNURAN header file. */
#include <unuran.h>
/* ------------------------------------------------------------- */
/* Mixture of truncated Gaussian on (-INFINITY,0] and */
/* Exponential distribution on [0,INFINITY) */
/* ------------------------------------------------------------- */
int main(void)
{
/* Declare the three UNURAN objects. */
UNUR_DISTR *distr; /* distribution object */
UNUR_PAR *par; /* parameter object */
UNUR_GEN *comp[2]; /* array of generator objects (components)*/
UNUR_GEN *gen; /* generator object for mixture */
double prob[2]; /* array of probabilities */
int i; /* loop variable */
double x; /* will hold the random number */
/* Create generators for components */
distr = unur_distr_normal(NULL,0); /* Gaussian distribution */
unur_distr_cont_set_domain(distr,-UNUR_INFINITY,0);
par = unur_pinv_new(distr); /* choose method PINV */
comp[0] = unur_init(par); /* initialize */
unur_distr_free(distr); /* free distribution obj. */
distr = unur_distr_exponential(NULL,0); /* Exponential distr. */
par = unur_pinv_new(distr); /* choose method PINV */
comp[1] = unur_init(par); /* initialize */
unur_distr_free(distr); /* free distribution obj. */
/* Probabilities for components (need not sum to 1) */
prob[0] = 0.4;
prob[1] = 0.3;
/* Create mixture */
par = unur_mixt_new(2,prob,comp);
/* We want to use inversion for the mixture as well. */
/* (Thus the above order of the components is important!) */
unur_mixt_set_useinversion(par,TRUE);
/* Initialize generator object */
gen = unur_init(par);
/* we do not need the components any more */
for (i=0; i<2; i++)
unur_free(comp[i]);
/* It is important to check if the creation of the generator */
/* object was successful. Otherwise `gen' is the NULL pointer */
/* and would cause a segmentation fault if used for sampling. */
if (gen == NULL) {
fprintf(stderr, "ERROR: cannot create generator object\n");
exit (EXIT_FAILURE);
}
/* Now you can use the generator object `gen' to sample from */
/* the distribution. Eg.: */
for (i=0; i<10; i++) {
x = unur_sample_cont(gen);
printf("%f\n",x);
}
/* When you do not need the generator object any more, you */
/* can destroy it. */
unur_free(gen);
exit (EXIT_SUCCESS);
} /* end of main() */
/* ------------------------------------------------------------- */
|