| 1 |
#include "S.h" |
| 2 |
#include "loess.h" |
| 3 |
#include <stdio.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
|
| 8 |
static char *surf_stat; |
| 9 |
|
| 10 |
int error_status = 0; |
| 11 |
char *error_message = NULL; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
static void |
| 16 |
loess_(double *y, double *x_, int *size_info, double *weights, double *span, |
| 17 |
int *degree, int *parametric, int *drop_square, int *normalize, |
| 18 |
char **statistics, char **surface, double *cell, char **trace_hat_in, |
| 19 |
int *iterations, double *fitted_values, double *fitted_residuals, |
| 20 |
double *enp, double *residual_scale, double *one_delta, double *two_delta, |
| 21 |
double *pseudovalues, double *trace_hat_out, double *diagonal, |
| 22 |
double *robust, double *divisor, int *parameter, int *a, double *xi, |
| 23 |
double *vert, double *vval); |
| 24 |
|
| 25 |
void F77_SUB(lowesw)(double*, int*, double*, double*); |
| 26 |
void F77_SUB(lowesp)(int*, double*, double*, double*, |
| 27 |
double*, double*, double*); |
| 28 |
|
| 29 |
static void |
| 30 |
condition(char **surface, char *new_stat, char **trace_hat_in) |
| 31 |
{ |
| 32 |
if(!strcmp(*surface, "interpolate")) { |
| 33 |
if(!strcmp(new_stat, "none")) |
| 34 |
surf_stat = "interpolate/none"; |
| 35 |
else if(!strcmp(new_stat, "exact")) |
| 36 |
surf_stat = "interpolate/exact"; |
| 37 |
else if(!strcmp(new_stat, "approximate")) |
| 38 |
{ |
| 39 |
if(!strcmp(*trace_hat_in, "approximate")) |
| 40 |
surf_stat = "interpolate/2.approx"; |
| 41 |
else if(!strcmp(*trace_hat_in, "exact")) |
| 42 |
surf_stat = "interpolate/1.approx"; |
| 43 |
} |
| 44 |
} |
| 45 |
else if(!strcmp(*surface, "direct")) { |
| 46 |
if(!strcmp(new_stat, "none")) |
| 47 |
surf_stat = "direct/none"; |
| 48 |
else if(!strcmp(new_stat, "exact")) |
| 49 |
surf_stat = "direct/exact"; |
| 50 |
else if(!strcmp(new_stat, "approximate")) |
| 51 |
surf_stat = "direct/approximate"; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
int |
| 56 |
comp(const void *d1, const void *d2) |
| 57 |
{ |
| 58 |
double *_d1 = (double *)d1; |
| 59 |
double *_d2 = (double *)d2; |
| 60 |
|
| 61 |
if(*_d1 < *_d2) |
| 62 |
return(-1); |
| 63 |
else if(*_d1 == *_d2) |
| 64 |
return(0); |
| 65 |
else |
| 66 |
return(1); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
void |
| 71 |
loess_model_setup(loess_model *model) { |
| 72 |
int i; |
| 73 |
|
| 74 |
model->span = 0.75; |
| 75 |
model->degree = 2; |
| 76 |
model->normalize = TRUE; |
| 77 |
|
| 78 |
for(i = 0; i < 8; i++) { |
| 79 |
model->parametric[i] = FALSE; |
| 80 |
model->drop_square[i] = FALSE; |
| 81 |
} |
| 82 |
|
| 83 |
model->family = "gaussian"; |
| 84 |
} |
| 85 |
|
| 86 |
void |
| 87 |
loess_inputs_setup(double *x, double *y, double *w, long n, |
| 88 |
long p, loess_inputs *inputs) { |
| 89 |
int i; |
| 90 |
|
| 91 |
inputs->y = MALLOC(n * sizeof(double)); |
| 92 |
inputs->x = MALLOC(n * p * sizeof(double)); |
| 93 |
inputs->weights = MALLOC(n * sizeof(double)); |
| 94 |
|
| 95 |
for(i = 0; i < (n * p); i++) { |
| 96 |
inputs->x[i] = x[i]; |
| 97 |
} |
| 98 |
|
| 99 |
for(i = 0; i < n; i++) { |
| 100 |
inputs->y[i] = y[i]; |
| 101 |
inputs->weights[i] = w[i]; |
| 102 |
} |
| 103 |
|
| 104 |
inputs->n = n; |
| 105 |
inputs->p = p; |
| 106 |
} |
| 107 |
|
| 108 |
void loess_outputs_setup(long n, long p, loess_outputs *outputs) { |
| 109 |
outputs->fitted_values = MALLOC(n * sizeof(double)); |
| 110 |
outputs->fitted_residuals = MALLOC(n * sizeof(double)); |
| 111 |
outputs->diagonal = MALLOC(n * sizeof(double)); |
| 112 |
outputs->robust = MALLOC(n * sizeof(double)); |
| 113 |
outputs->divisor = MALLOC(p * sizeof(double)); |
| 114 |
outputs->pseudovalues = MALLOC(n * sizeof(double)); |
| 115 |
} |
| 116 |
|
| 117 |
void |
| 118 |
loess_kd_tree_setup(long n, long p, loess_kd_tree *kd_tree) { |
| 119 |
int max_kd; |
| 120 |
|
| 121 |
max_kd = n > 200 ? n : 200; |
| 122 |
|
| 123 |
kd_tree->parameter = MALLOC(7 * sizeof(int)); |
| 124 |
kd_tree->a = MALLOC(max_kd * sizeof(int)); |
| 125 |
kd_tree->xi = MALLOC(max_kd * sizeof(double)); |
| 126 |
kd_tree->vert = MALLOC(p * 2 * sizeof(double)); |
| 127 |
kd_tree->vval = MALLOC((p + 1) * max_kd * sizeof(double)); |
| 128 |
} |
| 129 |
|
| 130 |
void |
| 131 |
loess_control_setup(loess_control *control) { |
| 132 |
control->surface = "interpolate"; |
| 133 |
control->statistics = "approximate"; |
| 134 |
control->cell = 0.2; |
| 135 |
control->trace_hat = "wait.to.decide"; |
| 136 |
control->iterations = 4; |
| 137 |
} |
| 138 |
|
| 139 |
void |
| 140 |
loess_setup(double *x, double *y, double *w, long n, long p, loess *lo) |
| 141 |
{ |
| 142 |
loess_inputs_setup(x, y, w, n, p, lo->inputs); |
| 143 |
loess_model_setup(lo->model); |
| 144 |
loess_control_setup(lo->control); |
| 145 |
loess_outputs_setup(n, p, lo->outputs); |
| 146 |
loess_kd_tree_setup(n, p, lo->kd_tree); |
| 147 |
} |
| 148 |
|
| 149 |
static void |
| 150 |
loess_(double *y, double *x_, int *size_info, double *weights, double *span, |
| 151 |
int *degree, int *parametric, int *drop_square, int *normalize, |
| 152 |
char **statistics, char **surface, double *cell, char **trace_hat_in, |
| 153 |
int *iterations, double *fitted_values, double *fitted_residuals, |
| 154 |
double *enp, double *residual_scale, double *one_delta, double *two_delta, |
| 155 |
double *pseudovalues, double *trace_hat_out, double *diagonal, |
| 156 |
double *robust, double *divisor, int *parameter, int *a, double *xi, |
| 157 |
double *vert, double *vval) |
| 158 |
{ |
| 159 |
double *x, *x_tmp, new_cell, trL, delta1, delta2, sum_squares = 0, |
| 160 |
pseudo_resid, *temp, *xi_tmp, *vert_tmp, *vval_tmp, |
| 161 |
*diag_tmp, trL_tmp = 0, d1_tmp = 0, d2_tmp = 0, sum, mean; |
| 162 |
int i, j, k, p, N, D, sum_drop_sqr = 0, sum_parametric = 0, |
| 163 |
setLf, nonparametric = 0, *order_parametric, |
| 164 |
*order_drop_sqr, zero = 0, max_kd; |
| 165 |
int *param_tmp, *a_tmp; |
| 166 |
int cut; |
| 167 |
char *new_stat; |
| 168 |
|
| 169 |
D = size_info[0]; |
| 170 |
N = size_info[1]; |
| 171 |
max_kd = (N > 200 ? N : 200); |
| 172 |
*one_delta = *two_delta = *trace_hat_out = 0; |
| 173 |
|
| 174 |
x = MALLOC(D * N * sizeof(double)); |
| 175 |
x_tmp = MALLOC(D * N * sizeof(double)); |
| 176 |
temp = MALLOC(N * sizeof(double)); |
| 177 |
a_tmp = MALLOC(max_kd * sizeof(int)); |
| 178 |
xi_tmp = MALLOC(max_kd * sizeof(double)); |
| 179 |
vert_tmp = MALLOC(D * 2 * sizeof(double)); |
| 180 |
vval_tmp = MALLOC((D + 1) * max_kd * sizeof(double)); |
| 181 |
diag_tmp = MALLOC(N * sizeof(double)); |
| 182 |
param_tmp = MALLOC(N * sizeof(int)); |
| 183 |
order_parametric = MALLOC(D * sizeof(int)); |
| 184 |
order_drop_sqr = MALLOC(D * sizeof(int)); |
| 185 |
|
| 186 |
new_cell = (*span) * (*cell); |
| 187 |
for(i = 0; i < N; i++) |
| 188 |
robust[i] = 1; |
| 189 |
|
| 190 |
for(i = 0; i < (N * D); i++) |
| 191 |
x_tmp[i] = x_[i]; |
| 192 |
|
| 193 |
if((*normalize) && (D > 1)) { |
| 194 |
cut = ceil(0.100000000000000000001 * N); |
| 195 |
for(i = 0; i < D; i++) { |
| 196 |
k = i * N; |
| 197 |
for(j = 0; j < N; j++) |
| 198 |
temp[j] = x_[k + j]; |
| 199 |
qsort(temp, N, sizeof(double), comp); |
| 200 |
sum = 0; |
| 201 |
for(j = cut; j <= (N - cut - 1); j++) |
| 202 |
sum = sum + temp[j]; |
| 203 |
mean = sum / (N - 2 * cut); |
| 204 |
sum = 0; |
| 205 |
for(j = cut; j <= (N - cut - 1); j++) { |
| 206 |
temp[j] = temp[j] - mean; |
| 207 |
sum = sum + temp[j] * temp[j]; |
| 208 |
} |
| 209 |
divisor[i] = sqrt(sum / (N - 2 * cut - 1)); |
| 210 |
for(j = 0; j < N; j++) { |
| 211 |
p = k + j; |
| 212 |
x_tmp[p] = x_[p] / divisor[i]; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
else |
| 217 |
for(i = 0; i < D; i++) divisor[i] = 1; |
| 218 |
|
| 219 |
j = D - 1; |
| 220 |
for(i = 0; i < D; i++) { |
| 221 |
sum_drop_sqr = sum_drop_sqr + drop_square[i]; |
| 222 |
sum_parametric = sum_parametric + parametric[i]; |
| 223 |
if(parametric[i]) |
| 224 |
order_parametric[j--] = i; |
| 225 |
else |
| 226 |
order_parametric[nonparametric++] = i; |
| 227 |
} |
| 228 |
|
| 229 |
for(i = 0; i < D; i++) { |
| 230 |
order_drop_sqr[i] = 2 - drop_square[order_parametric[i]]; |
| 231 |
k = i * N; |
| 232 |
p = order_parametric[i] * N; |
| 233 |
for(j = 0; j < N; j++) |
| 234 |
x[k + j] = x_tmp[p + j]; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
if((*degree) == 1 && sum_drop_sqr) { |
| 239 |
error_status = 1; |
| 240 |
error_message = "Specified the square of a factor predictor to be "\ |
| 241 |
"dropped when degree = 1"; |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
if(D == 1 && sum_drop_sqr) { |
| 246 |
error_status = 1; |
| 247 |
error_message = "Specified the square of a predictor to be dropped "\ |
| 248 |
"with only one numeric predictor"; |
| 249 |
return; |
| 250 |
} |
| 251 |
|
| 252 |
if(sum_parametric == D) { |
| 253 |
error_status = 1; |
| 254 |
error_message = "Specified parametric for all predictors"; |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
for(j = 0; j <= (*iterations); j++) { |
| 260 |
new_stat = j ? "none" : *statistics; |
| 261 |
for(i = 0; i < N; i++) |
| 262 |
robust[i] = weights[i] * robust[i]; |
| 263 |
condition(surface, new_stat, trace_hat_in); |
| 264 |
setLf = !strcmp(surf_stat, "interpolate/exact"); |
| 265 |
loess_raw(y, x, weights, robust, &D, &N, span, degree, |
| 266 |
&nonparametric, order_drop_sqr, &sum_drop_sqr, |
| 267 |
&new_cell, &surf_stat, fitted_values, parameter, a, |
| 268 |
xi, vert, vval, diagonal, &trL, &delta1, &delta2, |
| 269 |
&setLf); |
| 270 |
if(j == 0) { |
| 271 |
*trace_hat_out = trL; |
| 272 |
*one_delta = delta1; |
| 273 |
*two_delta = delta2; |
| 274 |
} |
| 275 |
for(i = 0; i < N; i++){ |
| 276 |
fitted_residuals[i] = y[i] - fitted_values[i]; |
| 277 |
}; |
| 278 |
if(j < (*iterations)) |
| 279 |
F77_SUB(lowesw)(fitted_residuals, &N, robust, temp); |
| 280 |
} |
| 281 |
|
| 282 |
if((*iterations) > 0) { |
| 283 |
F77_SUB(lowesp)(&N, y, fitted_values, weights, robust, temp, |
| 284 |
pseudovalues); |
| 285 |
loess_raw(pseudovalues, x, weights, weights, &D, &N, span, |
| 286 |
degree, &nonparametric, order_drop_sqr, &sum_drop_sqr, |
| 287 |
&new_cell, &surf_stat, temp, param_tmp, a_tmp, xi_tmp, |
| 288 |
vert_tmp, vval_tmp, diag_tmp, &trL_tmp, &d1_tmp, &d2_tmp, |
| 289 |
&zero); |
| 290 |
for(i = 0; i < N; i++) { |
| 291 |
pseudo_resid = pseudovalues[i] - temp[i]; |
| 292 |
sum_squares = sum_squares + weights[i] * pseudo_resid * pseudo_resid; |
| 293 |
} |
| 294 |
} else { |
| 295 |
for(i = 0; i < N; i++) |
| 296 |
sum_squares = sum_squares + weights[i] * |
| 297 |
fitted_residuals[i] * fitted_residuals[i]; |
| 298 |
} |
| 299 |
|
| 300 |
*enp = (*one_delta) + 2 * (*trace_hat_out) - N; |
| 301 |
*residual_scale = sqrt(sum_squares / (*one_delta)); |
| 302 |
|
| 303 |
|
| 304 |
free(x); |
| 305 |
free(x_tmp); |
| 306 |
free(temp); |
| 307 |
free(xi_tmp); |
| 308 |
free(vert_tmp); |
| 309 |
free(vval_tmp); |
| 310 |
free(diag_tmp); |
| 311 |
free(a_tmp); |
| 312 |
free(param_tmp); |
| 313 |
free(order_parametric); |
| 314 |
free(order_drop_sqr); |
| 315 |
} |
| 316 |
|
| 317 |
void |
| 318 |
loess_fit(loess *lo) |
| 319 |
{ |
| 320 |
int size_info[2], iterations; |
| 321 |
|
| 322 |
size_info[0] = lo->inputs->p; |
| 323 |
size_info[1] = lo->inputs->n; |
| 324 |
|
| 325 |
|
| 326 |
error_status = 0; |
| 327 |
lo->status.err_status = 0; |
| 328 |
lo->status.err_msg = NULL; |
| 329 |
|
| 330 |
iterations = (!strcmp(lo->model->family, "gaussian")) ? 0 : |
| 331 |
lo->control->iterations; |
| 332 |
if(!strcmp(lo->control->trace_hat, "wait.to.decide")) { |
| 333 |
if(!strcmp(lo->control->surface, "interpolate")) |
| 334 |
lo->control->trace_hat = (lo->inputs->n < 500) ? "exact" : "approximate"; |
| 335 |
else |
| 336 |
lo->control->trace_hat = "exact"; |
| 337 |
} |
| 338 |
loess_(lo->inputs->y, lo->inputs->x, size_info, lo->inputs->weights, |
| 339 |
&lo->model->span, |
| 340 |
&lo->model->degree, |
| 341 |
lo->model->parametric, |
| 342 |
lo->model->drop_square, |
| 343 |
&lo->model->normalize, |
| 344 |
&lo->control->statistics, |
| 345 |
&lo->control->surface, |
| 346 |
&lo->control->cell, |
| 347 |
&lo->control->trace_hat, |
| 348 |
&iterations, |
| 349 |
lo->outputs->fitted_values, |
| 350 |
lo->outputs->fitted_residuals, |
| 351 |
&lo->outputs->enp, |
| 352 |
&lo->outputs->residual_scale, |
| 353 |
&lo->outputs->one_delta, |
| 354 |
&lo->outputs->two_delta, |
| 355 |
lo->outputs->pseudovalues, |
| 356 |
&lo->outputs->trace_hat, |
| 357 |
lo->outputs->diagonal, |
| 358 |
lo->outputs->robust, |
| 359 |
lo->outputs->divisor, |
| 360 |
lo->kd_tree->parameter, |
| 361 |
lo->kd_tree->a, |
| 362 |
lo->kd_tree->xi, |
| 363 |
lo->kd_tree->vert, |
| 364 |
lo->kd_tree->vval); |
| 365 |
|
| 366 |
if(error_status){ |
| 367 |
lo->status.err_status = error_status; |
| 368 |
lo->status.err_msg = error_message; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
void loess_inputs_free(loess_inputs *inputs) { |
| 374 |
free(inputs->x); |
| 375 |
free(inputs->y); |
| 376 |
free(inputs->weights); |
| 377 |
} |
| 378 |
|
| 379 |
void loess_outputs_free(loess_outputs *outputs) { |
| 380 |
free(outputs->fitted_values); |
| 381 |
free(outputs->fitted_residuals); |
| 382 |
free(outputs->diagonal); |
| 383 |
free(outputs->robust); |
| 384 |
free(outputs->divisor); |
| 385 |
free(outputs->pseudovalues); |
| 386 |
} |
| 387 |
|
| 388 |
void loess_kd_tree_free(loess_kd_tree *kd_tree) { |
| 389 |
free(kd_tree->parameter); |
| 390 |
free(kd_tree->a); |
| 391 |
free(kd_tree->xi); |
| 392 |
free(kd_tree->vert); |
| 393 |
free(kd_tree->vval); |
| 394 |
} |
| 395 |
|
| 396 |
void |
| 397 |
loess_free_mem(loess *lo) |
| 398 |
{ |
| 399 |
loess_inputs_free(lo->inputs); |
| 400 |
loess_outputs_free(lo->outputs); |
| 401 |
loess_kd_tree_free(lo->kd_tree); |
| 402 |
} |
| 403 |
|
| 404 |
void |
| 405 |
loess_summary(loess *lo) |
| 406 |
{ |
| 407 |
printf("Number of Observations : %ld\n", lo->inputs->n); |
| 408 |
printf("Equivalent Number of Parameters: %.1f\n", lo->outputs->enp); |
| 409 |
if(!strcmp(lo->model->family, "gaussian")) |
| 410 |
printf("Residual Standard Error : "); |
| 411 |
else |
| 412 |
printf("Residual Scale Estimate: "); |
| 413 |
printf("%.4f\n", lo->outputs->residual_scale); |
| 414 |
} |
| 415 |
|