083064d163f5be6a84f0c0f873285c3ed908e65e
[pspp-builds.git] / src / language / stats / regression-export.h
1 /* PSPP - Comments for C files generated by REGRESSION's EXPORT subcommand.
2    Copyright (C) 2005 Free Software Foundation, Inc.
3    Written by Jason H Stover <jason@sakla.net>.
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., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 /*
21   Exported C code for a regression model. The EXPORT subcommand causes PSPP
22   to save a model as a small C program. This file contains some of the code
23   of that saved program.
24  */
25 #ifndef REG_EXPORT_COMMENTS_H
26 #define REG_EXPORT_COMMENTS_H
27 const char reg_header[] = "#ifndef REG_EXPORT_COMMENTS_H\n#define REG_EXPORT_COMMENTS_H\n"
28 "double pspp_reg_estimate (const double *, const char *[]);\n\n"
29 "double pspp_reg_variance (const double *var_vals, const char *[]);\n\n"
30 "double pspp_reg_confidence_interval_U "
31 "(const double *var_vals, const char *var_names[], double p);\n\n"
32 "double pspp_reg_confidence_interval_L "
33 "(const double *var_vals, const char *var_names[], double p);\n\n"
34 "double pspp_reg_prediction_interval_U "
35 "(const double *var_vals, const char *var_names[], double p);\n\n"
36 "double pspp_reg_prediction_interval_L "
37 "(const double *var_vals, const char *var_names[], double p);\n"
38 "#endif\n";
39
40 const char reg_preamble[] =  "/*\n   This program contains functions which return estimates\n"
41 "   and confidence intervals for a linear model. The EXPORT subcommand\n"
42 "   of the REGRESSION procedure of GNU PSPP generated this program.\n*/\n\n"
43 "#include <string.h>\n#include <math.h>\n#define PSPP_REG_MAXLEN 1024\n\n";
44
45 const char reg_mean_cmt[] =  "/*\n   Estimate the mean of Y, the dependent variable for\n"
46 "   the linear model of the form \n\n"
47 "      Y = b0 + b1 * X1 + b2 * X2 + ... + bk * Xk + error\n\n"
48 "   where X1, ..., Xk are the independent variables\n"
49 "   whose values are stored in var_vals and whose names, \n"
50 "   as known by PSPP, are stored in var_names. The estimated \n"
51 "   regression coefficients (i.e., the estimates of b0,...,bk) \n"
52 "   are stored in model_coeffs.\n*/\n";
53
54 const char reg_getvar[] = "{\n\t\tj = pspp_reg_getvar (var_names[i]);\n"
55 "\t\testimate += var_vals[j] * model_coeffs[j];\n"
56 "\t}\n\t\n\treturn estimate;\n}\n\n"
57 "/*\n    Variance of an estimated mean of this form:\n\t"
58 "Y = b0 + b1 * X1 + ... + bk * Xk\n    where X1,...Xk are the dependent variables,"
59 " stored in\n    var_vals and b0,...,bk are the estimated regression coefficients.\n*/\n"
60 "double\npspp_reg_variance (const double *var_vals, "
61 "const char *var_names[])\n{\n\t";
62
63 const char reg_export_t_quantiles_1[] = "/*\n    Quantiles for the T distribution.\n*/\n"
64 "static int\npspp_reg_t_quantile "
65 "(double prob)\n{\n\n\tint i;\n\tdouble quantiles[] = {\n\t\t";
66
67 const char reg_export_t_quantiles_2[] = "i = (int) 100.0 * prob;\n\treturn quantiles[i];\n}\n";
68
69 const char reg_variance[] = "double result = 0.0;\n\n\tfor(i = 0; i < n_vars; i++)\n\t"
70 "{\n\t\tj = pspp_reg_getvar (var_names[i]);\n\t\t"
71 "unshuffled_vals[j] = var_vals[i];\n\t}\n\t"
72 "for (i = 0; i < n_vars; i++)\n\t"
73 "{\n\t\tresult += cov[i][i] * unshuffled_vals[i] * unshuffled_vals[i];\n\t\t"
74 "for (j = i + 1; j < n_vars; j++)\n\t\t{\n\t\t\t"
75 "result += 2.0 * cov[i][j] * unshuffled_vals[i] * unshuffled_vals[j];"
76 "\n\t\t}\n\t}\n\treturn result;\n}\n";
77
78 const char reg_export_confidence_interval[] = "/*\n    Upper confidence limit for an "
79 "estimated mean b0 + b1 * X1 + ... + bk * Xk.\n    The confidence interval is a "
80 "100 * p percent confidence interval.\n*/\n"
81 "double pspp_reg_confidence_interval_U "
82 "(const double *var_vals, const char *var_names[], double p)\n{\n\t"
83 "double result;\n\t"
84 "result = sqrt (pspp_reg_variance (var_vals, var_names));\n\t"
85 "result *= pspp_reg_t_quantile ((1.0 + p) / 2.0);\n\t"
86 "result += pspp_reg_estimate (var_vals, var_names);\n\treturn result;\n}\n"
87 "/*\n    Lower confidence limit for an "
88 "estimated mean b0 + b1 * X1 + ... + bk * Xk.\n    The confidence interval is a "
89 "100 * p percent confidence interval.\n*/\n"
90 "double pspp_reg_confidence_interval_L "
91 "(const double *var_vals, const char *var_names[], double p)\n{\n\t"
92 "double result;\n\t"
93 "result = -sqrt (pspp_reg_variance (var_vals, var_names));\n\t"
94 "result *= pspp_reg_t_quantile ((1.0 + p) / 2.0);\n\t"
95 "result += pspp_reg_estimate (var_vals, var_names);\n\treturn result;\n}\n";
96
97 const char reg_export_prediction_interval_1[] = "/*\n    Upper prediction limit for a "
98 "predicted value b0 + b1 * X1 + ... + bk * Xk.\n    The prediction interval is a "
99 "100 * p percent prediction interval.\n*/\n"
100 "double pspp_reg_prediction_interval_U "
101 "(const double *var_vals, const char *var_names[], double p)\n{\n\t"
102 "double result;\n\tresult = sqrt (";
103
104 const char reg_export_prediction_interval_2[] = " + pspp_reg_variance (var_vals, var_names));\n"
105 "\tresult *= pspp_reg_t_quantile ((1.0 + p) / 2.0);\n\t"
106 "result += pspp_reg_estimate (var_vals, var_names);\n\treturn result;\n}\n"
107 "/*\n    Lower prediction limit for a "
108 "predicted value b0 + b1 * X1 + ... + bk * Xk.\n    The prediction interval is a "
109 "100 * p percent prediction interval.\n*/\n"
110 "double pspp_reg_prediction_interval_L "
111 "(const double *var_vals, const char *var_names[], double p)\n{\n\t"
112 "double result;\n\t"
113 "result = -sqrt (";
114
115 const char reg_export_prediction_interval_3[] = " + pspp_reg_variance (var_vals, var_names));"
116 "\n\tresult *= pspp_reg_t_quantile ((1.0 + p) / 2.0);\n\t"
117 "result += pspp_reg_estimate (var_vals, var_names);\n\treturn result;\n}\n";
118
119 /*
120   Change categorical values to binary vectors. The routine will use
121   an encoding in which a categorical variable with n values is mapped
122   to a vector with n-1 entries. Value 0 is mapped to the zero vector,
123   value 1 is mapped to a vector whose first entry is 1 and all others are
124   0, etc. For example, if a variable can have 'a', 'b' or 'c' as values,
125   then the value 'a' will be encoded as (0,0), 'b' as (1,0) and 'c' as
126   (0,1). If the design matrix used to create the model used a different
127   encoding, then the function pspp_reg_categorical_encode () will return
128   a vector which does not match its categorical value in the model.
129  */
130 const char reg_export_categorical_encode_1[] = "struct pspp_reg_categorical_variable\n"
131 "{\n\tchar * name;\n\tsize_t n_vals;\n\tchar *values[1024];\n};\n\n"
132 "/*\n   This function returns the binary vector which corresponds to the value\n"
133 "   of the categorical variable stored in 'value'. The name of the variable is\n"
134 "   stored in the 'var' argument. Notice the values stored in the\n"
135 "   pspp_categorical_variable structures all end with a space character.\n"
136 "   That means the values of the categorical variables you pass to any function\n"
137 "   in this program should also end with a space character.\n*/\n"
138 "static\ndouble * pspp_reg_get_value_vector (char *var, char *value)\n{\n\tdouble *result;\n\t"
139 "int i;\n\t";
140
141 const char reg_export_categorical_encode_2[] = "int v_index = 0;\n\t"
142 "while (v_index < n_vars && strncmp (var, varlist[i]->name, PSPP_REG_MAXLEN) != 0)\n\t{\n\t\t"
143 "v_index++;\n\t}\n\tresult = (double *) malloc (varlist[v_index]->n_vals * sizeof (*result));\n\t"
144 "for (i = 0; i < varlist[v_index]->n_vals; i++)\n\t{\n\t\t"
145 "if (strncmp ( (varlist[v_index]->values)[i], value, PSPP_REG_MAXLEN) == 0)\n\t\t{\n\t\t\t"
146 "result[i] = 1.0;\n\t\t}\n\t\telse result[i] = 0.0;\n\t}\n\n\t"
147 "return result;\n}\n\n";
148 #endif