## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----setup--------------------------------------------------------------------
library(dist.structure)
library(algebraic.dist)

## -----------------------------------------------------------------------------
sys <- cold_standby_dist(list(
  exponential(1),
  exponential(2),
  exponential(0.5)
))
class(sys)
dist.structure::is_dist_structure(sys)   # FALSE -- not a coherent system

## -----------------------------------------------------------------------------
mean(sys)                              # 1/1 + 1/2 + 1/0.5 = 3.5

## -----------------------------------------------------------------------------
set.seed(1)
x <- algebraic.dist::sampler(sys)(5000)
mean(x)                                # empirical ~ 3.5

## -----------------------------------------------------------------------------
set.seed(42)
S <- algebraic.dist::surv(sys)
S(c(1, 2, 3, 5), mc = 5000)           # survival at multiple t values

## -----------------------------------------------------------------------------
S(2, mc = 5000)                        # uses the same cache
S(2, mc = 10000)                       # new cache with 10000 samples

## -----------------------------------------------------------------------------
S <- algebraic.dist::surv(sys)
F_t <- function(t) 1 - S(t)
F_t(2) + S(2)                          # exactly 1

## -----------------------------------------------------------------------------
set.seed(1); S1 <- algebraic.dist::surv(sys); s1 <- S1(2)
set.seed(1); S2 <- algebraic.dist::surv(sys); s2 <- S2(2)
all.equal(s1, s2)

## -----------------------------------------------------------------------------
m <- 4; rate <- 2
sys <- cold_standby_dist(replicate(m, exponential(rate), simplify = FALSE))
# Monte Carlo survival at t = 3
set.seed(1)
mc_est <- algebraic.dist::surv(sys)(3, mc = 10000)
# Exact via Gamma
exact <- pgamma(3, shape = m, rate = rate, lower.tail = FALSE)
cat(sprintf("MC: %.4f, Exact: %.4f\n", mc_est, exact))

## ----error = TRUE-------------------------------------------------------------
try({
dist.structure::phi(sys, c(1, 1, 1, 1))
})

## ----error = TRUE-------------------------------------------------------------
try({
dist.structure::min_paths(sys)
})

