-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* incrementing dev version * updating news * Allow different initial values across particles also when using pairwise preference data (#406) * simplifications * used Cpp for all_topological_sorts. Much much faster, since it is recursive code * converted preferences to matrix for SMC * starting to set up preferences * done * styling * removed shuffle_unranked argument * restructured arguments to setup_rank_data for pairwise preferences * fixing some errors * removing unnecessary statement * fixed bug in augmented rankings for existing users * made a better progress reporter * updated news and description * fixed #407 (#408) * Fixing bug in Ulam distance (#411) * fixed bug and added test * simplifying * styling * Exporting exact partition function (#412) * fixing documentation typo * fixing #409 * Consistency checks with pairwise preferences (#414) * fixed issue with updated users with pairwise preferences * fixed #404
- Loading branch information
Showing
70 changed files
with
1,081 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#' @title Compute exact partition function | ||
#' | ||
#' @description For Cayley, Hamming, and Kendall distances, computationally | ||
#' tractable functions are available for the exact partition function. | ||
#' | ||
#' @param alpha Dispersion parameter. | ||
#' @param n_items Number of items. | ||
#' @param metric Distance function, one of "cayley", "hamming", or "kendall". | ||
#' | ||
#' @return The logarithm of the partition function. | ||
#' @export | ||
#' | ||
#' @references \insertAllCited{} | ||
#' | ||
#' @example inst/examples/compute_exact_partition_function_example.R | ||
#' @family partition function | ||
compute_exact_partition_function <- function( | ||
alpha, n_items, | ||
metric = c("cayley", "hamming", "kendall")) { | ||
metric <- match.arg(metric, c("cayley", "hamming", "kendall")) | ||
validate_integer(n_items) | ||
validate_positive(n_items) | ||
validate_positive(alpha) | ||
|
||
get_partition_function(alpha, n_items, metric, NULL) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,56 @@ | ||
splitpref <- function(preferences) { | ||
split( | ||
lapply(split( | ||
preferences[, c("bottom_item", "top_item"), drop = FALSE], | ||
preferences$assessor | ||
) | ||
), as.matrix) | ||
} | ||
|
||
generate_initial_ranking <- function( | ||
preferences, n_items, cl = NULL, shuffle_unranked = FALSE, | ||
random = FALSE, random_limit = 8L) { | ||
preferences, n_items, cl = NULL, max_topological_sorts) { | ||
UseMethod("generate_initial_ranking") | ||
} | ||
|
||
#' @export | ||
generate_initial_ranking.BayesMallowsTransitiveClosure <- function( | ||
preferences, n_items, cl = NULL, shuffle_unranked = FALSE, random = FALSE, | ||
random_limit = 8L) { | ||
preferences, n_items, cl = NULL, max_topological_sorts) { | ||
stopifnot(is.null(cl) || inherits(cl, "cluster")) | ||
if (n_items > random_limit && random) { | ||
stop(paste( | ||
"Number of items exceeds the limit for generation of random permutations,\n", | ||
"modify the random_limit argument to override this.\n" | ||
)) | ||
} | ||
|
||
prefs <- splitpref(preferences) | ||
|
||
if (is.null(cl)) { | ||
do.call(rbind, lapply( | ||
prefs, function(x, y, sr, r) create_ranks(as.matrix(x), y, sr, r), | ||
n_items, shuffle_unranked, random | ||
prefs, function(x, y, r) create_ranks(x, y, r), | ||
n_items, max_topological_sorts | ||
)) | ||
} else { | ||
do.call(rbind, parallel::parLapply( | ||
cl = cl, X = prefs, | ||
fun = function(x, y, sr, r) create_ranks(as.matrix(x), y, sr, r), | ||
n_items, shuffle_unranked, random | ||
fun = function(x, y, r) create_ranks(x, y, r), | ||
n_items, max_topological_sorts | ||
)) | ||
} | ||
} | ||
|
||
#' @export | ||
generate_initial_ranking.BayesMallowsIntransitive <- function( | ||
preferences, n_items, cl = NULL, shuffle_unranked = FALSE, | ||
random = FALSE, random_limit = 8L) { | ||
preferences, n_items, cl = NULL, max_topological_sorts) { | ||
n_assessors <- length(unique(preferences$assessor)) | ||
rankings <- replicate(n_assessors, sample(x = n_items, size = n_items), | ||
simplify = "numeric" | ||
) | ||
rankings <- matrix(rankings, ncol = n_items, nrow = n_assessors, byrow = TRUE) | ||
} | ||
|
||
create_ranks <- function(mat, n_items, shuffle_unranked, random) { | ||
if (!random) { | ||
g <- igraph::graph_from_edgelist(as.matrix(mat)) | ||
g <- as.integer(igraph::topo_sort(g)) | ||
|
||
all_items <- seq(from = 1, to = n_items, by = 1) | ||
|
||
if (!shuffle_unranked) { | ||
# Add unranked elements outside of the range at the end | ||
g_final <- c(g, setdiff(all_items, g)) | ||
} else { | ||
ranked_items <- unique(c(mat)) | ||
unranked_items <- setdiff(all_items, ranked_items) | ||
# Indices of ranked elements in final vector | ||
idx_ranked <- sort(sample(length(all_items), length(ranked_items))) | ||
g_final <- rep(NA, n_items) | ||
g_final[idx_ranked] <- g[g %in% ranked_items] | ||
g_final[is.na(g_final)] <- unranked_items[sample(length(unranked_items))] | ||
} | ||
|
||
# Convert from ordering to ranking | ||
return(create_ranking(rev(g_final))) | ||
} else { | ||
graph <- list() | ||
for (i in seq_len(n_items)) { | ||
graph[[i]] <- unique(mat[mat[, "top_item"] == i, "bottom_item"]) | ||
} | ||
indegree_init <- rep(0, n_items) | ||
indegree <- table(unlist(graph)) | ||
indegree_init[as.integer(names(indegree))] <- indegree | ||
attr(graph, "indegree") <- indegree_init | ||
|
||
e1 <- new.env() | ||
assign("x", list(), envir = e1) | ||
assign("num", 0L, envir = e1) | ||
all_topological_sorts(graph, n_items, e1) | ||
return(get("x", envir = e1)[[sample(get("num", envir = e1), 1)]]) | ||
} | ||
create_ranks <- function(mat, n_items, max_topological_sorts) { | ||
ret <- all_topological_sorts(mat, n_items, max_topological_sorts) | ||
u <- sample(min(max_topological_sorts, nrow(ret)), 1) | ||
ret <- ret[u, ] | ||
all_items <- seq(from = 1, to = n_items, by = 1) | ||
ranked_items <- unique(c(mat)) | ||
unranked_items <- setdiff(all_items, ranked_items) | ||
idx_ranked <- sort(sample(length(all_items), length(ranked_items))) | ||
g_final <- rep(NA, n_items) | ||
g_final[idx_ranked] <- ret[ret %in% ranked_items] | ||
g_final[is.na(g_final)] <- unranked_items[sample(length(unranked_items))] | ||
create_ranking(g_final) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.