Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter object and adaptation #19

Merged
merged 2 commits into from Aug 6, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion ChangeLog
@@ -1,4 +1,24 @@
2017-08-04 <adam.johansen@gmail.com>
2017-08-06 Leah South <leah.south@hdr.qut.edu.au>

* inst/include/algParam.h: A base class to contain
additional algorithm parameters and virtual functions to
adapt them.
* inst/include/moveset.h: Changing the MCMC step to perform
multiple iterations within the library rather than on user end.
* inst/include/sampler.h: Adding an additional template
parameter for additional parameters and allowing for
optional adaptation at multiple points.

* src/LinReg.cpp: Boolean return for MCMC function and
setting MCMC repeats separately.
* src/LinReg.cpp: Idem.
* inst/include/LinReg.h: Idem.
* inst/include/LinReg_LA.h: Idem.

* src/RcppExports.cpp: Regenerated.
* R/RcppExports.R: Idem.

2017-08-04 Adam M. Johansen <adam.johansen@gmail.com>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for cleaning that up.


* R/LinRegLA.R: Suppressing redundant call to data().
* R/LinReg.R: Idem.
Expand Down
16 changes: 8 additions & 8 deletions R/RcppExports.R
Expand Up @@ -2,26 +2,26 @@
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

blockpfGaussianOpt_impl <- function(data, part, lag) {
.Call(`_RcppSMC_blockpfGaussianOpt_impl`, data, part, lag)
.Call('RcppSMC_blockpfGaussianOpt_impl', PACKAGE = 'RcppSMC', data, part, lag)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are using an Rcpp version older than 0.12.12.

Please upgrade, and re-create the file, or explain why you must use on old version, possibly over at the Rcpp repo with an issue ticket there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking up on that. I've fixed it up now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

}

LinRegLA_impl <- function(Data, intemps, lNumber) {
.Call(`_RcppSMC_LinRegLA_impl`, Data, intemps, lNumber)
LinReg_impl <- function(Data, lNumber) {
.Call('RcppSMC_LinReg_impl', PACKAGE = 'RcppSMC', Data, lNumber)
}

LinReg_impl <- function(Data, lNumber) {
.Call(`_RcppSMC_LinReg_impl`, Data, lNumber)
LinRegLA_impl <- function(Data, intemps, lNumber) {
.Call('RcppSMC_LinRegLA_impl', PACKAGE = 'RcppSMC', Data, intemps, lNumber)
}

nonLinPMMH_impl <- function(data, lNumber, lMCMCits) {
.Call(`_RcppSMC_nonLinPMMH_impl`, data, lNumber, lMCMCits)
.Call('RcppSMC_nonLinPMMH_impl', PACKAGE = 'RcppSMC', data, lNumber, lMCMCits)
}

pfLineartBS_impl <- function(data, part, usef, fun) {
.Call(`_RcppSMC_pfLineartBS_impl`, data, part, usef, fun)
.Call('RcppSMC_pfLineartBS_impl', PACKAGE = 'RcppSMC', data, part, usef, fun)
}

pfNonlinBS_impl <- function(data, part) {
.Call(`_RcppSMC_pfNonlinBS_impl`, data, part)
.Call('RcppSMC_pfNonlinBS_impl', PACKAGE = 'RcppSMC', data, part)
}

2 changes: 1 addition & 1 deletion inst/include/LinReg.h
Expand Up @@ -43,5 +43,5 @@ namespace LinReg {
double logPosterior(long lTime, const rad_state & value);
void fInitialise(rad_state & value, double & logweight);
void fMove(long lTime, rad_state & value, double & logweight);
int fMCMC(long lTime, rad_state & value, double & logweight);
bool fMCMC(long lTime, rad_state & value, double & logweight);
}
2 changes: 1 addition & 1 deletion inst/include/LinReg_LA.h
Expand Up @@ -48,7 +48,7 @@ namespace LinReg_LA {

void fInitialise(rad_state & value, double & logweight);
void fMove(long lTime, rad_state & value, double & logweight);
int fMCMC(long lTime, rad_state & value, double & logweight);
bool fMCMC(long lTime, rad_state & value, double & logweight);

double integrand_ps(long,const rad_state &, void *);
double width_ps(long, void *);
Expand Down
63 changes: 63 additions & 0 deletions inst/include/algParam.h
@@ -0,0 +1,63 @@
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// algParam: A class that holds all of the algorithm parameters that can be adapted
//
// Copyright (C) 2017 Dirk Eddelbuettel, Adam Johansen and Leah South
//
// This file is part of RcppSMC.
//
// RcppSMC is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// RcppSMC is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RInside. If not, see <http://www.gnu.org/licenses/>.


//! \file
//! \brief A base class containing algorithm parameters and virtual
//! functions to adapt them.
//!

#ifndef __SMC_ALGPARAM_H
#define __SMC_ALGPARAM_H 1.0

#include <population.h>


namespace smc {

/// A base class which contains the algorithm parameters and virtual functions to adapt them.
template <class Space, class Params> class algParam {

protected:
Params param;

public:

/// Free the workspace allocated for the algorithm parameters.
virtual ~algParam() {
}

/// Holder function for updates to be done before the move step.
virtual void updateForMove(const population<Space> & pop) {}

/// Holder function for updates to be done before the MCMC step.
virtual void updateForMCMC(const population<Space> & pop, double acceptProb, int nResampled, int & nRepeats) {}

/// Holder function for updates to be done at the end of each iteration.
virtual void updateEnd(const population<Space> & pop) {}

/// Returns the parameters.
const Params & GetParams(void) const {return param;}
};
}


#endif
34 changes: 19 additions & 15 deletions inst/include/moveset.h
Expand Up @@ -46,25 +46,25 @@ namespace smc {
long (*pfMoveSelect)(long , const Space &, const double &);
///The functions which perform actual moves on a single particle.
void (**pfMoves)(long, Space &, double &);
///A Markov Chain Monte Carlo move for a single particle.
int (*pfMCMC)(long, Space &,double &);
///One iteration of a Markov Chain Monte Carlo move for a single particle.
bool (*pfMCMC)(long, Space &,double &);

public:
///Create a completely unspecified moveset
moveset();
///Create a reduced moveset with a single move
moveset(void (*pfInit)(Space &, double &),
void (*pfNewMoves)(long, Space &,double &),
int (*pfNewMCMC)(long,Space &,double &));
bool (*pfNewMCMC)(long,Space &,double &));
///Create a fully specified moveset
moveset(void (*pfInit)(Space &, double &),long (*pfMoveSelector)(long , const Space &, const double &),
long nMoves, void (**pfNewMoves)(long, Space &,double &),
int (*pfNewMCMC)(long,Space &, double &));
bool (*pfNewMCMC)(long,Space &, double &));

///Initialise the population of particles
void DoInit(population<Space> & pFrom, long N);
///Perform an MCMC move on the particles
int DoMCMC(long lTime, population<Space> & pFrom, long N);
bool DoMCMC(long lTime, population<Space> & pFrom, long N, int nRepeats, int & nAccepted);
///Select an appropriate move at time lTime and apply it to pFrom
void DoMove(long lTime, population<Space> & pFrom,long N);

Expand All @@ -78,7 +78,7 @@ namespace smc {

/// \brief Set the MCMC function
/// \param pfNewMCMC The function which performs an MCMC move
void SetMCMCFunction(int (*pfNewMCMC)(long,Space &,double &)) {pfMCMC = pfNewMCMC;}
void SetMCMCFunction(bool (*pfNewMCMC)(long,Space &,double &)) {pfMCMC = pfNewMCMC;}

/// \brief Set the move selection function
/// \param pfMoveSelectNew returns the index of move to perform at the specified time given the specified particle
Expand Down Expand Up @@ -114,7 +114,7 @@ namespace smc {
template <class Space>
moveset<Space>::moveset(void (*pfInit)(Space &, double &),
void (*pfNewMoves)(long, Space &,double &),
int (*pfNewMCMC)(long,Space &,double &))
bool (*pfNewMCMC)(long,Space &,double &))
{
SetInitialisor(pfInit);
SetMoveSelectionFunction(NULL);
Expand All @@ -133,7 +133,7 @@ namespace smc {
template <class Space>
moveset<Space>::moveset(void (*pfInit)(Space &, double &),long (*pfMoveSelector)(long ,const Space &, const double &),
long nMoves, void (**pfNewMoves)(long, Space &,double &),
int (*pfNewMCMC)(long,Space &,double &))
bool (*pfNewMCMC)(long,Space &,double &))
{
SetInitialisor(pfInit);
SetMoveSelectionFunction(pfMoveSelector);
Expand All @@ -157,16 +157,20 @@ namespace smc {
}

template <class Space>
int moveset<Space>::DoMCMC(long lTime, population<Space> & pFrom, long N)
{ if(pfMCMC) {
int count = 0;
for (long i=0; i<N; i++){
count += pfMCMC(lTime,pFrom.GetValueRefN(i),pFrom.GetLogWeightRefN(i));
bool moveset<Space>::DoMCMC(long lTime, population<Space> & pFrom, long N, int nRepeats, int & nAccepted)
{
if(pfMCMC && nRepeats>0) {
nAccepted = 0;
for (int j=0; j<nRepeats; j++){
for (long i=0; i<N; i++){
nAccepted += pfMCMC(lTime,pFrom.GetValueRefN(i),pFrom.GetLogWeightRefN(i));
}
}
return count;
return TRUE;
}
else {
return 0;
nAccepted = 0;
return FALSE; // an MCMC step was not performed because there was no user defined MCMC step or because the number of MCMC repeats was zero.
}
}

Expand Down