Skip to content

Commit

Permalink
Distributions Gallery: Add Triangular (arviz-devs#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleicazatti authored Oct 28, 2024
1 parent d17ee9f commit fef16e3
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
100 changes: 100 additions & 0 deletions docs/examples/gallery/triangular.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
jupytext:
text_representation:
extension: .md
format_name: myst
kernelspec:
display_name: Python 3
language: python
name: python3
---
# Triangular Distribution

<audio controls> <source src="../../_static/triangular.mp3" type="audio/mpeg"> This browser cannot play the pronunciation audio file for this distribution. </audio>

The Triangular distribution is a continuous probability distribution with a triangular shape. It is defined by three parameters: the lower bound $lower$, the mode $c$, and the upper bound $upper$. Where $lower \leq c \leq upper$.

The Triangular distribution is often called a "lack of knowledge" distribution because it can be used when there is no prior knowledge about the distribution of the random variable other than the minimum, maximum, and most likely values. It is often used in business simulations and decision-making, project management, and audio dithering.

## Probability Density Function (PDF):

```{code-cell}
---
tags: [remove-input]
mystnb:
image:
alt: Triangular Distribution PDF
---
from preliz import Triangular, style
style.use('preliz-doc')
lowers = [0., -1, 2]
cs = [2., 0., 6.5]
uppers = [4., 1, 8]
for lower, c, upper in zip(lowers, cs, uppers):
scale = upper - lower
c_ = (c - lower) / scale
Triangular(lower, c, upper).plot_pdf()
```

## Cumulative Distribution Function (CDF):

```{code-cell}
---
tags: [remove-input]
mystnb:
image:
alt: Triangular Distribution CDF
---
for lower, c, upper in zip(lowers, cs, uppers):
scale = upper - lower
c_ = (c - lower) / scale
Triangular(lower, c, upper).plot_cdf()
```

## Key properties and parameters:

```{eval-rst}
======== ==========================================
Support :math:`x \in [lower, upper]`
Mean :math:`\dfrac{lower + upper + c}{3}`
Variance :math:`\dfrac{upper^2 + lower^2 +c^2 - lower*upper - lower*c - upper*c}{18}`
======== ==========================================
```

**Probability Density Function (PDF):**

$$
f(x|lower, c, upper) =
\begin{cases}
0 & \text{for } x < lower \\
\frac{2(x - lower)}{(upper - lower)(c - lower)} & \text{for } lower \leq x < c \\
\frac{2}{upper - lower} & \text{for } x = c \\
\frac{2(upper - x)}{(upper - lower)(upper - c)} & \text{for } c < x \leq upper \\
\end{cases}
$$

**Cumulative Distribution Function (CDF):**

$$
F(x|lower, c, upper) =
\begin{cases}
0 & \text{for } x < lower \\
\frac{(x - lower)^2}{(upper - lower)(c - lower)} & \text{for } lower \leq x < c \\
1 - \frac{(upper - x)^2}{(upper - lower)(upper - c)} & \text{for } c \leq x \leq upper \\
1 & \text{for } x \geq upper
\end{cases}
$$

```{seealso}
:class: seealso
**Related Distributions:**
- [Uniform Distribution](uniform.md) - A distribution with constant probability that is also used when there is limited prior knowledge about the distribution of the random variable: in this case, only the $lower$ and $upper$ bounds are known.
```

## References

- Wikipedia. [Triangular distribution](https://en.wikipedia.org/wiki/Triangular_distribution).
2 changes: 1 addition & 1 deletion docs/gallery_content.rst
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ Continuous Distributions
Skew-Student's t

.. grid-item-card::
:link: ./api_reference.html#preliz.distributions.triangular.Triangular
:link: ./examples/gallery/triangular.html
:text-align: center
:shadow: none
:class-card: example-gallery
Expand Down

0 comments on commit fef16e3

Please sign in to comment.