/* sample from truncated inverse chi squared truncated above at "max" */ double TruncInvChisq(int df, double scale, double max, int invcdf) { double temp = 0, temp_pg, g_shape, g_scale; double out; int i; g_shape = (double)df / 2; g_scale = 2 / ((double)df * scale); if (invcdf) {/* inverse cdf method */ temp = runif(0, 1); temp_pg = pgamma(1 / max, g_shape, g_scale, 1, 0); temp = (temp * ((double)1 - temp_pg)) + temp_pg; out = qgamma(temp, g_shape, g_scale, 1, 0); } else {/* rejection sampling method */ for (i = 0; i < 10000; i++) { out = rgamma(g_shape, g_scale); if (out > 1 / max ) break; if (temp == 9999) { /* error("Too many rejections. Try the inverse-CDF method"); */ /* If there are too many rejections, inverse-CDF method */ temp = runif(0, 1); temp_pg = pgamma(1 / max, g_shape, g_scale, 1, 0); temp = (temp * ((double)1 - temp_pg)) + temp_pg; out = qgamma(temp, g_shape, g_scale, 1, 0); } } } return (1 / out); }
Type objective_function<Type>::operator() () { DATA_VECTOR(y); PARAMETER(phi); PARAMETER(shape); PARAMETER(scale); PARAMETER(sd); PARAMETER_VECTOR(u); Type res=0; res += density::AR1(phi)(u); vector<Type> unif = pnorm(u,Type(0),Type(1)); vector<Type> x = qgamma(unif,shape,scale); res -= dnorm(y,x,sd,true).sum(); return res; }
double qchisq(double p, double df, int lower_tail, int log_p) { return qgamma(p, 0.5 * df, 2.0, lower_tail, log_p); }
void printpts( double r, int k1, int k2 ) { double d1 = qgamma(C, (double)k1, 1/r, 1, 0); double d2 = qgamma(C, (double)k2, 1/r, 1, 0); double d = qgamma(C, (double)(k1+k2), 1/r, 1, 0); printf("%.5f\t%.5f\t%.5f\n", d1, d2, d); }
double qerlang( double area, int shape, double rate, int lower ) { return qgamma(area, (double)shape, 1/rate, lower, 0); }