## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 6,
                      fig.height = 4)
has_lp <- requireNamespace("lpSolve", quietly = TRUE)

## ----setup--------------------------------------------------------------------
library(tsvisibility)

## -----------------------------------------------------------------------------
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
g_hor
head(g_hor$edges)

## ----fig.height = 3.5---------------------------------------------------------
op <- par(mfrow = c(1, 2))
plot(g_nat)
plot(g_hor)
par(op)

## -----------------------------------------------------------------------------
visibility_matrix(c(1, 2, 1, 2, 1))
if (requireNamespace("igraph", quietly = TRUE)) {
  ig <- vgraph_to_igraph(runif(200))
  c(clustering = igraph::transitivity(ig, type = "global"),
    mean_distance = igraph::mean_distance(ig))
}

## -----------------------------------------------------------------------------
set.seed(1)
u <- runif(10000)
d <- degree_distribution(u)
head(cbind(d, theory = hvg_degree_iid(d$k)))
hvg_lambda(u, kmax = 12)$lambda   # log(3/2) = 0.405

## -----------------------------------------------------------------------------
z <- logistic_series(r = 4, n = 10000, x0 = 0.2)
hvg_lambda(z, kmax = 12)$lambda
visibility_irreversibility(u)
visibility_irreversibility(z)

## -----------------------------------------------------------------------------
p8 <- logistic_series(logistic_superstable(3), 800, x0 = 0.3)
merge(degree_distribution(p8), feigenbaum_degree_distribution(3),
      by = "k", suffixes = c("_empirical", "_theory"))

## -----------------------------------------------------------------------------
y <- logistic_series(logistic_superstable(1), 24, x0 = 0.3)
visibility_distribution(y)     # half of the points see 3, half see 5
basin_set(y, c(2, 8))
visibility_set(y, c(2, 8))

## -----------------------------------------------------------------------------
y4 <- logistic_series(logistic_superstable(2), 48, x0 = 0.3)
cg <- partial_visibility_curve(y4, method = "greedy")
total_visibility(y4, method = "greedy")$fraction

## ----eval = has_lp------------------------------------------------------------
ce <- partial_visibility_curve(y4, method = "exact")
total_visibility(y4, method = "exact")$fraction   # 1/4

## ----eval = has_lp------------------------------------------------------------
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))

## -----------------------------------------------------------------------------
pvc_theoretical_length(c(1:6, Inf))
pvc_theoretical_length(c(1:6, Inf), cascade = "period3")

## -----------------------------------------------------------------------------
plot(pvc_theoretical(1), type = "l", main = "Feigenbaum cascade")
for (k in 2:8) lines(pvc_theoretical(k), col = k)

