Added a --enable-debug option to configure and
[pspp-builds.git] / src / sample.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <math.h>
23 #include "alloc.h"
24 #include "command.h"
25 #include "error.h"
26 #include "lexer.h"
27 #include "random.h"
28 #include "str.h"
29 #include "var.h"
30
31 #include "debug-print.h"
32
33 /* The two different types of samples. */
34 enum
35   {
36     TYPE_A_FROM_B,              /* 5 FROM 10 */
37     TYPE_FRACTION               /* 0.5 */
38   };
39
40 /* SAMPLE transformation. */
41 struct sample_trns
42   {
43     struct trns_header h;
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. */
48   };
49
50 int sample_trns_proc (struct trns_header *, struct ccase *);
51
52 int
53 cmd_sample (void)
54 {
55   struct sample_trns *trns;
56
57   int type;
58   int a, b;
59   int frac;
60
61   lex_match_id ("SAMPLE");
62
63   if (!lex_force_num ())
64     return CMD_FAILURE;
65   if (!lex_integer_p ())
66     {
67       type = TYPE_FRACTION;
68       if (tokval <= 0 || tokval >= 1)
69         {
70           msg (SE, _("The sampling factor must be between 0 and 1 "
71                      "exclusive."));
72           return CMD_FAILURE;
73         }
74           
75       frac = tokval * 65536;
76       a = b = 0;
77     }
78   else
79     {
80       type = TYPE_A_FROM_B;
81       a = lex_integer ();
82       lex_get ();
83       if (!lex_force_match_id ("FROM"))
84         return CMD_FAILURE;
85       if (!lex_force_int ())
86         return CMD_FAILURE;
87       b = lex_integer ();
88       if (a >= b)
89         {
90           msg (SE, _("Cannot sample %d observations from a population of "
91                      "%d."),
92                a, b);
93           return CMD_FAILURE;
94         }
95       
96       frac = 0;
97     }
98   lex_get ();
99
100 #if DEBUGGING
101   if (type == TYPE_FRACTION)
102     printf ("SAMPLE %g.\n", frac / 65536.);
103   else
104     printf ("SAMPLE %d FROM %d.\n", a, b);
105 #endif
106
107   trns = xmalloc (sizeof *trns);
108   trns->h.proc = sample_trns_proc;
109   trns->h.free = NULL;
110   trns->type = type;
111   trns->n = a;
112   trns->N = b;
113   trns->m = trns->t = 0;
114   trns->frac = frac;
115   add_transformation ((struct trns_header *) trns);
116
117   return lex_end_of_command ();
118 }
119
120 int
121 sample_trns_proc (struct trns_header * trns, struct ccase *c unused)
122 {
123   struct sample_trns *t = (struct sample_trns *) trns;
124   double U;
125
126   if (t->type == TYPE_FRACTION)
127     return (rand_simple (0x10000) <= t->frac) - 2;
128
129   if (t->m >= t->n)
130     return -2;
131
132   U = rand_uniform (1);
133   if ((t->N - t->t) * U >= t->n - t->m)
134     {
135       t->t++;
136       return -2;
137     }
138   else
139     {
140       t->m++;
141       t->t++;
142       return -1;
143     }
144 }