/* <html><head>Semi-conjugate prior with normal likelihood (Section 9.4)</head><body><pre> */

#include "random.h"
#include &ltiomanip.h&gt
#include &ltiomanip.h&gt */
#include &ltfstream.h&gt */
using namespace std;

const int iter=10;   /* Number of iterations of the EM algorithm  */

const int m=500;     /* Number of replications                    */

const int t=10;      /* Number of iterations                      */

long idum=65537;     /* Seed for random number generator          */

int main (void)
{
  ofstream ans("semiconj.ans");
  int j,s,nu,nu0,nu1,n,n0,n1;
  double xbar,sxx,s0,s1;
  double theta,theta0,theta1,thetabar,thetass,thetavar;
  double phi,phi0,phi1,phibar,phiss,phivar;
  n=12;
  xbar=119;
  sxx=13045;
  s0=2700;
  nu0=11;
  n0=15;
  theta0=110;
  phi0=s0/(n0*(nu0-2));
  thetabar=0;
  phibar=0;
  thetass=0;
  phiss=0;
  ans << endl;
  ans << "Data quoted in P M Lee, `Bayesian Statistics: "
      << "An Introduction'," << endl;
  ans << "Arnold 1989, Section 2.13.  "
      << "Taking n=12, xbar=139, S=13,045 and"<<endl;
  ans << "prior for theta ~ N(theta0,S0/n0(nu0-2)), "
      << "that is, N(" << theta0 << ", "
      << phi0 << ")," << endl;
  ans << "and for phi independent and such that "
      << "phi ~ S0 chi_{nu0}^{-2}," << endl;
  ans << "that is, phi/"<<s0<<" is a chi-squared variate on "
      << nu0 << " d.f." << endl;
  ans << endl;
  ans << "Iterations of the EM algorithm give the following "
      << "values for theta";
  ans << endl;
  /*                   EM algorithm                         */
  theta=theta0;     /* Initialize                           */
  n1=nu0+n;
  for (j=0; j < iter; j++)  /* Iterate iter times           */
  {
    s1=s0+sxx+n*(xbar-theta)*(xbar-theta);
    theta1=(theta0/phi0+n*xbar/(s1/n1))/(1/phi0+n/(s1/n1));
    theta=theta1;
    ans << theta << " ";
  }
  ans << endl;
  /*                         Gibbs sampler                  */
  phi=sxx/(n-1);          /* Initialize                     */
  for (j=0; j < m; j++)   /* Replicate m times              */
  {
    for (s=0; s < t; s++) /* Iterate t times                */
    {
      phi1=1/((1/phi0)+(n/phi));
      theta1=phi1*((theta0/phi0)+(n*xbar/phi));
      /* theta | phi ~ N(theta1,phi1 */
      theta=theta1+sqrt(phi1)*gasdev(&idum);
      /* s1=s0+sum(x(i)-theta)^2 */
      s1=s0+sxx+n*(xbar-theta)*(xbar-theta);
      /* phi | theta ~ s1*\chi_{\nu1}^{-2} */
      phi=2*s1/gammadev((nu0+n)/2,&idum);
    }
    thetabar+=theta/m;
    phibar+=phi/m;
    thetass+=theta*theta;
    phiss+=phi*phi;
  }
  thetavar=(thetass-m*thetabar*thetabar)/(m-1);
  phivar=(phiss-m*phibar*phibar)/(m-1);
  ans << endl;
  ans << "The Gibbs sampler gives rise to the "
      << "following conclusions:"<<endl;
  ans << "We deduce posterior for theta has mean "
      << thetabar <<" and variance " << thetavar << endl;
  ans << "and that posterior for phi has mean " << phibar
      << " and variance " << phivar << endl;
  ans << endl;
}

/* </pre></body></html> */
