5 static const double a[] = {
6 -3.969683028665376e+01, 2.209460984245205e+02,
7 -2.759285104469687e+02, 1.383577518672690e+02,
8 -3.066479806614716e+01, 2.506628277459239e+00
10 static const double b[] = {
11 -5.447609879822406e+01, 1.615858368580409e+02,
12 -1.556989798598866e+02, 6.680131188771972e+01,
13 -1.328068155288572e+01
15 static const double c[] = {
16 -7.784894002430293e-03, -3.223964580411365e-01,
17 -2.400758277161838e+00, -2.549732539343734e+00,
18 4.374664141464968e+00, 2.938163982698783e+00
20 static const double d[] = {
21 7.784695709041462e-03, 3.224671290700398e-01,
22 2.445134137142996e+00, 3.754408661907416e+00
26 double plow = 0.02425;
27 double phigh = 1.0 - plow;
31 q = sqrt(-2.0 * log(p));
32 return (((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
33 ((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1.0);
34 }
else if (p <= phigh) {
38 return (((((a[0]*r+a[1])*r+a[2])*r+a[3])*r+a[4])*r+a[5])*q /
39 (((((b[0]*r+b[1])*r+b[2])*r+b[3])*r+b[4])*r+1.0);
42 q = sqrt(-2.0 * log(1.0 - p));
43 return -(((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
44 ((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1.0);
144 const lmmc_real_t tail = fmin(target, 1.0 - target);
145 return fabs(value - target) <= 1e-12 * tail;
155 if (quantile_probability_close(lower_cdf, probability)) {
159 if (quantile_probability_close(upper_cdf, probability)) {
171 for (iteration = 0; *value > probability;
173 if (iteration >= 1024 || *bound <= -DBL_MAX) {
176 *bound = *bound <= -DBL_MAX / 2.0 ? -DBL_MAX : *bound * 2.0;
177 status = evaluate_distribution(cdf, *bound, context, value);
188 for (iteration = 0; *value < probability;
190 if (iteration >= 1024 || *bound >= DBL_MAX) {
193 *bound = *bound >= DBL_MAX / 2.0 ? DBL_MAX : *bound * 2.0;
194 status = evaluate_distribution(cdf, *bound, context, value);
203 const void* context) {
207 if (status ==
LMMC_STATUS_OK && isfinite(pdf_value) && pdf_value > DBL_MIN) {
208 candidate = x - (cdf_value - probability) / pdf_value;
210 if (!isfinite(candidate) || candidate <= lower || candidate >= upper) {
211 candidate = lower * 0.5 + upper * 0.5;
224 if (!isfinite(x) || x <= lower || x >= upper) {
225 x = lower * 0.5 + upper * 0.5;
227 for (iteration = 0; iteration < 2048; ++iteration) {
229 status = evaluate_distribution(cdf, x, context, &cdf_value);
231 if (quantile_probability_close(cdf_value, probability)) {
236 if (cdf_value < probability) {
238 lower_cdf = cdf_value;
241 upper_cdf = cdf_value;
243 if (nextafter(lower, upper) == upper) {
244 return finish_quantile_bracket(
245 probability, lower, lower_cdf, upper, upper_cdf, out);
248 candidate = quantile_newton_candidate(
249 probability, x, cdf_value, lower, upper, pdf, context);
250 if (candidate == lower || candidate == upper) {
251 return finish_quantile_bracket(
252 probability, lower, lower_cdf, upper, upper_cdf, out);
273 status = evaluate_distribution(cdf, lower, context, &lower_cdf);
275 status = evaluate_distribution(cdf, upper, context, &upper_cdf);
279 status = expand_quantile_lower(probability, &lower, &lower_cdf, cdf, context);
285 status = expand_quantile_upper(probability, &upper, &upper_cdf, cdf, context);
290 if (lower_cdf > probability || upper_cdf < probability) {
293 return refine_quantile_bracket(probability, initial, lower, upper,
294 lower_cdf, upper_cdf, cdf, pdf, context, out);
304 result = fma(sigma, normal_quantile_rational(p), mu);
314 if (student_t_uses_normal_limit(df)) {
315 const lmmc_real_t result = normal_quantile_rational(p);
321 parameters.
first = df;
323 return bracketed_quantile(
324 p, normal_quantile_rational(p), -1.0, 1.0, 1, 1,
325 eval_t_cdf, eval_t_pdf, ¶meters, out);
334 z = normal_quantile_rational(p);
335 term = 1.0 - 2.0 / (9.0 * df) + z * sqrt(2.0 / (9.0 * df));
336 initial = df * term * term * term;
337 if (!isfinite(initial) || initial <= 0.0) {
340 parameters.
first = df;
342 return bracketed_quantile(
343 p, initial, 0.0, fmax(initial, 1.0), 0, 1,
344 eval_chi2_cdf, eval_chi2_pdf, ¶meters, out);
353 parameters.
first = df1;
355 return bracketed_quantile(
356 p, 1.0, 0.0, 1.0, 0, 1,
357 eval_f_cdf, eval_f_pdf, ¶meters, out);
367 initial = shape * scale;
368 if (!isfinite(initial) || initial <= 0.0) {
371 parameters.
first = shape;
372 parameters.
second = scale;
373 return bracketed_quantile(
374 p, initial, 0.0, fmax(initial, 1.0), 0, 1,
375 eval_gamma_cdf, eval_gamma_pdf, ¶meters, out);
383 if (!valid_positive(alpha) || !valid_positive(beta_param) ||
386 if (alpha >= beta_param) {
387 initial = 1.0 / (1.0 + beta_param / alpha);
389 ratio = alpha / beta_param;
390 initial = ratio / (1.0 + ratio);
392 parameters.
first = alpha;
393 parameters.
second = beta_param;
394 return bracketed_quantile(
395 p, initial, 0.0, 1.0, 0, 0,
396 eval_beta_cdf, eval_beta_pdf, ¶meters, out);
double lmmc_real_t
Public and internal LMMC real type.
lmmc_status_t(*) distribution_eval_fn(lmmc_real_t, const void *, lmmc_real_t *)
lmmc_status_t lmmc_dist_chi2_quantile(lmmc_real_t p, lmmc_real_t df, lmmc_real_t *out)
χ² 分布分位数函数。
lmmc_status_t lmmc_dist_normal_quantile(lmmc_real_t p, lmmc_real_t mu, lmmc_real_t sigma, lmmc_real_t *out)
正态分布分位数函数(逆 CDF)。
lmmc_status_t lmmc_dist_beta_pdf(lmmc_real_t x, lmmc_real_t alpha, lmmc_real_t beta, lmmc_real_t *out)
贝塔分布概率密度函数。
lmmc_status_t lmmc_dist_chi2_cdf(lmmc_real_t x, lmmc_real_t df, lmmc_real_t *out)
χ² 分布累积分布函数。
lmmc_status_t lmmc_dist_t_cdf(lmmc_real_t x, lmmc_real_t df, lmmc_real_t *out)
t 分布累积分布函数。
lmmc_status_t lmmc_dist_gamma_quantile(lmmc_real_t p, lmmc_real_t shape, lmmc_real_t scale, lmmc_real_t *out)
伽马分布分位数函数。
lmmc_status_t lmmc_dist_f_quantile(lmmc_real_t p, lmmc_real_t df1, lmmc_real_t df2, lmmc_real_t *out)
F 分布分位数函数。
lmmc_status_t lmmc_dist_gamma_pdf(lmmc_real_t x, lmmc_real_t shape, lmmc_real_t scale, lmmc_real_t *out)
伽马分布概率密度函数。
lmmc_status_t lmmc_dist_t_quantile(lmmc_real_t p, lmmc_real_t df, lmmc_real_t *out)
t 分布分位数函数。
lmmc_status_t lmmc_dist_beta_cdf(lmmc_real_t x, lmmc_real_t alpha, lmmc_real_t beta, lmmc_real_t *out)
贝塔分布累积分布函数。
lmmc_status_t lmmc_dist_gamma_cdf(lmmc_real_t x, lmmc_real_t shape, lmmc_real_t scale, lmmc_real_t *out)
伽马分布累积分布函数。
lmmc_status_t lmmc_dist_f_cdf(lmmc_real_t x, lmmc_real_t df1, lmmc_real_t df2, lmmc_real_t *out)
F 分布累积分布函数。
lmmc_status_t lmmc_dist_chi2_pdf(lmmc_real_t x, lmmc_real_t df, lmmc_real_t *out)
χ² 分布概率密度函数。
lmmc_status_t lmmc_dist_beta_quantile(lmmc_real_t p, lmmc_real_t alpha, lmmc_real_t beta, lmmc_real_t *out)
贝塔分布分位数函数。
lmmc_status_t lmmc_dist_t_pdf(lmmc_real_t x, lmmc_real_t df, lmmc_real_t *out)
t 分布概率密度函数。
lmmc_status_t lmmc_dist_f_pdf(lmmc_real_t x, lmmc_real_t df1, lmmc_real_t df2, lmmc_real_t *out)
F 分布概率密度函数。
lmmc_status_t
LMMC 库统一返回的状态码.
@ LMMC_STATUS_CONVERGENCE_FAILED
@ LMMC_STATUS_NUMERICAL_FAILURE
@ LMMC_STATUS_INVALID_ARGUMENT
@ LMMC_STATUS_OUT_OF_RANGE