Move all command implementations into a single 'commands' directory.
[pspp] / src / language / commands / sample.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009-2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <gsl/gsl_rng.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <math.h>
23
24 #include "data/dataset.h"
25 #include "data/variable.h"
26 #include "language/command.h"
27 #include "language/lexer/lexer.h"
28 #include "libpspp/compiler.h"
29 #include "libpspp/message.h"
30 #include "libpspp/str.h"
31 #include "math/random.h"
32
33 #include "gl/xalloc.h"
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 /* The two different types of samples. */
39 enum
40   {
41     TYPE_A_FROM_B,              /* 5 FROM 10 */
42     TYPE_FRACTION               /* 0.5 */
43   };
44
45 /* SAMPLE transformation. */
46 struct sample_trns
47   {
48     int type;                   /* One of TYPE_*. */
49     int n, N;                   /* TYPE_A_FROM_B: n from N. */
50     int m, t;                   /* TYPE_A_FROM_B: # picked so far; # so far. */
51     unsigned frac;              /* TYPE_FRACTION: a fraction of UINT_MAX. */
52   };
53
54 static const struct trns_class sample_trns_class;
55
56 int
57 cmd_sample (struct lexer *lexer, struct dataset *ds)
58 {
59   struct sample_trns *trns;
60
61   int type;
62   int a, b;
63   unsigned frac;
64
65   if (!lex_force_num (lexer))
66     return CMD_FAILURE;
67   if (!lex_is_integer (lexer))
68     {
69       unsigned long min = gsl_rng_min (get_rng ());
70       unsigned long max = gsl_rng_max (get_rng ());
71
72       type = TYPE_FRACTION;
73       if (!lex_force_num_range_open (lexer, "SAMPLE", 0, 1))
74         return CMD_FAILURE;
75
76       frac = lex_tokval (lexer) * (max - min) + min;
77       a = b = 0;
78     }
79   else
80     {
81       type = TYPE_A_FROM_B;
82       a = lex_integer (lexer);
83       lex_get (lexer);
84       if (!lex_force_match_id (lexer, "FROM"))
85         return CMD_FAILURE;
86       if (!lex_force_int_range (lexer, "FROM", a + 1, INT_MAX))
87         return CMD_FAILURE;
88       b = lex_integer (lexer);
89       frac = 0;
90     }
91   lex_get (lexer);
92
93   trns = xmalloc (sizeof *trns);
94   trns->type = type;
95   trns->n = a;
96   trns->N = b;
97   trns->m = trns->t = 0;
98   trns->frac = frac;
99   add_transformation (ds, &sample_trns_class, trns);
100
101   return CMD_SUCCESS;
102 }
103
104 /* Executes a SAMPLE transformation. */
105 static enum trns_result
106 sample_trns_proc (void *t_, struct ccase **c UNUSED,
107                   casenumber case_num UNUSED)
108 {
109   struct sample_trns *t = t_;
110   double U;
111
112   if (t->type == TYPE_FRACTION)
113     {
114       if (gsl_rng_get (get_rng ()) <= t->frac)
115         return TRNS_CONTINUE;
116       else
117         return TRNS_DROP_CASE;
118     }
119
120   if (t->m >= t->n)
121     return TRNS_DROP_CASE;
122
123   U = gsl_rng_uniform (get_rng ());
124   if ((t->N - t->t) * U >= t->n - t->m)
125     {
126       t->t++;
127       return TRNS_DROP_CASE;
128     }
129   else
130     {
131       t->m++;
132       t->t++;
133       return TRNS_CONTINUE;
134     }
135 }
136
137 static bool
138 sample_trns_free (void *t_)
139 {
140   struct sample_trns *t = t_;
141   free (t);
142   return true;
143 }
144
145 static const struct trns_class sample_trns_class = {
146   .name = "SAMPLE",
147   .execute = sample_trns_proc,
148   .destroy = sample_trns_free,
149 };