Skip to content

Instantly share code, notes, and snippets.

@n8thangreen
Created February 27, 2018 12:18
Show Gist options
  • Save n8thangreen/63b68041ce85d1bd38fe275a1b451072 to your computer and use it in GitHub Desktop.
Save n8thangreen/63b68041ce85d1bd38fe275a1b451072 to your computer and use it in GitHub Desktop.
#' Branch Joint Probability
#'
#' @param probs Branch conditional probabilities (array)
#'
#' @return
#' @export
#'
#' @examples
#' branch_joint_probs(probs) * cost
#'
branch_joint_probs <- function(probs) {
struct <- apply(probs, 2, function(x) !is.na(x))
num_from_nodes <- nrow(struct)
num_to_nodes <- ncol(struct)
for (i in 1:num_from_nodes) {
for (j in 1:num_from_nodes) {
if (!is.na(probs[i, j])) {
probs[j, ] <- probs[j, ] * as.numeric(probs[i, j])
}
}
}
return(probs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment