3 #include <gsl/gsl_roots.h>
4 #include <gsl/gsl_sf.h>
8 const struct fixed_string empty_string = {NULL, 0};
11 expr_error (void *aux UNUSED, const char *format, ...)
16 /* FIXME: we can do better about saying where the error
19 err_location (&e.where);
22 va_start (args, format);
23 err_vmsg (&e, format, args);
28 expr_ymd_to_ofs (double year, double month, double day)
34 if (y != year || m != month || d != day)
36 msg (SE, _("One of the arguments to a DATE function is not an integer. "
37 "The result will be system-missing."));
41 return calendar_gregorian_to_offset (y, m, d, expr_error, NULL);
45 expr_ymd_to_date (double year, double month, double day)
47 double ofs = expr_ymd_to_ofs (year, month, day);
48 return ofs != SYSMIS ? ofs * DAY_S : SYSMIS;
52 expr_wkyr_to_date (double week, double year)
58 msg (SE, _("The week argument to DATE.WKYR is not an integer. "
59 "The result will be system-missing."));
62 else if (w < 1 || w > 53)
64 msg (SE, _("The week argument to DATE.WKYR is outside the acceptable "
66 "The result will be system-missing."));
71 double yr_1_1 = expr_ymd_to_ofs (year, 1, 1);
73 return DAY_S * (yr_1_1 + WEEK_DAY * (w - 1));
80 expr_yrday_to_date (double year, double yday)
86 msg (SE, _("The day argument to DATE.YRDAY is not an integer. "
87 "The result will be system-missing."));
90 else if (yd < 1 || yd > 366)
92 msg (SE, _("The day argument to DATE.YRDAY is outside the acceptable "
94 "The result will be system-missing."));
99 double yr_1_1 = expr_ymd_to_ofs (year, 1, 1);
100 if (yr_1_1 != SYSMIS)
101 return DAY_S * (yr_1_1 + yd - 1.);
108 expr_yrmoda (double year, double month, double day)
110 if (year >= 0 && year <= 99)
112 else if (year != (int) year && year > 47516)
114 msg (SE, _("The year argument to YRMODA is greater than 47516. "
115 "The result will be system-missing."));
119 return expr_ymd_to_ofs (year, month, day);
123 compare_string (const struct fixed_string *a, const struct fixed_string *b)
127 for (i = 0; i < a->length && i < b->length; i++)
128 if (a->string[i] != b->string[i])
129 return a->string[i] < b->string[i] ? -1 : 1;
130 for (; i < a->length; i++)
131 if (a->string[i] != ' ')
133 for (; i < b->length; i++)
134 if (b->string[i] != ' ')
140 count_valid (double *d, size_t d_cnt)
146 for (i = 0; i < d_cnt; i++)
147 valid_cnt += is_valid (d[i]);
152 alloc_string (struct expression *e, size_t length)
154 struct fixed_string s;
156 s.string = pool_alloc (e->eval_pool, length);
161 copy_string (struct expression *e, const char *old, size_t length)
163 struct fixed_string s = alloc_string (e, length);
164 memcpy (s.string, old, length);
176 generalized_idf (double P, double a, double b, double (*cdf) (double, void *))
178 struct func_params params;
180 gsl_root_fsolver *fsolver;
195 fsolver = gsl_root_fsolver_alloc (gsl_root_fsolver_brent);
196 gsl_root_fsolver_set (fsolver, &f, 0, 1);
201 double x_lower, x_upper;
203 status = gsl_root_fsolver_iterate (fsolver);
207 x_lower = gsl_root_fsolver_x_lower (fsolver);
208 x_upper = gsl_root_fsolver_x_upper (fsolver);
209 status = gsl_root_test_interval (x_lower, x_upper, 0, 2 * DBL_EPSILON);
211 while (status == GSL_CONTINUE && ++iter < 100);
213 root = gsl_root_fsolver_root (fsolver);
214 gsl_root_fsolver_free (fsolver);
219 cdf_beta (double x, void *params_)
221 struct func_params *params = params_;
223 return gsl_cdf_beta_P (x, params->a, params->b) - params->Ptarget;
227 idf_beta (double P, double a, double b)
230 return generalized_idf (P, a, b, cdf_beta);
232 double x = a / (a + b);
234 while (fabs (dx) > 2 * DBL_EPSILON)
236 dx = (gsl_sf_beta_inc (a, b, x) - P) / gsl_ran_beta_pdf (x, a, b);
246 /* Returns the noncentral beta cumulative distribution function
247 value for the given arguments.
249 FIXME: The accuracy of this function is not entirely
250 satisfactory. We only match the example values given in AS
251 310 to the first 5 significant digits. */
253 ncdf_beta (double x, double a, double b, double lambda)
257 if (x <= 0. || x >= 1. || a <= 0. || b <= 0. || lambda <= 0.)
263 /* Algorithm AS 226. */
264 double x0, a0, beta, temp, gx, q, ax, sumq, sum;
265 double err_max = 2 * DBL_EPSILON;
270 x0 = floor (c - 5.0 * sqrt (c));
274 beta = (gsl_sf_lngamma (a0)
276 - gsl_sf_lngamma (a0 + b));
277 temp = gsl_sf_beta_inc (a0, b, x);
278 gx = exp (a0 * log (x) + b * log (1. - x) - beta - log (a0));
280 q = exp (-c + x0 * log (c)) - gsl_sf_lngamma (x0 + 1.);
292 gx = x * (a + b + iter - 1.) * gx / (a + iter);
298 err_bound = (temp - gx) * sumq;
300 while (iter < iter_max && err_bound > err_max);
306 /* Algorithm AS 310. */
308 int iter, iter_lower, iter_upper, iter1, iter2, j;
309 double t, q, r, psum, beta, s1, gx, fx, temp, ftemp, t0, s0, sum, s;
311 double err_max = 2 * DBL_EPSILON;
317 iter_lower = m - 5. * m_sqrt;
318 iter_upper = m + 5. * m_sqrt;
320 t = -c + m * log (c) - gsl_sf_lngamma (m + 1.);
324 beta = (gsl_sf_lngamma (a + m)
326 - gsl_sf_lngamma (a + m + b));
327 s1 = (a + m) * log (x) + b * log (1. - x) - log (a + m) - beta;
329 ftemp = temp = gsl_sf_beta_inc (a + m, b, x);
334 while (iter1 >= iter_lower && q >= err_max)
338 gx = (a + iter1) / (x * (a + b + iter1 - 1.)) * gx;
345 t0 = (gsl_sf_lngamma (a + b)
346 - gsl_sf_lngamma (a + 1.)
347 - gsl_sf_lngamma (b));
348 s0 = a * log (x) + b * log (1. - x);
351 for (j = 0; j < iter1; j++)
354 s += exp (t0 + s0 + j * log (x));
355 t1 = log (a + b + j) - log (a + 1. + j) + t0;
359 err_bound = (1. - gsl_sf_gamma_inc_P (iter1, c)) * (temp + s);
366 double ebd = err_bound + (1. - psum) * temp;
367 if (ebd < err_max || iter >= iter_upper)
375 gx = x * (a + b + iter2 - 1.) / (a + iter2) * gx;
384 cdf_bvnor (double x0, double x1, double r)
386 double z = x0 * x0 - 2. * r * x0 * x1 + x1 * x1;
387 return exp (-z / (2. * (1 - r * r))) * (2. * M_PI * sqrt (1 - r * r));
391 idf_fdist (double P, double df1, double df2)
393 double temp = idf_beta (P, df1 / 2, df2 / 2);
394 return temp * df2 / ((1. - temp) * df1);
398 * Mathlib : A C Library of Special Functions
399 * Copyright (C) 1998 Ross Ihaka
400 * Copyright (C) 2000 The R Development Core Team
402 * This program is free software; you can redistribute it and/or
404 * it under the terms of the GNU General Public License as
406 * the Free Software Foundation; either version 2 of the
408 * (at your option) any later version.
410 * This program is distributed in the hope that it will be
412 * but WITHOUT ANY WARRANTY; without even the implied warranty
414 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
415 * GNU General Public License for more details.
417 * You should have received a copy of the GNU General Public
419 * along with this program; if not, write to the Free Software
420 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
424 /* Returns the density of the noncentral beta distribution with
425 noncentrality parameter LAMBDA. */
427 npdf_beta (double x, double a, double b, double lambda)
429 if (lambda < 0. || a <= 0. || b <= 0.)
431 else if (lambda == 0.)
432 return gsl_ran_beta_pdf (x, a, b);
435 double max_error = 2 * DBL_EPSILON;
437 double term = gsl_ran_beta_pdf (x, a, b);
438 double lambda2 = 0.5 * lambda;
439 double weight = exp (-lambda2);
440 double sum = weight * term;
441 double psum = weight;
443 for (k = 1; k <= max_iter && 1 - psum < max_error; k++) {
444 weight *= lambda2 / k;
445 term *= x * (a + b) / a;
446 sum += weight * term;