From 857087cf019afa5d3b42f2de6bea04e0a76582f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98ystein=20S=C3=B8rensen?= Date: Mon, 25 Mar 2024 14:58:51 +0100 Subject: [PATCH] fixed #404 --- DESCRIPTION | 2 +- NEWS.md | 2 ++ src/particles.cpp | 15 ++++++++++++++- tests/testthat/test-smc_pairwise.R | 2 +- work-docs/pairwise_smc.R | 22 ++++++++++++++++------ 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b0c49d86..edfeaf7d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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"), diff --git a/NEWS.md b/NEWS.md index 997e4ef0..c5e8b328 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/src/particles.cpp b/src/particles.cpp index d3462a2b..b6f53951 100644 --- a/src/particles.cpp +++ b/src/particles.cpp @@ -105,7 +105,20 @@ std::vector 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); diff --git a/tests/testthat/test-smc_pairwise.R b/tests/testthat/test-smc_pairwise.R index 96164dc4..70efd96e 100644 --- a/tests/testthat/test-smc_pairwise.R +++ b/tests/testthat/test-smc_pairwise.R @@ -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( diff --git a/work-docs/pairwise_smc.R b/work-docs/pairwise_smc.R index f8b58cd3..dabf058b 100644 --- a/work-docs/pairwise_smc.R +++ b/work-docs/pairwise_smc.R @@ -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) )