Introduction to the R package TDA
Abstract
We present a short tutorial and introduction to the R package TDA, which provides tools for Topological Data Analysis. The package focuses on statistical analysis of persistent homology and density clustering. The package includes implementations of functions that extract topological information about the underlying space from data, such as the distance function, the distance to a measure, the kNN density estimator, the kernel density estimator, and the kernel distance. The salient topological features of the sublevel sets (or superlevel sets) of these functions can be quantified with persistent homology. This package provides an R interface for the efficient algorithms of the C++ libraries GUDHI, Dionysus, and PHAT, including a function for the persistent homology of the Rips filtration, and one for the persistent homology of sublevel sets (or superlevel sets) of arbitrary functions evaluated over a grid of points. The statistical significance of persistent homology features can be analyzed with functions that implement methods from Fasy et al. (2014), Chazal et al. (2015b) and Chazal et al. (2017). The R package TDA also includes the implementation of an algorithm for density clustering, which allows us to identify the spatial structure of a probability density function and visualize it by means of a dendrogram, the cluster tree.
Keywords: Topological Data Analysis, Persistent Homology, Density Clustering
1 Introduction
Topological Data Analysis (TDA) refers to a collection of methods for finding topological structure in data (Carlsson, 2009). Recent advances in computational topology have made it possible to actually compute topological invariants from data. The input of these procedures typically takes the form of a point cloud, regarded as possibly noisy observations from an unknown lower-dimensional set whose interesting topological features were lost during sampling. The output is a collection of data summaries that are used to estimate the topological features of .
One approach to TDA is persistent homology (Edelsbrunner and Harer, 2010), a method for studying the homology at multiple scales simultaneously. More precisely, it provides a framework and efficient algorithms to quantify the evolution of the topology of a family of nested topological spaces. Given a real-valued function , such as the ones described in Section 2, persistent homology describes how the topology of the lower level sets (or superlevel sets ) changes as increases from to (or decreases from to ). This information is encoded in the persistence diagram, a multiset of points in the plane, each corresponding to the birth and death of a homological feature that existed for some interval of .
This paper is devoted to the presentation of the R package TDA, which provides a user-friendly interface for the efficient algorithms of the C++ libraries GUDHI (Maria, 2014), Dionysus (Morozov, 2007), and PHAT (Bauer et al., 2012).
In Section 2, we describe how to compute some widely studied functions that, starting from a point cloud, provide some topological information about the underlying space: the distance function (distFct), the distance to a measure function (dtm), the k Nearest Neighbor density estimator (knnDE), the kernel density estimator (kde), and the kernel distance (kernelDist). Section 3 is devoted to the computation of persistence diagrams: the function gridDiag can be used to compute persistent homology of sublevel sets (or superlevel sets) of functions evaluated over a grid of points; the function ripsDiag returns the persistence diagram of the Rips filtration built on top of a point cloud.
One of the key challenges in persistent homology is to find a way to isolate the points of the persistence diagram representing the topological noise. Statistical methods for persistent homology provide an alternative to its exact computation. Knowing with high confidence that an approximated persistence diagrams is close to the true–computationally infeasible–diagram is often enough for practical purposes. Fasy et al. (2014), Chazal et al. (2015b), and Chazal et al. (2017) propose several statistical methods to construct confidence sets for persistence diagrams and other summary functions that allow us to separate topological signal from topological noise. The methods are implemented in the TDA package and described in Section 3.
Finally, the TDA package provides the implementation of an algorithm for density clustering. This method allows us to identify and visualize the spatial organization of the data, without specific knowledge about the data generating mechanism and in particular without any a priori information about the number of clusters. In Section 4, we describe the function clusterTree, that, given a density estimator, encodes the hierarchy of the connected components of its superlevel sets into a dendrogram, the cluster tree (Kpotufe and von Luxburg, 2011; Kent, 2013).
2 Distance Functions and Density Estimators
As a first toy example to using the TDA package, we show how to compute distance functions and density estimators over a grid of points. The setting is the typical one in TDA: a set of points has been sampled from some distribution and we are interested in recovering the topological features of the underlying space by studying some functions of the data. The following code generates a sample of 400 points from the unit circle and constructs a grid of points over which we will evaluate the functions.
> library("TDA")> X <- circleUnif(400)> Xlim <- c(-1.6, 1.6); Ylim <- c(-1.7, 1.7); by <- 0.065> Xseq <- seq(Xlim[1], Xlim[2], by = by)> Yseq <- seq(Ylim[1], Ylim[2], by = by)> Grid <- expand.grid(Xseq, Yseq)
The TDA package provides implementations of the following functions:
- •
The distance function is defined for each as and is computed for each point of the Grid with the following code:
> distance <- distFct(X = X, Grid = Grid)
- •
Given a probability measure , the distance to measure (DTM) is defined for each as
where , and and are tuning parameters. As m0 increases, DTM function becomes smoother, so m0 can be understood as a smoothing parameter. r affects less but also changes DTM function as well. The default value of r is 2. The DTM can be seen as a smoothed version of the distance function. See (Chazal et al., 2011, Definition 3.2) and (Chazal et al., 2015c, Equation (2)) for a formal definition of the "distance to measure" function.
Given , the empirical version of the DTM is
where and is the set containing the nearest neighbors of among .
For more details, see (Chazal et al., 2011) and (Chazal et al., 2015c).
The DTM is computed for each point of the Grid with the following code:
> m0 <- 0.1> DTM <- dtm(X = X, Grid = Grid, m0 = m0)
- •
The Nearest Neighbor density estimator, for each , is defined as
where is the volume of the Euclidean dimensional unit ball and is the Euclidean distance form point to its th closest neighbor among the points of . It is computed for each point of the Grid with the following code:
> k <- 60> kNN <- knnDE(X = X, Grid = Grid, k = k)
- •
The Gaussian Kernel Density Estimator (KDE), for each , is defined as
where is a smoothing parameter. It is computed for each point of the Grid with the following code:
> h <- 0.3> KDE <- kde(X = X, Grid = Grid, h = h)
- •
The Kernel distance estimator, for each , is defined as
where is the Gaussian Kernel with smoothing parameter . The Kernel distance is computed for each point of the Grid with the following code:
> h <- 0.3> Kdist <- kernelDist(X = X, Grid = Grid, h = h)
For this 2 dimensional example, we can visualize the functions using persp form the graphics package. For example the following code produces the KDE plot in Figure 1:
> persp(Xseq, Yseq,+ matrix(KDE, ncol = length(Yseq), nrow = length(Xseq)), xlab = "",+ ylab = "", zlab = "", theta = -20, phi = 35, ltheta = 50,+ col = 2, border = NA, main = "KDE", d = 0.5, scale = FALSE,+ expand = 3, shade = 0.9)
2.1 Bootstrap Confidence Bands
We can construct a confidence band for a function using the bootstrap algorithm, which we briefly describe using the kernel density estimator:
- 1.
Given a sample , compute the kernel density estimator ;
- 2.
Draw from (with replacement), and compute , where is the density estimator computed using ;
- 3.
Repeat the previous step times to obtain ;
- 4.
Compute
- 5.
The confidence band for is
Fasy et al. (2014) and Chazal et al. (2017) prove the validity of the bootstrap algorithm for kernel density estimators, distance to measure, and kernel distance, and use it in the framework of persistent homology. The bootstrap algorithm is implemented in the function bootstrapBand, which provides the option of parallelizing the algorithm (parallel = TRUE) using the package parallel. The following code computes a 90% confidence band for , showed in Figure 2.
> band <- bootstrapBand(X = X, FUN = kde, Grid = Grid, B = 100,+ parallel = FALSE, alpha = 0.1, h = h)
3 Persistent Homology
We provide an informal description of the implemented methods of persistent homology. We assume the reader is familiar with the basic concepts and, for a rigorous exposition, we refer to the textbook Edelsbrunner and Harer (2010).
3.1 Persistent Homology Over a Grid
In this section, we describe how to use the gridDiag function to compute the persistent homology of sublevel (and superlevel) sets of the functions described in Section 2. The function gridDiag evaluates a given real valued function over a triangulated grid, constructs a filtration of simplices using the values of the function, and computes the persistent homology of the filtration. From version 1.2, gridDiag works in arbitrary dimension. The core of the function is written in C++ and the user can choose to compute persistence diagrams using either the C++ library GUDHI, Dionysus, or PHAT.
The following code computes the persistent homology of the superlevel sets (sublevel = FALSE) of the kernel density estimator (FUN = kde, h = 0.3) using the point cloud stored in the matrix X from the previous example. The same code would work for the other functions defined in Section 2 (it is sufficient to replace kde and its smoothing parameter h with another function and the corresponding parameter). The function gridDiag returns an object of the class "diagram". The other inputs are the features of the grid over which the kde is evaluated (lim and by), the smoothing parameter h, and a logical variable that indicates whether a progress bar should be printed (printProgress).
> DiagGrid <- gridDiag(+ X = X, FUN = kde, h = 0.3, lim = cbind(Xlim, Ylim), by = by,+ sublevel = FALSE, library = "Dionysus", location = TRUE,+ printProgress = FALSE)
We plot the data and the diagram, using the function plot, implemented as a standard S3 method for objects of the class "diagram". The following command produces the third plot in Figure 3.
> plot(DiagGrid[["diagram"]], band = 2 * band[["width"]],+ main = "KDE Diagram")
The option (band = 2 * band[["width"]]) produces a pink confidence band for the persistence diagram, using the confidence band constructed for the corresponding kernel density estimator in the previous section. The features above the band can be interpreted as representing significant homological features, while points in the band are not significantly different from noise. The validity of the bootstrap confidence band for persistence diagrams of KDE, DTM, and Kernel Distance derive from the Stability Theorem (Chazal et al., 2012) and is discussed in detail in Fasy et al. (2014) and Chazal et al. (2017).
The function plot for the class "diagram" provide the options of rotating the diagram (rotated = TRUE), drawing the barcode in place of the diagram (barcode = TRUE), as well as other standard graphical options. See Figure 4.
> par(mfrow = c(1, 2), mai = c(0.8, 0.8, 0.3, 0.1))
> plot(DiagGrid[["diagram"]], rotated = TRUE, band = band[["width"]],
+ main = "Rotated Diagram")
> plot(DiagGrid[["diagram"]], barcode = TRUE, main = "Barcode")
3.2 Rips Diagrams
The Vietoris-Rips complex consists of simplices with vertices in and diameter at most . In other words, a simplex is included in the complex if each pair of vertices in is at most apart. The sequence of Rips complexes obtained by gradually increasing the radius creates a filtration.
The ripsDiag function computes the persistence diagram of the Rips filtration built on top of a point cloud.
The user can choose to compute the Rips filtration using either the C++ library GUDHI or Dionysus. Then for computing the persistence diagram from the Rips filtration, the user can use either the C++ library GUDHI, Dionysus, or PHAT.
The following code generates 60 points from two circles:
> Circle1 <- circleUnif(60)> Circle2 <- circleUnif(60, r = 2) + 3> Circles <- rbind(Circle1, Circle2)
We specify the limit of the Rips filtration and the max dimension of the homological features we are interested in (0 for components, 1 for loops, 2 for voids, etc.):
> maxscale <- 5 # limit of the filtration> maxdimension <- 1 # components and loops
and we generate the persistence diagram:
> DiagRips <- ripsDiag(X = Circles, maxdimension, maxscale,+ library = c("GUDHI", "Dionysus"), location = TRUE, printProgress = FALSE)Alternatively, using the option (dist = "arbitrary") in ripsDiag(), the input X can be an matrix of distances.
This option is useful when the user wants to consider a Rips filtration constructed using an arbitrary distance and is currently only available for the option (library = "Dionysus").
Finally we plot the data and the diagram, as in Figure 5.:
3.3 Alpha Complex Persistence Diagram
For a finite set of points , the Alpha complex is a simplicial subcomplex of the Delaunay complex of consisting of simplices of circumradius less than or equal to . For each , let be its Voronoi cell, i.e. , and be the closed ball with center and radius . Let consists of be the intersection of earh ball of radius with the voronoi cell of , i.e. . Then is defined as
See (Edelsbrunner and Harer, 2010, Section 3.4) and (Rouvreau, 2015). The sequence of Alpha complexes obtained by gradually increasing the parameter creates an Alpha complex filtration.
The alphaComplexDiag function computes the Alpha complex filtration built on top of a point cloud, using the C++ library GUDHI. Then for computing the persistence diagram from the Alpha complex filtration, the user can use either the C++ library GUDHI, Dionysus, or PHAT.
We first generate 30 points from a circle:
> X <- circleUnif(n = 30)
and the following code compute the persistence diagram of the alpha complex filtration using the point cloud X, with printing its progress (printProgress = FALSE). The function alphaComplexDiag returns an object of the class "diagram".
> # persistence diagram of alpha complex> DiagAlphaCmplx <- alphaComplexDiag(+ X = X, library = c("GUDHI", "Dionysus"), location = TRUE,+ printProgress = TRUE)
# Generated complex of size: 1150% 10 20 30 40 50 60 70 80 90 100%|----|----|----|----|----|----|----|----|----|----|***************************************************# Persistence timer: Elapsed time [ 0.000064 ] seconds
And we plot the diagram in Figure 6.
> # plot
> par(mfrow = c(1, 2))
> plot(DiagAlphaCmplx[["diagram"]], main = "Alpha complex persistence diagram")
> one <- which(DiagAlphaCmplx[["diagram"]][, 1] == 1)
> one <- one[which.max(
+ DiagAlphaCmplx[["diagram"]][one, 3] - DiagAlphaCmplx[["diagram"]][one, 2])]
> plot(X, col = 1, main = "Representative loop")
> for (i in seq(along = one)) {
+ for (j in seq_len(dim(DiagAlphaCmplx[["cycleLocation"]][[one[i]]])[1])) {
+ lines(DiagAlphaCmplx[["cycleLocation"]][[one[i]]][j, , ], pch = 19,
+ cex = 1, col = i + 1)
+ }
+ }
> par(mfrow = c(1, 1))
3.4 Persistence Diagram of Alpha Shape
The Alpha shape complex is the polytope with its boundary consisting of -exposed simplices, where a simplex is -exposed if there is an open ball of radius such that and . Suppose is filled with ice cream, then consider scooping out the ice cream with sphere-shaped spoon of radius without touching the points . is the remaining polytope with straightening round surfaces. See (Fischer, 2005) and (Edelsbrunner and Mücke, 1994). The sequence of Alpha shape complexes obtained by gradually increasing the parameter creates an Alpha shape complex filtration.
The alphaShapeDiag function computes the persistence diagram of the Alpha shape filtration built on top of a point cloud in 3 dimension, using the C++ library GUDHI. Then for computing the persistence diagram from the Alpha shape filtration, the user can use either the C++ library GUDHI, Dionysus, or PHAT. Currently the point data cloud should lie in 3 dimension.
We first generate 30 points from a cylinder:
> n <- 30> X <- cbind(circleUnif(n = n), runif(n = n, min = -0.1, max = 0.1))
and the following code compute the persistence diagram of the alpha shape filtration using the point cloud X, with printing its progress (printProgress = TRUE). The function alphaShapeDiag returns an object of the class "diagram".
> DiagAlphaShape <- alphaShapeDiag(+ X = X, maxdimension = 1, library = c("GUDHI", "Dionysus"), location = TRUE,+ printProgress = TRUE)
# Generated complex of size: 5670% 10 20 30 40 50 60 70 80 90 100%|----|----|----|----|----|----|----|----|----|----|***************************************************# Persistence timer: Elapsed time [ 0.000306 ] seconds
And we plot the diagram and first two dimension of data in Figure 7.
> par(mfrow = c(1, 2))
> plot(DiagAlphaShape[["diagram"]])
> plot(X[, 1:2], col = 2, main = "Representative loop of alpha shape filtration")
> one <- which(DiagAlphaShape[["diagram"]][, 1] == 1)
> one <- one[which.max(
+ DiagAlphaShape[["diagram"]][one, 3] - DiagAlphaShape[["diagram"]][one, 2])]
> for (i in seq(along = one)) {
+ for (j in seq_len(dim(DiagAlphaShape[["cycleLocation"]][[one[i]]])[1])) {
+ lines(
+ DiagAlphaShape[["cycleLocation"]][[one[i]]][j, , 1:2], pch = 19,
+ cex = 1, col = i)
+ }
+ }
> par(mfrow = c(1, 1))
3.5 Persistence Diagrams from Filtration
Rather than computing persistence diagrams from built-in function, it is also possible to compute persistence diagrams from a user-defined filtration. A filtration consists of simplicial complex and the filtration values on each simplex. The functions ripsDiag, alphaComplexDiag, alphaShapeDiag have their counterparts for computing corresponding filtrations instead of persistence diagrams: namely, ripsFiltration corresponds to the Rips filtration built on top of a point cloud, alphaComplexFiltration to the alpha complex filtration, and alphaShapeFiltration to the alpha shape filtration.
We first generate 100 points from a circle:
> X <- circleUnif(n = 100)
Then, after specifying the limit of the Rips filtration and the max dimension of the homological features, the following code compute the Rips filtration using the point cloud X.
> maxscale <- 0.4 # limit of the filtration> maxdimension <- 1 # components and loops> FltRips <- ripsFiltration(X = X, maxdimension = maxdimension,+ maxscale = maxscale, dist = "euclidean", library = "GUDHI",+ printProgress = TRUE)
# Generated complex of size: 2624
One way of defining a user-defined filtration is to build a filtration from a simplicial complex and function values on the vertices. The function funFiltration takes function values (FUNvalues) and simplicial complex (cmplx) as input, and build a filtration, where a filtration value on a simplex is defined as the maximum of function values on the vertices of the simplex.
In the following example, the function funFiltration construct a filtration from a Rips complex and the DTM function values on data points.
> m0 <- 0.1> dtmValues <- dtm(X = X, Grid = X, m0 = m0)> FltFun <- funFiltration(FUNvalues = dtmValues, cmplx = FltRips[["cmplx"]])
Once the filtration is computed, the function filtrationDiag computes the persistence diagram from the filtration. The user can choose to compute the persistence diagram using either the C++ library GUDHI or Dionysus.
> DiagFltFun <- filtrationDiag(filtration = FltFun, maxdimension = maxdimension,+ library = "Dionysus", location = TRUE, printProgress = TRUE)
0% 10 20 30 40 50 60 70 80 90 100%|----|----|----|----|----|----|----|----|----|----|***************************************************# Persistence timer: Elapsed time [ 0.000946 ] seconds
Then we plot the data and the diagram in Figure 8.
> par(mfrow = c(1, 2), mai=c(0.8, 0.8, 0.3, 0.3))
> plot(X, pch = 16, xlab = "",ylab = "")
> plot(DiagFltFun[["diagram"]], diagLim = c(0, 1))
3.6 Bottleneck and Wasserstein Distances
Standard metrics for measuring the distance between two persistence diagrams are the bottleneck distance and the th Wasserstein distance (Edelsbrunner and Harer, 2010). The TDA package includes the functions bottleneck and wasserstein, which are R wrappers of the functions “bottleneck_distance" and “wasserstein_distance" of the C++ library Dionysus.
We generate two persistence diagrams of the Rips filtrations built on top of the two (separate) circles of the previous example,
> Diag1 <- ripsDiag(Circle1, maxdimension = 1, maxscale = 5)> Diag2 <- ripsDiag(Circle2, maxdimension = 1, maxscale = 5)and we compute the bottleneck distance and the nd Wasserstein distance between the two diagrams. In the following code, the option dimension = 1 specifies that the distances between diagrams are computed using only one dimensional features (loops).
> print(bottleneck(Diag1[["diagram"]], Diag2[["diagram"]],+ dimension = 1))
[1] 1.409681
> print(wasserstein(Diag1[["diagram"]], Diag2[["diagram"]], p = 2,+ dimension = 1))
[1] 2.514505
3.7 Landscapes and Silhouettes
Persistence landscapes and silhouettes are real-valued functions that further summarize the information contained in a persistence diagram. They have been introduced and studied in Bubenik (2012), Chazal et al. (2015b), and Chazal et al. (2015a). We briefly introduce the two functions.
Landscape. The persistence landscape is a collection of continuous, piecewise linear functions that summarizes a persistence diagram. To define the landscape, consider the set of functions created by tenting each each point representing a birth-death pair in the persistence diagram as follows:
| (1) |
We obtain an arrangement of piecewise linear curves by overlaying the graphs of the functions ; see Figure 9 (left). The persistence landscape of is a summary of this arrangement. Formally, the persistence landscape of is the collection of functions
| (2) |
where max is the th largest value in the set; in particular, max is the usual maximum function. see Figure 9 (middle).
Silhouette. Consider a persistence diagram with off diagonal points . For every we define the power-weighted silhouette
The value can be thought of as a trade-off parameter between uniformly treating all pairs in the persistence diagram and considering only the most persistent pairs. Specifically, when is small, is dominated by the effect of low persistence features. Conversely, when is large, is dominated by the most persistent features; see Figure 9 (right).
The landscape and silhouette functions can be evaluated over a one-dimensional grid of points tseq using the functions landscape and silhouette. In the following code, we use the persistence diagram from Figure 5 to construct the corresponding landscape and silhouette for one-dimensional features (dimension = 1). The option (KK = 1) specifies that we are interested in the 1st landscape function, and (p = 1) is the power of the weights in the definition of the silhouette function.
> maxscale <- 5> tseq <- seq(0, maxscale, length = 1000) #domain> Land <- landscape(DiagRips[["diagram"]], dimension = 1, KK = 1, tseq)> Sil <- silhouette(DiagRips[["diagram"]], p = 1, dimension = 1, tseq)
The functions landscape and silhouette return real valued vectors, which can be simply plotted with plot(tseq, Land, type = "l"); plot(tseq, Sil, type = "l"). See Figure 10.
3.8 Confidence Bands for Landscapes and Silhouettes
Recent results in Chazal et al. (2015b) and Chazal et al. (2015a) show how to construct confidence bands for landscapes
and silhouettes, using a bootstrap algorithm (multiplier bootstrap).
This strategy is useful in the following scenario.
We have a very large dataset with points. There is a diagram and landscape corresponding to some filtration built on the data. When is large, computing is prohibitive. Instead, we draw subsamples, each of size . We compute a diagram and a landscape for each subsample yielding landscapes . (Assuming is much smaller than , these subsamples are essentially independent and identically distributed.) Then we compute , an estimate of , which can be regarded as an approximation of . The function multipBootstrap uses the landscapes to construct a confidence band for . The same strategy is valid for silhouette functions. We illustrate the method with a simple example.
First we sample points from two circles:
> N <- 4000> XX1 <- circleUnif(N / 2)> XX2 <- circleUnif(N / 2, r = 2) + 3> X <- rbind(XX1, XX2)
Then we specify the number of subsamples , the subsample size , and we create the objects that will store the diagrams and landscapes:
> m <- 80 # subsample size> n <- 10 # we will compute n landscapes using subsamples of size m> tseq <- seq(0, maxscale, length = 500) #domain of landscapes> #here we store n Rips diags> Diags <- list()> #here we store n landscapes> Lands <- matrix(0, nrow = n, ncol = length(tseq))
For times, we subsample from the large point cloud, compute Rips diagrams and the corresponding 1st landscape functions (KK = 1), using 1 dimensional features (dimension = 1):
> for (i in seq_len(n)) {+ subX <- X[sample(seq_len(N), m), ]+ Diags[[i]] <- ripsDiag(subX, maxdimension = 1, maxscale = 5)+ Lands[i, ] <- landscape(Diags[[i]][["diagram"]], dimension = 1,+ KK = 1, tseq)+ }
Finally we use the landscapes to construct a 95% confidence band for the mean landscape
> bootLand <- multipBootstrap(Lands, B = 100, alpha = 0.05,+ parallel = FALSE)which is plotted by the following code. See Figure 11.
> plot(tseq, bootLand[["mean"]], main = "Mean Landscape with 95% band")> polygon(c(tseq, rev(tseq)),+ c(bootLand[["band"]][, 1], rev(bootLand[["band"]][, 2])),+ col = "pink")> lines(tseq, bootLand[["mean"]], lwd = 2, col = 2)
3.9 Selection of Smoothing Parameters
An unsolved problem in topological inference is how to choose the smoothing parameters, for example h for KDE and m0 for DTM.
Chazal et al. (2017) suggest the following method, that we describe here for the kernel density estimator, but works also for the kernel distance and the distance to measure.
Let be the lifetimes of the features of a persistence diagram at scale . Let be the width of the confidence band for the kernel density estimator at scale , as described in Section 2.1. We define two quantities that measure the amount of significant information at level :
- •
The number of significant features, ;
- •
The total significant persistence, .
These measures are small when is small since is large. On the other hand, they are small when is large since then all the features of the KDE are smoothed out. Thus we have a kind of topological bias-variance tradeoff. We choose to maximize or .
The method is implemented in the function maxPersistence, as shown in the following toy example. First, we sample 1600 point from two circles (plus some clutter noise) and we specify the limits of the grid over which the KDE is evaluated:
> XX1 <- circleUnif(600)> XX2 <- circleUnif(1000, r = 1.5) + 2.5> noise <- cbind(runif(80, -2, 5), runif(80, -2, 5))> X <- rbind(XX1, XX2, noise)> # Grid limits> Xlim <- c(-2, 5)> Ylim <- c(-2, 5)> by <- 0.2
Then we specify a sequence of smoothing parameters among which we will select the optimal one, the number of bootstrap iterations and the level of the confidence bands to be computed:
> parametersKDE <- seq(0.1, 0.6, by = 0.05)> B <- 50 # number of bootstrap iterations. Should be large.> alpha <- 0.1 # level of the confidence bands
The function maxPersistence can be parallelized (parallel = TRUE) and a progress bar can be printed (printProgress = TRUE):
> maxKDE <- maxPersistence(kde, parametersKDE, X,+ lim = cbind(Xlim, Ylim), by = by, sublevel = FALSE,+ B = B, alpha = alpha, parallel = TRUE,+ printProgress = TRUE, bandFUN = "bootstrapBand")
0 10 20 30 40 50 60 70 80 90 100|----|----|----|----|----|----|----|----|----|----|***************************************************
The S3 methods summary and plot are implemented for the class "maxPersistence". We can display the values of the parameters that maximize the two criteria:
> print(summary(maxKDE))
Call:maxPersistence(FUN = kde, parameters = parametersKDE, X = X, lim = cbind(Xlim, Ylim), by = by, sublevel = FALSE, B = B, alpha = alpha, bandFUN = "bootstrapBand", parallel = TRUE, printProgress = TRUE)The number of significant features is maximized by[1] 0.25 0.30The total significant persistence is maximized by[1] 0.1and produce the summary plot of Figure 12.
> par(mfrow = c(1, 2), mai = c(0.8, 0.8, 0.35, 0.3))
> plot(X, pch = 16, cex = 0.5, main = "Two Circles")
> plot(maxKDE, main = "Max Persistence - KDE")
4 Density Clustering
The last example of this vignette illustrates the use of the function clusterTree, which is an implementation of Algorithm 1 in Kent et al. (2013).
First, we briefly describe the task of density clustering; we defer the reader to Kent (2013) for a more rigorous and complete description. Let be the density of the probability distribution generating the observed sample . For a threshold value , the corresponding super level set of is , and its -dimensional subsets are called high-density regions. The high-density clusters of are the maximal connected subsets of . By considering all the level sets simultaneously (from to ), we can record the evolution and the hierarchy of the high-density clusters of . This naturally leads to the notion of the cluster density tree of (see, e.g., Hartigan (1981)), defined as the collection of sets , which satisfies the tree property: implies that or or . We will refer to this construction as the -tree. Alternatively, Kent et al. (2013) introduced the -tree and -tree, which facilitate the interpretation of the tree by precisely encoding the probability content of each tree branch rather than the density level. Cluster trees are particularly useful for high dimensional data, whose spatial organization is difficult to represent.
We illustrate the strategy with a simple example. First we generate a 2D point cloud from three (not so well) separated clusters (see top left plot of Figure 13):
> X1 <- cbind(rnorm(300, 1, .8), rnorm(300, 5, 0.8))> X2 <- cbind(rnorm(300, 3.5, .8), rnorm(300, 5, 0.8))> X3 <- cbind(rnorm(300, 6, 1), rnorm(300, 1, 1))> XX <- rbind(X1, X2, X3)
Then we use the function clusterTree to compute cluster trees using the k Nearest Neighbors density estimator (k = 100 nearest neighbors) and the Gaussian kernel density estimator, with smoothing parameter h.
> Tree <- clusterTree(XX, k = 100, density = "knn",+ printProgress = FALSE)> TreeKDE <- clusterTree(XX, k = 100, h = 0.3, density = "kde",+ printProgress = FALSE)
Note that, even when kde is used to estimate the density, we have to provide the option (k = 100), so that the algorithm can compute the connected components at each level of the density using a k Nearest Neighbors graph.
The "clusterTree" objects Tree and TreeKDE contain information about the -tree, -tree and -tree. The function plot for objects of the class "clusterTree" produces the plots in Figure 13.
> plot(Tree, type = "lambda", main = "lambda Tree (knn)")> plot(Tree, type = "kappa", main = "kappa Tree (knn)")> plot(TreeKDE, type = "lambda", main = "lambda Tree (kde)")> plot(TreeKDE, type = "kappa", main = "kappa Tree (kde)")
References
- PHAT, a software library for persistent homology. Note: https://bitbucket.org/phat-code/phat/ Cited by: §1.
- Statistical topological data analysis using persistence landscapes. arXiv preprint arXiv:1207.6437. Cited by: §3.7.
- Topology and data. Bulletin of the American Mathematical Society 46 (2), pp. 255–308. Cited by: §1.
- Geometric inference for probability measures. Foundations of Computational Mathematics 11 (6), pp. 733–751. Cited by: 2nd item, 2nd item.
- The structure and stability of persistence modules. arXiv preprint arXiv:1207.3674. Cited by: §3.1.
- Subsampling methods for persistent homology. In Proceedings of the 32nd International Conference on Machine Learning, F. Bach and D. Blei (Eds.), Proceedings of Machine Learning Research, Vol. 37, Lille, France, pp. 2143–2151. External Links: Link Cited by: §3.7, §3.8.
- Robust topological inference: distance to a measure and kernel distance. J. Mach. Learn. Res. 18, pp. Paper No. 159, 40. External Links: ISSN 1532-4435, MathReview Entry Cited by: §1, §2.1, §3.1, §3.9, Abstract.
- Stochastic convergence of persistence landscapes and silhouettes. J. Comput. Geom. 6 (2), pp. 140–161. External Links: Document, Link, MathReview (Jussi S. Klemelä) Cited by: §1, §3.7, §3.8, Abstract.
- Rates of convergence for robust geometric inference. CoRR abs/1505.07602. External Links: Link Cited by: 2nd item, 2nd item.
- Computational topology: an introduction. American Mathematical Society. Cited by: §1, §3.3, §3.6, §3.
- Three-dimensional alpha shapes. ACM Trans. Graph. 13 (1), pp. 43–72. External Links: ISSN 0730-0301, Link, Document Cited by: §3.4.
- Confidence sets for persistence diagrams. Ann. Statist. 42 (6), pp. 2301–2339. External Links: ISSN 0090-5364, Document, Link, MathReview (Jonathan Cutler) Cited by: §1, §2.1, §3.1, Abstract.
- Introduction to alpha shapes. Note: http://www.cs.uu.nl/docs/vakken/ddm/texts/Delaunay/alphashapes.pdf Cited by: §3.4.
- Consistency of single linkage for high-density clusters. Journal of the American Statistical Association 76 (374), pp. 388–394. Cited by: §4.
- DeBaCl: a python package for interactive density-based clustering. arXiv preprint arXiv:1307.8136. Cited by: §4, §4.
- Level set trees for applied statistics. Ph.D. Thesis, Department of Statistics, Carnegie Mellon University. Cited by: §1, §4.
- Pruning nearest neighbor cluster trees. In International Conference on Machine Learning (ICML), Cited by: §1.
- GUDHI, simplicial complexes and persistent homology packages. Note: https://project.inria.fr/gudhi/software/ Cited by: §1.
- Dionysus, a c++ library for computing persistent homology. Note: https://www.mrzv.org/software/dionysus/ Cited by: §1.
- Alpha complex. In GUDHI User and Reference Manual, External Links: Link Cited by: §3.3.
Affiliation
Brittany T. Fasy
Computer Science Department
Montana State University
Website: http://www.cs.montana.edu/brittany/
Email: brittany.fasy@alumni.duke.edu
Jisu Kim
Department of Statistics
Seoul National University
Website: https://jkim82133.github.io/
Email: jkim82133@snu.ac.kr
Fabrizio Lecci
Email: fabrizio.lecci@gmail.com
Clément Maria
DataShape Group
INRIA Sophia Antipolis
Website: http://www-sop.inria.fr/members/Clement.Maria/
Email: clement.maria@inria.fr
David L. Millman
Computer Science Department
Montana State University
Website: http://millman.us
Email: david.millman@montana.edu
Vincent Rouvreau
GUDHI Group
INRIA Saclay
Email: vincent.rouvreau@inria.fr
TopStat Group
Carnegie Mellon University
Website: http://www.stat.cmu.edu/topstat/
Email: topstat@stat.cmu.edu