Skip to content

Commit

Permalink
think the index is working now
Browse files Browse the repository at this point in the history
  • Loading branch information
dicook committed Mar 7, 2024
1 parent 335541c commit faa90ce
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* TeachingDemos removed as a Suggests, and replaced with aplpack for drawing Chernoff faces
* addition of a pre-specified ellipse can be added to the 2D display
* palette can now be a vector of values
* a new projection pursuit index for finding anomalies relative to
a null variance-covariance matrix.

# tourr 1.1.0

Expand Down
2 changes: 1 addition & 1 deletion R/anomaly-pursuit.r
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ anomaly_index <- function() {
function(mat, ell2d) {

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

12 changes: 9 additions & 3 deletions R/display-xy.r
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,15 @@ display_xy <- function(center = TRUE, axes = "center", half_range = NULL,
if (nrow(ellipse) == nrow(proj)) {

# Project ellipse into 2D
# Notation in paper: ellipse=A, ellinv=A^(-1),
# e2=P^TA^(-1)P, ell2d=B
evc <- eigen(ellipse*ellsize)
ellinv <- (evc$vectors) %*% diag(evc$values) %*% t(evc$vectors)
e2 <- t(proj) %*% ellinv %*% proj
evc2 <- eigen(e2)
ell2d <- (evc2$vectors) %*% diag(sqrt(evc2$values)) %*% t(evc2$vectors)
ell2d <- (evc2$vectors) %*% sqrt(diag(evc2$values)) %*% t(evc2$vectors)
e3 <- eigen(ell2d)
ell2dinv <- (e3$vectors) %*% diag(e3$values) %*% t(e3$vectors)

# Compute the points on an ellipse
sph <- geozoo::sphere.hollow(2, 200)$points
Expand All @@ -168,12 +172,14 @@ display_xy <- function(center = TRUE, axes = "center", half_range = NULL,
sph2 <- sph2[order(sph2[,1], decreasing=T),]
sph <- rbind(sph1, sph2)
sph <- rbind(sph, sph[1,])
sph2d <- sph%*%ell2d/half_range
sph2d <- sph%*%ell2dinv/half_range

lines(sph2d)

# Colour points outside the pD ellipse
mdst <- sqrt(mahalanobis(data, center=rep(0, ncol(data)), cov=ellipse))
mdst <- mahalanobis(data,
center=rep(0, ncol(data)),
cov=ellipse)
#mdst <- mahal_dist(data, ellipse)
anomalies <- which(mdst > ellsize)
points(x[anomalies,],
Expand Down
8 changes: 6 additions & 2 deletions R/tour-guided-anomaly.r
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ 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 <- mahalanobis(data,
center=rep(0, ncol(data)),
cov=ellipse)
#mdst <- mahal_dist(data, ellipse)
anomalies <- which(mdst > ellsize)
stopifnot(length(anomalies) > 0)
Expand All @@ -56,7 +58,9 @@ guided_anomaly_tour <- function(index_f, d = 2, alpha = 0.5, cooling = 0.99,
e2 <- t(proj) %*% ellinv %*% proj
evc2 <- eigen(e2)
ell2d <- (evc2$vectors) %*% diag(sqrt(evc2$values)) %*% t(evc2$vectors)
index_f(as.matrix(data[anomalies,]) %*% proj, ell2d)
e3 <- eigen(ell2d)
ell2dinv <- (e3$vectors) %*% diag(e3$values) %*% t(e3$vectors)
index_f(as.matrix(data[anomalies,]) %*% proj, ell2dinv)
}

cur_index <- index(current)
Expand Down

0 comments on commit faa90ce

Please sign in to comment.