3 // PSPP - computes sample statistics.
4 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
5 // Written by Ben Pfaff <blp@gnu.org>.
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 // 02110-1301, USA. */
22 operator NEG (x) = -x;
24 operator ADD (a, b) = a + b;
25 operator SUB (a, b) = a - b;
27 absorb_miss operator MUL (a, b)
28 = (a == 0. || b == 0. ? 0.
29 : a == SYSMIS || b == SYSMIS ? SYSMIS
32 absorb_miss operator DIV (a, b)
34 : a == SYSMIS || b == SYSMIS ? SYSMIS
37 absorb_miss operator POW (a, b)
38 = (a == SYSMIS ? (b == 0. ? 1. : a)
39 : b == SYSMIS ? (a == 0. ? 0. : SYSMIS)
40 : a == 0. && b <= 0. ? SYSMIS
43 absorb_miss boolean operator AND (boolean a, boolean b)
46 : b == SYSMIS ? SYSMIS
49 absorb_miss boolean operator OR (boolean a, boolean b)
52 : b == SYSMIS ? SYSMIS
55 boolean operator NOT (boolean a)
60 // Numeric relational operators.
61 boolean operator EQ (a, b) = a == b;
62 boolean operator GE (a, b) = a >= b;
63 boolean operator GT (a, b) = a > b;
64 boolean operator LE (a, b) = a <= b;
65 boolean operator LT (a, b) = a < b;
66 boolean operator NE (a, b) = a != b;
68 // String relational operators.
69 boolean operator EQ_STRING (string a, string b) = compare_string (&a, &b) == 0;
70 boolean operator GE_STRING (string a, string b) = compare_string (&a, &b) >= 0;
71 boolean operator GT_STRING (string a, string b) = compare_string (&a, &b) > 0;
72 boolean operator LE_STRING (string a, string b) = compare_string (&a, &b) <= 0;
73 boolean operator LT_STRING (string a, string b) = compare_string (&a, &b) < 0;
74 boolean operator NE_STRING (string a, string b) = compare_string (&a, &b) != 0;
77 function ABS (x) = fabs (x);
78 extension function ACOS (x >= -1 && x <= 1) = acos (x);
79 function ASIN (x >= -1 && x <= 1) = asin (x);
80 function ATAN (x) = atan (x);
81 extension function ARCOS (x >= -1 && x <= 1) = acos (x);
82 function ARSIN (x >= -1 && x <= 1) = asin (x);
83 function ARTAN (x) = atan (x);
84 function COS (x) = cos (x);
85 function EXP (x) = check_errno (exp (x));
86 function LG10(x) = check_errno (log10 (x));
87 function LN (x) = check_errno (log (x));
88 function LNGAMMA (x >= 0) = gsl_sf_lngamma (x);
89 function MOD10 (x) = fmod (x, 10);
90 function RND (x) = x >= 0. ? floor (x + .5) : -floor (-x + .5);
91 function SIN (x) = sin (x);
92 function SQRT (x >= 0) = sqrt (x);
93 function TAN (x) = check_errno (tan (x));
94 function TRUNC (x) = x >= 0. ? floor (x) : -floor (-x);
96 absorb_miss function MOD (n, d)
99 return n != SYSMIS ? fmod (n, d) : SYSMIS;
101 return n != 0. ? SYSMIS : 0.;
104 // N-ary numeric functions.
105 absorb_miss boolean function ANY (x != SYSMIS, a[n])
110 for (i = 0; i < n; i++)
113 else if (a[i] == SYSMIS)
116 return sysmis ? SYSMIS : 0.;
119 boolean function ANY (string x, string a[n])
123 for (i = 0; i < n; i++)
124 if (!compare_string (&x, &a[i]))
129 function CFVAR.2 (a[n])
131 double mean, variance;
133 moments_of_doubles (a, n, NULL, &mean, &variance, NULL, NULL);
135 if (mean == SYSMIS || mean == 0 || variance == SYSMIS)
138 return sqrt (variance) / mean;
141 function MAX.1 (a[n])
147 for (i = 0; i < n; i++)
148 if (a[i] != SYSMIS && a[i] > max)
153 string function MAX (string a[n])
155 struct substring *max;
159 for (i = 1; i < n; i++)
160 if (compare_string (&a[i], max) > 0)
165 function MEAN.1 (a[n])
168 moments_of_doubles (a, n, NULL, &mean, NULL, NULL, NULL);
172 function MIN.1 (a[n])
178 for (i = 0; i < n; i++)
179 if (a[i] != SYSMIS && a[i] < min)
184 string function MIN (string a[n])
186 struct substring *min;
190 for (i = 1; i < n; i++)
191 if (compare_string (&a[i], min) < 0)
196 absorb_miss function NMISS (a[n])
199 size_t missing_cnt = 0;
201 for (i = 0; i < n; i++)
202 missing_cnt += a[i] == SYSMIS;
206 absorb_miss function NVALID (a[n])
209 size_t valid_cnt = 0;
211 for (i = 0; i < n; i++)
212 valid_cnt += a[i] != SYSMIS;
216 absorb_miss boolean function RANGE (x != SYSMIS, a[n*2])
221 for (i = 0; i < n; i++)
224 double y = a[2 * i + 1];
225 if (w != SYSMIS && y != SYSMIS)
227 if (w <= x && x <= y)
233 return sysmis ? SYSMIS : 0.;
236 boolean function RANGE (string x, string a[n*2])
240 for (i = 0; i < n; i++)
242 struct substring *w = &a[2 * i];
243 struct substring *y = &a[2 * i + 1];
244 if (compare_string (w, &x) <= 0 && compare_string (&x, y) <= 0)
253 moments_of_doubles (a, n, NULL, NULL, &variance, NULL, NULL);
254 return sqrt (variance);
257 function SUM.1 (a[n])
263 for (i = 0; i < n; i++)
269 function VARIANCE.2 (a[n])
272 moments_of_doubles (a, n, NULL, NULL, &variance, NULL, NULL);
276 // Time construction & extraction functions.
277 function TIME.HMS (h, m, s)
279 if ((h > 0. || m > 0. || s > 0.) && (h < 0. || m < 0. || s < 0.))
281 msg (SW, _("TIME.HMS cannot mix positive and negative arguments."));
285 return H_S * h + MIN_S * m + s;
287 function TIME.DAYS (days) = days * DAY_S;
288 function CTIME.DAYS (time) = time / DAY_S;
289 function CTIME.HOURS (time) = time / H_S;
290 function CTIME.MINUTES (time) = time / MIN_S;
291 function CTIME.SECONDS (time) = time;
293 // Date construction functions.
294 function DATE.DMY (d, m, y) = expr_ymd_to_date (y, m, d);
295 function DATE.MDY (m, d, y) = expr_ymd_to_date (y, m, d);
296 function DATE.MOYR (m, y) = expr_ymd_to_date (y, m, 1);
297 function DATE.QYR (q, y) = expr_ymd_to_date (y, q * 3 - 2, 1);
298 function DATE.WKYR (w, y) = expr_wkyr_to_date (w, y);
299 function DATE.YRDAY (y, yday) = expr_yrday_to_date (y, yday);
300 function YRMODA (y, m, d) = expr_yrmoda (y, m, d);
302 // Date extraction functions.
303 function XDATE.TDAY (date) = floor (date / DAY_S);
304 function XDATE.HOUR (date) = fmod (floor (date / H_S), DAY_H);
305 function XDATE.MINUTE (date) = fmod (floor (date / H_MIN), H_MIN);
306 function XDATE.SECOND (date) = fmod (date, MIN_S);
307 function XDATE.DATE (date) = floor (date / DAY_S) * DAY_S;
308 function XDATE.TIME (date) = fmod (date, DAY_S);
310 function XDATE.JDAY (date >= DAY_S) = calendar_offset_to_yday (date / DAY_S);
311 function XDATE.MDAY (date >= DAY_S) = calendar_offset_to_mday (date / DAY_S);
312 function XDATE.MONTH (date >= DAY_S)
313 = calendar_offset_to_month (date / DAY_S);
314 function XDATE.QUARTER (date >= DAY_S)
315 = (calendar_offset_to_month (date / DAY_S) - 1) / 3 + 1;
316 function XDATE.WEEK (date >= DAY_S)
317 = (calendar_offset_to_yday (date / DAY_S) - 1) / 7 + 1;
318 function XDATE.WKDAY (date >= DAY_S) = calendar_offset_to_wday (date / DAY_S);
319 function XDATE.YEAR (date >= DAY_S) = calendar_offset_to_year (date / DAY_S);
322 string function CONCAT (string a[n])
325 struct substring dst;
328 dst = alloc_string (e, MAX_STRING);
330 for (i = 0; i < n; i++)
332 struct substring *src = &a[i];
335 copy_len = src->length;
336 if (dst.length + copy_len > MAX_STRING)
337 copy_len = MAX_STRING - dst.length;
338 memcpy (&dst.string[dst.length], src->string, copy_len);
339 dst.length += copy_len;
345 function INDEX (string haystack, string needle)
347 if (needle.length == 0)
351 int limit = haystack.length - needle.length + 1;
353 for (i = 1; i <= limit; i++)
354 if (!memcmp (&haystack.string[i - 1], needle.string, needle.length))
360 function INDEX (string haystack, string needles, needle_len_d)
362 if (needle_len_d <= INT_MIN || needle_len_d >= INT_MAX
363 || (int) needle_len_d != needle_len_d
364 || needles.length == 0)
368 int needle_len = needle_len_d;
369 if (needle_len < 0 || needle_len > needles.length
370 || needles.length % needle_len != 0)
374 int limit = haystack.length - needle_len + 1;
376 for (i = 1; i <= limit; i++)
377 for (j = 0; j < needles.length; j += needle_len)
378 if (!memcmp (&haystack.string[i - 1], &needles.string[j],
387 function RINDEX (string haystack, string needle)
389 if (needle.length == 0)
393 int limit = haystack.length - needle.length + 1;
395 for (i = limit; i >= 1; i--)
396 if (!memcmp (&haystack.string[i - 1], needle.string, needle.length))
402 function RINDEX (string haystack, string needles, needle_len_d)
404 if (needle_len_d <= INT_MIN || needle_len_d >= INT_MAX
405 || (int) needle_len_d != needle_len_d
406 || needles.length == 0)
410 int needle_len = needle_len_d;
411 if (needle_len < 0 || needle_len > needles.length
412 || needles.length % needle_len != 0)
416 int limit = haystack.length - needle_len + 1;
418 for (i = limit; i >= 1; i--)
419 for (j = 0; j < needles.length; j += needle_len)
420 if (!memcmp (&haystack.string[i - 1],
421 &needles.string[j], needle_len))
428 function LENGTH (string s)
433 string function LOWER (string s)
437 for (i = 0; i < s.length; i++)
438 s.string[i] = tolower ((unsigned char) s.string[i]);
442 function MBLEN.BYTE (string s, idx)
444 if (idx < 0 || idx >= s.length || (int) idx != idx)
450 string function UPCASE (string s)
454 for (i = 0; i < s.length; i++)
455 s.string[i] = toupper ((unsigned char) s.string[i]);
459 absorb_miss string function LPAD (string s, n)
462 if (n < 0 || n > MAX_STRING || (int) n != n)
464 else if (s.length >= n)
468 struct substring t = alloc_string (e, n);
469 memset (t.string, ' ', n - s.length);
470 memcpy (&t.string[(int) n - s.length], s.string, s.length);
475 absorb_miss string function LPAD (string s, n, string c)
478 if (n < 0 || n > MAX_STRING || (int) n != n || c.length != 1)
480 else if (s.length >= n)
484 struct substring t = alloc_string (e, n);
485 memset (t.string, c.string[0], n - s.length);
486 memcpy (&t.string[(int) n - s.length], s.string, s.length);
491 absorb_miss string function RPAD (string s, n)
494 if (n < 0 || n > MAX_STRING || (int) n != n)
496 else if (s.length >= n)
500 struct substring t = alloc_string (e, n);
501 memcpy (t.string, s.string, s.length);
502 memset (&t.string[s.length], ' ', n - s.length);
507 absorb_miss string function RPAD (string s, n, string c)
510 if (n < 0 || n > MAX_STRING || (int) n != n || c.length != 1)
512 else if (s.length >= n)
516 struct substring t = alloc_string (e, n);
517 memcpy (t.string, s.string, s.length);
518 memset (&t.string[s.length], c.string[0], n - s.length);
523 string function LTRIM (string s)
525 while (s.length > 0 && s.string[0] == ' ')
533 string function LTRIM (string s, string c)
537 while (s.length > 0 && s.string[0] == c.string[0])
548 string function RTRIM (string s)
550 while (s.length > 0 && s.string[s.length - 1] == ' ')
555 string function RTRIM (string s, string c)
559 while (s.length > 0 && s.string[s.length - 1] == c.string[0])
567 function NUMBER (string s, ni_format f)
570 data_in (ss_head (s, f->w), f->type, f->d, 0, &out, 0);
574 absorb_miss string function STRING (x, no_format f)
578 struct substring dst;
581 dst = alloc_string (e, f->w);
582 assert (!fmt_is_string (f->type));
583 data_out (&v, f, dst.string);
587 absorb_miss string function SUBSTR (string s, ofs)
590 if (ofs >= 1 && ofs <= s.length && (int) ofs == ofs)
591 return copy_string (e, &s.string[(int) ofs - 1], s.length - ofs + 1);
596 absorb_miss string function SUBSTR (string s, ofs, cnt)
599 if (ofs >= 1 && ofs <= s.length && (int) ofs == ofs
600 && cnt >= 1 && cnt <= INT_MAX && (int) cnt == cnt)
602 int cnt_max = s.length - (int) ofs + 1;
603 return copy_string (e, &s.string[(int) ofs - 1],
604 cnt <= cnt_max ? cnt : cnt_max);
611 operator SQUARE (x) = x * x;
612 boolean operator NUM_TO_BOOLEAN (x)
614 if (x == 0. || x == 1. || x == SYSMIS)
618 msg (SE, _("A number being treated as a Boolean in an "
619 "expression was found to have a value other than "
620 "0 (false), 1 (true), or the system-missing value. "
621 "The result was forced to 0."));
626 operator BOOLEAN_TO_NUM (boolean x) = x;
628 // Beta distribution.
629 function PDF.BETA (x >= 0 && x <= 1, a > 0, b > 0)
630 = gsl_ran_beta_pdf (x, a, b);
631 function CDF.BETA (x >= 0 && x <= 1, a > 0, b > 0) = gsl_cdf_beta_P (x, a, b);
632 function IDF.BETA (P >= 0 && P <= 1, a > 0, b > 0)
633 = gslextras_cdf_beta_Pinv (P, a, b);
634 no_opt function RV.BETA (a > 0, b > 0) = gsl_ran_beta (get_rng (), a, b);
635 function NCDF.BETA (x >= 0, a > 0, b > 0, lambda > 0)
636 = ncdf_beta (x, a, b, lambda);
637 function NPDF.BETA (x >= 0, a > 0, b > 0, lambda > 0)
638 = npdf_beta (x, a, b, lambda);
640 // Bivariate normal distribution.
641 function CDF.BVNOR (x0, x1, r >= -1 && r <= 1) = cdf_bvnor (x0, x1, r);
642 function PDF.BVNOR (x0, x1, r >= -1 && r <= 1)
643 = gsl_ran_bivariate_gaussian_pdf (x0, x1, 1, 1, r);
645 // Cauchy distribution.
646 function CDF.CAUCHY (x, a, b > 0) = gsl_cdf_cauchy_P ((x - a) / b, 1);
647 function IDF.CAUCHY (P > 0 && P < 1, a, b > 0)
648 = a + b * gsl_cdf_cauchy_Pinv (P, 1);
649 function PDF.CAUCHY (x, a, b > 0) = gsl_ran_cauchy_pdf ((x - a) / b, 1) / b;
650 no_opt function RV.CAUCHY (a, b > 0) = a + b * gsl_ran_cauchy (get_rng (), 1);
652 // Chi-square distribution.
653 function CDF.CHISQ (x >= 0, df > 0) = gsl_cdf_chisq_P (x, df);
654 function IDF.CHISQ (P >= 0 && P < 1, df > 0) = gsl_cdf_chisq_Pinv (P, df);
655 function PDF.CHISQ (x >= 0, df > 0) = gsl_ran_chisq_pdf (x, df);
656 no_opt function RV.CHISQ (df > 0) = gsl_ran_chisq (get_rng (), df);
657 function NCDF.CHISQ (x >= 0, df > 0, c) = unimplemented;
658 function NPDF.CHISQ (x >= 0, df > 0, c) = unimplemented;
659 function SIG.CHISQ (x >= 0, df > 0) = gsl_cdf_chisq_Q (x, df);
661 // Exponential distribution.
662 function CDF.EXP (x >= 0, a > 0) = gsl_cdf_exponential_P (x, 1. / a);
663 function IDF.EXP (P >= 0 && P < 1, a > 0)
664 = gsl_cdf_exponential_Pinv (P, 1. / a);
665 function PDF.EXP (x >= 0, a > 0) = gsl_ran_exponential_pdf (x, 1. / a);
666 no_opt function RV.EXP (a > 0) = gsl_ran_exponential (get_rng (), 1. / a);
668 // Exponential power distribution.
669 extension function PDF.XPOWER (x, a > 0, b >= 0)
670 = gsl_ran_exppow_pdf (x, a, b);
671 no_opt extension function RV.XPOWER (a > 0, b >= 0)
672 = gsl_ran_exppow (get_rng (), a, b);
675 function CDF.F (x >= 0, df1 > 0, df2 > 0) = gsl_cdf_fdist_P (x, df1, df2);
676 function IDF.F (P >= 0 && P < 1, df1 > 0, df2 > 0) = idf_fdist (P, df1, df2);
677 function PDF.F (x >= 0, df1 > 0, df2 > 0) = gsl_ran_fdist_pdf (x, df1, df2);
678 no_opt function RV.F (df1 > 0, df2 > 0) = gsl_ran_fdist (get_rng (), df1, df2);
679 function NCDF.F (x >= 0, df1 > 0, df2 > 0, lambda >= 0) = unimplemented;
680 function NPDF.F (x >= 0, df1 > 0, df2 > 0, lmabda >= 0) = unimplemented;
681 function SIG.F (x >= 0, df1 > 0, df2 > 0) = gsl_cdf_fdist_Q (x, df1, df2);
683 // Gamma distribution.
684 function CDF.GAMMA (x >= 0, a > 0, b > 0) = gsl_cdf_gamma_P (x, a, 1. / b);
685 function IDF.GAMMA (P >= 0 && P <= 1, a > 0, b > 0)
686 = gsl_cdf_gamma_Pinv (P, a, 1. / b);
687 function PDF.GAMMA (x >= 0, a > 0, b > 0) = gsl_ran_gamma_pdf (x, a, 1. / b);
688 no_opt function RV.GAMMA (a > 0, b > 0)
689 = gsl_ran_gamma (get_rng (), a, 1. / b);
691 // Half-normal distribution.
692 function CDF.HALFNRM (x, a, b > 0) = unimplemented;
693 function IDF.HALFNRM (P > 0 && P < 1, a, b > 0) = unimplemented;
694 function PDF.HALFNRM (x, a, b > 0) = unimplemented;
695 no_opt function RV.HALFNRM (a, b > 0) = unimplemented;
697 // Inverse Gaussian distribution.
698 function CDF.IGAUSS (x > 0, a > 0, b > 0) = unimplemented;
699 function IDF.IGAUSS (P >= 0 && P < 1, a > 0, b > 0) = unimplemented;
700 function PDF.IGAUSS (x > 0, a > 0, b > 0) = unimplemented;
701 no_opt function RV.IGAUSS (a > 0, b > 0) = unimplemented;
703 // Landau distribution.
704 extension function PDF.LANDAU (x) = gsl_ran_landau_pdf (x);
705 no_opt extension function RV.LANDAU () = gsl_ran_landau (get_rng ());
707 // Laplace distribution.
708 function CDF.LAPLACE (x, a, b > 0) = gsl_cdf_laplace_P ((x - a) / b, 1);
709 function IDF.LAPLACE (P > 0 && P < 1, a, b > 0)
710 = a + b * gsl_cdf_laplace_Pinv (P, 1);
711 function PDF.LAPLACE (x, a, b > 0) = gsl_ran_laplace_pdf ((x - a) / b, 1) / b;
712 no_opt function RV.LAPLACE (a, b > 0)
713 = a + b * gsl_ran_laplace (get_rng (), 1);
715 // Levy alpha-stable distribution.
716 no_opt extension function RV.LEVY (c, alpha > 0 && alpha <= 2)
717 = gsl_ran_levy (get_rng (), c, alpha);
719 // Levy skew alpha-stable distribution.
720 no_opt extension function RV.LVSKEW (c, alpha > 0 && alpha <= 2,
721 beta >= -1 && beta <= 1)
722 = gsl_ran_levy_skew (get_rng (), c, alpha, beta);
724 // Logistic distribution.
725 function CDF.LOGISTIC (x, a, b > 0) = gsl_cdf_logistic_P ((x - a) / b, 1);
726 function IDF.LOGISTIC (P > 0 && P < 1, a, b > 0)
727 = a + b * gsl_cdf_logistic_Pinv (P, 1);
728 function PDF.LOGISTIC (x, a, b > 0)
729 = gsl_ran_logistic_pdf ((x - a) / b, 1) / b;
730 no_opt function RV.LOGISTIC (a, b > 0)
731 = a + b * gsl_ran_logistic (get_rng (), 1);
733 // Lognormal distribution.
734 function CDF.LNORMAL (x >= 0, m > 0, s > 0)
735 = gsl_cdf_lognormal_P (x, log (m), s);
736 function IDF.LNORMAL (P >= 0 && P < 1, m > 0, s > 0)
737 = gsl_cdf_lognormal_Pinv (P, log (m), s);
738 function PDF.LNORMAL (x >= 0, m > 0, s > 0)
739 = gsl_ran_lognormal_pdf (x, log (m), s);
740 no_opt function RV.LNORMAL (m > 0, s > 0)
741 = gsl_ran_lognormal (get_rng (), log (m), s);
743 // Normal distribution.
744 function CDF.NORMAL (x, u, s > 0) = gsl_cdf_gaussian_P (x - u, s);
745 function IDF.NORMAL (P > 0 && P < 1, u, s > 0)
746 = u + gsl_cdf_gaussian_Pinv (P, s);
747 function PDF.NORMAL (x, u, s > 0) = gsl_ran_gaussian_pdf ((x - u) / s, 1) / s;
748 no_opt function RV.NORMAL (u, s > 0) = u + gsl_ran_gaussian (get_rng (), s);
749 function CDFNORM (x) = gsl_cdf_ugaussian_P (x);
750 function PROBIT (P > 0 && P < 1) = gsl_cdf_ugaussian_Pinv (P);
751 no_opt function NORMAL (s > 0) = gsl_ran_gaussian (get_rng (), s);
753 // Normal tail distribution.
754 function PDF.NTAIL (x, a > 0, sigma > 0)
755 = gsl_ran_gaussian_tail_pdf (x, a, sigma);
756 no_opt function RV.NTAIL (a > 0, sigma > 0)
757 = gsl_ran_gaussian_tail (get_rng (), a, sigma);
759 // Pareto distribution.
760 function CDF.PARETO (x >= a, a > 0, b > 0) = gsl_cdf_pareto_P (x, b, a);
761 function IDF.PARETO (P >= 0 && P < 1, a > 0, b > 0)
762 = gsl_cdf_pareto_Pinv (P, b, a);
763 function PDF.PARETO (x >= a, a > 0, b > 0) = gsl_ran_pareto_pdf (x, b, a);
764 no_opt function RV.PARETO (a > 0, b > 0) = gsl_ran_pareto (get_rng (), b, a);
766 // Rayleigh distribution.
767 extension function CDF.RAYLEIGH (x, sigma > 0) = gsl_cdf_rayleigh_P (x, sigma);
768 extension function IDF.RAYLEIGH (P >= 0 && P <= 1, sigma > 0)
769 = gsl_cdf_rayleigh_Pinv (P, sigma);
770 extension function PDF.RAYLEIGH (x, sigma > 0)
771 = gsl_ran_rayleigh_pdf (x, sigma);
772 no_opt extension function RV.RAYLEIGH (sigma > 0)
773 = gsl_ran_rayleigh (get_rng (), sigma);
775 // Rayleigh tail distribution.
776 extension function PDF.RTAIL (x, a, sigma)
777 = gsl_ran_rayleigh_tail_pdf (x, a, sigma);
778 no_opt extension function RV.RTAIL (a, sigma)
779 = gsl_ran_rayleigh_tail (get_rng (), a, sigma);
781 // Studentized maximum modulus distribution.
782 function CDF.SMOD (x > 0, a >= 1, b >= 1) = unimplemented;
783 function IDF.SMOD (P >= 0 && P < 1, a >= 1, b >= 1) = unimplemented;
785 // Studentized range distribution.
786 function CDF.SRANGE (x > 0, a >= 1, b >= 1) = unimplemented;
787 function IDF.SRANGE (P >= 0 && P < 1, a >= 1, b >= 1) = unimplemented;
789 // Student t distribution.
790 function CDF.T (x, df > 0) = gsl_cdf_tdist_P (x, df);
791 function IDF.T (P > 0 && P < 1, df > 0) = gsl_cdf_tdist_Pinv (P, df);
792 function PDF.T (x, df > 0) = gsl_ran_tdist_pdf (x, df);
793 no_opt function RV.T (df > 0) = gsl_ran_tdist (get_rng (), df);
794 function NCDF.T (x, df > 0, nc) = unimplemented;
795 function NPDF.T (x, df > 0, nc) = unimplemented;
797 // Type-1 Gumbel distribution.
798 extension function CDF.T1G (x, a, b) = gsl_cdf_gumbel1_P (x, a, b);
799 extension function IDF.T1G (P >= 0 && P <= 1, a, b)
800 = gsl_cdf_gumbel1_P (P, a, b);
801 extension function PDF.T1G (x, a, b) = gsl_ran_gumbel1_pdf (x, a, b);
802 no_opt extension function RV.T1G (a, b) = gsl_ran_gumbel1 (get_rng (), a, b);
804 // Type-2 Gumbel distribution.
805 extension function CDF.T2G (x, a, b) = gsl_cdf_gumbel2_P (x, a, b);
806 extension function IDF.T2G (P >= 0 && P <= 1, a, b)
807 = gsl_cdf_gumbel2_P (P, a, b);
808 extension function PDF.T2G (x, a, b) = gsl_ran_gumbel2_pdf (x, a, b);
809 no_opt extension function RV.T2G (a, b) = gsl_ran_gumbel2 (get_rng (), a, b);
811 // Uniform distribution.
812 function CDF.UNIFORM (x <= b, a <= x, b) = gsl_cdf_flat_P (x, a, b);
813 function IDF.UNIFORM (P >= 0 && P <= 1, a <= b, b)
814 = gsl_cdf_flat_Pinv (P, a, b);
815 function PDF.UNIFORM (x <= b, a <= x, b) = gsl_ran_flat_pdf (x, a, b);
816 no_opt function RV.UNIFORM (a <= b, b) = gsl_ran_flat (get_rng (), a, b);
817 no_opt function UNIFORM (b >= 0) = gsl_ran_flat (get_rng (), 0, b);
819 // Weibull distribution.
820 function CDF.WEIBULL (x >= 0, a > 0, b > 0) = gsl_cdf_weibull_P (x, a, b);
821 function IDF.WEIBULL (P >= 0 && P < 1, a > 0, b > 0)
822 = gsl_cdf_weibull_Pinv (P, a, b);
823 function PDF.WEIBULL (x >= 0, a > 0, b > 0) = gsl_ran_weibull_pdf (x, a, b);
824 no_opt function RV.WEIBULL (a > 0, b > 0) = gsl_ran_weibull (get_rng (), a, b);
826 // Bernoulli distribution.
827 function CDF.BERNOULLI (k == 0 || k == 1, p >= 0 && p <= 1)
829 function PDF.BERNOULLI (k == 0 || k == 1, p >= 0 && p <= 1)
830 = gsl_ran_bernoulli_pdf (k, p);
831 no_opt function RV.BERNOULLI (p >= 0 && p <= 1)
832 = gsl_ran_bernoulli (get_rng (), p);
834 // Binomial distribution.
835 function CDF.BINOM (k, n > 0 && n == floor (n), p >= 0 && p <= 1)
836 = gslextras_cdf_binomial_P (k, p, n);
837 function PDF.BINOM (k >= 0 && k == floor (k) && k <= n,
838 n > 0 && n == floor (n),
840 = gsl_ran_binomial_pdf (k, p, n);
841 no_opt function RV.BINOM (p > 0 && p == floor (p), n >= 0 && n <= 1)
842 = gsl_ran_binomial (get_rng (), p, n);
844 // Geometric distribution.
845 function CDF.GEOM (k >= 1 && k == floor (k), p >= 0 && p <= 1)
846 = gslextras_cdf_geometric_P (k, p);
847 function PDF.GEOM (k >= 1 && k == floor (k),
849 = gsl_ran_geometric_pdf (k, p);
850 no_opt function RV.GEOM (p >= 0 && p <= 1) = gsl_ran_geometric (get_rng (), p);
852 // Hypergeometric distribution.
853 function CDF.HYPER (k >= 0 && k == floor (k) && k <= c,
854 a > 0 && a == floor (a),
855 b > 0 && b == floor (b) && b <= a,
856 c > 0 && c == floor (c) && c <= a)
857 = gslextras_cdf_hypergeometric_P (k, c, a - c, b);
858 function PDF.HYPER (k >= 0 && k == floor (k) && k <= c,
859 a > 0 && a == floor (a),
860 b > 0 && b == floor (b) && b <= a,
861 c > 0 && c == floor (c) && c <= a)
862 = gsl_ran_hypergeometric_pdf (k, c, a - c, b);
863 no_opt function RV.HYPER (a > 0 && a == floor (a),
864 b > 0 && b == floor (b) && b <= a,
865 c > 0 && c == floor (c) && c <= a)
866 = gsl_ran_hypergeometric (get_rng (), c, a - c, b);
868 // Logarithmic distribution.
869 extension function PDF.LOG (k >= 1, p > 0 && p <= 1)
870 = gsl_ran_logarithmic_pdf (k, p);
871 no_opt extension function RV.LOG (p > 0 && p <= 1)
872 = gsl_ran_logarithmic (get_rng (), p);
874 // Negative binomial distribution.
875 function CDF.NEGBIN (k >= 1, n == floor (n), p > 0 && p <= 1)
876 = gslextras_cdf_negative_binomial_P (k, p, n);
877 function PDF.NEGBIN (k >= 1, n == floor (n), p > 0 && p <= 1)
878 = gsl_ran_negative_binomial_pdf (k, p, n);
879 no_opt function RV.NEGBIN (n == floor (n), p > 0 && p <= 1)
880 = gsl_ran_negative_binomial (get_rng (), p, n);
882 // Poisson distribution.
883 function CDF.POISSON (k >= 0 && k == floor (k), mu > 0)
884 = gslextras_cdf_poisson_P (k, mu);
885 function PDF.POISSON (k >= 0 && k == floor (k), mu > 0)
886 = gsl_ran_poisson_pdf (k, mu);
887 no_opt function RV.POISSON (mu > 0) = gsl_ran_poisson (get_rng (), mu);
890 absorb_miss boolean function MISSING (x) = x == SYSMIS || !finite (x);
891 absorb_miss boolean function SYSMIS (x) = x == SYSMIS || !finite (x);
892 no_opt boolean function SYSMIS (num_var v)
895 return case_num (c, v) == SYSMIS;
897 no_opt boolean function VALUE (num_var v)
900 return case_num (c, v);
903 no_opt operator VEC_ELEM_NUM (idx)
907 if (idx >= 1 && idx <= vector_get_var_cnt (v))
909 const struct variable *var = vector_get_var (v, (size_t) idx - 1);
910 double value = case_num (c, var);
911 return !var_is_num_user_missing (var, value) ? value : SYSMIS;
916 msg (SE, _("SYSMIS is not a valid index value for vector "
917 "%s. The result will be set to SYSMIS."),
918 vector_get_name (v));
920 msg (SE, _("%g is not a valid index value for vector %s. "
921 "The result will be set to SYSMIS."),
922 idx, vector_get_name (v));
927 absorb_miss no_opt string operator VEC_ELEM_STR (idx)
932 if (idx >= 1 && idx <= vector_get_var_cnt (v))
934 struct variable *var = vector_get_var (v, (size_t) idx - 1);
935 return copy_string (e, case_str (c, var), var_get_width (var));
940 msg (SE, _("SYSMIS is not a valid index value for vector "
941 "%s. The result will be set to the empty string."),
942 vector_get_name (v));
944 msg (SE, _("%g is not a valid index value for vector %s. "
945 "The result will be set to the empty string."),
946 idx, vector_get_name (v));
953 no_opt operator NUM_VAR ()
957 double d = case_num (c, v);
958 return !var_is_num_user_missing (v, d) ? d : SYSMIS;
961 no_opt string operator STR_VAR ()
966 struct substring s = alloc_string (e, var_get_width (v));
967 memcpy (s.string, case_str (c, v), var_get_width (v));
971 no_opt perm_only function LAG (num_var v, pos_int n_before)
974 struct ccase *c = lagged_case (ds, n_before);
977 double x = case_num (c, v);
978 return !var_is_num_user_missing (v, x) ? x : SYSMIS;
984 no_opt perm_only function LAG (num_var v)
987 struct ccase *c = lagged_case (ds, 1);
990 double x = case_num (c, v);
991 return !var_is_num_user_missing (v, x) ? x : SYSMIS;
997 no_opt perm_only string function LAG (str_var v, pos_int n_before)
1001 struct ccase *c = lagged_case (ds, n_before);
1003 return copy_string (e, case_str (c, v), var_get_width (v));
1005 return empty_string;
1008 no_opt perm_only string function LAG (str_var v)
1012 struct ccase *c = lagged_case (ds, 1);
1014 return copy_string (e, case_str (c, v), var_get_width (v));
1016 return empty_string;
1019 no_opt operator NUM_SYS ()
1023 return case_num (c, v) == SYSMIS;
1026 no_opt operator NUM_VAL ()
1030 return case_num (c, v);
1033 no_opt operator CASENUM ()