/
isoreg.c
276 lines (209 loc) · 6.24 KB
/
isoreg.c
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* --- BiPAVA ---
* Bidirectional Implementation of Pool Adjacent Violators Algorithm
* By Wail Alkowaileet
* Center for Complex Engineering Systems at KACST and MIT
* w.alkowaileet(at)cces-kacst-mit.org
* FastIsoReg is a modification to the previous implentation to accelorate the process
* of the Isotonic Regression.
* Copyright (C) 2015 Wail Alkowaileet
* ---
* R : A Computer Language for Statistical Data Analysis
* Copyright (C) 2003 The R Foundation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, a copy is available at
* https://www.R-project.org/Licenses/
*/
#include <R.h>
#include "modreg.h"
typedef struct list list;
//LinkedList
struct list
{
list *next;
double avg;
double sum;
int counter;
int blockStartIndex;
int blockEndIndex;
int obselete; //deprecated
};
//Is the element in current block range ?
int checkBoundires(list *node, int index)
{
return (node->blockStartIndex <= index && node->blockEndIndex >= index);
}
//Get the value that is recent (whereas it's stored in the Array or the list)
double getRecentValue(double *x, list *block, int i)
{
double retVal = x[i];
if(block != NULL && checkBoundires(block, i))
retVal = block->avg;
return retVal;
}
//[x]->[y]->[z] After inserting [a] will be: [a]->[x]->[y]->[z]
list* insertFirst(list *head, int index)
{
list *newHead =(list*)malloc(sizeof(list));
newHead->next = head;
newHead->avg = 0;
newHead->sum = 0;
newHead->counter = 0;
newHead->blockEndIndex = newHead->blockStartIndex = index;
return newHead;
}
//Merge two adjacent blocks into one block by:
// 1) Computing the new average.
// 2) combine the ranges.
list* merge(list *block1, list *block2)
{
block1->blockEndIndex = block2->blockEndIndex;
block1->counter += block2->counter;
block1->sum += block2->sum;
block1->avg = block1->sum/block1->counter;
block1->next = block2->next;
free(block2);
}
//
list* addBackwardValue(list* node, double value, int startIndex)
{
node->sum += value;
node->counter++;
node->avg = node->sum/node->counter;
node->blockStartIndex = startIndex;
return node;
}
list* addForwardValue(list* node, double value, int endIndex)
{
if(node->next != NULL && checkBoundires(node->next, endIndex))
{
merge(node, node->next);
}
else
{
node->sum += value;
node->counter++;
node->avg = node->sum/node->counter;
node->blockEndIndex = endIndex;
}
return node;
}
void deleteList(list *head)
{
list *node = head;
list *temp = NULL;
while(node != NULL)
{
temp = node;
node = node->next;
free(temp);
}
}
double *getNewValues(double *x, int n, list *blocks, double *y)
{
int i=0, j;
list *node = blocks;
while(i<n)
{
if(node != NULL && checkBoundires(node, i))
{
for(j=i;j<=node->blockEndIndex;j++)
y[j] = node->avg;
i = node->blockEndIndex+1;
node = node->next;
}
else
{
y[i] = x[i];
i++;
}
}
deleteList(blocks);
return y;
}
// Faster implementation of isoreg
double *PAV(double *x, int n, double *y)
{
//Definitions;
const double DOUBLE_MAX = 1e+200;
const int false = 0;
const int true = 1;
list *blocks = NULL; // list of blocks
int i, j, k, blockStartIndex;
int doAvg = false;
int forward = false;
//start from the last element of the array
i=n-1;
while(i>=1)
{
if(getRecentValue(x, blocks, i) > getRecentValue(x, blocks, i-1) && !doAvg)
{
i--;
continue;
}
//There is a violation
if(!doAvg)
{
blocks = insertFirst(blocks, i);
//Do Backward updating
blockStartIndex = i;
doAvg = true; //Start updating the avgs.
k=i-1; //Violater index
}
if(forward)
{
addForwardValue(blocks, x[i], i);
forward = false;
blockStartIndex = blocks->blockEndIndex;
}
else
addBackwardValue(blocks, x[i], i);
//update backward
for(j=k;j>=0 && doAvg;j--)
{
if(blocks->avg < x[j] )
{
addBackwardValue(blocks, x[j], j);
}
else
doAvg = false;
}
//Move to the first element that has NOT been visited yet
i=j+1;
if (blockStartIndex+1>=n || !(doAvg = getRecentValue(x, blocks->next, blockStartIndex+1) < blocks->avg))
{
continue;
}
//After updating backward, a violation appeared at the end of the block
//Go there and fix it
k=blocks->blockStartIndex-1; //This will hold the first index to look at after finishing the forward update
i=(++blockStartIndex); // First element after the block range
forward = true; // Run forward update
}
return getNewValues(x, n, blocks, y);
}
SEXP isoreg(SEXP y)
{
int n = LENGTH(y), i, ip, known, n_ip;
double tmp, slope;
SEXP yc, yf, iKnots, ans;
const char *anms[] = {"y", "yf", ""};
double *yPAV;
/* unneeded: y = coerceVector(y, REALSXP); */
PROTECT(ans = mkNamed(VECSXP, anms));
SET_VECTOR_ELT(ans, 0, y);
SET_VECTOR_ELT(ans, 1, yf = allocVector(REALSXP, n));
PAV(REAL(y), n, REAL(yf));
UNPROTECT(1);
return(ans);
}