Skip to content

Commit

Permalink
compute rank using floor function
Browse files Browse the repository at this point in the history
  • Loading branch information
alxsrobert committed Jun 21, 2024
1 parent 48ab189 commit 50a8153
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/moves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ Rcpp::List cpp_move_alpha(Rcpp::List param, Rcpp::List data, Rcpp::List config,
double old_loglike = 0.0, new_loglike = 0.0, p_accept = 0.0;
Rcpp::IntegerVector possible_ancestors;
Rcpp::IntegerVector t_inf_i;
int rank;

for (int i = 0; i < N; i++) {
if (alpha[i] == NA_INTEGER ){
Expand Down Expand Up @@ -355,8 +356,9 @@ Rcpp::List cpp_move_alpha(Rcpp::List param, Rcpp::List data, Rcpp::List config,
// loglike with current value
old_loglike = cpp_ll_all(data, config, param, i+1, list_custom_ll); // offset
// proposal (+/- 1)
new_alpha[i] = possible_ancestors[unif_rand() * possible_ancestors.size()];
new_kappa[i] = unif_rand() * K + 1;
rank = floor(unif_rand() * possible_ancestors.size());
new_alpha[i] = possible_ancestors[rank];
new_kappa[i] = floor(unif_rand() * K) + 1;
// loglike with current value
new_loglike = cpp_ll_all(data, config, new_param, i+1, list_custom_ll);

Expand Down Expand Up @@ -445,7 +447,7 @@ Rcpp::List cpp_move_ancestors(Rcpp::List param, Rcpp::List data, Rcpp::List conf

double old_loglike = 0.0, new_loglike = 0.0, p_accept = 0.0, runif = 0.0;
Rcpp::IntegerVector all_desc;
int j_clust;
int j_clust, rank_ances, rank_index;

for (int i = 0; i < N; i++) {
if (alpha[i] == NA_INTEGER ){
Expand Down Expand Up @@ -479,7 +481,8 @@ Rcpp::List cpp_move_ancestors(Rcpp::List param, Rcpp::List data, Rcpp::List conf
size_t new_ances;
Rcpp::IntegerVector possible_index;
if (possible_ances.size()>0){
new_ances = possible_ances[unif_rand()*possible_ances.size()];
rank_ances = floor(unif_rand() * possible_ances.size());
new_ances = possible_ances[rank_ances];

new_alpha[new_ances-1] = NA_INTEGER;

Expand All @@ -492,8 +495,9 @@ Rcpp::List cpp_move_ancestors(Rcpp::List param, Rcpp::List data, Rcpp::List conf
changes[1] = new_ances;
old_loglike = cpp_ll_all(data, config, param, changes, list_custom_ll);

size_t new_index = possible_index[unif_rand() * possible_index.size()];

rank_index = floor(unif_rand() * possible_index.size());
size_t new_index = possible_index[rank_index];

new_alpha[i] = new_index;
new_kappa[i] = new_kappa[new_ances-1];
new_kappa[new_ances-1] = NA_INTEGER;
Expand Down Expand Up @@ -604,10 +608,9 @@ Rcpp::List cpp_move_swap_cases(Rcpp::List param, Rcpp::List data,
Rcpp::IntegerVector swap_alpha, swap_t_inf, swap_kappa;
Rcpp::List swapinfo; // contains alpha, kappa and t_inf
Rcpp::IntegerVector local_cases, descendents, desc_i_swap;
Rcpp::IntegerVector tree_i, tree_j;
Rcpp::String gen_tree, gen_tree_j;
Rcpp::IntegerVector vec_swap(N);
int vect_k, i_swap, size_descendents, n_loc, i, k, j_clust;
int vect_k, i_swap, size_descendents, n_loc, i, k, j_clust, rank;
double old_loglike = 0.0, new_loglike = 0.0, p_accept = 0.0;

Rcpp::StringVector all_gen = clone(genotype); // copy of data$genotype
Expand All @@ -629,7 +632,8 @@ Rcpp::List cpp_move_swap_cases(Rcpp::List param, Rcpp::List data,

descendents = cpp_find_descendents(alpha, cluster_i, i+1);
size_descendents = descendents.size();
i_swap = descendents[unif_rand() * size_descendents];
rank = floor(unif_rand() * size_descendents);
i_swap = descendents[rank];
// The local likelihood is defined as the likelihood computed for the
// cases affected by the swap; these include:
// - 'i_swap'
Expand Down Expand Up @@ -695,7 +699,7 @@ Rcpp::List cpp_move_swap_cases(Rcpp::List param, Rcpp::List data,
}

if(swap_ances.size() > 0){
i_swap = swap_ances[unif_rand() * swap_ances.size()];
i_swap = swap_ances[floor(unif_rand() * swap_ances.size())];
desc_i_swap = cpp_find_descendents(alpha, cluster_i, i_swap);

int t_inf_i = new_t_inf[i];
Expand Down

0 comments on commit 50a8153

Please sign in to comment.