double test_tdist2_pdf (double x) { return gsl_ran_tdist_pdf (x, 12.75); }
double tdist (double x, void *p) { double * c = (double *)p; return gsl_ran_tdist_pdf (x, c[0]); }
double gsl_cdf_tdist_Pinv (const double P, const double nu) { double x, ptail; if (P == 1.0) { return GSL_POSINF; } else if (P == 0.0) { return GSL_NEGINF; } if (nu == 1.0) { x = tan (M_PI * (P - 0.5)); return x; } else if (nu == 2.0) { x = (2 * P - 1) / sqrt (2 * P * (1 - P)); return x; } ptail = (P < 0.5) ? P : 1 - P; if (sqrt (M_PI * nu / 2) * ptail > pow (0.05, nu / 2)) { double xg = gsl_cdf_ugaussian_Pinv (P); x = inv_cornish_fisher (xg, nu); } else { /* Use an asymptotic expansion of the tail of integral */ double beta = gsl_sf_beta (0.5, nu / 2); if (P < 0.5) { x = -sqrt (nu) * pow (beta * nu * P, -1.0 / nu); } else { x = sqrt (nu) * pow (beta * nu * (1 - P), -1.0 / nu); } /* Correct nu -> nu/(1+nu/x^2) in the leading term to account for higher order terms. This avoids overestimating x, which makes the iteration unstable due to the rapidly decreasing tails of the distribution. */ x /= sqrt (1 + nu / (x * x)); } { double dP, phi; unsigned int n = 0; start: dP = P - gsl_cdf_tdist_P (x, nu); phi = gsl_ran_tdist_pdf (x, nu); if (dP == 0.0 || n++ > 32) goto end; { double lambda = dP / phi; double step0 = lambda; double step1 = ((nu + 1) * x / (x * x + nu)) * (lambda * lambda / 4.0); double step = step0; if (fabs (step1) < fabs (step0)) { step += step1; } if (P > 0.5 && x + step < 0) x /= 2; else if (P < 0.5 && x + step > 0) x /= 2; else x += step; if (fabs (step) > 1e-10 * fabs (x)) goto start; } end: if (fabs(dP) > GSL_SQRT_DBL_EPSILON * P) { GSL_ERROR_VAL("inverse failed to converge", GSL_EFAILED, GSL_NAN); } return x; } }
double gsl_cdf_tdist_Qinv (const double Q, const double nu) { double x, qtail; if (Q == 0.0) { return GSL_POSINF; } else if (Q == 1.0) { return GSL_NEGINF; } if (nu == 1.0) { x = tan (M_PI * (0.5 - Q)); return x; } else if (nu == 2.0) { x = (1 - 2 * Q) / sqrt (2 * Q * (1 - Q)); return x; } qtail = (Q < 0.5) ? Q : 1 - Q; if (sqrt (M_PI * nu / 2) * qtail > pow (0.05, nu / 2)) { double xg = gsl_cdf_ugaussian_Qinv (Q); x = inv_cornish_fisher (xg, nu); } else { /* Use an asymptotic expansion of the tail of integral */ double beta = gsl_sf_beta (0.5, nu / 2); if (Q < 0.5) { x = sqrt (nu) * pow (beta * nu * Q, -1.0 / nu); } else { x = -sqrt (nu) * pow (beta * nu * (1 - Q), -1.0 / nu); } /* Correct nu -> nu/(1+nu/x^2) in the leading term to account for higher order terms. This avoids overestimating x, which makes the iteration unstable due to the rapidly decreasing tails of the distribution. */ x /= sqrt (1 + nu / (x * x)); } { double dQ, phi; unsigned int n = 0; start: dQ = Q - gsl_cdf_tdist_Q (x, nu); phi = gsl_ran_tdist_pdf (x, nu); if (dQ == 0.0 || n++ > 32) goto end; { double lambda = - dQ / phi; double step0 = lambda; double step1 = ((nu + 1) * x / (x * x + nu)) * (lambda * lambda / 4.0); double step = step0; if (fabs (step1) < fabs (step0)) { step += step1; } if (Q < 0.5 && x + step < 0) x /= 2; else if (Q > 0.5 && x + step > 0) x /= 2; else x += step; if (fabs (step) > 1e-10 * fabs (x)) goto start; } } end: return x; }
static double one_t(double in, void *params){ double mu = ((double*)params)[0]; double sigma = ((double*)params)[1]; double df = ((double*)params)[2]; return log(gsl_ran_tdist_pdf((in-mu)/(sigma/sqrt(df)), df)); }
int main(int argc, char **argv){ distlist distribution = Normal; char msg[10000], c; int pval = 0, qval = 0; double param1 = GSL_NAN, param2 =GSL_NAN, findme = GSL_NAN; char number[1000]; sprintf(msg, "%s [opts] number_to_lookup\n\n" "Look up a probability or p-value for a given standard distribution.\n" "[This is still loosely written and counts as beta. Notably, negative numbers are hard to parse.]\n" "E.g.:\n" "%s -dbin 100 .5 34\n" "sets the distribution to a Binomial(100, .5), and find the odds of 34 appearing.\n" "%s -p 2 \n" "find the area of the Normal(0,1) between -infty and 2. \n" "\n" "-pval Find the p-value: integral from -infinity to your value\n" "-qval Find the q-value: integral from your value to infinity\n" "\n" "After giving an optional -p or -q, specify the distribution. \n" "Default is Normal(0, 1). Other options:\n" "\t\t-binom Binomial(n, p)\n" "\t\t-beta Beta(a, b)\n" "\t\t-f F distribution(df1, df2)\n" "\t\t-norm Normal(mu, sigma)\n" "\t\t-negative bin Negative binomial(n, p)\n" "\t\t-poisson Poisson(L)\n" "\t\t-t t distribution(df)\n" "I just need enough letters to distinctly identify a distribution.\n" , argv[0], argv[0], argv[0]); opterr=0; if(argc==1){ printf("%s", msg); return 0; } while ((c = getopt (argc, argv, "B:b:F:f:N:n:pqT:t:")) != -1){ switch (c){ case 'B': case 'b': if (optarg[0]=='i') distribution = Binomial; else if (optarg[0]=='e') distribution = Beta; else { printf("I can't parse the option -b%s\n", optarg); exit(0); } param1 = atof(argv[optind]); param2 = atof(argv[optind+1]); findme = atof(argv[optind+2]); break; case 'F': case 'f': distribution = F; param1 = atof(argv[optind]); findme = atof(argv[optind+1]); break; case 'H': case 'h': printf("%s", msg); return 0; case 'n': case 'N': if (optarg[0]=='o'){ //normal param1 = atof(argv[optind]); param2 = atof(argv[optind+1]); findme = atof(argv[optind+2]); } else if (optarg[0]=='e'){ distribution = Negbinom; param1 = atof(argv[optind]); param2 = atof(argv[optind+1]); findme = atof(argv[optind+2]); } else { printf("I can't parse the option -n%s\n", optarg); exit(0); } break; case 'p': if (!optarg || optarg[0] == 'v') pval++; else if (optarg[0] == 'o'){ distribution = Poisson; param1 = atof(argv[optind]); findme = atof(argv[optind+1]); } else { printf("I can't parse the option -p%s\n", optarg); exit(0); } break; case 'q': qval++; break; case 'T': case 't': distribution = T; param1 = atof(argv[optind]); findme = atof(argv[optind+1]); break; case '?'://probably a negative number if (optarg) snprintf(number, 1000, "%c%s", optopt, optarg); else snprintf(number, 1000, "%c", optopt); if (gsl_isnan(param1)) param1 = -atof(number); else if (gsl_isnan(param2)) param2 = -atof(number); else if (gsl_isnan(findme)) findme = -atof(number); } } if (gsl_isnan(findme)) findme = atof(argv[optind]); //defaults, as promised if (gsl_isnan(param1)) param1 = 0; if (gsl_isnan(param2)) param2 = 1; if (!pval && !qval){ double val = distribution == Beta ? gsl_ran_beta_pdf(findme, param1, param2) : distribution == Binomial ? gsl_ran_binomial_pdf(findme, param2, param1) : distribution == F ? gsl_ran_fdist_pdf(findme, param1, param2) : distribution == Negbinom ? gsl_ran_negative_binomial_pdf(findme, param2, param1) : distribution == Normal ? gsl_ran_gaussian_pdf(findme, param2)+param1 : distribution == Poisson ? gsl_ran_poisson_pdf(findme, param1) : distribution == T ? gsl_ran_tdist_pdf(findme, param1) : GSL_NAN; printf("%g\n", val); return 0; } if (distribution == Binomial){ printf("Sorry, the GSL doesn't have a Binomial CDF.\n"); return 0; } if (distribution == Negbinom){ printf("Sorry, the GSL doesn't have a Negative Binomial CDF.\n"); return 0; } if (distribution == Poisson){ printf("Sorry, the GSL doesn't have a Poisson CDF.\n"); return 0; } if (pval){ double val = distribution == Beta ? gsl_cdf_beta_P(findme, param1, param2) : distribution == F ? gsl_cdf_fdist_P(findme, param1, param2) : distribution == Normal ? gsl_cdf_gaussian_P(findme-param1, param2) : distribution == T ? gsl_cdf_tdist_P(findme, param1) : GSL_NAN; printf("%g\n", val); return 0; } if (qval){ double val = distribution == Beta ? gsl_cdf_beta_Q(findme, param1, param2) : distribution == F ? gsl_cdf_fdist_Q(findme, param1, param2) : distribution == Normal ? gsl_cdf_gaussian_Q(findme-param1, param2) : distribution == T ? gsl_cdf_tdist_Q(findme, param1) : GSL_NAN; printf("%g\n", val); } }
double skew_student(double x, double mu, double sig, double skew, double nu){ return 2.*gsl_ran_tdist_pdf((x-mu)/sig, nu)* \ gsl_cdf_tdist_P(skew*((x-mu)/sig)*sqrt((nu+1.)/(nu+(x-mu)*(x-mu)/sig/sig)),(nu+1.))/sig;}