1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2008, 2010 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include <gsl/gsl_roots.h>
20 #include <gsl/gsl_sf.h>
21 #include <libpspp/assertion.h>
22 #include <libpspp/pool.h>
25 const struct substring empty_string = {NULL, 0};
28 expr_ymd_to_ofs (double year, double month, double day)
36 if (y != year || m != month || d != day)
38 msg (SE, _("One of the arguments to a DATE function is not an integer. "
39 "The result will be system-missing."));
43 ofs = calendar_gregorian_to_offset (y, m, d, &error);
46 msg (SE, "%s", error);
53 expr_ymd_to_date (double year, double month, double day)
55 double ofs = expr_ymd_to_ofs (year, month, day);
56 return ofs != SYSMIS ? ofs * DAY_S : SYSMIS;
60 expr_wkyr_to_date (double week, double year)
66 msg (SE, _("The week argument to DATE.WKYR is not an integer. "
67 "The result will be system-missing."));
70 else if (w < 1 || w > 53)
72 msg (SE, _("The week argument to DATE.WKYR is outside the acceptable "
74 "The result will be system-missing."));
79 double yr_1_1 = expr_ymd_to_ofs (year, 1, 1);
81 return DAY_S * (yr_1_1 + WEEK_DAY * (w - 1));
88 expr_yrday_to_date (double year, double yday)
94 msg (SE, _("The day argument to DATE.YRDAY is not an integer. "
95 "The result will be system-missing."));
98 else if (yd < 1 || yd > 366)
100 msg (SE, _("The day argument to DATE.YRDAY is outside the acceptable "
101 "range of 1 to 366. "
102 "The result will be system-missing."));
107 double yr_1_1 = expr_ymd_to_ofs (year, 1, 1);
108 if (yr_1_1 != SYSMIS)
109 return DAY_S * (yr_1_1 + yd - 1.);
116 expr_yrmoda (double year, double month, double day)
118 if (year >= 0 && year <= 99)
120 else if (year != (int) year && year > 47516)
122 msg (SE, _("The year argument to YRMODA is greater than 47516. "
123 "The result will be system-missing."));
127 return expr_ymd_to_ofs (year, month, day);
143 /* Stores in *UNIT the unit whose name is NAME.
145 static enum date_unit
146 recognize_unit (struct substring name, enum date_unit *unit)
151 const struct substring name;
153 static const struct unit_name unit_names[] =
155 { DATE_YEARS, SS_LITERAL_INITIALIZER ("years") },
156 { DATE_QUARTERS, SS_LITERAL_INITIALIZER ("quarters") },
157 { DATE_MONTHS, SS_LITERAL_INITIALIZER ("months") },
158 { DATE_WEEKS, SS_LITERAL_INITIALIZER ("weeks") },
159 { DATE_DAYS, SS_LITERAL_INITIALIZER ("days") },
160 { DATE_HOURS, SS_LITERAL_INITIALIZER ("hours") },
161 { DATE_MINUTES, SS_LITERAL_INITIALIZER ("minutes") },
162 { DATE_SECONDS, SS_LITERAL_INITIALIZER ("seconds") },
164 const int unit_name_cnt = sizeof unit_names / sizeof *unit_names;
166 const struct unit_name *un;
168 for (un = unit_names; un < &unit_names[unit_name_cnt]; un++)
169 if (ss_equals_case (un->name, name))
175 /* TRANSLATORS: Don't translate the the actual unit names `weeks', `days' etc
176 They must remain in their original English. */
177 msg (SE, _("Unrecognized date unit `%.*s'. "
178 "Valid date units are `years', `quarters', `months', "
179 "`weeks', `days', `hours', `minutes', and `seconds'."),
180 (int) ss_length (name), ss_data (name));
184 /* Returns the number of whole years from DATE1 to DATE2,
185 where a year is defined as the same or later month, day, and
188 year_diff (double date1, double date2)
194 assert (date2 >= date1);
195 calendar_offset_to_gregorian (date1 / DAY_S, &y1, &m1, &d1, &yd1);
196 calendar_offset_to_gregorian (date2 / DAY_S, &y2, &m2, &d2, &yd2);
201 int yd1 = 32 * m1 + d1;
202 int yd2 = 32 * m2 + d2;
204 || (yd2 == yd1 && fmod (date2, DAY_S) < fmod (date1, DAY_S)))
210 /* Returns the number of whole months from DATE1 to DATE2,
211 where a month is defined as the same or later day and time of
214 month_diff (double date1, double date2)
220 assert (date2 >= date1);
221 calendar_offset_to_gregorian (date1 / DAY_S, &y1, &m1, &d1, &yd1);
222 calendar_offset_to_gregorian (date2 / DAY_S, &y2, &m2, &d2, &yd2);
224 diff = ((y2 * 12) + m2) - ((y1 * 12) + m1);
227 || (d2 == d1 && fmod (date2, DAY_S) < fmod (date1, DAY_S))))
232 /* Returns the number of whole quarter from DATE1 to DATE2,
233 where a quarter is defined as three months. */
235 quarter_diff (double date1, double date2)
237 return month_diff (date1, date2) / 3;
240 /* Returns the number of seconds in the given UNIT. */
242 date_unit_duration (enum date_unit unit)
266 /* Returns the span from DATE1 to DATE2 in terms of UNIT_NAME. */
268 expr_date_difference (double date1, double date2, struct substring unit_name)
272 if (!recognize_unit (unit_name, &unit))
278 return (date2 >= date1
279 ? year_diff (date1, date2)
280 : -year_diff (date2, date1));
283 return (date2 >= date1
284 ? quarter_diff (date1, date2)
285 : -quarter_diff (date2, date1));
288 return (date2 >= date1
289 ? month_diff (date1, date2)
290 : -month_diff (date2, date1));
297 return trunc ((date2 - date1) / date_unit_duration (unit));
303 /* How to deal with days out of range for a given month. */
306 SUM_ROLLOVER, /* Roll them over to the next month. */
307 SUM_CLOSEST /* Use the last day of the month. */
310 /* Stores in *METHOD the method whose name is NAME.
313 recognize_method (struct substring method_name, enum date_sum_method *method)
315 if (ss_equals_case (method_name, ss_cstr ("closest")))
317 *method = SUM_CLOSEST;
320 else if (ss_equals_case (method_name, ss_cstr ("rollover")))
322 *method = SUM_ROLLOVER;
327 msg (SE, _("Invalid DATESUM method. "
328 "Valid choices are `closest' and `rollover'."));
333 /* Returns DATE advanced by the given number of MONTHS, with
334 day-of-month overflow resolved using METHOD. */
336 add_months (double date, int months, enum date_sum_method method)
342 calendar_offset_to_gregorian (date / DAY_S, &y, &m, &d, &yd);
355 assert (m >= 1 && m <= 12);
357 if (method == SUM_CLOSEST && d > calendar_days_in_month (y, m))
358 d = calendar_days_in_month (y, m);
360 output = calendar_gregorian_to_offset (y, m, d, &error);
361 if (output != SYSMIS)
362 output = (output * DAY_S) + fmod (date, DAY_S);
365 msg (SE, "%s", error);
371 /* Returns DATE advanced by the given QUANTITY of units given in
372 UNIT_NAME, with day-of-month overflow resolved using
375 expr_date_sum (double date, double quantity, struct substring unit_name,
376 struct substring method_name)
379 enum date_sum_method method;
381 if (!recognize_unit (unit_name, &unit)
382 || !recognize_method (method_name, &method))
388 return add_months (date, trunc (quantity) * 12, method);
391 return add_months (date, trunc (quantity) * 3, method);
394 return add_months (date, trunc (quantity), method);
401 return date + quantity * date_unit_duration (unit);
408 compare_string_3way (const struct substring *a, const struct substring *b)
412 for (i = 0; i < a->length && i < b->length; i++)
413 if (a->string[i] != b->string[i])
414 return a->string[i] < b->string[i] ? -1 : 1;
415 for (; i < a->length; i++)
416 if (a->string[i] != ' ')
418 for (; i < b->length; i++)
419 if (b->string[i] != ' ')
425 count_valid (double *d, size_t d_cnt)
431 for (i = 0; i < d_cnt; i++)
432 valid_cnt += is_valid (d[i]);
437 alloc_string (struct expression *e, size_t length)
441 s.string = pool_alloc (e->eval_pool, length);
446 copy_string (struct expression *e, const char *old, size_t length)
448 struct substring s = alloc_string (e, length);
449 memcpy (s.string, old, length);
453 /* Returns the noncentral beta cumulative distribution function
454 value for the given arguments.
456 FIXME: The accuracy of this function is not entirely
457 satisfactory. We only match the example values given in AS
458 310 to the first 5 significant digits. */
460 ncdf_beta (double x, double a, double b, double lambda)
464 if (x <= 0. || x >= 1. || a <= 0. || b <= 0. || lambda <= 0.)
470 /* Algorithm AS 226. */
471 double x0, a0, beta, temp, gx, q, ax, sumq, sum;
472 double err_max = 2 * DBL_EPSILON;
477 x0 = floor (c - 5.0 * sqrt (c));
481 beta = (gsl_sf_lngamma (a0)
483 - gsl_sf_lngamma (a0 + b));
484 temp = gsl_sf_beta_inc (a0, b, x);
485 gx = exp (a0 * log (x) + b * log (1. - x) - beta - log (a0));
487 q = exp (-c + x0 * log (c)) - gsl_sf_lngamma (x0 + 1.);
499 gx = x * (a + b + iter - 1.) * gx / (a + iter);
505 err_bound = (temp - gx) * sumq;
507 while (iter < iter_max && err_bound > err_max);
513 /* Algorithm AS 310. */
515 int iter, iter_lower, iter_upper, iter1, iter2, j;
516 double t, q, r, psum, beta, s1, gx, fx, temp, ftemp, t0, s0, sum, s;
518 double err_max = 2 * DBL_EPSILON;
524 iter_lower = m - 5. * m_sqrt;
525 iter_upper = m + 5. * m_sqrt;
527 t = -c + m * log (c) - gsl_sf_lngamma (m + 1.);
531 beta = (gsl_sf_lngamma (a + m)
533 - gsl_sf_lngamma (a + m + b));
534 s1 = (a + m) * log (x) + b * log (1. - x) - log (a + m) - beta;
536 ftemp = temp = gsl_sf_beta_inc (a + m, b, x);
541 while (iter1 >= iter_lower && q >= err_max)
545 gx = (a + iter1) / (x * (a + b + iter1 - 1.)) * gx;
552 t0 = (gsl_sf_lngamma (a + b)
553 - gsl_sf_lngamma (a + 1.)
554 - gsl_sf_lngamma (b));
555 s0 = a * log (x) + b * log (1. - x);
558 for (j = 0; j < iter1; j++)
561 s += exp (t0 + s0 + j * log (x));
562 t1 = log (a + b + j) - log (a + 1. + j) + t0;
566 err_bound = (1. - gsl_sf_gamma_inc_P (iter1, c)) * (temp + s);
573 double ebd = err_bound + (1. - psum) * temp;
574 if (ebd < err_max || iter >= iter_upper)
582 gx = x * (a + b + iter2 - 1.) / (a + iter2) * gx;
591 cdf_bvnor (double x0, double x1, double r)
593 double z = pow2 (x0) - 2. * r * x0 * x1 + pow2 (x1);
594 return exp (-z / (2. * (1 - r * r))) * (2. * M_PI * sqrt (1 - r * r));
598 idf_fdist (double P, double df1, double df2)
600 double temp = gsl_cdf_beta_Pinv (P, df1 / 2, df2 / 2);
601 return temp * df2 / ((1. - temp) * df1);
605 * Mathlib : A C Library of Special Functions
606 * Copyright (C) 1998 Ross Ihaka
607 * Copyright (C) 2000 The R Development Core Team
609 * This program is free software; you can redistribute it and/or
611 * it under the terms of the GNU General Public License as
613 * the Free Software Foundation; either version 2 of the
615 * (at your option) any later version.
617 * This program is distributed in the hope that it will be
619 * but WITHOUT ANY WARRANTY; without even the implied warranty
621 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
622 * GNU General Public License for more details.
624 * You should have received a copy of the GNU General Public
626 * along with this program; if not, write to the Free Software
627 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
631 /* Returns the density of the noncentral beta distribution with
632 noncentrality parameter LAMBDA. */
634 npdf_beta (double x, double a, double b, double lambda)
636 if (lambda < 0. || a <= 0. || b <= 0.)
638 else if (lambda == 0.)
639 return gsl_ran_beta_pdf (x, a, b);
642 double max_error = 2 * DBL_EPSILON;
644 double term = gsl_ran_beta_pdf (x, a, b);
645 double lambda2 = 0.5 * lambda;
646 double weight = exp (-lambda2);
647 double sum = weight * term;
648 double psum = weight;
650 for (k = 1; k <= max_iter && 1 - psum < max_error; k++) {
651 weight *= lambda2 / k;
652 term *= x * (a + b) / a;
653 sum += weight * term;