3 #include <gsl/gsl_roots.h>
4 #include <gsl/gsl_sf.h>
5 #include <libpspp/pool.h>
8 const struct substring empty_string = {NULL, 0};
11 expr_error (void *aux UNUSED, const char *format, ...)
16 m.category = MSG_SYNTAX;
17 m.severity = MSG_ERROR;
18 va_start (args, format);
19 m.text = xvasprintf (format, args);
26 expr_ymd_to_ofs (double year, double month, double day)
32 if (y != year || m != month || d != day)
34 msg (SE, _("One of the arguments to a DATE function is not an integer. "
35 "The result will be system-missing."));
39 return calendar_gregorian_to_offset (y, m, d, expr_error, NULL);
43 expr_ymd_to_date (double year, double month, double day)
45 double ofs = expr_ymd_to_ofs (year, month, day);
46 return ofs != SYSMIS ? ofs * DAY_S : SYSMIS;
50 expr_wkyr_to_date (double week, double year)
56 msg (SE, _("The week argument to DATE.WKYR is not an integer. "
57 "The result will be system-missing."));
60 else if (w < 1 || w > 53)
62 msg (SE, _("The week argument to DATE.WKYR is outside the acceptable "
64 "The result will be system-missing."));
69 double yr_1_1 = expr_ymd_to_ofs (year, 1, 1);
71 return DAY_S * (yr_1_1 + WEEK_DAY * (w - 1));
78 expr_yrday_to_date (double year, double yday)
84 msg (SE, _("The day argument to DATE.YRDAY is not an integer. "
85 "The result will be system-missing."));
88 else if (yd < 1 || yd > 366)
90 msg (SE, _("The day argument to DATE.YRDAY is outside the acceptable "
92 "The result will be system-missing."));
97 double yr_1_1 = expr_ymd_to_ofs (year, 1, 1);
99 return DAY_S * (yr_1_1 + yd - 1.);
106 expr_yrmoda (double year, double month, double day)
108 if (year >= 0 && year <= 99)
110 else if (year != (int) year && year > 47516)
112 msg (SE, _("The year argument to YRMODA is greater than 47516. "
113 "The result will be system-missing."));
117 return expr_ymd_to_ofs (year, month, day);
121 compare_string (const struct substring *a, const struct substring *b)
125 for (i = 0; i < a->length && i < b->length; i++)
126 if (a->string[i] != b->string[i])
127 return a->string[i] < b->string[i] ? -1 : 1;
128 for (; i < a->length; i++)
129 if (a->string[i] != ' ')
131 for (; i < b->length; i++)
132 if (b->string[i] != ' ')
138 count_valid (double *d, size_t d_cnt)
144 for (i = 0; i < d_cnt; i++)
145 valid_cnt += is_valid (d[i]);
150 alloc_string (struct expression *e, size_t length)
154 s.string = pool_alloc (e->eval_pool, length);
159 copy_string (struct expression *e, const char *old, size_t length)
161 struct substring s = alloc_string (e, length);
162 memcpy (s.string, old, length);
166 /* Returns the noncentral beta cumulative distribution function
167 value for the given arguments.
169 FIXME: The accuracy of this function is not entirely
170 satisfactory. We only match the example values given in AS
171 310 to the first 5 significant digits. */
173 ncdf_beta (double x, double a, double b, double lambda)
177 if (x <= 0. || x >= 1. || a <= 0. || b <= 0. || lambda <= 0.)
183 /* Algorithm AS 226. */
184 double x0, a0, beta, temp, gx, q, ax, sumq, sum;
185 double err_max = 2 * DBL_EPSILON;
190 x0 = floor (c - 5.0 * sqrt (c));
194 beta = (gsl_sf_lngamma (a0)
196 - gsl_sf_lngamma (a0 + b));
197 temp = gsl_sf_beta_inc (a0, b, x);
198 gx = exp (a0 * log (x) + b * log (1. - x) - beta - log (a0));
200 q = exp (-c + x0 * log (c)) - gsl_sf_lngamma (x0 + 1.);
212 gx = x * (a + b + iter - 1.) * gx / (a + iter);
218 err_bound = (temp - gx) * sumq;
220 while (iter < iter_max && err_bound > err_max);
226 /* Algorithm AS 310. */
228 int iter, iter_lower, iter_upper, iter1, iter2, j;
229 double t, q, r, psum, beta, s1, gx, fx, temp, ftemp, t0, s0, sum, s;
231 double err_max = 2 * DBL_EPSILON;
237 iter_lower = m - 5. * m_sqrt;
238 iter_upper = m + 5. * m_sqrt;
240 t = -c + m * log (c) - gsl_sf_lngamma (m + 1.);
244 beta = (gsl_sf_lngamma (a + m)
246 - gsl_sf_lngamma (a + m + b));
247 s1 = (a + m) * log (x) + b * log (1. - x) - log (a + m) - beta;
249 ftemp = temp = gsl_sf_beta_inc (a + m, b, x);
254 while (iter1 >= iter_lower && q >= err_max)
258 gx = (a + iter1) / (x * (a + b + iter1 - 1.)) * gx;
265 t0 = (gsl_sf_lngamma (a + b)
266 - gsl_sf_lngamma (a + 1.)
267 - gsl_sf_lngamma (b));
268 s0 = a * log (x) + b * log (1. - x);
271 for (j = 0; j < iter1; j++)
274 s += exp (t0 + s0 + j * log (x));
275 t1 = log (a + b + j) - log (a + 1. + j) + t0;
279 err_bound = (1. - gsl_sf_gamma_inc_P (iter1, c)) * (temp + s);
286 double ebd = err_bound + (1. - psum) * temp;
287 if (ebd < err_max || iter >= iter_upper)
295 gx = x * (a + b + iter2 - 1.) / (a + iter2) * gx;
304 cdf_bvnor (double x0, double x1, double r)
306 double z = x0 * x0 - 2. * r * x0 * x1 + x1 * x1;
307 return exp (-z / (2. * (1 - r * r))) * (2. * M_PI * sqrt (1 - r * r));
311 idf_fdist (double P, double df1, double df2)
313 double temp = gslextras_cdf_beta_Pinv (P, df1 / 2, df2 / 2);
314 return temp * df2 / ((1. - temp) * df1);
318 * Mathlib : A C Library of Special Functions
319 * Copyright (C) 1998 Ross Ihaka
320 * Copyright (C) 2000 The R Development Core Team
322 * This program is free software; you can redistribute it and/or
324 * it under the terms of the GNU General Public License as
326 * the Free Software Foundation; either version 2 of the
328 * (at your option) any later version.
330 * This program is distributed in the hope that it will be
332 * but WITHOUT ANY WARRANTY; without even the implied warranty
334 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
335 * GNU General Public License for more details.
337 * You should have received a copy of the GNU General Public
339 * along with this program; if not, write to the Free Software
340 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
344 /* Returns the density of the noncentral beta distribution with
345 noncentrality parameter LAMBDA. */
347 npdf_beta (double x, double a, double b, double lambda)
349 if (lambda < 0. || a <= 0. || b <= 0.)
351 else if (lambda == 0.)
352 return gsl_ran_beta_pdf (x, a, b);
355 double max_error = 2 * DBL_EPSILON;
357 double term = gsl_ran_beta_pdf (x, a, b);
358 double lambda2 = 0.5 * lambda;
359 double weight = exp (-lambda2);
360 double sum = weight * term;
361 double psum = weight;
363 for (k = 1; k <= max_iter && 1 - psum < max_error; k++) {
364 weight *= lambda2 / k;
365 term *= x * (a + b) / a;
366 sum += weight * term;