1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2010, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
23 #include "language/command.h"
24 #include "language/lexer/lexer.h"
25 #include "libpspp/compiler.h"
26 #include "math/moments.h"
28 #include "gl/xalloc.h"
31 #define _(msgid) gettext (msgid)
34 read_values (struct lexer *lexer, double **values, double **weights, size_t *cnt)
41 while (lex_is_number (lexer))
43 double value = lex_tokval (lexer);
46 if (lex_match (lexer, T_ASTERISK))
48 if (!lex_is_number (lexer))
50 lex_error (lexer, _("expecting weight value"));
53 weight = lex_tokval (lexer);
60 *values = xnrealloc (*values, cap, sizeof **values);
61 *weights = xnrealloc (*weights, cap, sizeof **weights);
64 (*values)[*cnt] = value;
65 (*weights)[*cnt] = weight;
73 cmd_debug_moments (struct lexer *lexer, struct dataset *ds UNUSED)
75 int retval = CMD_FAILURE;
76 double *values = NULL;
77 double *weights = NULL;
83 if (lex_match_id (lexer, "ONEPASS"))
85 if (!lex_force_match (lexer, T_SLASH))
90 struct moments *m = NULL;
92 m = moments_create (MOMENT_KURTOSIS);
93 if (!read_values (lexer, &values, &weights, &cnt))
98 for (i = 0; i < cnt; i++)
99 moments_pass_one (m, values[i], weights[i]);
100 for (i = 0; i < cnt; i++)
101 moments_pass_two (m, values[i], weights[i]);
102 moments_calculate (m, &weight, &M[0], &M[1], &M[2], &M[3]);
107 struct moments1 *m = NULL;
109 m = moments1_create (MOMENT_KURTOSIS);
110 if (!read_values (lexer, &values, &weights, &cnt))
112 moments1_destroy (m);
115 for (i = 0; i < cnt; i++)
116 moments1_add (m, values[i], weights[i]);
117 moments1_calculate (m, &weight, &M[0], &M[1], &M[2], &M[3]);
118 moments1_destroy (m);
121 fprintf (stderr, "W=%.3f", weight);
122 for (i = 0; i < 4; i++)
124 fprintf (stderr, " M%zu=", i + 1);
126 fprintf (stderr, "sysmis");
127 else if (fabs (M[i]) <= 0.0005)
128 fprintf (stderr, "0.000");
130 fprintf (stderr, "%.3f", M[i]);
132 fprintf (stderr, "\n");
134 retval = CMD_SUCCESS;