-
Notifications
You must be signed in to change notification settings - Fork 0
/
userrb9.c
executable file
·206 lines (174 loc) · 3.8 KB
/
userrb9.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
/* User functions for the Rb9 example in section 5.5.3 of Ph.D. thesis.
Data is for tumour counts is taken from
Haigis and Dove, 2003 (see thesis for full reference). */
#include<stdio.h>
#include<math.h>
#define tpi 6.283185307179586477
extern double loggamma(double x);
extern double sdrand();
/* Hyperparameters */
double alpha1=2.0,alpha2=1.0,beta1=0.1,beta2=2.0;
/* Internal functions used in required user functions */
double boxm();
/* Function to return number of models */
void getkmax(int *kmax){
*kmax=10;
return;
}
/* Function to return the dimension of each model */
void getnk(int kmax,int *nk){
int k;
for(k=0;k<6;k++){
nk[k]=4;
}
for(k=6;k<kmax;k++){
nk[k]=5;
}
return;
}
/* Function to return initial conditions for RWM runs */
void getic(int k, int nkk, double *rwm){
int j,ql,qk;
int nlambda[10],nkappa[10];
double u;
for(j=0;j<10;j++){
nlambda[j]=3;
nkappa[j]=1;
}
nlambda[7]=4;
nlambda[8]=4;
nlambda[9]=4;
nkappa[6]=2;
ql=nlambda[k];
qk=nkappa[k];
for(j=0;j<ql;j++){
u=exp(boxm());
rwm[j]=43.87879*u;
}
for(j=ql;j<ql+qk;j++){
u=exp(boxm());
rwm[j]=2.152937*u;
}
}
/* Function to return log of posterior up to additive const at (k,theta)
likelihood returned in llh1 - prior settings as in Thesis chapter 3 */
double lpost(int k,int nkk,double *theta, double *llh1){
int i,j,ql,qk,cumnobs;
int nobs[4],nlambda[10],nkappa[10],pindic[4];
double lp,llh,lambda[4],kappa[4],dlgdummy1,dlgdummy2,kappamin1;
int X[66]={121,169,112,199,80,121,194,140,131,199,262,
121,140,166,150,103,5,15,13,9,15,13,13,9,18,
12,8,7,16,11,12,8,14,12,20,12,8,11,10,10,10,
7,8,7,8,10,11,7,4,6,9,7,5,7,3,7,4,11,15,10,
6,10,6,12,6,11};
for(i=0;i<nkk;i++){
if(theta[i]<0){
lp=-1000000.0;
return lp;
}
}
nobs[0]=16;
nobs[1]=17;
nobs[2]=15;
nobs[3]=18;
for(j=0;j<10;j++){
nlambda[j]=3;
nkappa[j]=1;
}
nlambda[7]=4;
nlambda[8]=4;
nlambda[9]=4;
nkappa[6]=2;
ql=nlambda[k];
qk=nkappa[k];
pindic[0]=1;
pindic[1]=0;
pindic[2]=0;
pindic[3]=1;
if(k==3||k==9){
pindic[1]=1;
}
if(k==2||k==9){
pindic[2]=1;
}
if(k==0||k==4||k==7){
pindic[3]=0;
}
lambda[0]=theta[0];
lambda[1]=theta[1];
if(k<4||k==6){
lambda[2]=theta[1];
}
else{
lambda[2]=theta[2];
}
if(k<7){
lambda[3]=theta[2];
}
else{
lambda[3]=theta[3];
}
if(k<7){
kappa[0]=theta[3];
}
else{
kappa[0]=theta[4];
}
/* know next 3 lines aren't always true but for cases which
it is wrong don't use the incorrect values of kappa */
kappa[1]=kappa[0];
kappa[2]=kappa[0];
kappa[3]=kappa[0];
if(k==6){
kappa[3]=theta[4];
}
/* prior */
lp=0.0;
for(i=0;i<ql;i++){
lp+=alpha1*log(beta1)+(alpha1-1.0)*log(theta[i])
-beta1*theta[i]-loggamma(alpha1);
}
for(i=ql;i<ql+qk;i++){
lp+=alpha2*log(beta2)+(alpha2-1.0)*log(theta[i])-
beta2*theta[i]-loggamma(alpha2);
}
/* likelihood */
llh=0.0;
cumnobs=0;
for(i=0;i<4;i++){
if(pindic[i]==0){
/*Poisson*/
for(j=0;j<nobs[i];j++){
dlgdummy1=X[cumnobs]+1;
llh+=(-lambda[i]+X[cumnobs]*log(lambda[i])-loggamma(dlgdummy1));
cumnobs++;
}
}
else{
/* Negative Binomial */
kappamin1=1.0/kappa[i];
for(j=0;j<nobs[i];j++){
dlgdummy1=X[cumnobs]+1;
dlgdummy2=X[cumnobs]+kappamin1;
llh+=(X[cumnobs]*log(lambda[i])+loggamma(dlgdummy2)-
loggamma(dlgdummy1)+kappamin1*log(kappamin1)-
loggamma(kappamin1)-(X[cumnobs]+kappamin1)*
log(lambda[i]+kappamin1));
cumnobs++;
}
}
}
*llh1=llh;
lp+=llh;
return lp;
}
double boxm(){
/*Function for returning single N(0,1) variable */
double u1,u2,out;
u1=sdrand();
u2=sdrand();
u1=tpi*u1;
u2=sqrt(-2.0*log(u2));
out=u2*cos(u1);
return out;
}