---
title: "Reliability, Availability, and Maintainability (RAM)"
output: 
  learnr::tutorial:
    progressive: true
    allow_skip: true
runtime: shiny_prerendered
---

```{r setup, include=FALSE}
library(learnr)
library(WeibullR.learnr)
```

## Introduction

Welcome to the tutorial on **Reliability, Availability, and Maintainability (RAM)**! In this tutorial, you will learn about these fundamental concepts and their applications.

## What is RAM?

The terms Reliability, Availability, and Maintainability may sound similar, but they actually represent different concepts.

**Reliability**: The ability of an item to perform its intended function without failure over a specified period.

**Availability**: The proportion of time an item is in a functioning condition.

**Maintainability**: The ease and speed with which an item can be restored to operational status after a failure.

That being said, all 3 terms are inter-related. A car that is not very reliable is probably not available very often. A car that is also difficult to maintain is probably available even less. 

## Learning Objectives

This learning module provides a quick reference for basic RAM concepts. After completing this module, you should have a basic understanding of and a chance to apply the following concepts:

* Reliability
* Availability
* Mean Time to Repair (MTTR)
* Mean Time to Failure (MTTF)
* Mean Time Between Failures (MTBF)
* Failure Rate
* Probability of Failure
* $B_n$ or $L_n$ life

## Reliability

Reliability is the probability that an item will not fail under defined conditions for a specified period.

$$ Reliability = (1 - (Failed Time/Total Time))*100 $$
Unreliability is the opposite of reliability, that is, the probability that an item will fail under defined conditions for a specified period. 

$$ Unreliability = 1 - Reliability = (Failed Time/Total Time)*100 $$

### Example

```{r reliability, echo=FALSE}
question("An item has run for 3 years total and was failed for 5 of those days. What is the reliability of the item?",
  answer("90%"),
  answer("0.5%"),
  answer("99.5%", correct = TRUE),
  answer("75%"),
  random_answer_order = TRUE
)
```

## Availability

Availability is the probability that an item will be available for service. Unavailable time includes failed time and scheduled maintenance.

$$ Availability = (1 - (Unavailable Time/Total Time))*100 $$

Unavailable time is different than standby time, where standby time is when the item is available for service, but is not being used. 

```{r availability, echo=FALSE}
question("An item has run for 3 years total, was failed for 5 days, and had scheduled maintenance for 14 days. What is the availability of the item?",
  answer("95%"),
  answer("1.7%"),
  answer("98.3%", correct = TRUE),
  answer("50%"),
  random_answer_order = TRUE
)
```

## Mean Time to Repair (MTTR)

The MTTR is a measure of maintainability and is the mean time to repair an item.

$$ MTTR = \sum_{i=1}^n RepairTime_i/RepairCount $$

```{r mttr, echo=FALSE}
question("1000 items run for 3 years total. During that time, 5 failures occur with repair times in days of 5, 10, 15, 8, and 12. What is the MTTR?",
  answer("15 days"),
  answer("5 days"),
  answer("10 days", correct = TRUE),
  answer("8 days"),
  random_answer_order = TRUE
)
```

## Mean Time to Failure (MTTF)

The MTTF is a measure of reliability for *non-repairable* items. It is the ratio of total time by the number of failures for a given item

$$ MTTF = Total Time/FailureCount $$

```{r mttf, echo=FALSE}
question("1000 items run for 3 years total. During that time, 5 failures occur. What is the MTTF?",
  answer("200 years"),
  answer("0.6 years"),
  answer("600 years", correct = TRUE),
  answer("500 years"),
  random_answer_order = TRUE
)
```

## Mean Time Between Failures (MTBF)

MTBF is a measure of reliability for *repairable systems*. The calculation is the same as MTTF, just that the interpretation is slightly different. 

```{r mtbf, echo=FALSE}
question("An item fails 5 times during a total time of 45,000 hours. What is the MTBF of the item?",
  answer("0.0001 hours"),
  answer("10000 hours"),
  answer("9000 hours", correct = TRUE),
  answer("225,000 hours"),
  random_answer_order = TRUE
)
```

## Failure Rate

The failure rate is the ratio of the number of failures by the total operational time for a given item and a specified period.

A key assumption of MTBF is that the failure rate is constant. Under this assumption, the failure rate is the inverse of the MTBF.

$$ \lambda = 1/MTBF $$
```{r failure-rate, echo=FALSE}
question("100 items run for a total time of 5000 hours in a year. Out of these items, 75 have failures. What is the failure rate in hours?",
  answer("0.75"),
  answer("6667"),
  answer("0.00015", correct = TRUE),
  answer("0.015"),
  random_answer_order = TRUE
)
```

## Probability of Failure

Failures do not often occur at predictable times and usually occur randomly according to a probability distribution.

When the failure rate is assumed constant, the Exponential distribution is common.

In this case, the cumulative probability of *failure* prior to time *t*, ***F(t)***, is given as the following

$$ F(t) = 1 - e^{-t\lambda} $$
Where $\lambda$ is the failure rate.

The cumulative probability of *survival* prior to time *t*, ***R(t)***, is the opposite of ***F(t)***, that is

$$ R(t) = 1 - F(t) $$

```{r exponential, echo=FALSE}
question("An item has a failure probability that follows the exponential distribution with a failure rate of 0.1. What is the probability of survival at time 5?",
  answer("39.3%"),
  answer("60.7%", correct = TRUE),
  answer("6.1%"),
  answer("93.9%"),
  random_answer_order = TRUE
)
```

## $B_n$ or $L_n$ Life

The $B_n$ or $L_n$ life is the time at which *n* % of a population are expected to fail (or 1-*n* % are expected to survive). For example, the B10/L10 life is the time at which 10% of a population are expected to fail, B90/L90 is when 90% are expected to fail, and so on.

To calculate the $B_n$ life, we can solve for *t* in the cumulative probability function. For the exponential function, that is

$$ t = -ln(1-F(t))/\lambda $$

Where *F(t)* is analogous with *n* in this case.

```{r B-n, echo=FALSE}
question("An item has a failure probability that follows the exponential distribution with a failure rate of 0.1. What is the B10?",
  answer("0.105"),
  answer("1.05", correct = TRUE),
  answer("60.7%"),
  answer("39.3%"),
  random_answer_order = TRUE
)
```

## Helper Functions

This package has several helper functions for RAM calculations. 

To calculate the reliability of an item that ran for 3 years total and was failed for 5 of those days:

```{r echo=TRUE, eval=FALSE}
rel(5, 3*365)
```

To calculate the availability of an item that ran 3 years total, was failed for 5 days, and had scheduled maintenance for 14 days:

```{r echo=TRUE, eval=FALSE}
avail(5+14, 3*365)
```

The MTTR can be estimated with the base function `mean`. The MTTR for 5 failures with repair times in days of 5, 10, 15, 8, and 12:

```{r echo=TRUE, eval=FALSE}
mean(c(5, 10, 15, 8, 12))
```

To estimate the MTTF for 1000 items that ran for 3 years total:

```{r echo=TRUE, eval=FALSE}
mttf(5+14, 3*365)
```

To estimate the MTBF for an item that failed 5 times over a total time of 45,000 hours:

```{r echo=TRUE, eval=FALSE}
mtbf(5, 45000)
```

To estimate the failure rate for 100 items that ran for 5000 hours and had 75 failures:

```{r echo=TRUE, eval=FALSE}
fr(75, 100*5000)
```

The Exponential failure probability can be estimated with the base function `pexp`. To estimate the probability of survival at time 5 for an item with a failure rate of 0.1:

```{r echo=TRUE, eval=FALSE}
1-pexp(5, 0.1)
```

The $B_n$ life for the Exponential distribution can be estimated with the base function `qexp`. To estimate the B10 life for an item with a failure rate of 0.1:

```{r echo=TRUE, eval=FALSE}
qexp(0.1, 0.1)
```

## Summary

In this tutorial, we've introduced the concepts of Reliability, Availability, and Maintainability. You have learned to calculate reliability using different assumptions and explored the importance of these concepts in system design.

## References

- Abernethy, R.B. (2004) The New Weibull Handbook. Fifth Edition.

- Aden-Buie G, Schloerke B, Allaire J, Rossell Hayes A (2023). learnr: Interactive Tutorials for R. https://rstudio.github.io/learnr/, https://github.com/rstudio/learnr.

