a9583d7bf0b68402fed44d3176f338cd549f17d9
[pspp-builds.git] / src / 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
44 const char reg_mean_cmt[] =  "/*\n   Estimate the mean of Y, the dependent variable for\n"
45 "   the linear model of the form \n\n"
46 "      Y = b0 + b1 * X1 + b2 * X2 + ... + bk * Xk + error\n\n"
47 "   where X1, ..., Xk are the independent variables\n"
48 "   whose values are stored in var_vals and whose names, \n"
49 "   as known by PSPP, are stored in var_names. The estimated \n"
50 "   regression coefficients (i.e., the estimates of b0,...,bk) \n"
51 "   are stored in model_coeffs.\n*/\n";
52
53 const char reg_getvar[] = "{\n\t\tj = pspp_reg_getvar (var_names[i]);\n"
54 "\t\testimate += var_vals[j] * model_coeffs[j];\n"
55 "\t}\n\t\n\treturn estimate;\n}\n\n"
56 "/*\n    Variance of an estimated mean of this form:\n\t"
57 "Y = b0 + b1 * X1 + ... + bk * Xk\n    where X1,...Xk are the dependent variables,"
58 " stored in\n    var_vals and b0,...,bk are the estimated regression coefficients.\n*/\n"
59 "double\npspp_reg_variance (const double *var_vals, "
60 "const char *var_names[])\n{\n\t";
61
62 const char reg_export_t_quantiles_1[] = "/*\n    Quantiles for the T distribution.\n*/\n"
63 "static int\npspp_reg_t_quantile "
64 "(double prob)\n{\n\n\tint i;\n\tdouble quantiles[] = {\n\t\t";
65
66 const char reg_export_t_quantiles_2[] = "i = (int) 100.0 * prob;\n\treturn quantiles[i];\n}\n";
67
68 const char reg_variance[] = "double result = 0.0;\n\n\tfor(i = 0; i < n_vars; i++)\n\t"
69 "{\n\t\tj = pspp_reg_getvar (var_names[i]);\n\t\t"
70 "unshuffled_vals[j] = var_vals[i];\n\t}\n\t"
71 "for (i = 0; i < n_vars; i++)\n\t"
72 "{\n\t\tresult += cov[i][i] * unshuffled_vals[i] * unshuffled_vals[i];\n\t\t"
73 "for (j = i + 1; j < n_vars; j++)\n\t\t{\n\t\t\t"
74 "result += 2.0 * cov[i][j] * unshuffled_vals[i] * unshuffled_vals[j];"
75 "\n\t\t}\n\t}\n\treturn result;\n}\n";
76
77 const char reg_export_confidence_interval[] = "/*\n    Upper confidence limit for an "
78 "estimated mean b0 + b1 * X1 + ... + bk * Xk.\n    The confidence interval is a "
79 "100 * p percent confidence interval.\n*/\n"
80 "double pspp_reg_confidence_interval_U "
81 "(const double *var_vals, const char *var_names[], double p)\n{\n\t"
82 "double result;\n\t"
83 "result = sqrt (pspp_reg_variance (var_vals, var_names));\n\t"
84 "result *= pspp_reg_t_quantile ((1.0 + p) / 2.0);\n\t"
85 "result += pspp_reg_estimate (var_vals, var_names);\n\treturn result;\n}\n"
86 "/*\n    Lower confidence limit for an "
87 "estimated mean b0 + b1 * X1 + ... + bk * Xk.\n    The confidence interval is a "
88 "100 * p percent confidence interval.\n*/\n"
89 "double pspp_reg_confidence_interval_L "
90 "(const double *var_vals, const char *var_names[], double p)\n{\n\t"
91 "double result;\n\t"
92 "result = -sqrt (pspp_reg_variance (var_vals, var_names));\n\t"
93 "result *= pspp_reg_t_quantile ((1.0 + p) / 2.0);\n\t"
94 "result += pspp_reg_estimate (var_vals, var_names);\n\treturn result;\n}\n";
95
96 const char reg_export_prediction_interval_1[] = "/*\n    Upper prediction limit for a "
97 "predicted value b0 + b1 * X1 + ... + bk * Xk.\n    The prediction interval is a "
98 "100 * p percent prediction interval.\n*/\n"
99 "double pspp_reg_prediction_interval_U "
100 "(const double *var_vals, const char *var_names[], double p)\n{\n\t"
101 "double result;\n\tresult = sqrt (";
102
103 const char reg_export_prediction_interval_2[] = " + pspp_reg_variance (var_vals, var_names));\n"
104 "\tresult *= pspp_reg_t_quantile ((1.0 + p) / 2.0);\n\t"
105 "result += pspp_reg_estimate (var_vals, var_names);\n\treturn result;\n}\n"
106 "/*\n    Lower prediction limit for a "
107 "predicted value b0 + b1 * X1 + ... + bk * Xk.\n    The prediction interval is a "
108 "100 * p percent prediction interval.\n*/\n"
109 "double pspp_reg_prediction_interval_L "
110 "(const double *var_vals, const char *var_names[], double p)\n{\n\t"
111 "double result;\n\t"
112 "result = -sqrt (";
113
114 const char reg_export_prediction_interval_3[] = " + pspp_reg_variance (var_vals, var_names));"
115 "\n\tresult *= pspp_reg_t_quantile ((1.0 + p) / 2.0);\n\t"
116 "result += pspp_reg_estimate (var_vals, var_names);\n\treturn result;\n}\n";
117
118 /*
119   Change categorical values to binary vectors. The routine will use
120   an encoding in which a categorical variable with n values is mapped
121   to a vector with n-1 entries. Value 0 is mapped to the zero vector,
122   value 1 is mapped to a vector whose first entry is 1 and all others are
123   0, etc. For example, if a variable can have 'a', 'b' or 'c' as values,
124   then the value 'a' will be encoded as (0,0), 'b' as (1,0) and 'c' as
125   (0,1). If the design matrix used to create the model used a different
126   encoding, then the function pspp_reg_categorical_encode () will return
127   a vector which does not match its categorical value in the model.
128  */
129 const char reg_export_categorical_encode_1[] = "struct pspp_reg_categorical_variable\n"
130 "{\n\tchar * name;\n\tsize_t n_vals;\n\tchar *values[1024];\n};\n\n"
131 "/*\n   This function returns the binary vector which corresponds to the value\n"
132 "   of the categorical variable stored in 'value'. The name of the variable is\n"
133 "   stored in the 'var' argument. Notice the values stored in the\n"
134 "   pspp_categorical_variable structures all end with a space character.\n"
135 "   That means the values of the categorical variables you pass to any function\n"
136 "   in this program should also end with a space character.\n*/\n"
137 "static\ndouble * pspp_reg_get_value_vector (char *var, char *value)\n{\n\tdouble *result;\n\t"
138 "int i;\n\t";
139
140 const char reg_export_categorical_encode_2[] = "int v_index = 0;\n\t"
141 "while (v_index < n_vars && strcmp (var, varlist[i]->name) != 0)\n\t{\n\t\t"
142 "v_index++;\n\t}\n\tresult = (double *) malloc (varlist[v_index]->n_vals * sizeof (*result));\n\t"
143 "for (i = 0; i < varlist[v_index]->n_vals; i++)\n\t{\n\t\t"
144 "if (strcmp ( (varlist[v_index]->values)[i], value) == 0)\n\t\t{\n\t\t\t"
145 "result[i] = 1.0;\n\t\t}\n\t\telse result[i] = 0.0;\n\t}\n\n\t"
146 "return result;\n}\n\n";
147 #endif