3 // PSPP - a program for statistical analysis.
4 // Copyright (C) 2005, 2006, 2009, 2010 Free Software Foundation, Inc.
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 operator NEG (x) = -x;
21 operator ADD (a, b) = a + b;
22 operator SUB (a, b) = a - b;
24 absorb_miss operator MUL (a, b)
25 = (a == 0. || b == 0. ? 0.
26 : a == SYSMIS || b == SYSMIS ? SYSMIS
29 absorb_miss operator DIV (a, b)
31 : a == SYSMIS || b == SYSMIS ? SYSMIS
34 absorb_miss operator POW (a, b)
35 = (a == SYSMIS ? (b == 0. ? 1. : a)
36 : b == SYSMIS ? (a == 0. ? 0. : SYSMIS)
37 : a == 0. && b <= 0. ? SYSMIS
40 absorb_miss boolean operator AND (boolean a, boolean b)
43 : b == SYSMIS ? SYSMIS
46 absorb_miss boolean operator OR (boolean a, boolean b)
49 : b == SYSMIS ? SYSMIS
52 boolean operator NOT (boolean a)
57 // Numeric relational operators.
58 boolean operator EQ (a, b) = a == b;
59 boolean operator GE (a, b) = a >= b;
60 boolean operator GT (a, b) = a > b;
61 boolean operator LE (a, b) = a <= b;
62 boolean operator LT (a, b) = a < b;
63 boolean operator NE (a, b) = a != b;
65 // String relational operators.
66 boolean operator EQ_STRING (string a, string b) = compare_string (&a, &b) == 0;
67 boolean operator GE_STRING (string a, string b) = compare_string (&a, &b) >= 0;
68 boolean operator GT_STRING (string a, string b) = compare_string (&a, &b) > 0;
69 boolean operator LE_STRING (string a, string b) = compare_string (&a, &b) <= 0;
70 boolean operator LT_STRING (string a, string b) = compare_string (&a, &b) < 0;
71 boolean operator NE_STRING (string a, string b) = compare_string (&a, &b) != 0;
74 function ABS (x) = fabs (x);
75 extension function ACOS (x >= -1 && x <= 1) = acos (x);
76 function ASIN (x >= -1 && x <= 1) = asin (x);
77 function ATAN (x) = atan (x);
78 extension function ARCOS (x >= -1 && x <= 1) = acos (x);
79 function ARSIN (x >= -1 && x <= 1) = asin (x);
80 function ARTAN (x) = atan (x);
81 function COS (x) = cos (x);
82 function EXP (x) = check_errno (exp (x));
83 function LG10(x) = check_errno (log10 (x));
84 function LN (x) = check_errno (log (x));
85 function LNGAMMA (x >= 0) = gsl_sf_lngamma (x);
86 function MOD10 (x) = fmod (x, 10);
87 function RND (x) = x >= 0. ? floor (x + .5) : -floor (-x + .5);
88 function SIN (x) = sin (x);
89 function SQRT (x >= 0) = sqrt (x);
90 function TAN (x) = check_errno (tan (x));
91 function TRUNC (x) = x >= 0. ? floor (x) : -floor (-x);
93 absorb_miss function MOD (n, d)
96 return n != SYSMIS ? fmod (n, d) : SYSMIS;
98 return n != 0. ? SYSMIS : 0.;
101 // N-ary numeric functions.
102 absorb_miss boolean function ANY (x != SYSMIS, a[n])
107 for (i = 0; i < n; i++)
110 else if (a[i] == SYSMIS)
113 return sysmis ? SYSMIS : 0.;
116 boolean function ANY (string x, string a[n])
120 for (i = 0; i < n; i++)
121 if (!compare_string (&x, &a[i]))
126 function CFVAR.2 (a[n])
128 double mean, variance;
130 moments_of_doubles (a, n, NULL, &mean, &variance, NULL, NULL);
132 if (mean == SYSMIS || mean == 0 || variance == SYSMIS)
135 return sqrt (variance) / mean;
138 function MAX.1 (a[n])
144 for (i = 0; i < n; i++)
145 if (a[i] != SYSMIS && a[i] > max)
150 string function MAX (string a[n])
152 struct substring *max;
156 for (i = 1; i < n; i++)
157 if (compare_string (&a[i], max) > 0)
162 function MEAN.1 (a[n])
165 moments_of_doubles (a, n, NULL, &mean, NULL, NULL, NULL);
169 function MIN.1 (a[n])
175 for (i = 0; i < n; i++)
176 if (a[i] != SYSMIS && a[i] < min)
181 string function MIN (string a[n])
183 struct substring *min;
187 for (i = 1; i < n; i++)
188 if (compare_string (&a[i], min) < 0)
193 absorb_miss function NMISS (a[n])
196 size_t missing_cnt = 0;
198 for (i = 0; i < n; i++)
199 missing_cnt += a[i] == SYSMIS;
203 absorb_miss function NVALID (a[n])
206 size_t valid_cnt = 0;
208 for (i = 0; i < n; i++)
209 valid_cnt += a[i] != SYSMIS;
213 absorb_miss boolean function RANGE (x != SYSMIS, a[n*2])
218 for (i = 0; i < n; i++)
221 double y = a[2 * i + 1];
222 if (w != SYSMIS && y != SYSMIS)
224 if (w <= x && x <= y)
230 return sysmis ? SYSMIS : 0.;
233 boolean function RANGE (string x, string a[n*2])
237 for (i = 0; i < n; i++)
239 struct substring *w = &a[2 * i];
240 struct substring *y = &a[2 * i + 1];
241 if (compare_string (w, &x) <= 0 && compare_string (&x, y) <= 0)
250 moments_of_doubles (a, n, NULL, NULL, &variance, NULL, NULL);
251 return sqrt (variance);
254 function SUM.1 (a[n])
260 for (i = 0; i < n; i++)
266 function VARIANCE.2 (a[n])
269 moments_of_doubles (a, n, NULL, NULL, &variance, NULL, NULL);
273 // Time construction & extraction functions.
274 function TIME.HMS (h, m, s)
276 if ((h > 0. || m > 0. || s > 0.) && (h < 0. || m < 0. || s < 0.))
278 msg (SW, _("TIME.HMS cannot mix positive and negative arguments."));
282 return H_S * h + MIN_S * m + s;
284 function TIME.DAYS (days) = days * DAY_S;
285 function CTIME.DAYS (time) = time / DAY_S;
286 function CTIME.HOURS (time) = time / H_S;
287 function CTIME.MINUTES (time) = time / MIN_S;
288 function CTIME.SECONDS (time) = time;
290 // Date construction functions.
291 function DATE.DMY (d, m, y) = expr_ymd_to_date (y, m, d);
292 function DATE.MDY (m, d, y) = expr_ymd_to_date (y, m, d);
293 function DATE.MOYR (m, y) = expr_ymd_to_date (y, m, 1);
294 function DATE.QYR (q, y)
296 if (q < 1.0 || q > 4.0 || q != (int) q)
298 msg (SW, _("The first argument to DATE.QYR must be 1, 2, 3, or 4."));
301 return expr_ymd_to_date (y, q * 3 - 2, 1);
303 function DATE.WKYR (w, y) = expr_wkyr_to_date (w, y);
304 function DATE.YRDAY (y, yday) = expr_yrday_to_date (y, yday);
305 function YRMODA (y, m, d) = expr_yrmoda (y, m, d);
307 // Date extraction functions.
308 function XDATE.TDAY (date) = floor (date / DAY_S);
309 function XDATE.HOUR (date) = fmod (floor (date / H_S), DAY_H);
310 function XDATE.MINUTE (date) = fmod (floor (date / H_MIN), H_MIN);
311 function XDATE.SECOND (date) = fmod (date, MIN_S);
312 function XDATE.DATE (date) = floor (date / DAY_S) * DAY_S;
313 function XDATE.TIME (date) = fmod (date, DAY_S);
315 function XDATE.JDAY (date >= DAY_S) = calendar_offset_to_yday (date / DAY_S);
316 function XDATE.MDAY (date >= DAY_S) = calendar_offset_to_mday (date / DAY_S);
317 function XDATE.MONTH (date >= DAY_S)
318 = calendar_offset_to_month (date / DAY_S);
319 function XDATE.QUARTER (date >= DAY_S)
320 = (calendar_offset_to_month (date / DAY_S) - 1) / 3 + 1;
321 function XDATE.WEEK (date >= DAY_S)
322 = (calendar_offset_to_yday (date / DAY_S) - 1) / 7 + 1;
323 function XDATE.WKDAY (date >= DAY_S) = calendar_offset_to_wday (date / DAY_S);
324 function XDATE.YEAR (date >= DAY_S) = calendar_offset_to_year (date / DAY_S);
326 // Date arithmetic functions.
327 no_abbrev function DATEDIFF (date2 >= DAY_S, date1 >= DAY_S, string unit)
328 = expr_date_difference (date1, date2, unit);
329 no_abbrev function DATESUM (date, quantity, string unit)
330 = expr_date_sum (date, quantity, unit, ss_cstr ("closest"));
331 no_abbrev function DATESUM (date, quantity, string unit, string method)
332 = expr_date_sum (date, quantity, unit, method);
336 string function CONCAT (string a[n])
339 struct substring dst;
342 dst = alloc_string (e, MAX_STRING);
344 for (i = 0; i < n; i++)
346 struct substring *src = &a[i];
349 copy_len = src->length;
350 if (dst.length + copy_len > MAX_STRING)
351 copy_len = MAX_STRING - dst.length;
352 memcpy (&dst.string[dst.length], src->string, copy_len);
353 dst.length += copy_len;
359 function INDEX (string haystack, string needle)
361 if (needle.length == 0)
365 int limit = haystack.length - needle.length + 1;
367 for (i = 1; i <= limit; i++)
368 if (!memcmp (&haystack.string[i - 1], needle.string, needle.length))
374 function INDEX (string haystack, string needles, needle_len_d)
376 if (needle_len_d <= INT_MIN || needle_len_d >= INT_MAX
377 || (int) needle_len_d != needle_len_d
378 || needles.length == 0)
382 int needle_len = needle_len_d;
383 if (needle_len < 0 || needle_len > needles.length
384 || needles.length % needle_len != 0)
388 int limit = haystack.length - needle_len + 1;
390 for (i = 1; i <= limit; i++)
391 for (j = 0; j < needles.length; j += needle_len)
392 if (!memcmp (&haystack.string[i - 1], &needles.string[j],
401 function RINDEX (string haystack, string needle)
403 if (needle.length == 0)
407 int limit = haystack.length - needle.length + 1;
409 for (i = limit; i >= 1; i--)
410 if (!memcmp (&haystack.string[i - 1], needle.string, needle.length))
416 function RINDEX (string haystack, string needles, needle_len_d)
418 if (needle_len_d <= INT_MIN || needle_len_d >= INT_MAX
419 || (int) needle_len_d != needle_len_d
420 || needles.length == 0)
424 int needle_len = needle_len_d;
425 if (needle_len < 0 || needle_len > needles.length
426 || needles.length % needle_len != 0)
430 int limit = haystack.length - needle_len + 1;
432 for (i = limit; i >= 1; i--)
433 for (j = 0; j < needles.length; j += needle_len)
434 if (!memcmp (&haystack.string[i - 1],
435 &needles.string[j], needle_len))
442 function LENGTH (string s)
447 string function LOWER (string s)
451 for (i = 0; i < s.length; i++)
452 s.string[i] = tolower ((unsigned char) s.string[i]);
456 function MBLEN.BYTE (string s, idx)
458 if (idx < 0 || idx >= s.length || (int) idx != idx)
464 string function UPCASE (string s)
468 for (i = 0; i < s.length; i++)
469 s.string[i] = toupper ((unsigned char) s.string[i]);
473 absorb_miss string function LPAD (string s, n)
476 if (n < 0 || n > MAX_STRING || (int) n != n)
478 else if (s.length >= n)
482 struct substring t = alloc_string (e, n);
483 memset (t.string, ' ', n - s.length);
484 memcpy (&t.string[(int) n - s.length], s.string, s.length);
489 absorb_miss string function LPAD (string s, n, string c)
492 if (n < 0 || n > MAX_STRING || (int) n != n || c.length != 1)
494 else if (s.length >= n)
498 struct substring t = alloc_string (e, n);
499 memset (t.string, c.string[0], n - s.length);
500 memcpy (&t.string[(int) n - s.length], s.string, s.length);
505 absorb_miss string function RPAD (string s, n)
508 if (n < 0 || n > MAX_STRING || (int) n != n)
510 else if (s.length >= n)
514 struct substring t = alloc_string (e, n);
515 memcpy (t.string, s.string, s.length);
516 memset (&t.string[s.length], ' ', n - s.length);
521 absorb_miss string function RPAD (string s, n, string c)
524 if (n < 0 || n > MAX_STRING || (int) n != n || c.length != 1)
526 else if (s.length >= n)
530 struct substring t = alloc_string (e, n);
531 memcpy (t.string, s.string, s.length);
532 memset (&t.string[s.length], c.string[0], n - s.length);
537 string function LTRIM (string s)
539 while (s.length > 0 && s.string[0] == ' ')
547 string function LTRIM (string s, string c)
551 while (s.length > 0 && s.string[0] == c.string[0])
562 string function RTRIM (string s)
564 while (s.length > 0 && s.string[s.length - 1] == ' ')
569 string function RTRIM (string s, string c)
573 while (s.length > 0 && s.string[s.length - 1] == c.string[0])
581 function NUMBER (string s, ni_format f)
584 data_in (ss_head (s, f->w), LEGACY_NATIVE, f->type, f->d, 0, 0, NULL, &out, 0);
588 absorb_miss string function STRING (x, no_format f)
592 struct substring dst;
597 assert (!fmt_is_string (f->type));
598 s = data_out (&v, LEGACY_NATIVE, f);
599 dst = alloc_string (e, strlen (s));
600 strcpy (dst.string, s);
605 absorb_miss string function SUBSTR (string s, ofs)
608 if (ofs >= 1 && ofs <= s.length && (int) ofs == ofs)
609 return copy_string (e, &s.string[(int) ofs - 1], s.length - ofs + 1);
614 absorb_miss string function SUBSTR (string s, ofs, cnt)
617 if (ofs >= 1 && ofs <= s.length && (int) ofs == ofs
618 && cnt >= 1 && cnt <= INT_MAX && (int) cnt == cnt)
620 int cnt_max = s.length - (int) ofs + 1;
621 return copy_string (e, &s.string[(int) ofs - 1],
622 cnt <= cnt_max ? cnt : cnt_max);
628 absorb_miss no_opt no_abbrev string function VALUELABEL (var v)
632 const char *label = var_lookup_value_label (v, case_data (c, v));
634 return copy_string (e, label, strlen (label));
640 operator SQUARE (x) = x * x;
641 boolean operator NUM_TO_BOOLEAN (x, string op_name)
643 if (x == 0. || x == 1. || x == SYSMIS)
647 msg (SE, _("An operand of the %.*s operator was found to have a value "
648 "other than 0 (false), 1 (true), or the system-missing "
649 "value. The result was forced to 0."),
650 (int) op_name.length, op_name.string);
655 operator BOOLEAN_TO_NUM (boolean x) = x;
657 // Beta distribution.
658 function PDF.BETA (x >= 0 && x <= 1, a > 0, b > 0)
659 = gsl_ran_beta_pdf (x, a, b);
660 function CDF.BETA (x >= 0 && x <= 1, a > 0, b > 0) = gsl_cdf_beta_P (x, a, b);
661 function IDF.BETA (P >= 0 && P <= 1, a > 0, b > 0)
662 = gsl_cdf_beta_Pinv (P, a, b);
663 no_opt function RV.BETA (a > 0, b > 0) = gsl_ran_beta (get_rng (), a, b);
664 function NCDF.BETA (x >= 0, a > 0, b > 0, lambda > 0)
665 = ncdf_beta (x, a, b, lambda);
666 function NPDF.BETA (x >= 0, a > 0, b > 0, lambda > 0)
667 = npdf_beta (x, a, b, lambda);
669 // Bivariate normal distribution.
670 function CDF.BVNOR (x0, x1, r >= -1 && r <= 1) = cdf_bvnor (x0, x1, r);
671 function PDF.BVNOR (x0, x1, r >= -1 && r <= 1)
672 = gsl_ran_bivariate_gaussian_pdf (x0, x1, 1, 1, r);
674 // Cauchy distribution.
675 function CDF.CAUCHY (x, a, b > 0) = gsl_cdf_cauchy_P ((x - a) / b, 1);
676 function IDF.CAUCHY (P > 0 && P < 1, a, b > 0)
677 = a + b * gsl_cdf_cauchy_Pinv (P, 1);
678 function PDF.CAUCHY (x, a, b > 0) = gsl_ran_cauchy_pdf ((x - a) / b, 1) / b;
679 no_opt function RV.CAUCHY (a, b > 0) = a + b * gsl_ran_cauchy (get_rng (), 1);
681 // Chi-square distribution.
682 function CDF.CHISQ (x >= 0, df > 0) = gsl_cdf_chisq_P (x, df);
683 function IDF.CHISQ (P >= 0 && P < 1, df > 0) = gsl_cdf_chisq_Pinv (P, df);
684 function PDF.CHISQ (x >= 0, df > 0) = gsl_ran_chisq_pdf (x, df);
685 no_opt function RV.CHISQ (df > 0) = gsl_ran_chisq (get_rng (), df);
686 function NCDF.CHISQ (x >= 0, df > 0, c) = unimplemented;
687 function NPDF.CHISQ (x >= 0, df > 0, c) = unimplemented;
688 function SIG.CHISQ (x >= 0, df > 0) = gsl_cdf_chisq_Q (x, df);
690 // Exponential distribution.
691 function CDF.EXP (x >= 0, a > 0) = gsl_cdf_exponential_P (x, 1. / a);
692 function IDF.EXP (P >= 0 && P < 1, a > 0)
693 = gsl_cdf_exponential_Pinv (P, 1. / a);
694 function PDF.EXP (x >= 0, a > 0) = gsl_ran_exponential_pdf (x, 1. / a);
695 no_opt function RV.EXP (a > 0) = gsl_ran_exponential (get_rng (), 1. / a);
697 // Exponential power distribution.
698 extension function PDF.XPOWER (x, a > 0, b >= 0)
699 = gsl_ran_exppow_pdf (x, a, b);
700 no_opt extension function RV.XPOWER (a > 0, b >= 0)
701 = gsl_ran_exppow (get_rng (), a, b);
704 function CDF.F (x >= 0, df1 > 0, df2 > 0) = gsl_cdf_fdist_P (x, df1, df2);
705 function IDF.F (P >= 0 && P < 1, df1 > 0, df2 > 0) = idf_fdist (P, df1, df2);
706 function PDF.F (x >= 0, df1 > 0, df2 > 0) = gsl_ran_fdist_pdf (x, df1, df2);
707 no_opt function RV.F (df1 > 0, df2 > 0) = gsl_ran_fdist (get_rng (), df1, df2);
708 function NCDF.F (x >= 0, df1 > 0, df2 > 0, lambda >= 0) = unimplemented;
709 function NPDF.F (x >= 0, df1 > 0, df2 > 0, lmabda >= 0) = unimplemented;
710 function SIG.F (x >= 0, df1 > 0, df2 > 0) = gsl_cdf_fdist_Q (x, df1, df2);
712 // Gamma distribution.
713 function CDF.GAMMA (x >= 0, a > 0, b > 0) = gsl_cdf_gamma_P (x, a, 1. / b);
714 function IDF.GAMMA (P >= 0 && P <= 1, a > 0, b > 0)
715 = gsl_cdf_gamma_Pinv (P, a, 1. / b);
716 function PDF.GAMMA (x >= 0, a > 0, b > 0) = gsl_ran_gamma_pdf (x, a, 1. / b);
717 no_opt function RV.GAMMA (a > 0, b > 0)
718 = gsl_ran_gamma (get_rng (), a, 1. / b);
720 // Half-normal distribution.
721 function CDF.HALFNRM (x, a, b > 0) = unimplemented;
722 function IDF.HALFNRM (P > 0 && P < 1, a, b > 0) = unimplemented;
723 function PDF.HALFNRM (x, a, b > 0) = unimplemented;
724 no_opt function RV.HALFNRM (a, b > 0) = unimplemented;
726 // Inverse Gaussian distribution.
727 function CDF.IGAUSS (x > 0, a > 0, b > 0) = unimplemented;
728 function IDF.IGAUSS (P >= 0 && P < 1, a > 0, b > 0) = unimplemented;
729 function PDF.IGAUSS (x > 0, a > 0, b > 0) = unimplemented;
730 no_opt function RV.IGAUSS (a > 0, b > 0) = unimplemented;
732 // Landau distribution.
733 extension function PDF.LANDAU (x) = gsl_ran_landau_pdf (x);
734 no_opt extension function RV.LANDAU () = gsl_ran_landau (get_rng ());
736 // Laplace distribution.
737 function CDF.LAPLACE (x, a, b > 0) = gsl_cdf_laplace_P ((x - a) / b, 1);
738 function IDF.LAPLACE (P > 0 && P < 1, a, b > 0)
739 = a + b * gsl_cdf_laplace_Pinv (P, 1);
740 function PDF.LAPLACE (x, a, b > 0) = gsl_ran_laplace_pdf ((x - a) / b, 1) / b;
741 no_opt function RV.LAPLACE (a, b > 0)
742 = a + b * gsl_ran_laplace (get_rng (), 1);
744 // Levy alpha-stable distribution.
745 no_opt extension function RV.LEVY (c, alpha > 0 && alpha <= 2)
746 = gsl_ran_levy (get_rng (), c, alpha);
748 // Levy skew alpha-stable distribution.
749 no_opt extension function RV.LVSKEW (c, alpha > 0 && alpha <= 2,
750 beta >= -1 && beta <= 1)
751 = gsl_ran_levy_skew (get_rng (), c, alpha, beta);
753 // Logistic distribution.
754 function CDF.LOGISTIC (x, a, b > 0) = gsl_cdf_logistic_P ((x - a) / b, 1);
755 function IDF.LOGISTIC (P > 0 && P < 1, a, b > 0)
756 = a + b * gsl_cdf_logistic_Pinv (P, 1);
757 function PDF.LOGISTIC (x, a, b > 0)
758 = gsl_ran_logistic_pdf ((x - a) / b, 1) / b;
759 no_opt function RV.LOGISTIC (a, b > 0)
760 = a + b * gsl_ran_logistic (get_rng (), 1);
762 // Lognormal distribution.
763 function CDF.LNORMAL (x >= 0, m > 0, s > 0)
764 = gsl_cdf_lognormal_P (x, log (m), s);
765 function IDF.LNORMAL (P >= 0 && P < 1, m > 0, s > 0)
766 = gsl_cdf_lognormal_Pinv (P, log (m), s);
767 function PDF.LNORMAL (x >= 0, m > 0, s > 0)
768 = gsl_ran_lognormal_pdf (x, log (m), s);
769 no_opt function RV.LNORMAL (m > 0, s > 0)
770 = gsl_ran_lognormal (get_rng (), log (m), s);
772 // Normal distribution.
773 function CDF.NORMAL (x, u, s > 0) = gsl_cdf_gaussian_P (x - u, s);
774 function IDF.NORMAL (P > 0 && P < 1, u, s > 0)
775 = u + gsl_cdf_gaussian_Pinv (P, s);
776 function PDF.NORMAL (x, u, s > 0) = gsl_ran_gaussian_pdf ((x - u) / s, 1) / s;
777 no_opt function RV.NORMAL (u, s > 0) = u + gsl_ran_gaussian (get_rng (), s);
778 function CDFNORM (x) = gsl_cdf_ugaussian_P (x);
779 function PROBIT (P > 0 && P < 1) = gsl_cdf_ugaussian_Pinv (P);
780 no_opt function NORMAL (s > 0) = gsl_ran_gaussian (get_rng (), s);
782 // Normal tail distribution.
783 function PDF.NTAIL (x, a > 0, sigma > 0)
784 = gsl_ran_gaussian_tail_pdf (x, a, sigma);
785 no_opt function RV.NTAIL (a > 0, sigma > 0)
786 = gsl_ran_gaussian_tail (get_rng (), a, sigma);
788 // Pareto distribution.
789 function CDF.PARETO (x >= a, a > 0, b > 0) = gsl_cdf_pareto_P (x, b, a);
790 function IDF.PARETO (P >= 0 && P < 1, a > 0, b > 0)
791 = gsl_cdf_pareto_Pinv (P, b, a);
792 function PDF.PARETO (x >= a, a > 0, b > 0) = gsl_ran_pareto_pdf (x, b, a);
793 no_opt function RV.PARETO (a > 0, b > 0) = gsl_ran_pareto (get_rng (), b, a);
795 // Rayleigh distribution.
796 extension function CDF.RAYLEIGH (x, sigma > 0) = gsl_cdf_rayleigh_P (x, sigma);
797 extension function IDF.RAYLEIGH (P >= 0 && P <= 1, sigma > 0)
798 = gsl_cdf_rayleigh_Pinv (P, sigma);
799 extension function PDF.RAYLEIGH (x, sigma > 0)
800 = gsl_ran_rayleigh_pdf (x, sigma);
801 no_opt extension function RV.RAYLEIGH (sigma > 0)
802 = gsl_ran_rayleigh (get_rng (), sigma);
804 // Rayleigh tail distribution.
805 extension function PDF.RTAIL (x, a, sigma)
806 = gsl_ran_rayleigh_tail_pdf (x, a, sigma);
807 no_opt extension function RV.RTAIL (a, sigma)
808 = gsl_ran_rayleigh_tail (get_rng (), a, sigma);
810 // Studentized maximum modulus distribution.
811 function CDF.SMOD (x > 0, a >= 1, b >= 1) = unimplemented;
812 function IDF.SMOD (P >= 0 && P < 1, a >= 1, b >= 1) = unimplemented;
814 // Studentized range distribution.
815 function CDF.SRANGE (x > 0, a >= 1, b >= 1) = unimplemented;
816 function IDF.SRANGE (P >= 0 && P < 1, a >= 1, b >= 1) = unimplemented;
818 // Student t distribution.
819 function CDF.T (x, df > 0) = gsl_cdf_tdist_P (x, df);
820 function IDF.T (P > 0 && P < 1, df > 0) = gsl_cdf_tdist_Pinv (P, df);
821 function PDF.T (x, df > 0) = gsl_ran_tdist_pdf (x, df);
822 no_opt function RV.T (df > 0) = gsl_ran_tdist (get_rng (), df);
823 function NCDF.T (x, df > 0, nc) = unimplemented;
824 function NPDF.T (x, df > 0, nc) = unimplemented;
826 // Type-1 Gumbel distribution.
827 extension function CDF.T1G (x, a, b) = gsl_cdf_gumbel1_P (x, a, b);
828 extension function IDF.T1G (P >= 0 && P <= 1, a, b)
829 = gsl_cdf_gumbel1_P (P, a, b);
830 extension function PDF.T1G (x, a, b) = gsl_ran_gumbel1_pdf (x, a, b);
831 no_opt extension function RV.T1G (a, b) = gsl_ran_gumbel1 (get_rng (), a, b);
833 // Type-2 Gumbel distribution.
834 extension function CDF.T2G (x, a, b) = gsl_cdf_gumbel2_P (x, a, b);
835 extension function IDF.T2G (P >= 0 && P <= 1, a, b)
836 = gsl_cdf_gumbel2_P (P, a, b);
837 extension function PDF.T2G (x, a, b) = gsl_ran_gumbel2_pdf (x, a, b);
838 no_opt extension function RV.T2G (a, b) = gsl_ran_gumbel2 (get_rng (), a, b);
840 // Uniform distribution.
841 function CDF.UNIFORM (x <= b, a <= x, b) = gsl_cdf_flat_P (x, a, b);
842 function IDF.UNIFORM (P >= 0 && P <= 1, a <= b, b)
843 = gsl_cdf_flat_Pinv (P, a, b);
844 function PDF.UNIFORM (x <= b, a <= x, b) = gsl_ran_flat_pdf (x, a, b);
845 no_opt function RV.UNIFORM (a <= b, b) = gsl_ran_flat (get_rng (), a, b);
846 no_opt function UNIFORM (b >= 0) = gsl_ran_flat (get_rng (), 0, b);
848 // Weibull distribution.
849 function CDF.WEIBULL (x >= 0, a > 0, b > 0) = gsl_cdf_weibull_P (x, a, b);
850 function IDF.WEIBULL (P >= 0 && P < 1, a > 0, b > 0)
851 = gsl_cdf_weibull_Pinv (P, a, b);
852 function PDF.WEIBULL (x >= 0, a > 0, b > 0) = gsl_ran_weibull_pdf (x, a, b);
853 no_opt function RV.WEIBULL (a > 0, b > 0) = gsl_ran_weibull (get_rng (), a, b);
855 // Bernoulli distribution.
856 function CDF.BERNOULLI (k == 0 || k == 1, p >= 0 && p <= 1)
858 function PDF.BERNOULLI (k == 0 || k == 1, p >= 0 && p <= 1)
859 = gsl_ran_bernoulli_pdf (k, p);
860 no_opt function RV.BERNOULLI (p >= 0 && p <= 1)
861 = gsl_ran_bernoulli (get_rng (), p);
863 // Binomial distribution.
864 function CDF.BINOM (k, n > 0 && n == floor (n), p >= 0 && p <= 1)
865 = gsl_cdf_binomial_P (k, p, n);
866 function PDF.BINOM (k >= 0 && k == floor (k) && k <= n,
867 n > 0 && n == floor (n),
869 = gsl_ran_binomial_pdf (k, p, n);
870 no_opt function RV.BINOM (p > 0 && p == floor (p), n >= 0 && n <= 1)
871 = gsl_ran_binomial (get_rng (), p, n);
873 // Geometric distribution.
874 function CDF.GEOM (k >= 1 && k == floor (k), p >= 0 && p <= 1)
875 = gsl_cdf_geometric_P (k, p);
876 function PDF.GEOM (k >= 1 && k == floor (k),
878 = gsl_ran_geometric_pdf (k, p);
879 no_opt function RV.GEOM (p >= 0 && p <= 1) = gsl_ran_geometric (get_rng (), p);
881 // Hypergeometric distribution.
882 function CDF.HYPER (k >= 0 && k == floor (k) && k <= c,
883 a > 0 && a == floor (a),
884 b > 0 && b == floor (b) && b <= a,
885 c > 0 && c == floor (c) && c <= a)
886 = gsl_cdf_hypergeometric_P (k, c, a - c, b);
887 function PDF.HYPER (k >= 0 && k == floor (k) && k <= c,
888 a > 0 && a == floor (a),
889 b > 0 && b == floor (b) && b <= a,
890 c > 0 && c == floor (c) && c <= a)
891 = gsl_ran_hypergeometric_pdf (k, c, a - c, b);
892 no_opt function RV.HYPER (a > 0 && a == floor (a),
893 b > 0 && b == floor (b) && b <= a,
894 c > 0 && c == floor (c) && c <= a)
895 = gsl_ran_hypergeometric (get_rng (), c, a - c, b);
897 // Logarithmic distribution.
898 extension function PDF.LOG (k >= 1, p > 0 && p <= 1)
899 = gsl_ran_logarithmic_pdf (k, p);
900 no_opt extension function RV.LOG (p > 0 && p <= 1)
901 = gsl_ran_logarithmic (get_rng (), p);
903 // Negative binomial distribution.
904 function CDF.NEGBIN (k >= 1, n == floor (n), p > 0 && p <= 1)
905 = gsl_cdf_negative_binomial_P (k, p, n);
906 function PDF.NEGBIN (k >= 1, n == floor (n), p > 0 && p <= 1)
907 = gsl_ran_negative_binomial_pdf (k, p, n);
908 no_opt function RV.NEGBIN (n == floor (n), p > 0 && p <= 1)
909 = gsl_ran_negative_binomial (get_rng (), p, n);
911 // Poisson distribution.
912 function CDF.POISSON (k >= 0 && k == floor (k), mu > 0)
913 = gsl_cdf_poisson_P (k, mu);
914 function PDF.POISSON (k >= 0 && k == floor (k), mu > 0)
915 = gsl_ran_poisson_pdf (k, mu);
916 no_opt function RV.POISSON (mu > 0) = gsl_ran_poisson (get_rng (), mu);
919 absorb_miss boolean function MISSING (x) = x == SYSMIS || !finite (x);
920 absorb_miss boolean function SYSMIS (x) = x == SYSMIS || !finite (x);
921 no_opt boolean function SYSMIS (num_var v)
924 return case_num (c, v) == SYSMIS;
926 no_opt boolean function VALUE (num_var v)
929 return case_num (c, v);
932 no_opt operator VEC_ELEM_NUM (idx)
936 if (idx >= 1 && idx <= vector_get_var_cnt (v))
938 const struct variable *var = vector_get_var (v, (size_t) idx - 1);
939 double value = case_num (c, var);
940 return !var_is_num_missing (var, value, MV_USER) ? value : SYSMIS;
945 msg (SE, _("SYSMIS is not a valid index value for vector "
946 "%s. The result will be set to SYSMIS."),
947 vector_get_name (v));
949 msg (SE, _("%g is not a valid index value for vector %s. "
950 "The result will be set to SYSMIS."),
951 idx, vector_get_name (v));
956 absorb_miss no_opt string operator VEC_ELEM_STR (idx)
961 if (idx >= 1 && idx <= vector_get_var_cnt (v))
963 struct variable *var = vector_get_var (v, (size_t) idx - 1);
964 return copy_string (e, case_str (c, var), var_get_width (var));
969 msg (SE, _("SYSMIS is not a valid index value for vector "
970 "%s. The result will be set to the empty string."),
971 vector_get_name (v));
973 msg (SE, _("%g is not a valid index value for vector %s. "
974 "The result will be set to the empty string."),
975 idx, vector_get_name (v));
982 no_opt operator NUM_VAR ()
986 double d = case_num (c, v);
987 return !var_is_num_missing (v, d, MV_USER) ? d : SYSMIS;
990 no_opt string operator STR_VAR ()
995 struct substring s = alloc_string (e, var_get_width (v));
996 memcpy (s.string, case_str (c, v), var_get_width (v));
1000 no_opt perm_only function LAG (num_var v, pos_int n_before)
1003 const struct ccase *c = lagged_case (ds, n_before);
1006 double x = case_num (c, v);
1007 return !var_is_num_missing (v, x, MV_USER) ? x : SYSMIS;
1013 no_opt perm_only function LAG (num_var v)
1016 const struct ccase *c = lagged_case (ds, 1);
1019 double x = case_num (c, v);
1020 return !var_is_num_missing (v, x, MV_USER) ? x : SYSMIS;
1026 no_opt perm_only string function LAG (str_var v, pos_int n_before)
1030 const struct ccase *c = lagged_case (ds, n_before);
1032 return copy_string (e, case_str (c, v), var_get_width (v));
1034 return empty_string;
1037 no_opt perm_only string function LAG (str_var v)
1041 const struct ccase *c = lagged_case (ds, 1);
1043 return copy_string (e, case_str (c, v), var_get_width (v));
1045 return empty_string;
1048 no_opt operator NUM_SYS ()
1052 return case_num (c, v) == SYSMIS;
1055 no_opt operator NUM_VAL ()
1059 return case_num (c, v);
1062 no_opt operator CASENUM ()