1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
31 #include "debug-print.h"
33 /* The two different types of samples. */
36 TYPE_A_FROM_B, /* 5 FROM 10 */
37 TYPE_FRACTION /* 0.5 */
40 /* SAMPLE transformation. */
44 int type; /* One of TYPE_*. */
45 int n, N; /* TYPE_A_FROM_B: n from N. */
46 int m, t; /* TYPE_A_FROM_B: # selected so far; # so far. */
47 int frac; /* TYPE_FRACTION: a fraction out of 65536. */
50 int sample_trns_proc (struct trns_header *, struct ccase *);
55 struct sample_trns *trns;
61 lex_match_id ("SAMPLE");
63 if (!lex_force_num ())
65 if (!lex_integer_p ())
68 if (tokval <= 0 || tokval >= 1)
70 msg (SE, _("The sampling factor must be between 0 and 1 "
75 frac = tokval * 65536;
83 if (!lex_force_match_id ("FROM"))
85 if (!lex_force_int ())
90 msg (SE, _("Cannot sample %d observations from a population of "
101 if (type == TYPE_FRACTION)
102 printf ("SAMPLE %g.\n", frac / 65536.);
104 printf ("SAMPLE %d FROM %d.\n", a, b);
107 trns = xmalloc (sizeof *trns);
108 trns->h.proc = sample_trns_proc;
113 trns->m = trns->t = 0;
115 add_transformation ((struct trns_header *) trns);
117 return lex_end_of_command ();
121 sample_trns_proc (struct trns_header * trns, struct ccase *c unused)
123 struct sample_trns *t = (struct sample_trns *) trns;
126 if (t->type == TYPE_FRACTION)
127 return (rand_simple (0x10000) <= t->frac) - 2;
132 U = rand_uniform (1);
133 if ((t->N - t->t) * U >= t->n - t->m)