예제 #1
0
void Metropolis::regression_adapt(int numSteps, int stepSize)
{
	std::vector<STM::ParName> parNames (parameters.names());
	
	std::map<STM::ParName, std::map<std::string, double *> > regressionData;
	for(const auto & par : parNames)
	{
		regressionData[par]["log_variance"] = new double [numSteps];
		regressionData[par]["variance"] = new double [numSteps];
		regressionData[par]["acceptance"] = new double [numSteps];
	}
	
	for(int i = 0; i < numSteps; i++)
	{
		// compute acceptance rates for the current variance term
		parameters.set_acceptance_rates(do_sample(stepSize));
	
		for(const auto & par : parNames)
		{
			// save regression data for each parameter
			regressionData[par]["log_variance"][i] = std::log(parameters.sampler_variance(par));
			regressionData[par]["variance"][i] = parameters.sampler_variance(par);
			regressionData[par]["acceptance"][i] = parameters.acceptance_rate(par);
			
			// choose new variances at random for each parameter; drawn from a gamma with mean 2.38 and sd 2
			parameters.set_sampler_variance(par, gsl_ran_gamma(rng.get(), 1.4161, 1.680672));
		}
	
	}
	
	// perform regression for each parameter and clean up
	for(const auto & par : parNames)
	{
		// first compute the correlation for variance and log_variance, use whichever is higher
		double corVar = gsl_stats_correlation(regressionData[par]["variance"], 1,
				regressionData[par]["acceptance"], 1, numSteps);
		double corLogVar = gsl_stats_correlation(regressionData[par]["log_variance"], 1,
				regressionData[par]["acceptance"], 1, numSteps);

		double beta0, beta1, cov00, cov01, cov11, sumsq, targetVariance;
		if(corVar >= corLogVar)
		{
			gsl_fit_linear(regressionData[par]["variance"], 1, 
					regressionData[par]["acceptance"], 1, numSteps, &beta0, &beta1, 
					&cov00, &cov01, &cov11, &sumsq);
			targetVariance = (parameters.optimal_acceptance_rate() - beta0)/beta1;
		} else
		{
			gsl_fit_linear(regressionData[par]["log_variance"], 1, 
					regressionData[par]["acceptance"], 1, numSteps, &beta0, &beta1,
					&cov00, &cov01, &cov11, &sumsq);
			targetVariance = std::exp((parameters.optimal_acceptance_rate() - beta0)/beta1);
		}
		parameters.set_sampler_variance(par, targetVariance);

		delete [] regressionData[par]["log_variance"];
		delete [] regressionData[par]["variance"];
		delete [] regressionData[par]["acceptance"];
	}
}
예제 #2
0
파일: Sampler.cpp 프로젝트: apolitis/imp
ConfigurationSet *Sampler::create_sample() const {
  IMP_OBJECT_LOG;
  set_was_used(true);
  // IMP_LOG_TERSE( "Sampling " << num_opt << " particles."<<std::endl);
  /*if (num_opt == 0) {
    IMP_WARN("There are no particles to optimize."<<std::endl);
    return nullptr;
    }*/
  return do_sample();
}
예제 #3
0
    virtual void sample (AbstractStorage *stor_, size_t multiplier)
    {
        stor_t *stor = downcast (stor_);

        // initialized late since stor might be empty at ctor time
        rho_ = stor->particle_density ();
        ndata_ += multiplier;
        walltime_ -= gclock ();

        for (size_t n1 = 0; n1 != multiplier; ++n1)
        {
            key_t k1 = stor->random_particle (&random);

            if (method_ == "full")
                do_sample (stor, k1, stor->enumerate_all ());
            else // window
                do_sample (stor, k1, stor->enumerate_box (k1, rmax_));
        }

        walltime_ += gclock ();
    }
예제 #4
0
void Metropolis::auto_adapt()
{
	if(outputLevel >= EngineOutputLevel::Normal) 
	{
		std::cerr << timestamp() << " Starting automatic adaptation" << std::endl;
	}
	std::vector<STM::ParName> parNames (parameters.names());
	
	// disable thinning for the adaptation phase
	int oldThin = thinSize;
	thinSize = 1;
	
	regression_adapt(10, 100); // use the first two loops to try a regression approach	
	int nLoops = 2;
	
	while(nLoops < minAdaptationLoops or ((not parameters.adapted()) and nLoops < maxAdaptationLoops))	
	{
		nLoops++;
		parameters.set_acceptance_rates(do_sample(adaptationSampleSize));

		for(const auto & par : parNames) {
			double ratio;
			if(parameters.acceptance_rate(par) == 0) 
				ratio = 1e-2;
			else
				ratio = parameters.acceptance_rate(par) / parameters.optimal_acceptance_rate();
			parameters.set_sampler_variance(par, ratio*parameters.sampler_variance(par));
		}
		
		if(outputLevel >= EngineOutputLevel::Talkative) {
			std::cerr << "\n    " << timestamp() << " iter " << parameters.iteration() << "\n";
			parameters.print_adaptation(isatty(fileno(stderr)), 2);
// 			std::cerr << "    " << parameters.str_acceptance_rates(isatty(fileno(stderr))) << "\n";
// 			std::cerr << "    sampler variance:\n";
// 			std::cerr << "    " << parameters.str_sampling_variance(isatty(fileno(stderr))) << std::endl;
		}
		currentSamples.clear();
		if(saveResumeData)
			serialize_all();
	}
	parameters.reset(); // adaptation samples are not included in the burnin period
	if(outputLevel >= EngineOutputLevel::Normal) {
		std::cerr << timestamp() << " Adaptation completed successfully" << std::endl;
	}
	
	thinSize = oldThin;
}
예제 #5
0
int main(int argc, char * argv[]) {

	uint32_t feature_set;
	uint16_t sample;
	float volts,ave_volts;
	
	uint32_t spi,cs,initok;
	
	spi=0;	
	cs=0;	
	ave_volts=0;
	initok=0;	

	fprintf(stdout,"Initialising librpip...\n"); 	
	feature_set = librpipInit(LIBRPIP_BOARD_DETECT, LIBRPIP_FLAG_DEBUG_ON, 0);
	fprintf(stdout,"\n"); 	
	
	if(feature_set == 0) {
		fprintf(stdout,"librpip failed to initialise!\n");
	} else {
		if(spi==0  && (feature_set & LIBRPIP_FEATURE_SPI0)) {
			initok=1;
		}		

		if(spi==1  && (feature_set & LIBRPIP_FEATURE_SPI1)) {
			initok=1;
		}	

		if(initok) {			
			set_spi_config(spi,cs);
			struct librpip_tx* tx = setup_transaction();
			while(1) {
				sample = do_sample(spi,cs,tx);
				volts = 3.3*(float)sample/4095.0;   //12 bit
				ave_volts = 9.0*ave_volts/10.0 + volts/10.0;   //essentially a simple filter to calm the jitter
				fprintf(stdout,"sample %u, volts %.3fv, average volts %.3fv\n",sample,volts,ave_volts);
				usleep(10000);
			}
			librpipTransactionDestroy(tx);  //kind of pointless after an infinite loop but its a very good habit to destroy what you create			
		}
	}
	
	librpipClose();	
	return 0;
}
예제 #6
0
void Metropolis::run_sampler(int n)
{
	set_up_rng();

	if(not parameters.adapted())
		auto_adapt();

	int burninCompleted = parameters.iteration();
	int numCompleted = 0;
	// for safety, always start by re-computing the current likelihood
	currentLL = likelihood->compute_log_likelihood(parameters);
	bool computeDevianceNow = false;
	while(numCompleted < n) {
		int sampleSize;
		if(burninCompleted < burnin)
		{
			sampleSize = ( (burnin - burninCompleted < outputBufferSize) ? 
					(burnin - burninCompleted) : outputBufferSize);
		}
		else
		{
			sampleSize = ((n - numCompleted < outputBufferSize) ? (n - numCompleted) : 
					outputBufferSize);
			computeDevianceNow = computeDIC;
		}
		currentSamples.reserve(sampleSize);
		if(computeDevianceNow)
			sampleDeviance.reserve(sampleSize);
		do_sample(sampleSize, computeDevianceNow);
		
		if(burninCompleted < burnin)
		{
			burninCompleted += sampleSize;		
		}
		else
		{
			if(computeDIC)
				prepare_deviance(); // this function takes care of clearing the old vector

			STMOutput::OutputBuffer buffer (currentSamples, parameters.names(),
					STMOutput::OutputKeyType::posterior, posteriorOptions);
			outputQueue->push(buffer);	// note that this may block if the queue is busy
			numCompleted += sampleSize;		
		}

		currentSamples.clear();
		if(saveResumeData)
			serialize_all();
		
		/* if desired: some output with the current time */
		if(outputLevel >= EngineOutputLevel::Normal) {
			if(numCompleted == 0)
			{
				std::cerr << timestamp() << "   MCMC burnin iteration " << burninCompleted
						<< " of " << burnin << std::endl;						
			}
			else
			{
				std::cerr << timestamp() << "   MCMC iteration " << numCompleted << " of " 
						<< n << std::endl;			
			}
		}
	}
	// end of sampling; compute DIC and output if needed
	if(computeDIC)
	{
		STMParameters::STModelParameters tbarPars (parameters);
		for(const auto & p : thetaBar.first)
			tbarPars.update(p);
		double devThetaBar = -2.0 * likelihood->compute_log_likelihood(tbarPars);

		double pd = DBar.first - devThetaBar;
		double DIC = devThetaBar + 2.0 * pd;
		
		// now just save it all
		std::ostringstream dicOutput;
		dicOutput << "pD: " << pd << "\n";
		dicOutput << "Mean deviance (d-bar): " << DBar.first << "\n";
		dicOutput << "Deviance of mean (d(theta-bar)): " << devThetaBar << "\n";
		dicOutput << "DIC: " << DIC << "\n";
		STMOutput::OutputBuffer buffer (dicOutput.str(), STMOutput::OutputKeyType::dic, 
			posteriorOptions);
		outputQueue->push(buffer);	
	}
	if(saveResumeData)
		serialize_all();

}
예제 #7
0
void    mdvi_shrink_glyph_grey(DviContext *dvi, DviFont *font,
    DviFontChar *pk, DviGlyph *dest)
{
    int    rows_left, rows;
    int    cols_left, cols, init_cols;
    long    sampleval, samplemax;
    BmUnit    *old_ptr;
    void    *image;
    int    w, h;
    int    x, y;
    DviGlyph *glyph;
    BITMAP     *map;
    Ulong    *pixels;
    int    npixels;
    Ulong    colortab[2];
    int    hs, vs;
    DviDevice *dev;

    hs = dvi->params.hshrink;
    vs = dvi->params.vshrink;
    dev = &dvi->device;
    
    glyph = &pk->glyph;
    map = (BITMAP *)glyph->data;
    
    x = (int)glyph->x / hs;
    init_cols = (int)glyph->x - x * hs;
    if(init_cols <= 0)
        init_cols += hs;
    else
        x++;
    w = x + ROUND((int)glyph->w - glyph->x, hs);

    cols = (int)glyph->y + 1;
    y = cols / vs;
    rows = cols - y * vs;
    if(rows <= 0) {
        rows += vs;
        y--;
    }
    h = y + ROUND((int)glyph->h - cols, vs) + 1;
    ASSERT(w && h);
    
    /* before touching anything, do this */
    image = dev->create_image(dev->device_data, w, h, BITMAP_BITS);
    if(image == NULL) {
        mdvi_shrink_glyph(dvi, font, pk, dest);
        return;
    }
    
    /* save these colors */
    pk->fg = MDVI_CURRFG(dvi);
    pk->bg = MDVI_CURRBG(dvi);
    
    samplemax = vs * hs;
    npixels = samplemax + 1;
    pixels = get_color_table(&dvi->device, npixels, pk->fg, pk->bg,
            dvi->params.gamma, dvi->params.density);
    if(pixels == NULL) {
        npixels = 2;
        colortab[0] = pk->fg;
        colortab[1] = pk->bg;
        pixels = &colortab[0];
    }
    
    /* setup the new glyph */
    dest->data = image;
    dest->x = x;
    dest->y = glyph->y / vs;
    dest->w = w;
    dest->h = h;

    y = 0;
    old_ptr = map->data;
    rows_left = glyph->h;

    while(rows_left && y < h) {
        x = 0;
        if(rows > rows_left)
            rows = rows_left;
        cols_left = glyph->w;
        cols = init_cols;
        while(cols_left && x < w) {
            if(cols > cols_left)
                cols = cols_left;
            sampleval = do_sample(old_ptr, map->stride,
                glyph->w - cols_left, cols, rows);
            /* scale the sample value by the number of grey levels */
            if(npixels - 1 != samplemax)
                sampleval = ((npixels-1) * sampleval) / samplemax;
            ASSERT(sampleval < npixels);
            dev->put_pixel(image, x, y, pixels[sampleval]);
            cols_left -= cols;
            cols = hs;
            x++;
        }
        for(; x < w; x++)
            dev->put_pixel(image, x, y, pixels[0]);
        old_ptr = bm_offset(old_ptr, rows * map->stride);
        rows_left -= rows;
        rows = vs;
        y++;
    }
    
    for(; y < h; y++) {
        for(x = 0; x < w; x++)
            dev->put_pixel(image, x, y, pixels[0]);
    }

        dev->image_done(image);
    DEBUG((DBG_BITMAPS, "shrink_glyph_grey: (%dw,%dh,%dx,%dy) -> (%dw,%dh,%dx,%dy)\n",
        glyph->w, glyph->h, glyph->x, glyph->y,
        dest->w, dest->h, dest->x, dest->y));
}
예제 #8
0
void    mdvi_shrink_glyph(DviContext *dvi, DviFont *font,
    DviFontChar *pk, DviGlyph *dest)
{
    int    rows_left, rows, init_cols;
    int    cols_left, cols;
    BmUnit    *old_ptr, *new_ptr;
    BITMAP    *oldmap, *newmap;
    BmUnit    m, *cp;
    DviGlyph *glyph;
    int    sample, min_sample;
    int    old_stride;
    int    new_stride;
    int    x, y;
    int    w, h;
    int    hs, vs;
    
    hs = dvi->params.hshrink;
    vs = dvi->params.vshrink;
    
    min_sample = vs * hs * dvi->params.density / 100;

    glyph = &pk->glyph;
    oldmap = (BITMAP *)glyph->data;
        
    x = (int)glyph->x / hs;
    init_cols = (int)glyph->x - x * hs;
    if(init_cols <= 0)
        init_cols += hs;
    else
        x++;
    w = x + ROUND((int)glyph->w - glyph->x, hs);

    cols = (int)glyph->y + 1;
    y = cols / vs;
    rows = cols - y * vs;
    if(rows <= 0) {
        rows += vs;
        y--;
    }
    h = y + ROUND((int)glyph->h - cols, vs) + 1;

    /* create the new glyph */
    newmap = bitmap_alloc(w, h);
    dest->data = newmap;
    dest->x = x;
    dest->y = glyph->y / vs;
    dest->w = w;
    dest->h = h;

    old_ptr = oldmap->data;
    old_stride = oldmap->stride;
    new_ptr = newmap->data;
    new_stride = newmap->stride;
    rows_left = glyph->h;

    while(rows_left) {
        if(rows > rows_left)
            rows = rows_left;
        cols_left = glyph->w;
        m = FIRSTMASK;
        cp = new_ptr;
        cols = init_cols;
        while(cols_left > 0) {
            if(cols > cols_left)
                cols = cols_left;
            sample = do_sample(old_ptr, old_stride,
                glyph->w - cols_left, cols, rows);
            if(sample >= min_sample)
                *cp |= m;
            if(m == LASTMASK) {
                m = FIRSTMASK;
                cp++;
            } else
                NEXTMASK(m);
            cols_left -= cols;
            cols = hs;
        }
        new_ptr = bm_offset(new_ptr, new_stride);
        old_ptr = bm_offset(old_ptr, rows * old_stride);
        rows_left -= rows;
        rows = vs;
    }    
    DEBUG((DBG_BITMAPS, "shrink_glyph: (%dw,%dh,%dx,%dy) -> (%dw,%dh,%dx,%dy)\n",
        glyph->w, glyph->h, glyph->x, glyph->y,
        dest->w, dest->h, dest->x, dest->y));
    if(DEBUGGING(BITMAP_DATA))
        bitmap_print(stderr, newmap);
}