/*Purpose:Bayesian analysis by using Gibbs Sampling */ /*Date: 4-24-2000 Name: Duo Zhou */ /*Use of random number generator including rgamma, rnorm, */ /*runif, and discrete random number generator to generate the*/ /*posterior histogram based on Hiearchical centering one way */ /*anova model with improper priors. */ #include #include #include #include #include "rnorm.cc" #include "rgamma.cc" #include "runif1.cc" #define nrows 7 #define ncols 5 #define N 35 #define m 20000 double y[nrows][ncols]; double atemp[nrows]; double alpha[nrows][m]; double beta[m]; double tao[m]; double sigma[m]; double sum (double array[nrows][ncols], int r, int c); double sumyi (double array[nrows][ncols], int r, int c); double sum_asq(double array[nrows], double b, int r); double ebeta(double array1[nrows], int r); double esigma (double array1[nrows][ncols], double array2[nrows], int r, int c); double rgamma(int *idum, int ia, double alpha); double rnorm(int *idum); double ran1(int *idum); class Randint { unsigned long randx; public: Randint (long s=0) { randx=s; } void seed (long s) { randx=s; } int abs(int x) { return x&0x7fffffff; } static double max() { return 2147483648.0;} int draw() { return randx = randx*1103515245 + 12345;} double fdraw () { return abs(draw()) / max(); } int operator() () {return abs (draw()); } }; class Urand: public Randint { int n; public: Urand (int nn) { n=nn; } int operator()() {int r = n*fdraw(); return (r==n)? n-1: r; } }; class Erand: public Randint { int mean; public: Erand (int me) { mean = me; } int operator ()() { return -mean*log((max()-draw())/max()+.5);} }; main () { int i, j, k, l, salpha, talpha; /*double salpha, talpha;*/ int fd; int n=35, a=-2, b=-2, c=-2, d=-2; FILE *fp; double sumy, mu1, std1, btemp, mu2, std2, sbeta, tbeta, sa1, ta1; int *idum1=&a, *idum2=&b, *idum3=&c, *idum4=&d; Urand draw(m); /*read in data*/ char file1[11]="obs.dat"; if ((fp=fopen(file1, "r"))==NULL) { printf("cannot open file\n"); exit(0); } printf("infile: %s\n", file1); for (i=0; i=(m-1000)) printf("%7.4lf ", btemp); for (l=0; l=(m-1000)) printf("%7.4lf ", atemp[l]); } c=-draw()/m/nrows; idum3=&c; sa1=((double)N)/2; salpha=(int) floor (sa1); sbeta=esigma(y, atemp, nrows, ncols); sigma[k+1]=sbeta/rgamma(idum3, salpha, sa1); d=-draw()/m/nrows; idum4=&d; ta1=((double) nrows)/2; talpha= (int) floor(ta1); tbeta=sum_asq(atemp, btemp, nrows); tao[k+1]=tbeta/rgamma(idum4, talpha, ta1); if (k>=(m-1000)) printf("%10e %7.4lf\n", tao[k+1], sigma[k+1]); } } double sum (double array[nrows][ncols],int r, int c) { int i, j; double sum1=0; for (i=0; i-->