-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregmodels.cpp
More file actions
225 lines (189 loc) · 4.77 KB
/
Copy pathregmodels.cpp
File metadata and controls
225 lines (189 loc) · 4.77 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
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
/*
FILE: REGMODELS.CPP
*/
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <float.h>
#include "regmodels.h"
//tests if two vectors are equal
//we assume that the two vectors are sorted
int sameregression(int lenA1,int* A1,int lenA2,int* A2)
{
int i;
if(lenA1!=lenA2)
{
return 0;
}
//the two vectors have the same length
//are their elements equal?
for(i=0;i<lenA1;i++)
{
if(A1[i]!=A2[i])
{
return 0;
}
}
return 1;
}
// this function checks whether our new regression is in the top "nMaxRegs"
// based on marginal likelihood. If the new model is better than any of the
// existing regressions in the list, it is added to the list
// and the worst regression is discarded. Here "regressions" represents
// the head of the list, "lenA" is the number of predictors
// and "logmarglikA" is the marginal likelihood of the regression
// with predictors A.
void AddRegression(int nMaxRegs, LPRegression regressions, int lenA, int* A, gsl_matrix* beta, double MC, double laplace)
{
int i, j = 0;
LPRegression p = regressions;
LPRegression pnext = p->Next;
while(NULL != pnext && j < nMaxRegs)
{
//return if we have previously found this regression
if(sameregression(lenA, A, pnext->lenA, pnext->A))
{
return;
}
//go to the next element in the list if the current
//regression has a larger log marginal likelihood than
//the new regression A
if(pnext->MC > MC)
{
p = pnext;
pnext = p->Next;
}
else //otherwise stop; this is where we insert the new regression
{
break;
}
j++;
}
// if we reached "nMaxRegs" we did not beat any of the top 10 with the new regression.
// Otherwise we add it like normal.
if(nMaxRegs == j)
{
return;
}
//create a new element of the list
LPRegression newp = new Regression;
newp->lenA = lenA;
newp->MC = MC;
newp->laplace = laplace;
newp->A = new int[lenA];
newp->beta = new double[lenA + 1];
//copy the predictors
for(i=0;i<lenA;i++)
{
newp->A[i] = A[i];
}
for(i = 0; i < lenA + 1; i++)
{
newp->beta[i] = gsl_matrix_get(beta, i, 0);
}
//insert the new element in the list
p->Next = newp;
newp->Next = pnext;
// now we move through the list until we either reach the end of it, or reach the
// element just after the "nMaxRegs" element.
while(j < nMaxRegs && NULL!=pnext)
{
p = pnext;
pnext = p->Next;
j++;
}
// if we reach nMaxRegs, we have to discard the new worst element in the list.
if(nMaxRegs == j)
{
DeleteLastRegression(regressions);
}
return;
}
//this function deletes all the elements of the list
//with the head "regressions"
//remark that the head is not touched
void DeleteAllRegressions(LPRegression regressions)
{
//this is the first regression
LPRegression p = regressions->Next;
LPRegression pnext;
while(NULL!=p)
{
//save the link to the next element of p
pnext = p->Next;
//delete the element specified by p
//first free the memory of the vector of regressors
delete[] p->A;
p->Next = NULL;
delete p;
//move to the next element
p = pnext;
}
return;
}
//this function deletes the last element of the list
//with the head "regressions"
//again, the head is not touched
void DeleteLastRegression(LPRegression regressions)
{
//this is the element before the first regression
LPRegression pprev = regressions;
//this is the first regression
LPRegression p = regressions->Next;
//if the list does not have any elements, return
if(NULL==p)
{
return;
}
//the last element of the list is the only
//element that has the "Next" field equal to NULL
while(NULL!=p->Next)
{
pprev = p;
p = p->Next;
}
//now "p" should give the last element
//delete it
delete[] p->A;
p->Next = NULL;
delete p;
//now the previous element in the list
//becomes the last element
pprev->Next = NULL;
return;
}
//this function saves the regressions in the list with
//head "regressions" in a file with name "filename"
void SaveRegressions(char* filename,LPRegression regressions)
{
int i;
//open the output file
FILE* out = fopen(filename,"w");
if(NULL==out)
{
printf("Cannot open output file [%s]\n",filename);
exit(1);
}
//this is the first regression
LPRegression p = regressions->Next;
while(NULL!=p)
{
for(i=0;i<p->lenA;i++)
{
fprintf(out,"\t%i",p->A[i]);
}
//print the log marginal likelhood and the number of predictors
fprintf(out,"%.3f\t%.3f",p->MC, p->laplace);
//now save the predictors
for(i=0;i<p->lenA + 1;i++)
{
fprintf(out,"\t%.3f",p->beta[i]);
}
fprintf(out,"\n");
//go to the next regression
p = p->Next;
}
//close the output file
fclose(out);
return;
}