3 #include <gsl/gsl_roots.h>
4 #include <gsl/gsl_sf.h>
5 #include <libpspp/assertion.h>
6 #include <libpspp/pool.h>
9 const struct substring empty_string = {NULL, 0};
12 expr_error (void *aux UNUSED, const char *format, ...)
17 m.category = MSG_SYNTAX;
18 m.severity = MSG_ERROR;
19 va_start (args, format);
20 m.text = xvasprintf (format, args);
27 expr_ymd_to_ofs (double year, double month, double day)
33 if (y != year || m != month || d != day)
35 msg (SE, _("One of the arguments to a DATE function is not an integer. "
36 "The result will be system-missing."));
40 return calendar_gregorian_to_offset (y, m, d, expr_error, NULL);
44 expr_ymd_to_date (double year, double month, double day)
46 double ofs = expr_ymd_to_ofs (year, month, day);
47 return ofs != SYSMIS ? ofs * DAY_S : SYSMIS;
51 expr_wkyr_to_date (double week, double year)
57 msg (SE, _("The week argument to DATE.WKYR is not an integer. "
58 "The result will be system-missing."));
61 else if (w < 1 || w > 53)
63 msg (SE, _("The week argument to DATE.WKYR is outside the acceptable "
65 "The result will be system-missing."));
70 double yr_1_1 = expr_ymd_to_ofs (year, 1, 1);
72 return DAY_S * (yr_1_1 + WEEK_DAY * (w - 1));
79 expr_yrday_to_date (double year, double yday)
85 msg (SE, _("The day argument to DATE.YRDAY is not an integer. "
86 "The result will be system-missing."));
89 else if (yd < 1 || yd > 366)
91 msg (SE, _("The day argument to DATE.YRDAY is outside the acceptable "
93 "The result will be system-missing."));
98 double yr_1_1 = expr_ymd_to_ofs (year, 1, 1);
100 return DAY_S * (yr_1_1 + yd - 1.);
107 expr_yrmoda (double year, double month, double day)
109 if (year >= 0 && year <= 99)
111 else if (year != (int) year && year > 47516)
113 msg (SE, _("The year argument to YRMODA is greater than 47516. "
114 "The result will be system-missing."));
118 return expr_ymd_to_ofs (year, month, day);
134 /* Stores in *UNIT the unit whose name is NAME.
136 static enum date_unit
137 recognize_unit (struct substring name, enum date_unit *unit)
142 const struct substring name;
144 static const struct unit_name unit_names[] =
146 { DATE_YEARS, SS_LITERAL_INITIALIZER ("years") },
147 { DATE_QUARTERS, SS_LITERAL_INITIALIZER ("quarters") },
148 { DATE_MONTHS, SS_LITERAL_INITIALIZER ("months") },
149 { DATE_WEEKS, SS_LITERAL_INITIALIZER ("weeks") },
150 { DATE_DAYS, SS_LITERAL_INITIALIZER ("days") },
151 { DATE_HOURS, SS_LITERAL_INITIALIZER ("hours") },
152 { DATE_MINUTES, SS_LITERAL_INITIALIZER ("minutes") },
153 { DATE_SECONDS, SS_LITERAL_INITIALIZER ("seconds") },
155 const int unit_name_cnt = sizeof unit_names / sizeof *unit_names;
157 const struct unit_name *un;
159 for (un = unit_names; un < &unit_names[unit_name_cnt]; un++)
160 if (ss_equals_case (un->name, name))
166 msg (SE, _("Unrecognized date unit \"%.*s\". "
167 "Valid date units are \"years\", \"quarters\", \"months\", "
168 "\"weeks\", \"days\", \"hours\", \"minutes\", and \"seconds\"."),
169 (int) ss_length (name), ss_data (name));
173 /* Returns the number of whole years from DATE1 to DATE2,
174 where a year is defined as the same or later month, day, and
177 year_diff (double date1, double date2)
183 assert (date2 >= date1);
184 calendar_offset_to_gregorian (date1 / DAY_S, &y1, &m1, &d1, &yd1);
185 calendar_offset_to_gregorian (date2 / DAY_S, &y2, &m2, &d2, &yd2);
190 int yd1 = 32 * m1 + d1;
191 int yd2 = 32 * m2 + d2;
193 || (yd2 == yd1 && fmod (date2, DAY_S) < fmod (date1, DAY_S)))
199 /* Returns the number of whole months from DATE1 to DATE2,
200 where a month is defined as the same or later day and time of
203 month_diff (double date1, double date2)
209 assert (date2 >= date1);
210 calendar_offset_to_gregorian (date1 / DAY_S, &y1, &m1, &d1, &yd1);
211 calendar_offset_to_gregorian (date2 / DAY_S, &y2, &m2, &d2, &yd2);
213 diff = ((y2 * 12) + m2) - ((y1 * 12) + m1);
216 || (d2 == d1 && fmod (date2, DAY_S) < fmod (date1, DAY_S))))
221 /* Returns the number of whole quarter from DATE1 to DATE2,
222 where a quarter is defined as three months. */
224 quarter_diff (double date1, double date2)
226 return month_diff (date1, date2) / 3;
229 /* Returns the number of seconds in the given UNIT. */
231 date_unit_duration (enum date_unit unit)
255 /* Returns the span from DATE1 to DATE2 in terms of UNIT_NAME. */
257 expr_date_difference (double date1, double date2, struct substring unit_name)
261 if (!recognize_unit (unit_name, &unit))
267 return (date2 >= date1
268 ? year_diff (date1, date2)
269 : -year_diff (date2, date1));
272 return (date2 >= date1
273 ? quarter_diff (date1, date2)
274 : -quarter_diff (date2, date1));
277 return (date2 >= date1
278 ? month_diff (date1, date2)
279 : -month_diff (date2, date1));
286 return trunc ((date2 - date1) / date_unit_duration (unit));
292 /* How to deal with days out of range for a given month. */
295 SUM_ROLLOVER, /* Roll them over to the next month. */
296 SUM_CLOSEST /* Use the last day of the month. */
299 /* Stores in *METHOD the method whose name is NAME.
302 recognize_method (struct substring method_name, enum date_sum_method *method)
304 if (ss_equals_case (method_name, ss_cstr ("closest")))
306 *method = SUM_CLOSEST;
309 else if (ss_equals_case (method_name, ss_cstr ("rollover")))
311 *method = SUM_ROLLOVER;
316 msg (SE, _("Invalid DATESUM method. "
317 "Valid choices are \"closest\" and \"rollover\"."));
322 /* Returns DATE advanced by the given number of MONTHS, with
323 day-of-month overflow resolved using METHOD. */
325 add_months (double date, int months, enum date_sum_method method)
330 calendar_offset_to_gregorian (date / DAY_S, &y, &m, &d, &yd);
343 assert (m >= 1 && m <= 12);
345 if (method == SUM_CLOSEST && d > calendar_days_in_month (y, m))
346 d = calendar_days_in_month (y, m);
348 output = calendar_gregorian_to_offset (y, m, d, expr_error, NULL);
349 if (output != SYSMIS)
350 output = (output * DAY_S) + fmod (date, DAY_S);
354 /* Returns DATE advanced by the given QUANTITY of units given in
355 UNIT_NAME, with day-of-month overflow resolved using
358 expr_date_sum (double date, double quantity, struct substring unit_name,
359 struct substring method_name)
362 enum date_sum_method method;
364 if (!recognize_unit (unit_name, &unit)
365 || !recognize_method (method_name, &method))
371 return add_months (date, trunc (quantity) * 12, method);
374 return add_months (date, trunc (quantity) * 3, method);
377 return add_months (date, trunc (quantity), method);
384 return date + quantity * date_unit_duration (unit);
391 compare_string (const struct substring *a, const struct substring *b)
395 for (i = 0; i < a->length && i < b->length; i++)
396 if (a->string[i] != b->string[i])
397 return a->string[i] < b->string[i] ? -1 : 1;
398 for (; i < a->length; i++)
399 if (a->string[i] != ' ')
401 for (; i < b->length; i++)
402 if (b->string[i] != ' ')
408 count_valid (double *d, size_t d_cnt)
414 for (i = 0; i < d_cnt; i++)
415 valid_cnt += is_valid (d[i]);
420 alloc_string (struct expression *e, size_t length)
424 s.string = pool_alloc (e->eval_pool, length);
429 copy_string (struct expression *e, const char *old, size_t length)
431 struct substring s = alloc_string (e, length);
432 memcpy (s.string, old, length);
436 /* Returns the noncentral beta cumulative distribution function
437 value for the given arguments.
439 FIXME: The accuracy of this function is not entirely
440 satisfactory. We only match the example values given in AS
441 310 to the first 5 significant digits. */
443 ncdf_beta (double x, double a, double b, double lambda)
447 if (x <= 0. || x >= 1. || a <= 0. || b <= 0. || lambda <= 0.)
453 /* Algorithm AS 226. */
454 double x0, a0, beta, temp, gx, q, ax, sumq, sum;
455 double err_max = 2 * DBL_EPSILON;
460 x0 = floor (c - 5.0 * sqrt (c));
464 beta = (gsl_sf_lngamma (a0)
466 - gsl_sf_lngamma (a0 + b));
467 temp = gsl_sf_beta_inc (a0, b, x);
468 gx = exp (a0 * log (x) + b * log (1. - x) - beta - log (a0));
470 q = exp (-c + x0 * log (c)) - gsl_sf_lngamma (x0 + 1.);
482 gx = x * (a + b + iter - 1.) * gx / (a + iter);
488 err_bound = (temp - gx) * sumq;
490 while (iter < iter_max && err_bound > err_max);
496 /* Algorithm AS 310. */
498 int iter, iter_lower, iter_upper, iter1, iter2, j;
499 double t, q, r, psum, beta, s1, gx, fx, temp, ftemp, t0, s0, sum, s;
501 double err_max = 2 * DBL_EPSILON;
507 iter_lower = m - 5. * m_sqrt;
508 iter_upper = m + 5. * m_sqrt;
510 t = -c + m * log (c) - gsl_sf_lngamma (m + 1.);
514 beta = (gsl_sf_lngamma (a + m)
516 - gsl_sf_lngamma (a + m + b));
517 s1 = (a + m) * log (x) + b * log (1. - x) - log (a + m) - beta;
519 ftemp = temp = gsl_sf_beta_inc (a + m, b, x);
524 while (iter1 >= iter_lower && q >= err_max)
528 gx = (a + iter1) / (x * (a + b + iter1 - 1.)) * gx;
535 t0 = (gsl_sf_lngamma (a + b)
536 - gsl_sf_lngamma (a + 1.)
537 - gsl_sf_lngamma (b));
538 s0 = a * log (x) + b * log (1. - x);
541 for (j = 0; j < iter1; j++)
544 s += exp (t0 + s0 + j * log (x));
545 t1 = log (a + b + j) - log (a + 1. + j) + t0;
549 err_bound = (1. - gsl_sf_gamma_inc_P (iter1, c)) * (temp + s);
556 double ebd = err_bound + (1. - psum) * temp;
557 if (ebd < err_max || iter >= iter_upper)
565 gx = x * (a + b + iter2 - 1.) / (a + iter2) * gx;
574 cdf_bvnor (double x0, double x1, double r)
576 double z = x0 * x0 - 2. * r * x0 * x1 + x1 * x1;
577 return exp (-z / (2. * (1 - r * r))) * (2. * M_PI * sqrt (1 - r * r));
581 idf_fdist (double P, double df1, double df2)
583 double temp = gslextras_cdf_beta_Pinv (P, df1 / 2, df2 / 2);
584 return temp * df2 / ((1. - temp) * df1);
588 * Mathlib : A C Library of Special Functions
589 * Copyright (C) 1998 Ross Ihaka
590 * Copyright (C) 2000 The R Development Core Team
592 * This program is free software; you can redistribute it and/or
594 * it under the terms of the GNU General Public License as
596 * the Free Software Foundation; either version 2 of the
598 * (at your option) any later version.
600 * This program is distributed in the hope that it will be
602 * but WITHOUT ANY WARRANTY; without even the implied warranty
604 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
605 * GNU General Public License for more details.
607 * You should have received a copy of the GNU General Public
609 * along with this program; if not, write to the Free Software
610 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
614 /* Returns the density of the noncentral beta distribution with
615 noncentrality parameter LAMBDA. */
617 npdf_beta (double x, double a, double b, double lambda)
619 if (lambda < 0. || a <= 0. || b <= 0.)
621 else if (lambda == 0.)
622 return gsl_ran_beta_pdf (x, a, b);
625 double max_error = 2 * DBL_EPSILON;
627 double term = gsl_ran_beta_pdf (x, a, b);
628 double lambda2 = 0.5 * lambda;
629 double weight = exp (-lambda2);
630 double sum = weight * term;
631 double psum = weight;
633 for (k = 1; k <= max_iter && 1 - psum < max_error; k++) {
634 weight *= lambda2 / k;
635 term *= x * (a + b) / a;
636 sum += weight * term;