Alternaria
fit cylinders and ellipsoids to fungus
psf_moves.cpp
Go to the documentation of this file.
00001 /*
00002  * This work is licensed under a Creative Commons 
00003  * Attribution-Noncommercial-Share Alike 3.0 United States License.
00004  * 
00005  *    http://creativecommons.org/licenses/by-nc-sa/3.0/us/
00006  * 
00007  * You are free:
00008  * 
00009  *    to Share - to copy, distribute, display, and perform the work
00010  *    to Remix - to make derivative works
00011  * 
00012  * Under the following conditions:
00013  * 
00014  *    Attribution. You must attribute the work in the manner specified by the
00015  *    author or licensor (but not in any way that suggests that they endorse you
00016  *    or your use of the work).
00017  * 
00018  *    Noncommercial. You may not use this work for commercial purposes.
00019  * 
00020  *    Share Alike. If you alter, transform, or build upon this work, you may
00021  *    distribute the resulting work only under the same or similar license to
00022  *    this one.
00023  * 
00024  * For any reuse or distribution, you must make clear to others the license
00025  * terms of this work. The best way to do this is by including this header.
00026  * 
00027  * Any of the above conditions can be waived if you get permission from the
00028  * copyright holder.
00029  * 
00030  * Apart from the remix rights granted under this license, nothing in this
00031  * license impairs or restricts the author's moral rights.
00032  */
00033 
00034 
00046 #include <config.h>
00047 
00048 #include <cstdlib>
00049 #include <cmath>
00050 #include <cassert>
00051 #include <iostream>
00052 #include <iomanip>
00053 
00054 #include <inttypes.h>
00055 
00056 #include <jwsc/base/limits.h>
00057 #include <jwsc/prob/pdf.h>
00058 
00059 #include <jwsc++/base/exception.h>
00060 
00061 #include "sampler.h"
00062 #include "psf_model.h"
00063 #include "psf_moves.h"
00064 
00065 
00066 using std::isinf;
00067 using std::isnan;
00068 using namespace jwsc;
00069 using jwscxx::base::Exception;
00070 
00071 
00077 bool PSF_move::run(Sampler_move_parameters* params) throw (Exception)
00078 {
00079     params->init_proposals();
00080 
00081     PSF_model_density* density = params->psf->get_density()->clone();
00082 
00083     float mu    = params->psf->get_alpha();
00084     float sigma = density->get_alpha().get_sigma();
00085     float min   = density->get_alpha().get_min();
00086     float max   = density->get_alpha().get_max();
00087     density->set_alpha(mu, sigma, min, max);
00088 
00089     mu    = params->psf->get_beta();
00090     sigma = density->get_beta().get_sigma();
00091     min   = density->get_beta().get_min();
00092     max   = density->get_beta().get_max();
00093     density->set_beta(mu, sigma, min, max);
00094 
00095     mu    = params->psf->get_gamma();
00096     sigma = density->get_gamma().get_sigma();
00097     min   = density->get_gamma().get_min();
00098     max   = density->get_gamma().get_max();
00099     density->set_gamma(mu, sigma, min, max);
00100 
00101     params->psf_proposal->set(density->sample_alpha(), density->sample_beta(),
00102             density->sample_gamma(), params->imaging_proposal);
00103 
00104     delete density;
00105 
00106     params->psf_proposal->update_h(params->imaging_proposal);
00107 
00108     params->alternaria_proposal->update_scene(params->psf_proposal, 
00109             params->imaging_proposal);
00110 
00111     params->ll_proposal = params->alternaria_proposal->calc_log_likelihood(
00112             params->psf_proposal, params->imaging_proposal, params->data,
00113             params->data_avg);
00114 
00115     double log_alpha = params->ll_proposal;
00116     log_alpha -= params->ll;
00117     log_alpha += params->psf_proposal->get_log_prob();
00118     log_alpha -= params->psf->get_log_prob();
00119 
00120 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN
00121     assert(!isinf(log_alpha) && !isnan(log_alpha));
00122 #endif
00123 
00124     double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0));
00125 
00126     if (log_u <= log_alpha)
00127     {
00128         return true;
00129     }
00130     return false;
00131 }
00132 
00133 
00140 const std::ostringstream& PSF_move::get_info(Sampler_move_parameters* params)
00141 { 
00142     assert(params->psf);
00143     info.str("");
00144     info << std::left << std::setprecision(4) << std::setw(9)
00145          << params->psf->get_alpha()
00146          << std::left << std::setprecision(4) << std::setw(9)
00147          << params->psf->get_beta() 
00148          << std::left << std::setprecision(4) << std::setw(9)
00149          << params->psf->get_gamma(); 
00150     return info;
00151 }
00152 
00153 
00160 const std::ostringstream& PSF_move::get_proposal_info
00161 (
00162     Sampler_move_parameters* params
00163 )
00164 { 
00165     assert(params->psf_proposal);
00166     proposal_info.str("");
00167     proposal_info << std::left << std::setprecision(4) << std::setw(9)
00168                   << params->psf_proposal->get_alpha()
00169                   << std::left << std::setprecision(4) << std::setw(9)
00170                   << params->psf_proposal->get_beta() 
00171                   << std::left << std::setprecision(4) << std::setw(9)
00172                   << params->psf_proposal->get_gamma(); 
00173     return proposal_info;
00174 }
00175 
00176 
00182 bool PSF_alpha_move::run(Sampler_move_parameters* params) throw (Exception)
00183 {
00184     params->init_proposals();
00185 
00186     PSF_model_density* density = params->psf->get_density()->clone();
00187     float mu    = params->psf->get_alpha();
00188     float sigma = density->get_alpha().get_sigma();
00189     float min   = density->get_alpha().get_min();
00190     float max   = density->get_alpha().get_max();
00191     density->set_alpha(mu, sigma, min, max);
00192 
00193     params->psf_proposal->set_alpha(density->sample_alpha(), 
00194             params->imaging_proposal);
00195 
00196     delete density;
00197 
00198     params->psf_proposal->update_h(params->imaging_proposal);
00199 
00200     params->alternaria_proposal->update_scene(params->psf_proposal, 
00201             params->imaging_proposal);
00202 
00203     params->ll_proposal = params->alternaria_proposal->calc_log_likelihood(
00204             params->psf_proposal, params->imaging_proposal, params->data,
00205             params->data_avg);
00206 
00207     double log_alpha = params->ll_proposal;
00208     log_alpha -= params->ll;
00209     log_alpha += params->psf_proposal->get_log_prob();
00210     log_alpha -= params->psf->get_log_prob();
00211 
00212 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN
00213     assert(!isinf(log_alpha) && !isnan(log_alpha));
00214 #endif
00215 
00216     double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0));
00217 
00218     if (log_u <= log_alpha)
00219     {
00220         return true;
00221     }
00222     return false;
00223 }
00224 
00225 
00231 const std::ostringstream& PSF_alpha_move::get_info
00232 (
00233     Sampler_move_parameters* params
00234 )
00235 { 
00236     assert(params->psf);
00237     info.str("");
00238     info << std::left << std::setprecision(4) << std::setw(9)
00239          << params->psf->get_alpha(); 
00240     return info;
00241 }
00242 
00243 
00249 const std::ostringstream& PSF_alpha_move::get_proposal_info
00250 (
00251     Sampler_move_parameters* params
00252 ) 
00253 { 
00254     assert(params->psf_proposal);
00255     proposal_info.str("");
00256     proposal_info << std::left << std::setprecision(4) << std::setw(9)
00257                   << params->psf_proposal->get_alpha(); 
00258     return proposal_info;
00259 }
00260 
00261 
00267 bool PSF_beta_move::run(Sampler_move_parameters* params) throw (Exception)
00268 {
00269     params->init_proposals();
00270 
00271     PSF_model_density* density = params->psf->get_density()->clone();
00272     float mu    = params->psf->get_beta();
00273     float sigma = density->get_beta().get_sigma();
00274     float min   = density->get_beta().get_min();
00275     float max   = density->get_beta().get_max();
00276     density->set_beta(mu, sigma, min, max);
00277 
00278     params->psf_proposal->set_beta(density->sample_beta(),
00279             params->imaging_proposal);
00280 
00281     delete density;
00282 
00283     params->psf_proposal->update_h(params->imaging_proposal);
00284 
00285     params->alternaria_proposal->update_scene(params->psf_proposal, 
00286             params->imaging_proposal);
00287 
00288     params->ll_proposal = params->alternaria_proposal->calc_log_likelihood(
00289             params->psf_proposal, params->imaging_proposal, params->data,
00290             params->data_avg);
00291 
00292     double log_alpha = params->ll_proposal;
00293     log_alpha -= params->ll;
00294     log_alpha += params->psf_proposal->get_log_prob();
00295     log_alpha -= params->psf->get_log_prob();
00296 
00297 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN
00298     assert(!isinf(log_alpha) && !isnan(log_alpha));
00299 #endif
00300 
00301     double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0));
00302 
00303     if (log_u <= log_alpha)
00304     {
00305         return true;
00306     }
00307     return false;
00308 }
00309 
00310 
00316 const std::ostringstream& PSF_beta_move::get_info
00317 (
00318     Sampler_move_parameters* params
00319 )
00320 { 
00321     assert(params->psf);
00322     info.str("");
00323     info << std::left << std::setprecision(4) << std::setw(9)
00324          << params->psf->get_beta(); 
00325     return info;
00326 }
00327 
00328 
00334 const std::ostringstream& PSF_beta_move::get_proposal_info
00335 (
00336     Sampler_move_parameters* params
00337 ) 
00338 { 
00339     assert(params->psf_proposal);
00340     proposal_info.str("");
00341     proposal_info << std::left << std::setprecision(4) << std::setw(9)
00342                   << params->psf_proposal->get_beta(); 
00343     return proposal_info;
00344 }
00345 
00346 
00352 bool PSF_gamma_move::run(Sampler_move_parameters* params) throw (Exception)
00353 {
00354     params->init_proposals();
00355 
00356     PSF_model_density* density = params->psf->get_density()->clone();
00357     float mu    = params->psf->get_gamma();
00358     float sigma = density->get_gamma().get_sigma();
00359     float min   = density->get_gamma().get_min();
00360     float max   = density->get_gamma().get_max();
00361     density->set_gamma(mu, sigma, min, max);
00362 
00363     params->psf_proposal->set_gamma(density->sample_gamma(),
00364             params->imaging_proposal);
00365 
00366     delete density;
00367 
00368     params->psf_proposal->update_h(params->imaging_proposal);
00369 
00370     params->alternaria_proposal->update_scene(params->psf_proposal, 
00371             params->imaging_proposal);
00372 
00373     params->ll_proposal = params->alternaria_proposal->calc_log_likelihood(
00374             params->psf_proposal, params->imaging_proposal, params->data,
00375             params->data_avg);
00376 
00377     double log_alpha = params->ll_proposal;
00378     log_alpha -= params->ll;
00379     log_alpha += params->psf_proposal->get_log_prob();
00380     log_alpha -= params->psf->get_log_prob();
00381 
00382 #if defined ALTERNARIA_HAVE_ISINF && defined ALTERNARIA_HAVE_ISNAN
00383     assert(!isinf(log_alpha) && !isnan(log_alpha));
00384 #endif
00385 
00386     double log_u = log(sample_uniform_pdf_d(JWSC_MIN_LOG_ARG, 1.0));
00387 
00388     if (log_u <= log_alpha)
00389     {
00390         return true;
00391     }
00392     return false;
00393 }
00394 
00395 
00401 const std::ostringstream& PSF_gamma_move::get_info
00402 (
00403     Sampler_move_parameters* params
00404 )
00405 { 
00406     assert(params->psf);
00407     info.str("");
00408     info << std::left << std::setprecision(4) << std::setw(9)
00409          << params->psf->get_gamma(); 
00410     return info;
00411 }
00412 
00413 
00419 const std::ostringstream& PSF_gamma_move::get_proposal_info
00420 (
00421     Sampler_move_parameters* params
00422 ) 
00423 { 
00424     assert(params->psf_proposal);
00425     proposal_info.str("");
00426     proposal_info << std::left << std::setprecision(4) << std::setw(9)
00427                   << params->psf_proposal->get_gamma(); 
00428     return proposal_info;
00429 }