Skip to content

Commit

Permalink
anomaly pursuit is definitely working
Browse files Browse the repository at this point in the history
  • Loading branch information
dicook committed Mar 7, 2024
1 parent a273eb8 commit 735219d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export(lda_pp)
export(linear_breaks)
export(little_tour)
export(local_tour)
export(mahal_dist)
export(manual_slice)
export(mapColors)
export(mapShapes)
Expand Down
3 changes: 2 additions & 1 deletion R/anomaly-pursuit.r
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ anomaly_index <- function() {

function(mat, ell2d) {

mat_tab <- mean(sqrt(mahalanobis(mat, center=c(0,0), cov=ell2d)))
mat_tab <- #mean(mahal_dist(mat, ell2d))
mean(sqrt(mahalanobis(mat, center=c(0,0), cov=ell2d)))
}
}

5 changes: 3 additions & 2 deletions R/display-xy.r
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' @param ellipse pxp variance-covariance matrix defining ellipse, default NULL. Useful for
#' comparing data with some null hypothesis
#' @param ellsize This can be considered the equivalent of a critical value, used to
#' scale the ellipse larger or smaller to capture more or fewer anomalies. Default 1.
#' scale the ellipse larger or smaller to capture more or fewer anomalies. Default 3.
#' @param palette name of color palette for point colour, used by \code{\link{hcl.colors}}, default "Zissou 1"
#' @param ... other arguments passed on to \code{\link{animate}} and
#' \code{\link{display_xy}}
Expand Down Expand Up @@ -66,7 +66,7 @@ display_xy <- function(center = TRUE, axes = "center", half_range = NULL,
col = "black", pch = 20, cex = 1,
edges = NULL, edges.col = "black", edges.width=1,
obs_labels = NULL,
ellipse = NULL, ellsize = 1,
ellipse = NULL, ellsize = 3,
palette="Zissou 1", ...) {
# Needed for CRAN checks
labels <- NULL
Expand Down Expand Up @@ -174,6 +174,7 @@ display_xy <- function(center = TRUE, axes = "center", half_range = NULL,

# Colour points outside the pD ellipse
mdst <- sqrt(mahalanobis(data, center=rep(0, ncol(data)), cov=ellipse))
#mdst <- mahal_dist(data, ellipse)
anomalies <- which(mdst > ellsize)
cat(length(anomalies), "\n")
points(x[anomalies,],
Expand Down
22 changes: 22 additions & 0 deletions R/linear-algebra.r
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,25 @@ orthonormalise_by <- function(x, by) {
#' @keywords algebra
#' @export
proj_dist <- function(x, y) sqrt(sum((x %*% t(x) - y %*% t(y))^2))

#' Calculate the Mahalanobis distance between points and center.
#'
#' Computes the Mahalanobis distance using a provided variance-covariance
#' matrix of observations from 0.
#
#' @param x matrix of data
#' @param vc pre-determined variance-covariance matrix
#' @keywords algebra
#' @export
mahal_dist <- function(x, vc) {
n <- dim(x)[1]
p <- dim(x)[2]
mn <- rep(0, p)
ev <- eigen(vc)
vcinv <- ev$vectors %*% diag(1/ev$values) %*% t(ev$vectors)
x <- x - matrix(rep(mn, n), ncol = p, byrow = T)
dx <- NULL
for (i in 1:n)
dx <- c(dx, x[i, ] %*% vcinv %*% as.matrix(x[i, ]))
return(dx)
}
5 changes: 3 additions & 2 deletions R/tour-guided-anomaly.r
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#' @param ellipse pxp variance-covariance matrix defining ellipse, default NULL.
#' Useful for comparing data with some hypothesized null.
#' @param ellsize This can be considered the equivalent of a critical value, used to
#' scale the ellipse larger or smaller to capture more or fewer anomalies. Default 1.
#' scale the ellipse larger or smaller to capture more or fewer anomalies. Default 3.
#' @param ... arguments sent to the search_f
#' @seealso \code{\link{slice_index}} for an example of an index functions.
#' \code{\link{search_geodesic}}, \code{\link{search_better}},
Expand All @@ -31,7 +31,7 @@
#' ellipse=cov(flea[,1:6])), ellipse=cov(flea[,1:6]), axes="off")
guided_anomaly_tour <- function(index_f, d = 2, alpha = 0.5, cooling = 0.99,
max.tries = 25, max.i = Inf,
ellipse, ellsize=1,
ellipse, ellsize=3,
search_f = search_geodesic, ...) {
h <- NULL

Expand All @@ -47,6 +47,7 @@ guided_anomaly_tour <- function(index_f, d = 2, alpha = 0.5, cooling = 0.99,
index <- function(proj) {
# Check which observations are outside pD ellipse
mdst <- sqrt(mahalanobis(data, center=rep(0, ncol(data)), cov=ellipse))
#mdst <- mahal_dist(data, ellipse)
anomalies <- which(mdst > ellsize)
stopifnot(length(anomalies) > 0)
# Project ellipse into 2D
Expand Down
4 changes: 2 additions & 2 deletions man/display_xy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 735219d

Please sign in to comment.