-
Notifications
You must be signed in to change notification settings - Fork 0
/
usertoy2.c
executable file
·84 lines (71 loc) · 1.71 KB
/
usertoy2.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
/* User functions for a second toy example not included in Ph.D. thesis.
The example has 5 equally weighted models.
Model k has dimension k, (k=1,...,5) and is
a mixture of two normals, the first component having weight 0.3, mean
(5,...,5) and cov matrix I_k, the second component having weight 0.7,
mean (-5,...,-5) and cov matrix 4*I_k. */
#include<stdio.h>
#include<math.h>
#define tpi 6.283185307179586477
/* Function to return number of models */
void getkmax(int *kmax){
*kmax=5;
return;
}
/* Function to return the dimension of each model */
void getnk(int kmax,int *nk){
int k;
for(k=0;k<kmax;k++){
nk[k]=k+1;
}
return;
}
/* Function to return initial conditions for RWM runs */
void getic(int k, int nkk, double *theta){
int j;
for(j=0;j<nkk;j++){
theta[j]=0;
}
}
/* Function to return log target distribution up to additive const at (k,theta)
value also returned in llh */
double lpost(int k,int nkk,double *theta, double *llh){
int i;
double work[nkk],mu1[nkk],mu2[nkk],sig1,sig2,w1,w2;
double lp,lptemp,modw;
sig1=1.0;
sig2=2.0;
w1=0.3;
w2=0.7;
if(k<4){
modw=1.0/pow(2.0,(k+1));
}
if(k==4){
modw=0.0625;
}
for(i=0;i<nkk;i++){
mu1[i]=5.0;
mu2[i]=-5.0;
}
lptemp=0.0;
for(i=0;i<nkk;i++){
work[i]=(theta[i]-mu1[i]);
lptemp-=pow(work[i],2.0)/(2.0*pow(sig1,2));
}
lptemp=exp(lptemp);
lptemp/=pow(tpi,(nkk/2.0));
lptemp/=pow(sig1,nkk);
lp=w1*lptemp;
lptemp=0.0;
for(i=0;i<nkk;i++){
work[i]=(theta[i]-mu2[i]);
lptemp-=pow(work[i],2.0)/(2.0*pow(sig2,2));
}
lptemp=exp(lptemp);
lptemp/=pow(tpi,(nkk/2.0));
lptemp/=pow(sig2,nkk);
lp+=w2*lptemp;
lp=log(modw*lp);
*llh=lp;
return lp;
}