#include "k.h" /* C implementation of Gibbs sampler input - kdata.txt - tables of A1 alleles (1 row of 6 values per table) cases followed by controls r0 r1 r2 s0 s1 s2 output - rslt.txt - a vector of prob obs is an outlier (length n) */ void main() { double jnk[6]; /* Work for fetching data */ double xsumsq=0.,lambda,p,num,r,fun1,fun2,cc,dd,rsig, *x,*A,*out,*deltot,zeta,kappa,tau2,epsilon; FILE *pinfile; FILE *rslttxt; int i,j,R,N, lcnt=0, /* input file line counter */ n=0, /* data counter */ nu=0,*delta, df=0, /* rchisq degrees of freedom*/ itmp=0, o[3],e[3], /* work for allele table */ ngib,burn; long idum=2348; /* random seed */ /* hardcoded constants */ zeta=1000.;kappa=4.;tau2=1.;epsilon=.01;ngib=500;burn=50; rslttxt=fopen("rslt.txt","w+"); if ((pinfile=fopen("kdata.txt","rt"))==NULL) { printf("I can't open file."); exit(1); } /* count no. of tables in file */ n=getnum(pinfile,jnk); while (!feof(pinfile)) n+=getnum(pinfile,jnk); rewind(pinfile); /* allocate memory */ if ((x=(double *)malloc(n*sizeof(double)))==NULL) { printf("I can't allocate memory."); exit(1); } if ((A=(double *)malloc(n*sizeof(double)))==NULL) { printf("I can't allocate memory.\n"); exit(1); } if ((deltot=(double *)malloc(n*sizeof(double)))==NULL) { printf("I can't allocate memory.\n"); exit(1); } if ((delta=(int *)malloc(n*sizeof(int)))==NULL) { printf("I can't allocate memory.\n"); exit(1); } if ((out=(double *)malloc((burn+ngib)*sizeof(double)))==NULL) { printf("I can't allocate memory.\n"); exit(1); } for (i=0;irunif(&idum)); } /* gibbs update */ for (i=0;irunif(&idum)); if (i>=burn) deltot[j]+=p; if(i>=burn && j==0) printf("%14.9f \n",p); /* dd=1/(1/tau2+1/lambda) doesn't change so it's before the loop */ cc=dd*(x[j]/lambda+kappa/tau2); A[j]=delta[j]*(sqrt(dd)*rnorm(&idum)+cc)+ (1-delta[j])*(sqrt(tau2)*rnorm(&idum)+kappa); r+=delta[j]; } } for (j=0;j