Visibility graphs and partial visibility curves with tsvisibility

library(tsvisibility)

1. From a time series to a graph

The natural visibility algorithm links two observations when the straight line joining them passes above all the observations in between; the horizontal visibility algorithm links them when every observation in between is lower than both.

x <- c(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5)
g_nat <- vgraph(x, type = "natural")
g_hor <- vgraph(x, type = "horizontal")
g_nat
#> Natural visibility graph: 11 nodes, 19 links, mean degree 3.4545
g_hor
#> Horizontal visibility graph: 11 nodes, 14 links, mean degree 2.5455
head(g_hor$edges)
#>      from to
#> [1,]    1  2
#> [2,]    1  3
#> [3,]    2  3
#> [4,]    3  4
#> [5,]    3  5
#> [6,]    4  5
op <- par(mfrow = c(1, 2))
plot(g_nat)
plot(g_hor)

par(op)

The adjacency matrix can be dense or sparse, and the graph can be converted to igraph to use any network measure:

visibility_matrix(c(1, 2, 1, 2, 1))
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    1    0    0    0
#> [2,]    1    0    1    1    0
#> [3,]    0    1    0    1    0
#> [4,]    0    1    1    0    1
#> [5,]    0    0    0    1    0
if (requireNamespace("igraph", quietly = TRUE)) {
  ig <- vgraph_to_igraph(runif(200))
  c(clustering = igraph::transitivity(ig, type = "global"),
    mean_distance = igraph::mean_distance(ig))
}
#>    clustering mean_distance 
#>     0.3510436     5.7420603

2. Classical exact results

For an uncorrelated series the degree distribution of the horizontal visibility graph is \(P(k) = \frac13 (2/3)^{k-2}\) and its mean degree is 4.

set.seed(1)
u <- runif(10000)
d <- degree_distribution(u)
head(cbind(d, theory = hvg_degree_iid(d$k)))
#>   k count      p     theory
#> 1 1     1 0.0001 0.00000000
#> 2 2  3357 0.3357 0.33333333
#> 3 3  2224 0.2224 0.22222222
#> 4 4  1446 0.1446 0.14814815
#> 5 5   961 0.0961 0.09876543
#> 6 6   675 0.0675 0.06584362
hvg_lambda(u, kmax = 12)$lambda   # log(3/2) = 0.405
#> [1] 0.3969659

Chaotic series have \(\lambda < \log(3/2)\) and are time irreversible:

z <- logistic_series(r = 4, n = 10000, x0 = 0.2)
hvg_lambda(z, kmax = 12)$lambda
#> [1] 0.3699599
visibility_irreversibility(u)
#> [1] 0.0009224551
visibility_irreversibility(z)
#> [1] 0.3387267

Periodic orbits of the period-doubling cascade give Feigenbaum graphs, whose degree distribution is known exactly:

p8 <- logistic_series(logistic_superstable(3), 800, x0 = 0.3)
merge(degree_distribution(p8), feigenbaum_degree_distribution(3),
      by = "k", suffixes = c("_empirical", "_theory"))
#>   k count p_empirical p_theory
#> 1 2   399     0.49875    0.500
#> 2 4   199     0.24875    0.250
#> 3 6   100     0.12500    0.125
#> 4 8    98     0.12250    0.125

3. Basins of visibility

The basin of visibility of a point is the set of points that it sees (itself included); the basin of a set of points is the union of their basins.

y <- logistic_series(logistic_superstable(1), 24, x0 = 0.3)
visibility_distribution(y)     # half of the points see 3, half see 5
#>   visibility count   fraction
#> 1          2     1 0.04166667
#> 2          3    12 0.50000000
#> 3          4     1 0.04166667
#> 4          5    10 0.41666667
basin_set(y, c(2, 8))
#>  [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE
#> [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
visibility_set(y, c(2, 8))
#> [1] 6

4. Total visibility and partial visibility curve

The total visibility number is the smallest fraction of points whose basins cover the whole series. The partial visibility curve gives, for each fraction \(\mu = m/n\) of chosen points, the largest fraction \(\nu\) of the series that they can see (Nuño and Muñoz, 2020).

Three solvers are available: a genetic algorithm ("ga", used in the original paper), a fast greedy heuristic ("greedy") and an exact integer linear programming formulation ("exact", requires lpSolve).

y4 <- logistic_series(logistic_superstable(2), 48, x0 = 0.3)
cg <- partial_visibility_curve(y4, method = "greedy")
total_visibility(y4, method = "greedy")$fraction
#> [1] 0.2708333
ce <- partial_visibility_curve(y4, method = "exact")
total_visibility(y4, method = "exact")$fraction   # 1/4
#> [1] 0.25

The exact curves of the periodic series coincide with the analytical \(k\)-polygonals of the paper:

plot(ce, main = "Period 4 (k = 2)")
lines(pvc_theoretical(2), col = "red", lwd = 2)
lines(cg, col = "blue", lty = 2)
legend("bottomright", c("exact", "theory", "greedy"),
       col = c("black", "red", "blue"), lty = c(1, 1, 2), bty = "n")

c(exact = pvc_length(ce), theory = pvc_theoretical_length(2))
#>   exact  theory 
#> 1.04056 1.04056

The lengths of the \(k\)-polygonals converge to the length of the partial visibility curve at the onset of chaos:

pvc_theoretical_length(c(1:6, Inf))
#> [1] 1.036176 1.040560 1.041235 1.041382 1.041422 1.041433 1.041439
pvc_theoretical_length(c(1:6, Inf), cascade = "period3")
#> [1] 1.087247 1.088768 1.089057 1.089127 1.089147 1.089153 1.089157
plot(pvc_theoretical(1), type = "l", main = "Feigenbaum cascade")
for (k in 2:8) lines(pvc_theoretical(k), col = k)

References