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., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include <language/command.h>
24 #include <language/lexer/lexer.h>
25 #include <math/moments.h>
30 #define _(msgid) gettext (msgid)
33 read_values (double **values, double **weights, size_t *cnt)
40 while (lex_is_number ())
42 double value = tokval;
47 if (!lex_is_number ())
49 lex_error (_("expecting weight value"));
59 *values = xnrealloc (*values, cap, sizeof **values);
60 *weights = xnrealloc (*weights, cap, sizeof **weights);
63 (*values)[*cnt] = value;
64 (*weights)[*cnt] = weight;
72 cmd_debug_moments (void)
74 int retval = CMD_FAILURE;
75 double *values = NULL;
76 double *weights = NULL;
82 if (lex_match_id ("ONEPASS"))
86 lex_force_match ('/');
89 fprintf (stderr, "%s => ", lex_rest_of_line (NULL));
94 struct moments *m = NULL;
96 m = moments_create (MOMENT_KURTOSIS);
97 if (!read_values (&values, &weights, &cnt))
102 for (i = 0; i < cnt; i++)
103 moments_pass_one (m, values[i], weights[i]);
104 for (i = 0; i < cnt; i++)
105 moments_pass_two (m, values[i], weights[i]);
106 moments_calculate (m, &weight, &M[0], &M[1], &M[2], &M[3]);
111 struct moments1 *m = NULL;
113 m = moments1_create (MOMENT_KURTOSIS);
114 if (!read_values (&values, &weights, &cnt))
116 moments1_destroy (m);
119 for (i = 0; i < cnt; i++)
120 moments1_add (m, values[i], weights[i]);
121 moments1_calculate (m, &weight, &M[0], &M[1], &M[2], &M[3]);
122 moments1_destroy (m);
125 fprintf (stderr, "W=%.3f", weight);
126 for (i = 0; i < 4; i++)
128 fprintf (stderr, " M%d=", i + 1);
130 fprintf (stderr, "sysmis");
131 else if (fabs (M[i]) <= 0.0005)
132 fprintf (stderr, "0.000");
134 fprintf (stderr, "%.3f", M[i]);
136 fprintf (stderr, "\n");
138 retval = lex_end_of_command ();