/* The DChoose() and FChoose() unit tests. */ static void utest_choose(ESL_RANDOMNESS *r, int n, int nbins, int be_verbose) { double *pd = NULL; float *pf = NULL; int *ct = NULL; int i; double X2, diff, exp, X2p; if ((pd = malloc(sizeof(double) * nbins)) == NULL) esl_fatal("malloc failed"); if ((pf = malloc(sizeof(float) * nbins)) == NULL) esl_fatal("malloc failed"); if ((ct = malloc(sizeof(int) * nbins)) == NULL) esl_fatal("malloc failed"); /* Sample a random multinomial probability vector. */ if (esl_dirichlet_DSampleUniform(r, nbins, pd) != eslOK) esl_fatal("dirichlet sample failed"); esl_vec_D2F(pd, nbins, pf); /* Sample observed counts using DChoose(). */ esl_vec_ISet(ct, nbins, 0); for (i = 0; i < n; i++) ct[esl_rnd_DChoose(r, pd, nbins)]++; /* X^2 test on those observed counts. */ for (X2 = 0., i=0; i < nbins; i++) { exp = (double) n * pd[i]; diff = (double) ct[i] - exp; X2 += diff*diff/exp; } if (esl_stats_ChiSquaredTest(nbins, X2, &X2p) != eslOK) esl_fatal("chi square eval failed"); if (be_verbose) printf("DChoose(): \t%g\n", X2p); if (X2p < 0.01) esl_fatal("chi squared test failed"); /* Repeat above for FChoose(). */ esl_vec_ISet(ct, nbins, 0); for (i = 0; i < n; i++) ct[esl_rnd_FChoose(r, pf, nbins)]++; for (X2 = 0., i=0; i < nbins; i++) { exp = (double) n * pd[i]; diff = (double) ct[i] - exp; X2 += diff*diff/exp; } if (esl_stats_ChiSquaredTest(nbins, X2, &X2p) != eslOK) esl_fatal("chi square eval failed"); if (be_verbose) printf("FChoose(): \t%g\n", X2p); if (X2p < 0.01) esl_fatal("chi squared test failed"); free(pd); free(pf); free(ct); return; }
/* Function: esl_mixgev_FitGuess() * * Purpose: Make initial randomized guesses at the parameters * of mixture GEV <mg>, using random number generator * <r> and observed data consisting of <n> values * <x[0..n-1]>. This guess is a suitable starting * point for a parameter optimization routine, such * as <esl_mixgev_FitComplete()>. * * Specifically, we estimate one 'central' guess * for a single Gumbel fit to the data, using the * method of moments. Then we add $\pm 10\%$ to that 'central' * $\mu$ and $\lambda$ to get each component * $\mu_i$ and $\lambda_i$. The $\alpha_i$ parameters * are generated by sampling uniformly from $-0.1..0.1$. * Mixture coefficients $q_i$ are sampled uniformly. * * Args: r - randomness source * x - vector of observed data values to fit, 0..n-1 * n - number of values in <x> * mg - mixture GEV to put guessed params into * * Returns: <eslOK> on success. */ int esl_mixgev_FitGuess(ESL_RANDOMNESS *r, double *x, int n, ESL_MIXGEV *mg) { double mean, variance; double mu, lambda; int k; esl_stats_DMean(x, n, &mean, &variance); lambda = eslCONST_PI / sqrt(6.*variance); mu = mean - 0.57722/lambda; esl_dirichlet_DSampleUniform(r, mg->K, mg->q); for (k = 0; k < mg->K; k++) { mg->mu[k] = mu + 0.2 * mu * (esl_random(r) - 0.5); mg->lambda[k] = lambda + 0.2 * lambda * (esl_random(r) - 0.5); if (mg->isgumbel[k]) mg->alpha[k] = 0.; else mg->alpha[k] = 0.2 * (esl_random(r) - 0.5); } return eslOK; }