Exemple #1
0
void Testmonitor_acf(CuTest* tc) {
  x = gsl_vector_alloc(1);
  y = gsl_vector_alloc(1);
  m = mcmclib_monitor_acf_alloc(1, MAX_LAG);
  ACF = gsl_matrix_alloc(MAX_LAG+1, 1);

  append(1.0);
  append(-1.0);
  CuAssertDblEquals(tc, 1.0, acf(0), TOL);

  append(1.0);
  double mean = 1.0/3.0;
  double var = 1.0 - mean*mean;
  CuAssertDblEquals(tc, var, acf(0), TOL);

  double acf_check = -2.0/3.0;
  CuAssertDblEquals(tc, acf_check, acf(1), TOL);

  append(-1.0);
  mean = 0.0;
  var = 1.0;
  CuAssertDblEquals(tc, 1.0, acf(0), TOL);
  CuAssertDblEquals(tc, -3.0/4.0, acf(1), TOL);

  gsl_vector* iact = gsl_vector_alloc(1);
  mcmclib_monitor_acf_get(m, ACF);
  mcmclib_iact_from_acf(ACF, iact);
  CuAssertDblEquals(tc, -0.5, gsl_vector_get(iact, 0), TOL);

  gsl_vector_free(iact);
  gsl_matrix_free(ACF);
  mcmclib_monitor_acf_free(m);
  gsl_vector_free(x);
  gsl_vector_free(y);
}
void
TempoTrackV2::get_rcf(const d_vec_t &dfframe_in, const d_vec_t &wv, d_vec_t &rcf)
{
    // calculate autocorrelation function
    // then rcf
    // just hard code for now... don't really need separate functions to do this

    // make acf

    d_vec_t dfframe(dfframe_in);

    MathUtilities::adaptiveThreshold(dfframe);

    d_vec_t acf(dfframe.size());


    for (unsigned int lag=0; lag<dfframe.size(); lag++)
    {
        double sum = 0.;
        double tmp = 0.;

        for (unsigned int n=0; n<(dfframe.size()-lag); n++)
        {
            tmp = dfframe[n] * dfframe[n+lag];
            sum += tmp;
        }
        acf[lag] = static_cast<double> (sum/ (dfframe.size()-lag));
    }

    // now apply comb filtering
    int numelem = 4;

    for (unsigned int i = 2;i < rcf.size();i++) // max beat period
    {
        for (int a = 1;a <= numelem;a++) // number of comb elements
        {
            for (int b = 1-a;b <= a-1;b++) // general state using normalisation of comb elements
            {
                rcf[i-1] += ( acf[(a*i+b)-1]*wv[i-1] ) / (2.*a-1.);	// calculate value for comb filter row
            }
        }
    }

    // apply adaptive threshold to rcf
    MathUtilities::adaptiveThreshold(rcf);

    double rcfsum =0.;
    for (unsigned int i=0; i<rcf.size(); i++)
    {
        rcf[i] += EPS ;
        rcfsum += rcf[i];
    }

    // normalise rcf to sum to unity
    for (unsigned int i=0; i<rcf.size(); i++)
    {
        rcf[i] /= (rcfsum + EPS);
    }
}
Exemple #3
0
void GrDrawContext::discard(GrRenderTarget* renderTarget) {
    RETURN_IF_ABANDONED
    SkASSERT(renderTarget);
    AutoCheckFlush acf(fContext);
    if (!this->prepareToDraw(renderTarget)) {
        return;
    }
    fDrawTarget->discard(renderTarget);
}
Exemple #4
0
void GrDrawContext::drawPaint(GrRenderTarget* rt,
                              const GrClip& clip,
                              const GrPaint& origPaint,
                              const SkMatrix& viewMatrix) {
    RETURN_IF_ABANDONED
    // set rect to be big enough to fill the space, but not super-huge, so we
    // don't overflow fixed-point implementations
    SkRect r;
    r.setLTRB(0, 0,
              SkIntToScalar(rt->width()),
              SkIntToScalar(rt->height()));
    SkTCopyOnFirstWrite<GrPaint> paint(origPaint);

    // by definition this fills the entire clip, no need for AA
    if (paint->isAntiAlias()) {
        paint.writable()->setAntiAlias(false);
    }

    bool isPerspective = viewMatrix.hasPerspective();

    // We attempt to map r by the inverse matrix and draw that. mapRect will
    // map the four corners and bound them with a new rect. This will not
    // produce a correct result for some perspective matrices.
    if (!isPerspective) {
        SkMatrix inverse;
        if (!viewMatrix.invert(&inverse)) {
            SkDebugf("Could not invert matrix\n");
            return;
        }
        inverse.mapRect(&r);
        this->drawRect(rt, clip, *paint, viewMatrix, r);
    } else {
        SkMatrix localMatrix;
        if (!viewMatrix.invert(&localMatrix)) {
            SkDebugf("Could not invert matrix\n");
            return;
        }

        AutoCheckFlush acf(fContext);
        if (!this->prepareToDraw(rt)) {
            return;
        }

        GrPipelineBuilder pipelineBuilder(*paint, rt, clip);
        fDrawTarget->drawBWRect(pipelineBuilder,
                                paint->getColor(),
                                SkMatrix::I(),
                                r,
                                NULL,
                                &localMatrix);
    }
}
Exemple #5
0
void GrDrawContext::clear(GrRenderTarget* renderTarget,
                          const SkIRect* rect,
                          const GrColor color,
                          bool canIgnoreRect) {
    RETURN_IF_ABANDONED
    SkASSERT(renderTarget);

    AutoCheckFlush acf(fContext);
    if (!this->prepareToDraw(renderTarget)) {
        return;
    }
    fDrawTarget->clear(rect, color, canIgnoreRect, renderTarget);
}
void omxComputeNumericDeriv::omxCalcFinalConstraintJacobian(FitContext* fc, int npar){
	allconstraints_functional acf(*fc, verbose);
	Eigen::MatrixWrapper< Eigen::ArrayXd > optimaM(optima);
	Eigen::VectorXd resulttmp(fc->state->numEqC + fc->state->numIneqC);
	Eigen::MatrixXd jactmp(fc->state->numEqC + fc->state->numIneqC, npar);
	acf(optimaM, resulttmp, jactmp);
	/*Gradient algorithm, iterations, and stepsize are hardcoded as they are for two reasons.
	 * 1.  Differentiating the constraint functions should not take long, expecially compared to 
	 * twice-differentiating the fitfunction, so it might as well be done carefully.
	 * 2.  The default behavior during the ComputeNumericDeriv step uses different values of 
	 * gradient stepsize and iterations depending on whether or not the MxModel contains thresholds,
	 * since the numerical accuracy of the -2logL is worse when multivariate-normal integration is involved.
	 * But, that has no bearing on the constraint functions, so it doesn't really make sense to use
	 * the stepsize and iterations stored in the omxComputeNumericDeriv object.
	 */
	fd_jacobian<true>(
		GradientAlgorithm_Central, 4, 1.0e-7,
    acf, resulttmp, optimaM, jactmp);
	
	fc->constraintFunVals = resulttmp;
	fc->constraintJacobian = jactmp;
	return;
}
Exemple #7
0
// Optimized for low memory usage
void arfit(const Matrix &x, int order, Matrix &a, Matrix &q, Matrix &v) {
  if(order + 2 >= x.rows())
    std::cerr << "ERROR: Not enough data. Try a smaller order." << std::endl << die();

  std::vector< Matrix > acf(order + 1);

  for(int p = 0; p <= order; ++p)
    acf[p] = x.block(p, 0, x.rows() - p, x.cols()).transpose() * x.block(0, 0, x.rows() - p, x.cols());

  std::vector< std::vector< Matrix > > vACF_0(order), vACF_1(order);

  for(int row = 0; row < order; ++row) {
    vACF_0[row].resize(order);
    vACF_1[row].resize(order);

    for(int col = 0; col < order; ++col)
      if(row <= col) {
        vACF_0[row][col] = acf[col-row];
        vACF_1[row][col] = acf[col-row+1];
      } else {
        vACF_0[row][col] = acf[row-col].transpose();
        vACF_1[row][col] = acf[row-col+1].transpose();
      }
  }

  v = mcat(vACF_0);

  Matrix ACF_1 = mcat(vACF_1);
  Matrix A = ACF_1 * v.inverse();
  Matrix Q = (v - A * ACF_1.transpose()) / x.rows();

  a = A.block(0, 0, x.cols(), x.cols() * order);
  q = Q.block(0, 0, x.cols(), x.cols());

  v /= x.rows();
}
Exemple #8
0
void posterior_summary(const gsl_matrix *theta, FILE *ofile, long M)
{

  size_t T=theta->size1;
  size_t npar=theta->size2;
  gsl_vector *tmp=gsl_vector_alloc(T);
  int i,j;
  double median,lower,upper;

  printf("\n Writing MCMC draws to out\n\n");
  FILE *file = fopen("out","w");
  for(i=0;i<T;i++){
    for(j=0;j<npar;j++)
      fprintf(file,"%14.6e ",mget(theta,i,j));
    fprintf(file,"\n");
  }

  fprintf(ofile,"\n\n Posterior Summary \n");
  fprintf(ofile,"\n T=%lu\n\n",T);
  fprintf(ofile,"\n      Mean          Median         Stdev           0.95 DI\n\n");
  for(i=0;i<npar;i++){
    gsl_matrix_get_col( tmp, theta, i);
    gsl_sort_vector(tmp);
    median=gsl_stats_median_from_sorted_data(tmp->data,tmp->stride,tmp->size);
    lower=gsl_stats_quantile_from_sorted_data(tmp->data,tmp->stride,tmp->size,0.025);
    upper=gsl_stats_quantile_from_sorted_data(tmp->data,tmp->stride,tmp->size,0.975);

    fprintf(ofile,"%2d %14.7e %14.7e %14.7e (%14.7e,%14.7e)\n"
	   ,i,mean(tmp),median,sqrt(var(tmp)),lower,upper);
  }

  long tau;
  if( M < 0 )
    tau=1000;
  else
    tau=M;

  gsl_vector *rho=gsl_vector_alloc(tau);

  fprintf(ofile,"\n                                ACF");
  fprintf(ofile,"\n      NSE          Ineff        1            50           100          500\n");
  for(i=0;i<npar;i++){
    gsl_matrix_get_col( tmp, theta, i);
    acf(tmp,tau,rho);

    /* write out ACF for each parameter */
    char file_name[20] = "acf.dat";
    sprintf( &file_name[7],"%d",i);
    FILE *fp_acf = fopen( file_name, "w");

    for(j=0;j<tau;j++)
      fprintf(fp_acf,"%g\n",vget(rho,j));

    fclose(fp_acf);


    /* get inefficiency factor using Newey-West estimate of Long-run var*/
    double ineff=1.0;
    for(j=0;j<tau-1;j++){
      ineff += 2.0*(tau-j-1)/tau*vget(rho,j);
      }
    /* numerical standard error for posterior mean */
    double nse=sqrt(var(tmp)*ineff/T);

    fprintf(ofile,"%2d %12.5e %12.5e %12.5e %12.5e %12.5e %12.5e\n"
	    ,i,nse,ineff,vget(rho,0),vget(rho,49),vget(rho,99),vget(rho,499));

    /* produce kernel density plot for each parameter */
    char file_name2[20] = "den.dat";

    sprintf( &file_name2[7],"%d",i);
    FILE *fp_den = fopen( file_name2, "w");

    double stdev = sqrt(var(tmp));
    lower = gsl_vector_min(tmp) - stdev;
    upper = gsl_vector_max(tmp) + stdev;


    den_est_file(tmp, lower , upper ,100, fp_den, -1.0);


  }

  fprintf(ofile,"\n\n");
  gsl_vector_free(rho);
  gsl_vector_free(tmp);
}