Skip to content

Commit

Permalink
renamed Particle to StaticParticle
Browse files Browse the repository at this point in the history
  • Loading branch information
osorensen committed Apr 22, 2024
1 parent 236754b commit 7dae8c8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 46 deletions.
26 changes: 13 additions & 13 deletions src/particles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

using namespace arma;

Particle::Particle(
StaticParticle::StaticParticle(
double alpha, const vec& rho, const mat& augmented_data,
const unsigned int n_assessors, const uvec& particle_consistent) :
alpha (alpha), rho (rho), augmented_data (augmented_data),
log_aug_prob (zeros(n_assessors)),
consistent(particle_consistent),
previous_distance(zeros(n_assessors)){}

std::vector<Particle> initialize_particles(
std::vector<StaticParticle> initialize_particles(
const Rcpp::List& initial_values,
unsigned int n_particles,
const SMCData& dat
Expand All @@ -26,7 +26,7 @@ std::vector<Particle> initialize_particles(
Rcpp::stop("Wrong format for initial values for rho.");
}

std::vector<Particle> pvec;
std::vector<StaticParticle> pvec;
pvec.reserve(n_particles);

for(size_t i{}; i < n_particles; i++) {
Expand All @@ -42,15 +42,15 @@ std::vector<Particle> initialize_particles(
}

pvec.emplace_back(
Particle(alpha_samples(i), rho_samples.col(i), augmented_data,
StaticParticle(alpha_samples(i), rho_samples.col(i), augmented_data,
dat.n_assessors, particle_consistent));
}

return pvec;
}

std::vector<Particle> augment_particles(
const std::vector<Particle>& pvec_init,
std::vector<StaticParticle> augment_particles(
const std::vector<StaticParticle>& pvec_init,
const SMCData& dat, const SMCAugmentation& aug
) {
auto pvec = pvec_init;
Expand Down Expand Up @@ -139,7 +139,7 @@ std::vector<Particle> augment_particles(
return pvec;
}

cube wrapup_rho(const std::vector<std::vector<Particle>>& particle_vectors) {
cube wrapup_rho(const std::vector<std::vector<StaticParticle>>& particle_vectors) {
cube rho_samples(particle_vectors[0][0].rho.size(),
particle_vectors[0].size(),
particle_vectors.size());
Expand All @@ -152,7 +152,7 @@ cube wrapup_rho(const std::vector<std::vector<Particle>>& particle_vectors) {
return rho_samples;
}

mat wrapup_alpha(const std::vector<std::vector<Particle>>& particle_vectors) {
mat wrapup_alpha(const std::vector<std::vector<StaticParticle>>& particle_vectors) {
mat alpha_samples(particle_vectors[0].size(), particle_vectors.size());
for(size_t i{}; i < particle_vectors.size(); i++) {
for(size_t j{}; j < particle_vectors[i].size(); j++) {
Expand All @@ -163,7 +163,7 @@ mat wrapup_alpha(const std::vector<std::vector<Particle>>& particle_vectors) {
return alpha_samples;
}

cube wrapup_augmented_data(const std::vector<Particle>& pvec) {
cube wrapup_augmented_data(const std::vector<StaticParticle>& pvec) {
cube augmented_data;
if(!pvec[0].augmented_data.is_empty()) {
augmented_data.set_size(pvec[0].augmented_data.n_rows,
Expand All @@ -177,29 +177,29 @@ cube wrapup_augmented_data(const std::vector<Particle>& pvec) {
}

Rcpp::List compute_particle_acceptance(
const std::vector<std::vector<Particle>>& particle_vectors, int mcmc_steps) {
const std::vector<std::vector<StaticParticle>>& particle_vectors, int mcmc_steps) {
vec alpha_acceptance(particle_vectors.size());
vec rho_acceptance(particle_vectors.size());
vec aug_acceptance(particle_vectors.size());
for(size_t i{}; i < particle_vectors.size(); i++) {
alpha_acceptance[i] =
std::accumulate(
particle_vectors[i].begin(), particle_vectors[i].end(), 0.0,
[](double accumulator, const Particle& p) {
[](double accumulator, const StaticParticle& p) {
return accumulator + p.alpha_acceptance;
});
alpha_acceptance[i] /= particle_vectors[i].size() * mcmc_steps;
rho_acceptance[i] =
std::accumulate(
particle_vectors[i].begin(), particle_vectors[i].end(), 0.0,
[](double accumulator, const Particle& p) {
[](double accumulator, const StaticParticle& p) {
return accumulator + p.rho_acceptance;
});
rho_acceptance[i] /= particle_vectors[i].size() * mcmc_steps;
aug_acceptance[i] =
std::accumulate(
particle_vectors[i].begin(), particle_vectors[i].end(), 0.0,
[](double accumulator, const Particle& p) {
[](double accumulator, const StaticParticle& p) {
return accumulator + p.aug_acceptance / p.aug_count;
});
aug_acceptance[i] /= particle_vectors[i].size();
Expand Down
14 changes: 7 additions & 7 deletions src/particles.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

#include "smc_classes.h"

std::vector<Particle> initialize_particles(
std::vector<StaticParticle> initialize_particles(
const Rcpp::List& initial_values,
unsigned int n_particles,
const SMCData& dat
);

std::vector<Particle> augment_particles(
const std::vector<Particle>& pvec,
std::vector<StaticParticle> augment_particles(
const std::vector<StaticParticle>& pvec,
const SMCData& dat,
const SMCAugmentation& aug
);

arma::cube wrapup_rho(const std::vector<std::vector<Particle>>& particle_vectors);
arma::mat wrapup_alpha(const std::vector<std::vector<Particle>>& pvec);
arma::cube wrapup_augmented_data(const std::vector<Particle>& pvec);
arma::cube wrapup_rho(const std::vector<std::vector<StaticParticle>>& particle_vectors);
arma::mat wrapup_alpha(const std::vector<std::vector<StaticParticle>>& pvec);
arma::cube wrapup_augmented_data(const std::vector<StaticParticle>& pvec);

Rcpp::List compute_particle_acceptance(const std::vector<std::vector<Particle>>& particle_vectors, int mcmc_steps);
Rcpp::List compute_particle_acceptance(const std::vector<std::vector<StaticParticle>>& particle_vectors, int mcmc_steps);
4 changes: 2 additions & 2 deletions src/run_smc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Rcpp::List run_smc(
Priors pris{priors};
SMCAugmentation aug{compute_options, smc_options};

std::vector<std::vector<Particle>> particle_vectors(new_data.size() + 1);
std::vector<std::vector<StaticParticle>> particle_vectors(new_data.size() + 1);
particle_vectors[0] = initialize_particles(initial_values, pars.n_particles, dat);

auto pfun = choose_partition_function(
Expand All @@ -44,7 +44,7 @@ Rcpp::List run_smc(
particle_vectors[t + 1].begin(), particle_vectors[t + 1].end(),
[&pars, &dat, &pris, &aug, distfun = std::ref(distfun),
pfun = std::ref(pfun)]
(Particle& p) {
(StaticParticle& p) {
for(size_t i{}; i < pars.mcmc_steps; i++) {
pars.update_rho(p, dat, distfun);
pars.update_alpha(p, dat, pfun, distfun, pris);
Expand Down
16 changes: 8 additions & 8 deletions src/smc_augmentation_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ SMCAugmentation::SMCAugmentation(
latent_sampling_lag { read_lag(smc_options) } {}

void SMCAugmentation::reweight(
std::vector<Particle>& pvec,
std::vector<StaticParticle>& pvec,
const SMCData& dat,
const std::unique_ptr<PartitionFunction>& pfun,
const std::unique_ptr<Distance>& distfun
) const {
if(dat.any_missing || dat.augpair) {
par_for_each(
pvec.begin(), pvec.end(), [distfun = &distfun](Particle& p){
pvec.begin(), pvec.end(), [distfun = &distfun](StaticParticle& p){
p.previous_distance = distfun->get()->matdist(p.augmented_data, p.rho);
});
pvec = augment_partial(pvec, dat);
}

par_for_each(
pvec.begin(), pvec.end(), [&dat, distfun = &distfun, pfun = &pfun]
(Particle& p){
(StaticParticle& p){
double item_correction_contribution{};
if(!p.consistent.is_empty()) {
for(size_t user{}; user < dat.n_assessors - dat.num_new_obs; user++) {
Expand Down Expand Up @@ -75,15 +75,15 @@ void SMCAugmentation::reweight(
);
}

std::vector<Particle> SMCAugmentation::augment_partial(
const std::vector<Particle>& pvec, const SMCData& dat
std::vector<StaticParticle> SMCAugmentation::augment_partial(
const std::vector<StaticParticle>& pvec, const SMCData& dat
) const {
std::vector<Particle> ret{pvec};
std::vector<StaticParticle> ret{pvec};
par_for_each(
ret.begin(), ret.end(),
[&dat, partial_aug_prop = std::ref(partial_aug_prop),
pairwise_aug_prop = std::ref(pairwise_aug_prop)]
(Particle& p){
(StaticParticle& p){
for (size_t user{}; user < dat.n_assessors; user++) {
if(user < dat.n_assessors - dat.num_new_obs) {
if(p.consistent.is_empty()) continue;
Expand All @@ -109,7 +109,7 @@ std::vector<Particle> SMCAugmentation::augment_partial(
}

void SMCAugmentation::update_missing_ranks(
Particle& p, const SMCData& dat, const std::unique_ptr<Distance>& distfun) const {
StaticParticle& p, const SMCData& dat, const std::unique_ptr<Distance>& distfun) const {
if(!dat.any_missing && !dat.augpair) return;

uvec indices_to_loop = find(max(dat.timepoint) - dat.timepoint < latent_sampling_lag);
Expand Down
20 changes: 10 additions & 10 deletions src/smc_classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include "classes.h"
#include "resampler.h"

struct Particle {
Particle(double alpha, const arma::vec& rho, const arma::mat& augmented_data,
struct StaticParticle {
StaticParticle(double alpha, const arma::vec& rho, const arma::mat& augmented_data,
const unsigned int n_assessors, const arma::uvec& particle_consistent);
~Particle() = default;
~StaticParticle() = default;

double alpha;
arma::vec rho;
Expand Down Expand Up @@ -43,19 +43,19 @@ struct SMCParameters {
~SMCParameters() = default;

void update_alpha(
Particle& p,
StaticParticle& p,
const SMCData& dat,
const std::unique_ptr<PartitionFunction>& pfun,
const std::unique_ptr<Distance>& distfun,
const Priors& priors) const;

void update_rho(
Particle& p,
StaticParticle& p,
const SMCData& dat,
const std::unique_ptr<Distance>& distfun
) const;

void resample(std::vector<Particle>& pvec);
void resample(std::vector<StaticParticle>& pvec);
const unsigned int mcmc_steps;
const std::string metric;
const unsigned int n_particles;
Expand All @@ -76,12 +76,12 @@ struct SMCAugmentation {
~SMCAugmentation() = default;

void reweight(
std::vector<Particle>& pvec,
std::vector<StaticParticle>& pvec,
const SMCData& dat,
const std::unique_ptr<PartitionFunction>& pfun,
const std::unique_ptr<Distance>& distfun) const;

void update_missing_ranks(Particle& p, const SMCData& dat,
void update_missing_ranks(StaticParticle& p, const SMCData& dat,
const std::unique_ptr<Distance>& distfun) const;

const int max_topological_sorts;
Expand All @@ -90,6 +90,6 @@ struct SMCAugmentation {
const std::unique_ptr<PartialProposal> partial_aug_prop;
const std::unique_ptr<PairwiseProposal> pairwise_aug_prop;
const unsigned int latent_sampling_lag;
std::vector<Particle> augment_partial(
const std::vector<Particle>& pvec, const SMCData& dat) const;
std::vector<StaticParticle> augment_partial(
const std::vector<StaticParticle>& pvec, const SMCData& dat) const;
};
12 changes: 6 additions & 6 deletions src/smc_parameters_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ SMCParameters::SMCParameters(
resampling_threshold { smc_options["resampling_threshold"]} {}

void SMCParameters::update_alpha(
Particle& p,
StaticParticle& p,
const SMCData& dat,
const std::unique_ptr<PartitionFunction>& pfun,
const std::unique_ptr<Distance>& distfun,
Expand All @@ -38,7 +38,7 @@ void SMCParameters::update_alpha(
}

void SMCParameters::update_rho(
Particle& p,
StaticParticle& p,
const SMCData& dat,
const std::unique_ptr<Distance>& distfun) const {
auto proposal = make_new_rho(
Expand All @@ -50,11 +50,11 @@ void SMCParameters::update_rho(
}
}

void SMCParameters::resample(std::vector<Particle>& pvec) {
void SMCParameters::resample(std::vector<StaticParticle>& pvec) {

arma::vec log_inc_wgt(pvec.size());
std::transform(pvec.cbegin(), pvec.cend(), log_inc_wgt.begin(),
[](const Particle& p){ return p.log_inc_wgt; });
[](const StaticParticle& p){ return p.log_inc_wgt; });

arma::vec probs = exp(log_inc_wgt - max(log_inc_wgt) -
log(sum(exp(log_inc_wgt - max(log_inc_wgt)))));
Expand All @@ -63,9 +63,9 @@ void SMCParameters::resample(std::vector<Particle>& pvec) {

if(ess < resampling_threshold) {
rejuvenate = true;
par_for_each(pvec.begin(), pvec.end(), [](Particle& p) { p.log_inc_wgt = 0;});
par_for_each(pvec.begin(), pvec.end(), [](StaticParticle& p) { p.log_inc_wgt = 0;});
arma::ivec index = resampler->resample(probs);
std::vector<Particle> pvec_old = pvec;
std::vector<StaticParticle> pvec_old = pvec;
for(size_t i{}; i < pvec.size(); i++) pvec[i] = pvec_old[index[i]];
} else {
rejuvenate = false;
Expand Down

0 comments on commit 7dae8c8

Please sign in to comment.