Exemplo n.º 1
0
DESIGN_mrf::DESIGN_mrf(datamatrix & dm,datamatrix & iv,
                       GENERAL_OPTIONS * o,DISTR * dp,FC_linear * fcl,
                       const MAP::map & m,vector<ST::string> & op,
                       vector<ST::string> & vn)
                      : DESIGN(o,dp,fcl)
  {

  if (errors==false)
    {

    read_options(op,vn);

    discrete = true;

    ma = m;
    type = Mrf;

    nrpar = ma.get_nrregions();
    consecutive=true;

    Zout = datamatrix(nrpar,1,1);
    index_Zout = statmatrix<int>(Zout.rows(),1);
    index_Zout.indexinit();
    }

  if (errors==false)
    init_data(dm,iv);

  if (errors==false)
    {
    compute_penalty();

    XWX = envmatdouble(0,nrpar);
    XWres = datamatrix(nrpar,1);
    Wsum = datamatrix(nrpar,1,1);

    compute_precision(1.0);

    compute_basisNull();

    identity=true;
    }

/*
  ofstream out("c:\\bayesx\\trunk\\testh\\results\\dm.raw");
  dm.prettyPrint(out);

  ofstream out2("c:\\bayesx\\trunk\\testh\\results\\iv.raw");
  iv.prettyPrint(out2);
*/

  }
Exemplo n.º 2
0
void DESIGN_mrf::compute_basisNull(void)
  {
  int i,j;

  basisNull = datamatrix(1,nrpar,1);

  if (centermethod==meanf || centermethod==cmeanintegral)
    {

    unsigned k;
    for (k=0;k<nrpar;k++)
      {
      if (posbeg[k] != -1)
        basisNull(0,k) = posend[k]-posbeg[k]+1;
      else
        basisNull(0,k) = 0;
      }

    }


  position_lin = -1;


  for(i=0;i<basisNull.rows();i++)
    {
    basisNullt.push_back(datamatrix(basisNull.cols(),1));
    for(j=0;j<basisNull.cols();j++)
      basisNullt[i](j,0) = basisNull(i,j);
    }


  // TEST
  /*
    ofstream out("c:\\bayesx\\test\\results\\data.res");
    data.prettyPrint(out);

    ofstream out2("c:\\bayesx\\test\\results\\designlin.res");
    designlinear.prettyPrint(out2);
   */
  // TEST

  }
Exemplo n.º 3
0
FC_cv::FC_cv(GENERAL_OPTIONS * o,DISTR * lp,
                 const ST::string & t, const ST::string & fp,
                 vector<FC_hrandom> * FChs)
  : FC(o,t,lp->nrobs,1,fp)
  {

  likep = lp;

  hrandoms = FChs;
  sampled_etas = datamatrix(likep->nrobs,o->compute_samplesize(),0);
  sampled_responses = datamatrix(likep->nrobs,o->compute_samplesize(),0);
  sampled_likelihood = datamatrix(likep->nrobs,o->compute_samplesize(),0);

  FC_sampled_l = FC(o,"",likep->nrobs,1,fp+"_like");

  effect = datamatrix(likep->nrobs,1,0);
  linpred = datamatrix(likep->nrobs,1,0);
  size =   hrandoms->size();
  get_ind();
  }
Exemplo n.º 4
0
double FC_cv::compute_logscore(void)
  {

  unsigned s,i;

//  ofstream out("c:\\bayesx\\testh\\results\\sampled_likelihood.res");
//  sampled_likelihood.prettyPrint(out);

  unsigned S = sampled_likelihood.cols();       // S = number of samples
  unsigned I = sampled_likelihood.rows();       // I = number of observations
                                                // i-th row, s-th column contains
                                                // log-likelihood of i-th
                                                // observation in sample s

  if (log_score.rows() != nrcat)
    log_score = datamatrix(nrcat,1,0);

  datamatrix lhelp(nrcat,S,0);

  unsigned in;

  for (i=0;i<I;i++)
    {

    in = ind(i,0);

    for (s=0;s<S;s++)
      {
      lhelp(in,s) += sampled_likelihood(i,s);
      }



//    log_score(in,0) = log(S) - log(log_score(in,0));
    }


  for (i=0;i<nrcat;i++)
    {
    log_score(i,0) = 0;
    for (s=0;s<S;s++)
      log_score(i,0) +=  exp(lhelp(i,s));

    log_score(i,0) = log(static_cast<double>(S)) - log(log_score(i,0));
    }

  return log_score.mean(0);

  }
Exemplo n.º 5
0
FULLCOND_mult::FULLCOND_mult(MCMCoptions * o,DISTRIBUTION * dp,
                             FULLCOND_nonp_basis * ba1,
                             FULLCOND_nonp_basis * ba2,
                             bool fi,
                             const ST::string & ti,
                             const ST::string & fp, const ST::string & pres,
                             const unsigned & c)
    : FULLCOND(o,datamatrix(dp->get_nrobs(),1),ti,1,1,fp)
{

    basis1p = ba1;
    basis2p = ba2;
    first = fi;
    ttype = mrf_rw;
}
Exemplo n.º 6
0
FULLCOND_mult::FULLCOND_mult(MCMCoptions * o,DISTRIBUTION * dp,
                             FULLCOND_random * rp,
                             FULLCOND_nonp_basis * ba,
                             bool fi,
                             const ST::string & ti,
                             const ST::string & fp, const ST::string & pres,
                             const unsigned & c)
    : FULLCOND(o,datamatrix(dp->get_nrobs(),1),ti,1,1,fp)
{

    reffectp = rp;
    basis1p = ba;
    first = fi;
    ttype = re_rw;
}
Exemplo n.º 7
0
baseline_reml::baseline_reml(MCMCoptions * o,
              const datamatrix & d, const datamatrix & leftint,
              const datamatrix & lefttrunc, const unsigned & nrk,
              const unsigned & degr, const unsigned & tgr, const unsigned & nrq,
              const unsigned & nrb, const knotpos & kp, const fieldtype & ft,
              const ST::string & ti, const ST::string & fp,
              const ST::string & pres, const double & l, const double & sl,
              const knotpos & gp, const int & gs, const bool & catsp, const double & rv)
  : spline_basis(o,d,nrk,degr,kp,ft,ti,fp,pres,l,sl,catsp,0.0,0.0,0.0,0.0,gs,rv)
  {
  unsigned i,j,k;

  baseline=true;
  varcoeff=false;

//  refcheck=false;

  gridpos = gp;
  double tmax=d.max(0);

  if(gridpos == MCMC::equidistant)
    {
    tgrid = tgr;
    tvalues = datamatrix(tgrid+1,1);
    tstep = tmax / tgrid;
    for(i=0;i<tvalues.rows();i++)
      tvalues(i,0) = i*tstep;
    }
  else if(gridpos == MCMC::quantiles)
    {
    tgrid = nrq*nrb;
    tvalues = datamatrix(tgrid+1,1);
    nrquant = nrq;
    nrbetween = nrb;
    datamatrix tquantiles = datamatrix(nrquant+1,1,0);
    for(i=1; i<nrquant; i++)
      {
      tquantiles(i,0) = d.quantile(((double)i/nrquant)*100,0);
      }
    tquantiles(nrquant,0) = tmax;

    double intmax, intmin, intstep;
    for(i=0; i<nrquant; i++)
      {
      intmin=tquantiles(i,0);
      intmax=tquantiles(i+1,0);
      intstep=(intmax-intmin)/nrbetween;
      for(j=0; j<nrbetween; j++)
        {
        tvalues(i*nrbetween+j,0) = intmin + j*intstep;
        }
      }
    tvalues(tgrid,0) = tmax;
    }
  else
    {
    make_index(d);
    vector<int>::iterator freqwork = freqoutput.begin();
    int * workindex = index.getV();
    tvalues = datamatrix(nrdiffobs,1,0);
    for(j=0;j<d.rows();j++,freqwork++,workindex++)
      if(freqwork==freqoutput.begin() || *freqwork!=*(freqwork-1))
        tvalues(*freqwork,0) = d(*workindex,0);
    }

  tsteps = datamatrix(tvalues.rows()-1,1,0);
  for(i=0; i<tsteps.rows(); i++)
    tsteps(i,0) = tvalues(i+1,0)-tvalues(i,0);

  interact_var = datamatrix(d.rows(),1,1);

  datamatrix betahelp(nrpar,1,0);
  DG = datamatrix(tvalues.rows(),degree+1,0);
  DGfirst = vector<int>(tvalues.rows());

  for(i=0;i<tvalues.rows();i++)
    {
    betahelp.assign( bspline(tvalues(i,0)) );
    j=degree+1;
    while(knot[j] <= tvalues(i,0) && j<nrknots+degree)
      j++;
    for(k=0;k<degree+1;k++)
      DG(i,k) = betahelp(k+j-(degree+1),0);
    DGfirst[i] = j-(degree+1);
    }

  tleft = vector<unsigned>(d.rows(),0);
  tright = vector<unsigned>(d.rows(),1);
  ttrunc = vector<unsigned>(d.rows(),0);

  // indices for truncation times

  if(lefttrunc.rows()>1)
    {
    for(i=0; i<lefttrunc.rows(); i++)
      {
      j=1;
      while(j<tvalues.rows() && tvalues(j,0) < lefttrunc(i,0))
        {
        ttrunc[i]++;
        j++;
        }
      }
    }

  for(i=0; i<d.rows(); i++)
    {
    j=0;
    while(j<tvalues.rows()-2 && tvalues(j,0)<d(i,0))
      {
      tright[i]++;
      j++;
      }
    }

  if(leftint.rows()>1)
    {
    for(i=0; i<d.rows(); i++)
      {
      if( leftint(i,0) < d(i,0))
        {
        j=0;
        while(j<tvalues.rows()-1 && tvalues(j,0)<leftint(i,0))
          {
          tleft[i]++;
          j++;
          }
        }
      else if(leftint(i,0)> d(i,0))
        {
        tleft[i] = tright[i]+1;
        }
      else
        {
        tleft[i] = tright[i];
        }
      }
    }
  else
    {
    for(i=0; i<d.rows(); i++)
      {
      tleft[i] = tright[i];
      }
    }

  }
Exemplo n.º 8
0
void baseline_reml::createreml(datamatrix & X,datamatrix & Z,
                                const unsigned & Xpos, const unsigned & Zpos)
  {
  unsigned i,j;

  double * workdata;
  double * workZ;
  double * workX;
  unsigned Xcols = X.cols();

  datamatrix refhelp;
  if(refcheck)
    {
    refhelp = bspline(reference);
    if(!varcoeff)
      X_ref = datamatrix(1,1);
    else
      X_ref = datamatrix(1,2);
    }

  // X für Daten berechen

  datamatrix knoten = datamatrix(nrpar,1,0.0);
  for(i=0;i<nrpar;i++)
    knoten(i,0) = knot[i];

  multBS_index(spline,knoten);

  workdata = spline.getV();
  workX = X.getV()+Xpos;
  if(varcoeff)
    {
    double * workintact = data_forfixed.getV();
    double * workX_VCM = X_VCM.getV()+1;
    for (i=0;i<spline.rows();i++,workdata++,workintact++,workX+=Xcols,workX_VCM+=2)
      {
      *workX = *workintact;
      *(workX+1) = *workdata**workintact;
      *workX_VCM = *workdata;
      }
    }
  else
    {
    for (i=0;i<spline.rows();i++,workdata++,workX+=Xcols)
      {
      *workX = *workdata;
      }
    }

  if(refcheck)
    {
    if(!varcoeff)
      {
      for(i=0; i<knoten.rows(); i++)
        X_ref(0,0) += knoten(i,0)*refhelp(i,0);
//      X_ref(0,0) -= splinemean;
      }
    else
      {
      X_ref(0,0) = 1.0;
      for(i=0; i<knoten.rows(); i++)
        X_ref(0,1) += knoten(i,0)*refhelp(i,0);
      }
    }

// Z für Daten berechnen

  compute_Kweights();
  datamatrix diffmatrix = weighteddiffmat(2,weight);
  diffmatrix = diffmatrix.transposed()*diffmatrix.transposed().sscp().inverse();

  if(refcheck)
    Z_ref = datamatrix(1,dimZ);

  unsigned Zcols = Z.cols();
  for(j=0;j<dimZ;j++)
    {
    multBS_index(spline,diffmatrix.getCol(j));

    workdata = spline.getV();
    workZ = Z.getV()+Zpos+j;

    if(refcheck)
      {
      Z_ref(0,j) = (refhelp.transposed()*diffmatrix.getCol(j))(0,0);
      }

    if(varcoeff)
      {
      double * workintact = data_forfixed.getV();
      double * workZ_VCM = Z_VCM.getV()+j;
      for (i=0;i<spline.rows();i++,workdata++,workintact++,workZ+=Zcols,workZ_VCM+=dimZ)
        {
        *workZ = *workdata**workintact;
        *workZ_VCM = *workdata;
        }
      }
    else
      {
      for (i=0;i<spline.rows();i++,workdata++,workZ+=Zcols)
        {
        *workZ = *workdata;
        }
      }
    }

  if(!varcoeff)
    {
// X für tvalues berechen

    t_X = datamatrix(tvalues.rows(),2,1);

    spline = datamatrix(tvalues.rows(),1,0);
    multDG(spline,knoten);

    workdata = spline.getV();
    for (i=0;i<spline.rows();i++,workdata++)
      {
      t_X(i,1) = *workdata;
      }

// Z für tvalues berechnen

    t_Z = datamatrix(tvalues.rows(),dimZ,0);
    for(j=0;j<dimZ;j++)
      {
      multDG(spline,diffmatrix.getCol(j));
      workdata = spline.getV();

      for (i=0;i<spline.rows();i++,workdata++)
        {
        t_Z(i,j) = *workdata;
        }
      }
    }

  if(gridsize>0)
    {
    X_grid=t_X.getCol(1);
    Z_grid=t_Z;
    }


  }
Exemplo n.º 9
0
double FC_cv::compute_energyscore(void)
  {


  unsigned s,i,j;

  unsigned S = sampled_responses.cols();
  unsigned I = sampled_responses.rows();

  //double * srp = sampled_responses.getV();
  // double * esp1;
  // double * esp2;


  datamatrix es1 = datamatrix(nrcat,S,0);
  datamatrix es2 = datamatrix(nrcat,S,0);

/*
  for (i=0;i<I;i++)
    {
    esp1 = es1.getV()+ind(i,0)*S;
    esp2 = es2.getV()+ind(i,0)*S;

    for (s=0;s<S-1;s++,srp++,esp1++,esp2++)
      {
      *esp1 += pow(sampled_responses(i,s)-likep->response_untransformed(i,0),2);
      *esp2 += pow(sampled_responses(i,s+1)-sampled_responses(i,s),2);
      }
    esp1++;
    *esp1 += pow(sampled_responses(i,s)-likep->response_untransformed(i,0),2);

    }
*/


  unsigned in;

  for (i=0;i<I;i++)
    {

    in = ind(i,0);

    for (s=0;s<S-1;s++)
      {
      es1(in,s) += pow(sampled_responses(i,s)-likep->response(i,0),2);
      es2(in,s) += pow(sampled_responses(i,s+1)-sampled_responses(i,s),2);
      }

    es1(in,S-1) += pow(sampled_responses(i,s)-likep->response(i,0),2);

    }

//  ofstream out("c:\\bayesx\\testh\\results\\es1.res");
//  es1.prettyPrint(out);



  if (e_score.rows() != nrcat)
    e_score = datamatrix(nrcat,1,0);

  double h;
  for (j=0;j<nrcat;j++)
    {
    for (s=0;s<S;s++)
      e_score(j,0) += sqrt(es1(j,s));

    e_score(j,0) /= S;

    h=0;
    for(s=0;s<S-1;s++)
      h += sqrt(es2(j,s));

    e_score(j,0) -= h/(2*(S-1));
    }


  return e_score.mean(0);
  }