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);
168 /* Returns the noncentral beta cumulative distribution function
169 value for the given arguments.
171 FIXME: The accuracy of this function is not entirely
172 satisfactory. We only match the example values given in AS
173 310 to the first 5 significant digits. */
175 ncdf_beta (double x, double a, double b, double lambda)
179 if (x <= 0. || x >= 1. || a <= 0. || b <= 0. || lambda <= 0.)
185 /* Algorithm AS 226. */
186 double x0, a0, beta, temp, gx, q, ax, sumq, sum;
187 double err_max = 2 * DBL_EPSILON;
192 x0 = floor (c - 5.0 * sqrt (c));
196 beta = (gsl_sf_lngamma (a0)
198 - gsl_sf_lngamma (a0 + b));
199 temp = gsl_sf_beta_inc (a0, b, x);
200 gx = exp (a0 * log (x) + b * log (1. - x) - beta - log (a0));
202 q = exp (-c + x0 * log (c)) - gsl_sf_lngamma (x0 + 1.);
214 gx = x * (a + b + iter - 1.) * gx / (a + iter);
220 err_bound = (temp - gx) * sumq;
222 while (iter < iter_max && err_bound > err_max);
228 /* Algorithm AS 310. */
230 int iter, iter_lower, iter_upper, iter1, iter2, j;
231 double t, q, r, psum, beta, s1, gx, fx, temp, ftemp, t0, s0, sum, s;
233 double err_max = 2 * DBL_EPSILON;
239 iter_lower = m - 5. * m_sqrt;
240 iter_upper = m + 5. * m_sqrt;
242 t = -c + m * log (c) - gsl_sf_lngamma (m + 1.);
246 beta = (gsl_sf_lngamma (a + m)
248 - gsl_sf_lngamma (a + m + b));
249 s1 = (a + m) * log (x) + b * log (1. - x) - log (a + m) - beta;
251 ftemp = temp = gsl_sf_beta_inc (a + m, b, x);
256 while (iter1 >= iter_lower && q >= err_max)
260 gx = (a + iter1) / (x * (a + b + iter1 - 1.)) * gx;
267 t0 = (gsl_sf_lngamma (a + b)
268 - gsl_sf_lngamma (a + 1.)
269 - gsl_sf_lngamma (b));
270 s0 = a * log (x) + b * log (1. - x);
273 for (j = 0; j < iter1; j++)
276 s += exp (t0 + s0 + j * log (x));
277 t1 = log (a + b + j) - log (a + 1. + j) + t0;
281 err_bound = (1. - gsl_sf_gamma_inc_P (iter1, c)) * (temp + s);
288 double ebd = err_bound + (1. - psum) * temp;
289 if (ebd < err_max || iter >= iter_upper)
297 gx = x * (a + b + iter2 - 1.) / (a + iter2) * gx;
306 cdf_bvnor (double x0, double x1, double r)
308 double z = x0 * x0 - 2. * r * x0 * x1 + x1 * x1;
309 return exp (-z / (2. * (1 - r * r))) * (2. * M_PI * sqrt (1 - r * r));
313 idf_fdist (double P, double df1, double df2)
315 double temp = gslextras_cdf_beta_Pinv (P, df1 / 2, df2 / 2);
316 return temp * df2 / ((1. - temp) * df1);
320 * Mathlib : A C Library of Special Functions
321 * Copyright (C) 1998 Ross Ihaka
322 * Copyright (C) 2000 The R Development Core Team
324 * This program is free software; you can redistribute it and/or
326 * it under the terms of the GNU General Public License as
328 * the Free Software Foundation; either version 2 of the
330 * (at your option) any later version.
332 * This program is distributed in the hope that it will be
334 * but WITHOUT ANY WARRANTY; without even the implied warranty
336 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
337 * GNU General Public License for more details.
339 * You should have received a copy of the GNU General Public
341 * along with this program; if not, write to the Free Software
342 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
346 /* Returns the density of the noncentral beta distribution with
347 noncentrality parameter LAMBDA. */
349 npdf_beta (double x, double a, double b, double lambda)
351 if (lambda < 0. || a <= 0. || b <= 0.)
353 else if (lambda == 0.)
354 return gsl_ran_beta_pdf (x, a, b);
357 double max_error = 2 * DBL_EPSILON;
359 double term = gsl_ran_beta_pdf (x, a, b);
360 double lambda2 = 0.5 * lambda;
361 double weight = exp (-lambda2);
362 double sum = weight * term;
363 double psum = weight;
365 for (k = 1; k <= max_iter && 1 - psum < max_error; k++) {
366 weight *= lambda2 / k;
367 term *= x * (a + b) / a;
368 sum += weight * term;