# Head/tail breaks

Head/tail breaks is a clustering algorithm for data with a heavy-tailed distribution such as power laws and lognormal distributions. The heavy-tailed distribution can be simply referred to the scaling pattern of far more small things than large ones, or alternatively numerous smallest, a very few largest, and some in between the smallest and largest. The classification is done through dividing things into large (or called the head) and small (or called the tail) things around the arithmetic mean or average, and then recursively going on for the division process for the large things or the head until the notion of far more small things than large ones is no longer valid, or with more or less similar things left only. Head/tail breaks is not just for classification, but also for visualization of big data by keeping the head, since the head is self-similar to the whole. Head/tail breaks can be applied not only to vector data such as points, lines and polygons, but also to raster data like digital elevation model (DEM).

## Motivation

The head/tail breaks is motivated by inability of conventional classification methods such as equal intervals, quantiles, geometric progressions, standard deviation, and natural breaks - commonly known as Jenks natural breaks optimization or k-means clustering to reveal the underlying scaling or living structure with the inherent hierarchy (or heterogeneity) characterized by the recurring notion of far more small things than large ones. Note that the notion of far more small things than large one is not only referred to geometric property, but also to topological and semantic properties. In this connection, the notion should be interpreted as far more unpopular (or less-connected) things than popular (or well-connected) ones, or far more meaningless things than meaningful ones. Head/tail breaks uses the mean or average to dichotomize a dataset into small and large values, rather than to characterize classes by average values, which is unlike k-means clustering or natural breaks. Through the head/tail breaks, a dataset is seen as a living structure with an inherent hierarchy with far more smalls than larges, or recursively perceived as the head of the head of the head and so on. It opens up new avenues of analyzing data from a holistic and organic point of view while considering different types of scales and scaling in spatial analysis.

## Method

Given some variable X that demonstrates a heavy-tailed distribution, there are far more small x than large ones. Take the average of all xi, and obtain the first mean m1. Then calculate the second mean for those xi greater than m1, and obtain m2. In the same recursive way, we can get m3 depending on whether the ending condition of no longer far more small x than large ones is met. For simplicity, we assume there are three means, m1, m2, and m3. This classification leads to four classes: [minimum, m1], (m1, m2], (m2, m3], (m3, maximum]. In general, it can be represented as a recursive function as follows:
The resulting number of classes is referred to as ht-index, an alternative index to fractal dimension for characterizing complexity of fractals or geographic features: the higher the ht-index, the more complex the fractals.

### Threshold or its sensitivity

The criterion to stop the iterative classification process using the head/tail breaks method is that the remaining data (i.e., the head part) are not heavy-tailed, or simply, the head part is no longer a minority (i.e., the proportion of the head part is no longer less than a threshold such as 40%). This threshold is suggested to be 40% by Jiang et al. (2013), just as the codes above (i.e., (length/head)/length(data) ≤ 40%). This process is called head/tail breaks 1.0. But sometimes a larger threshold, for example 50% or more, can be used, as Jiang and Yin (2014) noted in another article: "this condition can be relaxed for many geographic features, such as 50 percent or even more". However, all heads' percentage on average must be smaller than 40% (or 41, 42%), indicating far more small things than large ones. Many real-world data cannot be fit into a perfect long tailed distribution, therefore its threshold can be relaxed structurally. In head/tail breaks 2.0 the threshold only applies to the overall heads' percentage. This means that the percentages of all heads related to the tails should be around 40% on average. Individual classes can have any percentage spit around the average, as long as this averages out as a whole. For example, if there is data distributed in such a way that it has a clearly defined head and tail during the first and second iteration (length(head)/(length(data)<20%) but a much less well defined long tailed distribution for the third iteration (60% in the head), head/tail breaks 2.0 allows the iteration to continue into the fourth iteration which can be distributed 30% head - 70% tail again and so on. As long as the overall threshold is not surpassed the head/tail breaks classification holds.

### Rank-size plot and RA index

A good tool to display the scaling pattern, or the heavy-tailed distribution, is the rank-size plot, which is a scatter plot to display a set of values according to their ranks. With this tool, a new index termed as the ratio of areas (RA) in a rank-size plot was defined to characterize the scaling pattern. The RA index has been successfully used in the estimation of traffic conditions. However, the RA index can only be used as a complementary method to the ht-index, because it is ineffective to capture the scaling structure of geographic features.

### Other Indices based on the head/tail breaks

In addition to the ht-index, the following indices are also derived with the head/tail breaks.
* CRG-index. It is developed as a more sensitive ht-index to capture the slight changes of geographic features. In contrast to the ht-index, which is an integer, CRG-index is a real number.
* Unified metrics. Two unified metrics (UM1 and UM2) were proposed in an AAAG paper for characterizing the fractal nature of geographic features. One can be used to answer the question of “I know there are far more small things than large ones, but how small (or large) are these small (or large) things?”, whereas the other one to answer “I know there are far more small things than large ones, but how many more?”
* Fht-index: It is a fractional ht-index, which is able to capture fractional hierarchy. The fht-index might be of help for creating an intermediate scale between two consecutive map scales, leading to so called continuous map scales.

## Applications

Instead of more or less similar things, there are far more small things than large ones surrounding us. Given the ubiquity of the scaling pattern, head/tail breaks is found to be of use to statistical mapping, map generalization, cognitive mapping and even perception of beauty . It helps visualize big data, since big data are likely to show the scaling property of far more small things than large ones. Essentially geographic phenomena can be scaleful or scale-free. Scaleful phenomena can be explained by conventional mathematical or geographical operations, but scale-free phenomena can not. Head/tail breaks can be used to characterize the scale-free phenomena, which are in the majority. The visualization strategy is to recursively drop out the tail parts until the head parts are clear or visible enough. In addition, it helps delineate cities or natural cities to be more precise from various geographic information such as street networks, social media geolocation data, and nighttime images.

### Characterizing the imbalance

As the head/tail breaks method can be used iteratively to obtain head parts of a data set, this method actually captures the underlying hierarchy of the data set. For example, if we divide the array (19, 8, 7, 6, 2, 1, 1, 1, 0) with the head/tail breaks method, we can get two head parts, i.e., the first head part (19, 8, 7, 6) and the second head part (19). These two head parts as well as the original array form a three-level hierarchy:
* the 1st level (19),
* the 2nd level (19, 8, 7, 6), and
* the 3rd level (19, 8, 7, 6, 2, 1, 1, 1, 0).
The number of levels of the above-mentioned hierarchy is actually a characterization of the imbalance of the example array, and this number of levels has been termed as the ht-index. With the ht-index, we are able to compare degrees of imbalance of two data sets. For example, the ht-index of the example array (19, 8, 7, 6, 2, 1, 1, 1, 0) is 3, and the ht-index of another array (19, 8, 8, 8, 8, 8, 8, 8, 8) is 2. Therefore, the degree of imbalance of the former array is higher than that of the latter array.

### Delineating natural cities

The use of fractals in modelling human geography has for a longer period been seen as useful in measuring the spatial distribution of human settlements. Head/tail breaks can be used to do just that with a concept called natural cities. The term ‘natural cities’ refers to the human settlements or human activities in general on Earth's surface that are naturally or objectively defined and delineated from massive geographic information based on head/tail division rule, a non-recursive form of head/tail breaks. Such geographic information could be from various sources, such as massive street junctions and street ends, a massive number of street blocks, nighttime imagery and social media users’ locations etc. Based on these the different urban forms and configurations detected in cities can be derived. Distinctive from conventional cities, the adjective ‘natural’ could be explained not only by the sources of natural cities, but also by the approach to derive them . Natural cities are derived from a meaningful cutoff averaged from a massive number of units extracted from geographic information. Those units vary according to different kinds of geographic information, for example the units could be area units for the street blocks and pixel values for the nighttime images. A natural cities model has been created using ArcGIS model builder, it follows the same process of deriving natural cities from location-based social media, namely, building up huge triangular irregular network (TIN) based on the point features (street nodes in this case) and regarding the triangles which are smaller than a mean value as the natural cities. These natural cities can also be created from other open access information like OpenStreetMap and further be used as an alternative delineation of administrative boundaries. Scaling law can also at the same time correctly be identified and the administrative borders can be created to respect this by the delineation of the natural cities. This type methodology can help urban geographers and planners by correctly identifying the effective urban territorial scope of the areas they work in.
Natural cities can vary depending on the scale on which the natural cities are delineated, which is why optimally they have to be based on data from the whole world. Due to that being computationally impossible, a country or county scale is suggested as alternative. Due to the scale-free nature of natural cities and the data they are based on there are also possibilities to use the natural cities method for further measurements. One of the main advantages of natural cities is that it is derived bottom-up instead of top-down. That means that the borders are determined by the data of something physical rather than determined by an administrative government or administration. For example by calculating the natural cities of a natural city recursively the dense areas within a natural city are identified. These can be seen as city centers for example. By using the natural cities method in this way further border delineations can be made dependent on the scale the natural cities were generated from. Natural cities derived from smaller regional areas will provide less accurate but still usable results in certain analysis, like for example determining urban expansion over time. As mentioned before though, optimally natural cities should be based on a massive amount of for example street intersections for an entire country or even the world. This is because natural cities are based on the wisdom of crowds thinking, which needs the biggest set of available data for the best results. Also note that the structure of natural cities can be considered to be fractal in nature.
It is important when head/tail breaks are being used to generate natural cities, that the data is not aggregated afterwards. For example, the amount of generated natural cities can only be known after they are generated. It is not possible to use a pre-defined number of cities for an area or country and aggregate the results of the natural cities to administratively determined city borders. Naturally natural cities should follow Zipf's law, if they do not, the area is most likely too small, or data has probably been processed wrongly. An example of this is seen in a research where head/tail breaks were used to extract natural cities, but they were aggregated to administrative borders, which following that concluded that the cities do not follow Zipf's law. This happens more often in science, where papers actually produce results which are actually false.

### Color rendering DEM

Current color renderings for DEM or density map are essentially based on conventional classifications such as natural breaks or equal intervals, so they disproportionately exaggerate high elevations or high densities. As a matter of fact, there are not so many high elevations or high-density locations. It was found that coloring based head/tail breaks is more favorable than those by other classifications.

### Mapping scaling hierarchy

The pattern of far more small things than large ones frequently recurs in geographical data. A spiral layout inspired by the golden ratio or Fibonacci sequence can help visualize this recursive notion of scaling hierarchy and the different levels of scale.In other words, from the smallest to the largest scale, a map can be seen as a map of a map of a map, and so on.

### Further applications

Other applications of Head/tail breaks:
* Serving as a method for efficiently estimating the absolute Boltzmann's entropy of numerical raster data
* Quantifying the multiscale representation of a polyline
* Increasing computational efficiency in data-flow analysis by emphasizing the head part of the flow dataset
* Temporal analysis of urban expansion related to the thermal environment
* Image analysis where anisotropy is measured in point patterns extracted with a digital pulse transform with the use of head/tail breaks
* Visualizing and analyzing spatial patterns in bilateral trade
* To identify urban function graphs, note that this paper applies head/tail breaks on a Gaussian kernel density estimation which reduces the accuracy of the head/tail breaks method. Essentially a natural cities approach is taken but the initial data chosen to perform head/tail breaks on has been reduced beforehand. For a better representation of urban function graphs head/tail breaks may be applied as the first step in delineating these areas.
* Analyzing structures or hotspots naturally occurring within data to highlight areas of interest (Based on natural cities).
** (Over)Tourism analysis based on short term rentals (like AirBnB) by creating hotspots out of the distribution of rented out apartments.
** Measuring tourism intensification based on the fractal dimension delineated using natural cities
** Identifying urban hotspots based on taxi stops, where people are most likely to get out at major landmarks or public transport transfer areas. Head/tail breaks are applied to separate the less dense stops where few people exit, from the major stops where the most people exit.
** Determining traffic hotspots or congestion zones, which can be used to in turn determine road pricing. Natural cities is an effective approach when finding these areas.
** Using natural cities to identify the polycentric pattern of Chinese cities, i.e. identifying the multiple dense centers of activity found in cities.
** Determining how city growth affects the thermal environment in cities using natural cities as a measurement tool.
** Identifying resilient urban areas or systems.
** Identifying polycentric cities with night time imagery, which can be used to evaluate the urban development levels.
** Quantifying urban expansion by using POI data as indicators of built up areas.
** Detecting hierarchical crowd data with different clustering algorithms.
** Using twitter data obtained during the COVID-19 pandemic to analyze spatial hotspots with natural cities.
** Reducing carbon emissions by dividing urban spaces using head/tail breaks.
** Using remote sensing to identify core city expansion.
** Predicting urban growth with fractal dimension logistic curve modeling and head/tail breaks.
* Head/tail breaks can serve as a main indicator that phenomena are distributed long tailed and that Paretian thinking should favor Gaussian thinking in geographic spaces. For example within biodiversity and pedodiversity studies where there seem to be fractal relationships such as taxa-area relationships. Complementary to this the polygons of soil and vegetation maps also show scaling within their structures. This can be identified and highlighted by using head/tail breaks.
* In image feature and texture extraction, certain algorithms like the discrete pulse transform, where LULU smoothing is used to extract the features, can be sped up by using head/tail breaks in the algorithm by separating large features and smaller features more effectively.
* By analysing hierarchies in urban patterns (i.e. Streets, building outlines), visual salience can be determined because it follows a similar principle, namely a scaling law, or long tailed distribution. Head/tail breaks are an aid in determining the hierarchies present because of the scaling nature of urban morphology and could be of further use when studying urban street network applications. This is especially the case for accessibility analysis, combined with space syntax head/tail breaks allow for an in depth understanding of street network structure.
* Urban structures, like street networks have been proven to be fractal in nature. An important point to note is that this structure is not consisting of only one defined fractal, it is characterized by a multifractal complex network. This means that on different scales, the defined fractal can change. Head/tail breaks can be used to determine the structure of the complex network over different scales, as it adjusts based on the data with each new hierarchy.
* Head/tail breaks as a classification method can be used to visualize growth or spread patterns in for example a global pandemic, like the Covid-19 one. By using head/tail breaks, main spread events can be effectively mapped and visualized where locations with a high infection rate are highlighted specifically due to them being in the highest class. The risk measurement model based on the head/tail breaks approach can describe the spatial and temporal evolution characteristics of the risk of COVID-19, and can better predict the risk trend of future epidemics in each city and identify the risk of future epidemics even during low incidence periods.
* Rock fracture networks are properties of rocks which are very important in rock engineering with applications in mining, shale gas development or slope stability. Because of the self-similarity characteristics of these fractures combined with the fractal nature they inhibit, head/tail breaks provide accurate measurements and analysis into these rock fracture networks.
* Classifying tourist attractions into most visited, least visited and something in between for further research into the optimal route of sightseeing busses.
* Measuring the heterogeneity of crime distribution quantitatively while simultaneously considering the statistical and geometrical characteristics of crime distribution.
* Examining the urban sustainability of socioeconomic and environmental dynamics. The natural cities serve as the basic urban form measures to objectively capture the spatial patterns of the sustainability change.

## Software implementation

The following implementations are available under Free/Open Source Software licenses.
* HT calculator: a winform application for obtaining related metrics of head/tail breaks applying on a single data array.
* HT in JavaScript: a JavaScript implementation for applying head/tail breaks on a single data array.
* HT Mapping tool: a function in the free plug-in Axwoman 6.3 to ArcMap 10.2 that conducts geo-data symbolization automatically based on the head/tail breaks classification.
* HT in Python: Python and JavaScript code for the head/tail breaks algorithm. It works great for choropleth map coloring.
* pysal.esda.mapclassify: Python classification schemes for choropleth mapping, including head/tail breaks map classification.
* smoomapy 0.1.9: Brings smoothed maps through python.
* Ht-index calculator: A PostgreSQL function for calculating ht-index (also see).
* RA calculator: Software for calculating the ratio of areas (RA) in a rank-size plot (also see).
* HT 2.0 calculator: An excel sheet calculator which calculates both head/tail breaks 1.0 and head/tail breaks 2.0 with a version for smaller datasets and a version for very large (binned) datasets.
* classInt: R package that implements several methods for choosing univariate class intervals for mapping or other graphic purposes also includes head/tail breaks map classification.
* Natural cities python script: For generating natural cities using open source python packages
* Natural cities python script2: For generating natural cities from input point data using open source python packages