-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathbootstrap.c
More file actions
110 lines (90 loc) · 2.59 KB
/
bootstrap.c
File metadata and controls
110 lines (90 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
The following routines are written and tested by Stefano Merler
for
bootstrap, probabily based, samples estraction
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <grass/gis.h>
#include "global.h"
void Bootsamples(int n, double *prob, int *random_labels)
/*
given an array of probabilities of length n, extract a bootstrap sample
of n elements according to the vector of probabilities
*/
{
int i, j;
int *random_labels_flag;
double *random;
double *cumprob;
double probtot = .0;
for (i = 0; i < n; i++)
probtot += prob[i];
for (i = 0; i < n; i++)
prob[i] /= probtot;
random_labels_flag = (int *)G_calloc(n, sizeof(int));
random = (double *)G_calloc(n, sizeof(double));
cumprob = (double *)G_calloc(n, sizeof(double));
for (i = 0; i < n; ++i) {
random[i] = G_drand48();
random_labels[i] = n - 1;
random_labels_flag[i] = 0;
}
for (i = 0; i < n; i++) {
if (i > 0)
cumprob[i] = cumprob[i - 1] + prob[i];
else
cumprob[0] = prob[0];
for (j = 0; j < n; j++) {
if (random[j] < cumprob[i])
if (random_labels_flag[j] == 0) {
random_labels[j] = i;
random_labels_flag[j] = 1;
}
}
}
G_free(random);
G_free(cumprob);
G_free(random_labels_flag);
}
void Bootsamples_rseed(int n, double *prob, int *random_labels, int *idum)
/*
given an array of probabilities of length n, extract a bootstrap sample
of n elements according to the vector of probabilities
*/
{
int i, j;
int *random_labels_flag;
double *random;
double *cumprob;
double probtot = .0;
for (i = 0; i < n; i++)
probtot += prob[i];
for (i = 0; i < n; i++)
prob[i] /= probtot;
random_labels_flag = (int *)G_calloc(n, sizeof(int));
random = (double *)G_calloc(n, sizeof(double));
cumprob = (double *)G_calloc(n, sizeof(double));
for (i = 0; i < n; ++i) {
random[i] = (double)ran1(idum);
random_labels[i] = n - 1;
random_labels_flag[i] = 0;
}
for (i = 0; i < n; i++) {
if (i > 0)
cumprob[i] = cumprob[i - 1] + prob[i];
else
cumprob[0] = prob[0];
for (j = 0; j < n; j++) {
if (random[j] < cumprob[i])
if (random_labels_flag[j] == 0) {
random_labels[j] = i;
random_labels_flag[j] = 1;
}
}
}
G_free(random);
G_free(cumprob);
G_free(random_labels_flag);
}