Package 'tsvisibility'

Title: Visibility Graphs and Partial Visibility Curves of Time Series
Description: Maps time series into complex networks with the natural visibility algorithm of Lacasa et al. (2008) <doi:10.1073/pnas.0709247105> and the horizontal visibility algorithm of Luque et al. (2009) <doi:10.1103/PhysRevE.80.046103>, using fast exact algorithms. Provides degree statistics, exact results for uncorrelated and periodic series, Feigenbaum graphs (Luque et al., 2011) <doi:10.1371/journal.pone.0022411>, time irreversibility, sequential visibility motifs and plotting tools. It also implements the basins of visibility, the total visibility number and the partial visibility curve of Nuño and Muñoz (2020) <doi:10.1016/j.chaos.2019.109537>, computed with a genetic algorithm, a greedy heuristic or an exact integer linear programming formulation, together with the analytical partial visibility curves of the period doubling cascades of the logistic map.
Authors: Francisco J. Muñoz [aut, cre] (ORCID: <https://orcid.org/0000-0002-0874-6275>), Juan Carlos Nuño [aut]
Maintainer: Francisco J. Muñoz <[email protected]>
License: GPL (>= 3)
Version: 0.1.0
Built: 2026-12-02 21:10:37 UTC
Source: https://github.com/cran/tsvisibility

Help Index


Basins of visibility

Description

The basin of visibility BkB_k of a point Pk=(k,xk)P_k = (k, x_k) is the set of points of the series seen from PkP_k; by default the point sees itself. The basin of a set of points is the union of their basins (Nuño and Muñoz, 2020). The visibility of a point (or of a set) is the cardinality of its basin.

Usage

basin_point(x, k, type = c("horizontal", "natural"), itself = TRUE)

basin_set(x, v, type = c("horizontal", "natural"), itself = TRUE)

visibility_point(x, k, type = c("horizontal", "natural"), itself = TRUE)

visibility_set(x, v, type = c("horizontal", "natural"), itself = TRUE)

Arguments

x

A numeric series or a vgraph() object (then type is ignored).

k

Index of a single point of the series.

type

Either "horizontal" (default) or "natural".

itself

If TRUE (default) every point belongs to its own basin.

v

The selected set of points: a logical vector with the same length as the series, a 0/1 vector, or a vector of indices.

Value

basin_point() and basin_set() return a logical vector marking the points of the basin; visibility_point() and visibility_set() return its cardinality.

References

Nuño, J. C. and Muñoz, F. J. (2020). The partial visibility curve of the Feigenbaum cascade to chaos. Chaos, Solitons & Fractals, 131, 109537. doi:10.1016/j.chaos.2019.109537

Examples

x <- c(5, 3, 6, 7, 2, 1, 6, 7)
basin_point(x, k = 2, itself = FALSE)
visibility_point(x, k = 4)
basin_set(x, v = c(1, 4))
visibility_set(x, v = c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE))

Empirical degree distribution

Description

Empirical degree distribution

Usage

degree_distribution(
  x,
  type = c("horizontal", "natural"),
  mode = c("total", "out", "in")
)

Arguments

x

A numeric series or a vgraph() object (then type is ignored).

type

Either "horizontal" (default) or "natural".

mode

"total" (default) for the undirected degree, "out" for the number of future nodes seen from each node (forward visibility) and "in" for the number of past nodes seen (backward visibility).

Value

A data frame with columns k (degree), count and p (relative frequency), sorted by k.

Examples

set.seed(1)
degree_distribution(runif(1000))

Shannon entropy of the degree distribution

Description

Shannon entropy of the degree distribution

Usage

degree_entropy(
  x,
  type = c("horizontal", "natural"),
  mode = c("total", "out", "in"),
  base = exp(1)
)

Arguments

x

A numeric series, a vgraph() object, or a data frame with a column p of probabilities (such as the output of degree_distribution() or feigenbaum_degree_distribution()).

type

Either "horizontal" (default) or "natural".

mode

"total" (default) for the undirected degree, "out" for the number of future nodes seen from each node (forward visibility) and "in" for the number of past nodes seen (backward visibility).

base

Base of the logarithm (default: natural logarithm).

Value

The entropy h=−∑kP(k)log⁡P(k)h = -\sum_k P(k) \log P(k).

References

Luque, B., Lacasa, L., Ballesteros, F. J. and Robledo, A. (2012). Analytical properties of horizontal visibility graphs in the Feigenbaum scenario. Chaos, 22, 013109.

Examples

degree_entropy(feigenbaum_degree_distribution(3))
set.seed(2)
degree_entropy(runif(2000))

Constants of the Feigenbaum scenario of the logistic map

Description

Constants of the Feigenbaum scenario of the logistic map

Usage

feigenbaum_constants()

Value

A named list: r_inf (accumulation point of the period-doubling cascade), delta and alpha (Feigenbaum constants), r3 (onset of the period-3 window, 1+81 + \sqrt{8}), r3_inf (accumulation point of the period-3 cascade, approximate) and r_c (crossing point where the total visibility number changes from 1/4 to 1/3, approximate; Nuño and Muñoz, 2020).

References

Nuño, J. C. and Muñoz, F. J. (2020). The partial visibility curve of the Feigenbaum cascade to chaos. Chaos, Solitons & Fractals, 131, 109537. doi:10.1016/j.chaos.2019.109537

Examples

feigenbaum_constants()$r_inf

Degree distribution of Feigenbaum graphs

Description

Exact degree distribution of the horizontal visibility graph of the periodic orbits of period T=2nT = 2^n in the period-doubling cascade of unimodal maps (Feigenbaum graphs): P(n,k)=(1/2)k/2P(n, k) = (1/2)^{k/2} for k=2,4,…,2nk = 2, 4, \dots, 2n, P(n,2(n+1))=(1/2)nP(n, 2(n + 1)) = (1/2)^n and zero for odd kk. At the accumulation point (n=∞n = \infty) P(k)=(1/2)k/2P(k) = (1/2)^{k/2} for every even kk.

Usage

feigenbaum_degree_distribution(n, kmax = 60)

Arguments

n

Non-negative integer, or Inf for the onset of chaos.

kmax

Largest degree returned when n = Inf.

Value

A data frame with columns k and p.

References

Luque, B., Lacasa, L., Ballesteros, F. J. and Robledo, A. (2011). Feigenbaum graphs: a complex network perspective of chaos. PLoS ONE, 6, e22411. doi:10.1371/journal.pone.0022411

Examples

feigenbaum_degree_distribution(3)
# mean degree equals 4 (1 - 1 / (2 T))
with(feigenbaum_degree_distribution(3), sum(k * p))

Exact degree distribution of the HVG of an uncorrelated series

Description

For any series of independent and identically distributed continuous random variables the degree distribution of the horizontal visibility graph is P(k)=13(23)k−2P(k) = \frac{1}{3}\left(\frac{2}{3}\right)^{k-2}, k≥2k \ge 2, i.e. an exponential with λ=log⁡(3/2)\lambda = \log(3/2). In the directed version the in- and out-degree distributions are P(k)=(1/2)kP(k) = (1/2)^k, k≥1k \ge 1.

Usage

hvg_degree_iid(k, mode = c("total", "out", "in"))

Arguments

k

Integer vector of degrees.

mode

"total", "out" or "in".

Value

The probabilities P(k)P(k).

References

Luque, B., Lacasa, L., Ballesteros, F. and Luque, J. (2009). Horizontal visibility graphs: exact results for random time series. Physical Review E, 80, 046103. doi:10.1103/PhysRevE.80.046103

Lacasa, L., Núñez, A., Roldán, E., Parrondo, J. M. R. and Luque, B. (2012). Time series irreversibility: a visibility graph approach. The European Physical Journal B, 85, 217.

Examples

hvg_degree_iid(2:10)
sum(hvg_degree_iid(2:200))

Exponent of the exponential degree distribution of an HVG

Description

Fits P(k)∼e−λkP(k) \sim e^{-\lambda k} by least squares on log⁡P(k)\log P(k). For uncorrelated series λ=log⁡(3/2)≈0.405\lambda = \log(3/2) \approx 0.405; Luque et al. (2009) found λ<log⁡(3/2)\lambda < \log(3/2) for chaotic series and λ>log⁡(3/2)\lambda > \log(3/2) for correlated stochastic series.

Usage

hvg_lambda(x, kmin = 2, kmax = Inf, type = "horizontal")

Arguments

x

A numeric series or a vgraph() object.

kmin, kmax

Range of degrees used in the fit. By default every observed degree ≥2\ge 2.

type

Either "horizontal" (default) or "natural".

Value

A list with components lambda, intercept, r.squared, reference (log⁡(3/2)\log(3/2)) and data (the degree distribution used).

Examples

set.seed(3)
hvg_lambda(runif(5000))$lambda
hvg_lambda(logistic_series(4, 5000, x0 = 0.2))$lambda

Mean degree of the HVG of a periodic series

Description

The mean degree of the horizontal visibility graph of a periodic series of period TT (with no repeated values within a period) is kˉ=4(1−12T)\bar{k} = 4\left(1 - \frac{1}{2T}\right); it tends to 4 for aperiodic series.

Usage

hvg_mean_degree_periodic(period)

Arguments

period

Integer period T≥1T \ge 1.

Value

The mean degree.

References

Núñez, A. M., Lacasa, L., Gómez, J. P. and Luque, B. (2012). Visibility algorithms: a short review. In New Frontiers in Graph Theory, InTech.

Examples

hvg_mean_degree_periodic(c(1, 2, 4, 8, 16))

Time series of the logistic map

Description

Iterates xt+1=rxt(1−xt)x_{t+1} = r x_t (1 - x_t) after a transient of transient steps and returns n consecutive values.

Usage

logistic_series(r, n, x0 = NULL, transient = 1e+05, digits = 8)

Arguments

r

Parameter of the map, 0<r≤40 < r \le 4.

n

Length of the series.

x0

Initial condition in (0,1)(0, 1). By default a uniform random number (use set.seed() for reproducibility).

transient

Number of initial iterations discarded.

digits

Number of decimal digits kept (NULL for no rounding).

Details

Rounding (argument digits) is important when building horizontal visibility graphs of periodic orbits: in exact arithmetic the values that repeat along the orbit are equal and block the horizontal visibility, but floating point iteration produces tiny differences that would create spurious links.

Value

A numeric vector of length n.

See Also

logistic_superstable(), feigenbaum_constants().

Examples

logistic_series(r = 3.2, n = 10, x0 = 0.1)
x <- logistic_series(r = 4, n = 500, x0 = 0.2)
plot(x, type = "l")

Superstable parameters of the logistic map

Description

Values of rr at which the critical point x=1/2x = 1/2 belongs to the periodic orbit (superstable orbits). They converge very fast to the orbit and are convenient to generate the periodic series of the cascades.

Usage

logistic_superstable(k, cascade = c("feigenbaum", "period3"))

Arguments

k

Order (see Details); vectorised.

cascade

"feigenbaum" (default) or "period3".

Details

  • cascade = "feigenbaum": period 2k2^k, k=0,…,11k = 0, \dots, 11.

  • cascade = "period3": period 3⋅2k−13 \cdot 2^{k-1}, k=1,2,3k = 1, 2, 3.

The values were computed by bisection on fr(T)(1/2)=1/2f_r^{(T)}(1/2) = 1/2.

Value

A numeric vector of parameters.

Examples

logistic_superstable(0:5)
x <- logistic_series(logistic_superstable(3), 32, x0 = 0.3)
length(unique(x))  # period 8

Largest visibility of a single point

Description

max_visibility_point() returns the largest cardinality of the basin of visibility of a point of the series and greatest_basin_point() the index of a point that attains it (the first one in case of ties).

Usage

max_visibility_point(x, type = c("horizontal", "natural"), itself = TRUE)

greatest_basin_point(x, type = c("horizontal", "natural"))

Arguments

x

A numeric series or a vgraph() object (then type is ignored).

type

Either "horizontal" (default) or "natural".

itself

If TRUE (default) every point belongs to its own basin.

Value

A single integer.

Examples

x <- c(4, 5, 6, 3, 6, 5, 3, 6, 7, 4, 7)
max_visibility_point(x)
greatest_basin_point(x)

Partial visibility curve

Description

Computes the partial visibility curve of a time series (Nuño and Muñoz, 2020): for every cardinality m=0,1,2,…m = 0, 1, 2, \dots the largest visibility mVmV of a set of mm points (see partial_visibility_set()), normalised as μ=m/n\mu = m / n and ν=mV/n\nu = mV / n. The curve is computed until the whole series is seen (ν=1\nu = 1, which happens at the total visibility number) or until m_max.

Usage

partial_visibility_curve(
  x,
  type = c("horizontal", "natural"),
  method = c("ga", "greedy", "exact"),
  m_max = NULL,
  control = list(),
  keep_sets = FALSE,
  verbose = FALSE
)

Arguments

x

A numeric series or a vgraph() object (then type is ignored).

type

Either "horizontal" (default) or "natural".

method

One of "ga" (default), "greedy" or "exact".

m_max

Largest cardinality computed (default: the length of the series, i.e. until ν=1\nu = 1).

control

A list of parameters of the genetic algorithm: pop_size (100), num_iter (1000), prob_mut (0.01), num_inversion (10), num_cross (10), max_stall (200) and seed_greedy (TRUE).

keep_sets

If TRUE, the sets found are stored in the attribute "sets".

verbose

If TRUE, prints the progress.

Details

With method = "ga" the search for mm points is seeded with the best set found for m−1m - 1 points plus the best additional point, so the computed curve is non-decreasing.

Value

A data frame of class pvcurve with columns m, mu, visibility and nu, starting at (0,0)(0, 0). Attributes type, method and n store the settings.

References

Nuño, J. C. and Muñoz, F. J. (2020). The partial visibility curve of the Feigenbaum cascade to chaos. Chaos, Solitons & Fractals, 131, 109537. doi:10.1016/j.chaos.2019.109537

See Also

pvc_length(), pvc_theoretical(), plot.pvcurve().

Examples

x <- logistic_series(logistic_superstable(2), 48, x0 = 0.3)
cg <- partial_visibility_curve(x, method = "greedy")
pvc_length(cg)
plot(cg)
lines(pvc_theoretical(2), col = "red")

set.seed(1)
cga <- partial_visibility_curve(x, method = "ga",
                                control = list(num_iter = 300))
pvc_length(cga)
pvc_theoretical_length(2)

Partial visibility set

Description

Finds a set of exactly m points of the series whose basin of visibility (the union of the basins of its points, each point seeing itself) has the largest cardinality. This is the partial visibility set of Nuño and Muñoz (2020), i.e. a maximum partial dominating set of the visibility graph.

Usage

partial_visibility_set(
  x,
  m,
  type = c("horizontal", "natural"),
  method = c("ga", "greedy", "exact"),
  control = list()
)

Arguments

x

A numeric series or a vgraph() object (then type is ignored).

m

Number of points of the set (1≤m≤n1 \le m \le n).

type

Either "horizontal" (default) or "natural".

method

One of "ga" (default), "greedy" or "exact".

control

A list of parameters of the genetic algorithm: pop_size (100), num_iter (1000), prob_mut (0.01), num_inversion (10), num_cross (10), max_stall (200) and seed_greedy (TRUE).

Details

Three methods are available:

  • "ga": the genetic algorithm of Nuño and Muñoz (2020), with mutation, inversion, crossover, elitist selection and replacement of duplicated genomes. Genomes are repaired so that they always contain exactly m points, and the greedy solution is included in the initial population.

  • "greedy": chooses, one at a time, the point that adds the largest number of new points to the basin. Fast, but not optimal in general.

  • "exact": solves the problem as an integer linear program with lpSolve. It guarantees the optimum but its cost grows quickly with the length of the series (practical up to a few hundred points).

Value

A list with components set (indices of the chosen points), visibility (cardinality of its basin), fraction (visibility / n), m, n and method.

References

Nuño, J. C. and Muñoz, F. J. (2020). The partial visibility curve of the Feigenbaum cascade to chaos. Chaos, Solitons & Fractals, 131, 109537. doi:10.1016/j.chaos.2019.109537

See Also

total_visibility(), partial_visibility_curve().

Examples

x <- logistic_series(logistic_superstable(2), 40, x0 = 0.3)
partial_visibility_set(x, m = 3, method = "greedy")
set.seed(1)
partial_visibility_set(x, m = 3, method = "ga",
                       control = list(num_iter = 100))
if (requireNamespace("lpSolve", quietly = TRUE)) {
  partial_visibility_set(x, m = 3, method = "exact")
}

Frequency of the patterns (contiguous blocks) of a sequence

Description

Counts every contiguous block of length 1 to max_size of a sequence. Applied to the sequence of degrees or visibilities of a time series it reveals its visibility patterns (Muñoz and Nuño, 2020).

Usage

pattern_frequency(v, max_size = NULL, sep = "_")

Arguments

v

A vector (numeric, integer or character).

max_size

Largest length of the patterns (default: length(v)).

sep

Separator used to paste the elements of a pattern.

Value

A data frame with columns pattern, freq and length, sorted by length and decreasing frequency.

References

Muñoz, F. J. and Nuño, J. C. (2020). Universal visibility patterns of unimodal maps. Chaos, 30. doi:10.1063/5.0006652

Examples

pattern_frequency(c(1, 2, 3, 1, 2, 3), max_size = 3)
x <- logistic_series(logistic_superstable(2), 40, x0 = 0.3)
pattern_frequency(visibility_degree(x), max_size = 4)

Plot a time series and its visibility links

Description

Draws the series as vertical bars and the visibility links between its points: straight segments for natural visibility and horizontal segments (at the height of the lower point) for horizontal visibility. If select is given, the selected points are highlighted and only their links are drawn, which shows their basin of visibility (for instance, a partial or total visibility set).

Usage

plot_visibility(
  x,
  type = c("horizontal", "natural"),
  select = NULL,
  bars = TRUE,
  col_links = "grey40",
  col_select = "red",
  xlab = "t",
  ylab = "x",
  main = NULL,
  ...
)

## S3 method for class 'vgraph'
plot(x, ...)

Arguments

x

A numeric series or a vgraph() object (then type is ignored).

type

Either "horizontal" (default) or "natural".

select

Optional selection of points (logical mask, 0/1 vector or indices).

bars

If TRUE, vertical bars from 0 to each value are drawn.

col_links

Colour of the visibility links.

col_select

Colour of the selected points.

xlab, ylab, main

Axis labels and title.

...

Further graphical parameters passed to plot().

Value

The vgraph object, invisibly.

Examples

x <- c(1, 1, 2, 3, 4, 3, 2, 3, 1)
plot_visibility(x, type = "natural")
plot_visibility(x, type = "horizontal")
plot_visibility(x, type = "horizontal", select = c(5, 8))

y <- logistic_series(logistic_superstable(2), 24, x0 = 0.3)
tv <- total_visibility(y, method = "greedy")
plot_visibility(y, select = tv$set)

Plot a partial visibility curve

Description

Plot a partial visibility curve

Usage

## S3 method for class 'pvcurve'
plot(
  x,
  type = "b",
  xlab = expression(mu),
  ylab = expression(nu),
  xlim = NULL,
  ylim = c(0, 1),
  ...
)

## S3 method for class 'pvcurve'
lines(x, ...)

Arguments

x

A pvcurve object, from partial_visibility_curve() or pvc_theoretical().

type, xlab, ylab, xlim, ylim

Graphical parameters.

...

Further graphical parameters passed to plot() or lines().

Value

NULL, invisibly.

Examples

plot(pvc_theoretical(1), type = "l")
lines(pvc_theoretical(4), col = "red")

Length of a partial visibility curve

Description

Euclidean length of the polygonal line through the points (μ,ν)(\mu, \nu) of a partial visibility curve, L=∑jΔμj2+Δνj2L = \sum_j \sqrt{\Delta\mu_j^2 + \Delta\nu_j^2}.

Usage

pvc_length(curve)

Arguments

curve

A pvcurve object or any data frame (or list) with numeric components mu and nu sorted by mu.

Value

The length of the curve.

Examples

pvc_length(pvc_theoretical(3))

Analytical partial visibility curves of the period-doubling cascades

Description

Vertices of the exact partial visibility curves (horizontal visibility) of the periodic series of the logistic map obtained by Nuño and Muñoz (2020).

Usage

pvc_theoretical(k, cascade = c("feigenbaum", "period3"))

Arguments

k

Order of the cascade (see Details).

cascade

"feigenbaum" (default) or "period3".

Details

  • cascade = "feigenbaum", period T=2kT = 2^k, k≥0k \ge 0: the kk-polygonal is νk(μ)=(2k+3)μ\nu_k(\mu) = (2k + 3)\mu for 0≤μ≤1/(3⋅2k)0 \le \mu \le 1/(3 \cdot 2^k), νk(μ)=2kμ+2−k\nu_k(\mu) = 2k\mu + 2^{-k} for 1/(3⋅2k)≤μ≤2−(k+1)1/(3 \cdot 2^k) \le \mu \le 2^{-(k+1)} and νk(μ)=2jμ+2−j\nu_k(\mu) = 2j\mu + 2^{-j} for 2−(j+2)≤μ≤2−(j+1)2^{-(j+2)} \le \mu \le 2^{-(j+1)}, j=k−1,…,1j = k - 1, \dots, 1. It reaches ν=1\nu = 1 at the total visibility number μ=1/4\mu = 1/4 (1/31/3 for k=0k = 0).

  • cascade = "period3", period T=3⋅2k−1T = 3 \cdot 2^{k-1}, k≥1k \ge 1: νk(μ)=2(k+2)μ\nu_k(\mu) = 2(k + 2)\mu for 0≤μ≤1/(9⋅2k−1)0 \le \mu \le 1/(9 \cdot 2^{k-1}) and then νk(μ)=(2j+1)μ+1/(3⋅2j−1)\nu_k(\mu) = (2j + 1)\mu + 1/(3 \cdot 2^{j-1}) for j=k,…,0j = k, \dots, 0, ending at μ=1/3\mu = 1/3, ν=1\nu = 1.

Value

A data frame of class pvcurve with columns mu and nu (vertices of the polygonal).

References

Nuño, J. C. and Muñoz, F. J. (2020). The partial visibility curve of the Feigenbaum cascade to chaos. Chaos, Solitons & Fractals, 131, 109537. doi:10.1016/j.chaos.2019.109537

Examples

pvc_theoretical(1)
pvc_theoretical(2, cascade = "period3")
plot(pvc_theoretical(1), type = "l")
for (k in 2:6) lines(pvc_theoretical(k), col = k)

Length of the analytical partial visibility curves

Description

Length of the kk-polygonals of pvc_theoretical(). With k = Inf the limit at the accumulation point of the cascade is returned (computed with k=60k = 60, beyond which the value does not change in double precision): L∞≈1.0414387863956818L_\infty \approx 1.0414387863956818 for the Feigenbaum cascade and L∞≈1.089156529597995L_\infty \approx 1.089156529597995 for the period-3 cascade (Nuño and Muñoz, 2020).

Usage

pvc_theoretical_length(k, cascade = c("feigenbaum", "period3"))

Arguments

k

A vector of orders, possibly including Inf.

cascade

"feigenbaum" (default) or "period3".

Value

A numeric vector of lengths.

Examples

pvc_theoretical_length(0:6)
pvc_theoretical_length(Inf)
pvc_theoretical_length(Inf, cascade = "period3")
# the ratio (L_n - L_{n-1}) / (L_{n+1} - L_n) tends to 2
L <- pvc_theoretical_length(1:12)
head(diff(L)[-length(diff(L))] / diff(L)[-1])

Total visibility number

Description

Finds a smallest set of points whose basins of visibility cover the whole series (a minimum dominating set of the visibility graph). Its cardinality is the total visibility of the series; divided by the length of the series it is the total visibility number. For the horizontal visibility of the whole Feigenbaum cascade it equals 1/4 (Nuño and Muñoz, 2020).

Usage

total_visibility(
  x,
  type = c("horizontal", "natural"),
  method = c("ga", "greedy", "exact"),
  control = list()
)

Arguments

x

A numeric series or a vgraph() object (then type is ignored).

type

Either "horizontal" (default) or "natural".

method

One of "ga" (default), "greedy" or "exact".

control

A list of parameters of the genetic algorithm: pop_size (100), num_iter (1000), prob_mut (0.01), num_inversion (10), num_cross (10), max_stall (200) and seed_greedy (TRUE).

Details

With method = "greedy" and method = "ga" the result is an upper bound (redundant points are always removed); method = "exact" gives the optimum.

Value

A list with components set, size, fraction (size / n), n and method.

References

Nuño, J. C. and Muñoz, F. J. (2020). The partial visibility curve of the Feigenbaum cascade to chaos. Chaos, Solitons & Fractals, 131, 109537. doi:10.1016/j.chaos.2019.109537

Examples

x <- logistic_series(logistic_superstable(1), 40, x0 = 0.3)
total_visibility(x, method = "greedy")$fraction
if (requireNamespace("lpSolve", quietly = TRUE)) {
  total_visibility(x, method = "exact")$fraction
}

Visibility graph object

Description

Builds a lightweight vgraph object that stores the series, the type of visibility, the list of links and the degree of every node. Most functions of the package accept either a numeric series or a vgraph; passing a vgraph avoids recomputing the graph.

Usage

vgraph(x, type = c("horizontal", "natural"), tol = 0)

Arguments

x

A numeric vector (or ts) with the time series.

type

Either "horizontal" (default) or "natural".

tol

Non-negative tolerance used only for the natural visibility: node jj is visible from ii when its slope exceeds the maximum previous slope by more than tol. The default 0 reproduces the exact definition; a small positive value protects against rounding errors in (almost) collinear points.

Value

An object of class vgraph: a list with components x (the series), type, n (number of nodes), edges (see visibility_edges()) and degree (integer vector).

See Also

plot_visibility(), vgraph_to_igraph().

Examples

g <- vgraph(c(3, 1, 4, 1, 5, 9, 2, 6), type = "natural")
g
g$degree

Convert a visibility graph to an igraph object

Description

Convert a visibility graph to an igraph object

Usage

vgraph_to_igraph(x, type = c("horizontal", "natural"), directed = FALSE)

Arguments

x

A numeric series or a vgraph() object (then type is ignored).

type

Either "horizontal" (default) or "natural".

directed

If TRUE, a directed graph with links oriented forward in time (i→ji \to j, i<ji < j) is returned.

Value

An igraph object. The series is stored in the vertex attribute value and the visibility type in the graph attribute type.

Examples

if (requireNamespace("igraph", quietly = TRUE)) {
  ig <- vgraph_to_igraph(rnorm(50))
  igraph::transitivity(ig, type = "global")
  igraph::mean_distance(ig)
}

Degrees of a visibility graph

Description

Degrees of a visibility graph

Usage

visibility_degree(
  x,
  type = c("horizontal", "natural"),
  mode = c("total", "out", "in")
)

Arguments

x

A numeric series or a vgraph() object (then type is ignored).

type

Either "horizontal" (default) or "natural".

mode

"total" (default) for the undirected degree, "out" for the number of future nodes seen from each node (forward visibility) and "in" for the number of past nodes seen (backward visibility).

Value

An integer vector with one degree per observation.

Examples

x <- c(3, 1, 4, 1, 5, 9, 2, 6)
visibility_degree(x)
visibility_degree(x, mode = "out")

Visibility distribution of a time series

Description

Distribution of the visibilities vk=∣Bk∣v_k = |B_k| of the points of the series (degree plus one when the point sees itself). For instance, in the horizontal visibility graph of a series of period 2 half of the points see 3 points and the other half see 5.

Usage

visibility_distribution(x, type = c("horizontal", "natural"), itself = TRUE)

Arguments

x

A numeric series or a vgraph() object (then type is ignored).

type

Either "horizontal" (default) or "natural".

itself

If TRUE (default) every point belongs to its own basin.

Value

A data frame with columns visibility, count and fraction.

Examples

x <- logistic_series(logistic_superstable(1), 200, x0 = 0.3)
visibility_distribution(x)

Edges of the natural or horizontal visibility graph

Description

Computes the list of links of the visibility graph of a time series. The horizontal visibility graph is obtained in linear time with a stack algorithm; the natural visibility graph uses, for every node, a vectorised sweep over the slopes to the following nodes, which needs O(n2)O(n^2) operations instead of the O(n3)O(n^3) of the direct definition.

Usage

visibility_edges(x, type = c("horizontal", "natural"), tol = 0)

Arguments

x

A numeric vector (or a ts object) with the time series.

type

Either "horizontal" (default) or "natural".

tol

Non-negative tolerance used only for the natural visibility: node jj is visible from ii when its slope exceeds the maximum previous slope by more than tol. The default 0 reproduces the exact definition; a small positive value protects against rounding errors in (almost) collinear points.

Details

Two nodes i<ji < j see each other naturally when xk<xi+(xj−xi)(k−i)/(j−i)x_k < x_i + (x_j - x_i)(k - i)/(j - i) for all i<k<ji < k < j, which is equivalent to the slope from ii to jj being strictly larger than all the slopes from ii to the intermediate nodes. They see each other horizontally when xk<min⁡(xi,xj)x_k < \min(x_i, x_j) for all i<k<ji < k < j. Both inequalities are strict, so equal intermediate values block the visibility. Consecutive nodes are always linked.

Value

An integer matrix with two columns, from and to, with one row per link and from < to, sorted by from and then by to.

References

Lacasa, L., Luque, B., Ballesteros, F., Luque, J. and Nuño, J. C. (2008). From time series to complex networks: the visibility graph. Proceedings of the National Academy of Sciences, 105, 4972–4975. doi:10.1073/pnas.0709247105

Luque, B., Lacasa, L., Ballesteros, F. and Luque, J. (2009). Horizontal visibility graphs: exact results for random time series. Physical Review E, 80, 046103. doi:10.1103/PhysRevE.80.046103

See Also

vgraph(), visibility_matrix(), vgraph_to_igraph().

Examples

x <- c(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5)
visibility_edges(x, type = "horizontal")
visibility_edges(x, type = "natural")

Time irreversibility from directed visibility graphs

Description

Kullback-Leibler divergence between the out-degree (forward) and in-degree (backward) distributions of the directed visibility graph, D=∑kPout(k)log⁡Pout(k)Pin(k)D = \sum_k P_{out}(k) \log \frac{P_{out}(k)}{P_{in}(k)}. It vanishes asymptotically for time-reversible processes and is positive for irreversible ones. Only degrees observed in both distributions contribute to the sum.

Usage

visibility_irreversibility(x, type = c("horizontal", "natural"))

Arguments

x

A numeric series or a vgraph() object (then type is ignored).

type

Either "horizontal" (default) or "natural".

Value

The estimated divergence (in nats).

References

Lacasa, L., Núñez, A., Roldán, E., Parrondo, J. M. R. and Luque, B. (2012). Time series irreversibility: a visibility graph approach. The European Physical Journal B, 85, 217.

Examples

set.seed(4)
visibility_irreversibility(runif(5000))           # reversible: ~ 0
visibility_irreversibility(logistic_series(4, 5000, x0 = 0.3))  # > 0

Visibility (adjacency) matrix

Description

Returns the adjacency matrix of the natural or horizontal visibility graph, as a dense integer matrix or as a sparse matrix of package Matrix.

Usage

visibility_matrix(
  x,
  type = c("horizontal", "natural"),
  directed = FALSE,
  diag = FALSE,
  sparse = FALSE
)

Arguments

x

A numeric series or a vgraph() object (then type is ignored).

type

Either "horizontal" (default) or "natural".

directed

If TRUE, only the forward links i→ji \to j, i<ji < j, are stored (upper triangular matrix).

diag

If TRUE, the diagonal is set to 1 (every node sees itself, as in the basins of visibility).

sparse

If TRUE, a sparse dgCMatrix is returned (requires the Matrix package). Recommended for long series.

Value

An n×nn \times n matrix of zeros and ones.

Examples

visibility_matrix(c(1, 2, 1, 2, 1), type = "horizontal")
visibility_matrix(c(1, 2, 3, 4), type = "natural", diag = TRUE)

Sequential visibility-graph motifs

Description

Slides a window of size consecutive observations along the series, builds the visibility graph of each window and counts the different patterns of links. Since consecutive nodes are always linked, a motif is identified by its non-consecutive links (labelled "i-j" with positions inside the window); "none" is the path graph.

Usage

visibility_motifs(x, size = 4L, type = c("horizontal", "natural"))

Arguments

x

A numeric vector (or a ts object) with the time series.

size

Window size (default 4).

type

Either "horizontal" (default) or "natural".

Value

A data frame with columns motif, count and p.

References

Iacovacci, J. and Lacasa, L. (2016). Sequential visibility-graph motifs. Physical Review E, 93, 042309.

Examples

set.seed(5)
visibility_motifs(runif(2000), size = 4)