Skip to content

Commit

Permalink
fixed #404
Browse files Browse the repository at this point in the history
  • Loading branch information
osorensen committed Mar 25, 2024
1 parent 20b5634 commit 857087c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: BayesMallows
Type: Package
Title: Bayesian Preference Learning with the Mallows Rank Model
Version: 2.1.1.9006
Version: 2.1.1.9007
Authors@R: c(person("Oystein", "Sorensen",
email = "oystein.sorensen.1985@gmail.com",
role = c("aut", "cre"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# BayesMallows (development versions)

* The SMC function now check for consistency with previous latent ranks for
existing users also when data arrive in the form of pairwise preferences.
* A function compute_exact_partition_function() is now added, which returns the
logarithm of the exact partition function for Cayley, Hamming, and Kendall
distance.
Expand Down
15 changes: 14 additions & 1 deletion src/particles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,20 @@ std::vector<Particle> augment_particles(
}
} else if (dat.augpair) {
for(auto index : dat.updated_match) {
pvec[i].consistent(index) = 0;
pvec[i].consistent(index) = 1;
vec augmented = pvec[i].augmented_data(span::all, span(index));
auto items_above_for_user = dat.items_above[index];
size_t j{};
while(pvec[i].consistent(index) == 1 && j < items_above_for_user.size()) {
size_t k{};
while(pvec[i].consistent(index) == 1 && k < items_above_for_user[j].size()) {
if(augmented(items_above_for_user[j][k] - 1) > augmented(j)) {
pvec[i].consistent(index) = 0;
}
k++;
}
j++;
}
}

pvec[i].augmented_data.resize(dat.n_items, dat.rankings.n_cols);
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-smc_pairwise.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test_that("update_mallows works with existing users updating their data", {

expect_equal(
mean(m2$alpha_samples),
2.82143649083478
2.79625839950885
)

expect_equal(
Expand Down
22 changes: 16 additions & 6 deletions work-docs/pairwise_smc.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
library(BayesMallows)
m0 <- compute_mallows(
data = setup_rank_data(preferences = subset(beach_preferences, assessor == 2)),
data = setup_rank_data(preferences = subset(beach_preferences, assessor == 2 & bottom_item < 5 & top_item < 5),
n_items = 5),
compute_options = set_compute_options(nmc = 2000, burnin = 500)
)

m1 <- update_mallows(
model = m0,
new_data = setup_rank_data(
preferences = beach_preferences[1:10, ],
preferences = data.frame(
assessor = 1,
bottom_item = c(5, 4, 3, 2),
top_item = c(4, 3, 2, 1)
),
user_ids = 1,
n_items = 15
n_items = 5
),
smc_options = set_smc_options(n_particles = 2)
)

m2 <- update_mallows(
model = m1,
new_data = setup_rank_data(
preferences = beach_preferences[11:20, ],
user_ids = 1
setup_rank_data(
preferences = data.frame(
assessor = 1,
bottom_item = c(5, 4, 3, 2, 5),
top_item = c(4, 3, 2, 1, 3)
),
user_ids = 1,
n_items = 5
),
smc_options = set_smc_options(n_particles = 2)
)

0 comments on commit 857087c

Please sign in to comment.