Skip to content

Commit

Permalink
fixed bug induced by computing the limits once, not each projection
Browse files Browse the repository at this point in the history
  • Loading branch information
dicook committed Mar 27, 2024
1 parent da2e575 commit d888e29
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
37 changes: 26 additions & 11 deletions R/display-xy.r
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ display_xy <- function(center = TRUE, axes = "center", half_range = NULL,
init <- function(data) {
half_range <<- compute_half_range(half_range, data, center)
labels <<- abbreviate(colnames(data), 3)

if (!is.null(ellipse)) {
if (nrow(ellipse) == ncol(data)) {

if (is.null(ellc))
ellc <<- qchisq(0.95, ncol(data))
else
stopifnot(ellc > 0) # Needs to be positive
if (is.null(ellmu))
ellmu <<- rep(0, ncol(data))
else
stopifnot(length(ellmu) == ncol(data)) # Right dimension
message("Using ellc = ", format(ellc, digits = 2))
}
}
}

if (!is.null(edges)) {
Expand Down Expand Up @@ -157,15 +172,15 @@ display_xy <- function(center = TRUE, axes = "center", half_range = NULL,
if (!is.null(ellipse)) {
if (nrow(ellipse) == nrow(proj)) {

if (is.null(ellc))
ellc <- qchisq(0.95, nrow(proj))
else
stopifnot(ellc > 0) # Needs to be positive
if (is.null(ellmu))
ellmu <- rep(0, nrow(proj))
else
stopifnot(length(ellmu) == nrow(proj)) # Right dimension
message("Using ellc = ", format(ellc, digits = 2))
# if (is.null(ellc))
# ellc <- qchisq(0.95, nrow(proj))
# else
# stopifnot(ellc > 0) # Needs to be positive
# if (is.null(ellmu))
# ellmu <- rep(0, nrow(proj))
# else
# stopifnot(length(ellmu) == nrow(proj)) # Right dimension
# message("Using ellc = ", format(ellc, digits = 2))

# Project ellipse into 2D
# Notation in paper: ellipse=A, ellinv=A^(-1),
Expand All @@ -178,11 +193,10 @@ display_xy <- function(center = TRUE, axes = "center", half_range = NULL,
ellinv <- (evc$vectors) %*% diag(evc$values) %*% t(evc$vectors)
e2 <- t(proj) %*% ellipse %*% proj
evc2 <- eigen(e2)
ell2d <- (evc2$vectors) %*% sqrt(diag(evc2$values*ellc)) %*% t(evc2$vectors)
ell2d <- as.matrix((evc2$vectors)) %*% diag(sqrt(evc2$values*ellc)) %*% t(as.matrix(evc2$vectors))
#e3 <- eigen(ell2d)
#ell2dinv <- (e3$vectors) %*% diag(e3$values) %*% t(e3$vectors)


# Compute the points on an ellipse
# Generate points on a circle
sph <- geozoo::sphere.hollow(2, 200)$points
Expand Down Expand Up @@ -212,6 +226,7 @@ display_xy <- function(center = TRUE, axes = "center", half_range = NULL,
cov=ellipse)
#mdst <- mahal_dist(data, ellipse)
anomalies <- which(mdst > ellc)
#cat("1 ", length(anomalies), "\n")
points(x[anomalies,],
col = "red",
pch = 4,
Expand Down
11 changes: 6 additions & 5 deletions R/tour-guided-anomaly.r
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ guided_anomaly_tour <- function(index_f, d = 2, alpha = 0.5, cooling = 0.99,
if (nrow(ellipse) == nrow(proj)) {

if (is.null(ellc))
ellc <- qchisq(0.95, nrow(proj))
ellc <<- qchisq(0.95, nrow(proj))
else
stopifnot(ellc > 0) # Needs to be positive
if (is.null(ellmu))
ellmu <- rep(0, nrow(proj))
ellmu <<- rep(0, nrow(proj))
else
stopifnot(length(ellmu) == nrow(proj)) # Right dimension
message("Using ellc = ", format(ellc, digits = 2))
#message("Using ellc = ", format(ellc, digits = 2))

# Check which observations are outside pD ellipse
mdst <- mahalanobis(data,
Expand All @@ -67,13 +67,14 @@ guided_anomaly_tour <- function(index_f, d = 2, alpha = 0.5, cooling = 0.99,
#mdst <- mahal_dist(data, ellipse)
anomalies <- which(mdst > ellc)
stopifnot(length(anomalies) > 0)
#cat(length(anomalies), "\n")

# Project ellipse into 2D
evc <- eigen(ellipse) #
ellinv <- (evc$vectors) %*% diag(evc$values) %*% t(evc$vectors)
ellinv <- (evc$vectors) %*% as.matrix(diag(evc$values)) %*% t(evc$vectors)
e2 <- t(proj) %*% ellipse %*% proj
evc2 <- eigen(e2)
ell2d <- (evc2$vectors) %*% sqrt(diag(evc2$values*ellc)) %*% t(evc2$vectors)
ell2d <- as.matrix(evc2$vectors) %*% diag(sqrt(evc2$values*ellc)) %*% t(as.matrix(evc2$vectors))

ell2dinv <- (evc2$vectors) %*% diag(evc2$values*ellc) %*% t(evc2$vectors)
ellmu2d <- t(as.matrix(ellmu)) %*% proj
Expand Down

0 comments on commit d888e29

Please sign in to comment.