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

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

## -----------------------------------------------------------------------------
sys <- series_dist(list(
  exponential(0.5),
  exponential(0.3),
  exponential(0.2)
))
sys

## -----------------------------------------------------------------------------
algebraic.dist::surv(sys)(1)           # P(system survives past t = 1)
algebraic.dist::cdf(sys)(1)            # P(system fails by t = 1)
set.seed(1)
algebraic.dist::sampler(sys)(5)        # five system-lifetime samples

## -----------------------------------------------------------------------------
algebraic.dist::surv(sys)(1)
exp(-(0.5 + 0.3 + 0.2) * 1)

## -----------------------------------------------------------------------------
ncomponents(sys)                       # 3 components
phi(sys, c(1, 1, 0))                   # system functions? No (series needs all)
phi(sys, c(1, 1, 1))                   # Yes
min_paths(sys)                         # the single path: all components
min_cuts(sys)                          # three singleton cuts
system_signature(sys)                  # (1, 0, 0): fails at first failure

## -----------------------------------------------------------------------------
# 2-out-of-3 of heterogeneous exponentials; closed-form specialization
kofn <- exp_kofn(k = 2, rates = c(1, 2, 3))
ncomponents(kofn)
algebraic.dist::surv(kofn)(1)
system_signature(kofn)                 # (0, 1, 0) for 2-of-3

## -----------------------------------------------------------------------------
set.seed(42)
samples <- algebraic.dist::sampler(kofn)(5000)
mean(samples)

## -----------------------------------------------------------------------------
# Swap component 2 with a Weibull.
kofn2 <- substitute_component(kofn, j = 2,
  new_component = weibull_dist(shape = 2, scale = 1))
algebraic.dist::surv(kofn2)(1)

## -----------------------------------------------------------------------------
# Structural importance: fraction of pivotal states for component 1.
structural_importance(kofn, j = 1)

# Birnbaum (reliability) importance: dR/dp_j at given component
# reliabilities.
birnbaum_importance(kofn, j = 1, p = 0.9)

# Criticality importance at time t = 0.5.
criticality_importance(kofn, j = 1, t = 0.5)

# Vesely-Fussell importance at time t = 0.5 (via minimal cut sets).
vesely_fussell_importance(kofn, j = 1, t = 0.5)

