From 28ff3932fb8cd3ee0780a406672fd30347a39540 Mon Sep 17 00:00:00 2001 From: aleicazatti Date: Thu, 25 Jul 2024 11:55:46 -0300 Subject: [PATCH 1/2] Distribution Gallery: Add Gamma --- docs/examples/gallery/gamma.md | 99 ++++++++++++++++++++++++++++++++++ docs/gallery_content.rst | 2 +- docs/index.rst | 2 +- 3 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 docs/examples/gallery/gamma.md diff --git a/docs/examples/gallery/gamma.md b/docs/examples/gallery/gamma.md new file mode 100644 index 00000000..70e80c7d --- /dev/null +++ b/docs/examples/gallery/gamma.md @@ -0,0 +1,99 @@ +--- +jupytext: + text_representation: + extension: .md + format_name: myst +kernelspec: + display_name: Python 3 + language: python + name: python3 +--- +# Gamma Distribution + +The gamma distribution is a continuous probability distribution that describes the time until a specified number of events occur in a Poisson process, in which $\alpha$ events occur continuously and independently at a constant average rate $\beta$. The gamma distribution is characterized by two parameters: the shape parameter $\alpha$ and the rate parameter $\beta$. + +The gamma distribution is widely used in many fields, including physics, biology, finance, and reliability engineering. Examples include modeling the time until a specified number of decays of a radioactive atom, the time until a specified number of seizures in patients with epilepsy, the time until a specified number of failures of a machine, and the time until a specified number of changes in the price of a stock. + +In Bayesian modeling, the gamma distribution is used as a conjugate prior for the precision (inverse variance) of the normal distribution. It is also the conjugate prior for the exponential distribution, which is a special case of the gamma distribution with $\alpha = 1$. + +## Probability Density Function (PDF): + +```{code-cell} +--- +tags: [remove-input] +mystnb: + image: + alt: Gamma Distribution PDF +--- + +import numpy as np +import arviz as az +from preliz import Gamma +az.style.use('arviz-doc') +alphas = [1, 3, 7.5] +betas = [0.5, 1., 1.] + +for alpha, beta in zip(alphas, betas): + Gamma(alpha, beta).plot_pdf() +``` + +## Cumulative Distribution Function (CDF): + +```{code-cell} +--- +tags: [remove-input] +mystnb: + image: + alt: Gamma Distribution CDF +--- + +for alpha, beta in zip(alphas, betas): + Gamma(alpha, beta).plot_cdf() +``` + +## Key properties and parameters: + +```{eval-rst} + +======== ========================================== +Support :math:`x \in (0, \infty)` +Mean :math:`\frac{\alpha}{\beta}` +Variance :math:`\frac{\alpha}{\beta^2}` +======== ========================================== +``` + +**Probability Density Function (PDF):** + +$$ +f(x|\alpha, \beta) = \frac{\beta^\alpha x^{\alpha - 1} e^{-\beta x}}{\Gamma(\alpha)} +$$ + +where $\Gamma$ is the [gamma function](https://en.wikipedia.org/wiki/Gamma_function). + +**Cumulative Distribution Function (CDF):** + +$$ +F(x|\alpha, \beta) = \frac{1}{\Gamma(\alpha)} \gamma(\alpha, \beta x) +$$ + +where $\gamma$ is the [lower incomplete gamma function](https://en.wikipedia.org/wiki/Incomplete_gamma_function). + +```{seealso} +:class: seealso + +**Common Alternatives:** + +- [Exponential Distribution](exponential.md) - A special case of the Gamma distribution with the shape parameter $\alpha = 1$. +- [Chi-Squared Distribution](chisquared.md) - A special case of the Gamma distribution with the shape parameter $\alpha = \nu/2$ and the scale parameter $\beta = 1/2$. + +**Related Distributions:** + +- [Inverse Gamma Distribution](inverse-gamma.md) - The reciprocal of a gamma-distributed random variable. +- [Poisson Distribution](poisson.md) - While the Gamma distribution models the time until a specified number of events occur in a Poisson process, the Poisson distribution models the number of events in fixed intervals of time or space. +- [Negative Binomial Distribution](negative-binomial.md) - The discrete counterpart of the Gamma distribution, modeling the number of trials needed to achieve a specified number of successes in a sequence of Bernoulli trials. +``` + + +## References + +- [Wikipedia - Gamma distribution](https://en.wikipedia.org/wiki/Gamma_distribution) \ No newline at end of file diff --git a/docs/gallery_content.rst b/docs/gallery_content.rst index fb0226b5..b3af00d8 100644 --- a/docs/gallery_content.rst +++ b/docs/gallery_content.rst @@ -91,7 +91,7 @@ Continuous Distributions Exponential .. grid-item-card:: - :link: ./api_reference.html#preliz.distributions.gamma.Gamma + :link: ./examples/gallery/gamma.html :text-align: center :shadow: none :class-card: example-gallery diff --git a/docs/index.rst b/docs/index.rst index eaa5ef8e..ccbaf4e1 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1,4 +1,4 @@ -``````````````A tool-box for prior elicitation +A tool-box for prior elicitation =================================================== |PyPI version| From 195c9c795fab19217edae24619fb43712cdbbb2e Mon Sep 17 00:00:00 2001 From: Alejandro Icazatti Date: Fri, 26 Jul 2024 11:31:59 -0300 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Osvaldo A Martin --- docs/examples/gallery/gamma.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/examples/gallery/gamma.md b/docs/examples/gallery/gamma.md index 70e80c7d..d822784a 100644 --- a/docs/examples/gallery/gamma.md +++ b/docs/examples/gallery/gamma.md @@ -12,9 +12,9 @@ kernelspec: The gamma distribution is a continuous probability distribution that describes the time until a specified number of events occur in a Poisson process, in which $\alpha$ events occur continuously and independently at a constant average rate $\beta$. The gamma distribution is characterized by two parameters: the shape parameter $\alpha$ and the rate parameter $\beta$. -The gamma distribution is widely used in many fields, including physics, biology, finance, and reliability engineering. Examples include modeling the time until a specified number of decays of a radioactive atom, the time until a specified number of seizures in patients with epilepsy, the time until a specified number of failures of a machine, and the time until a specified number of changes in the price of a stock. +The gamma distribution is widely used in many settings, including modeling the time until a specified number of decays of a radioactive atom, the time until a specified number of seizures in patients with epilepsy, the time until a specified number of failures of a machine, and the time until a specified number of changes in the price of a stock. -In Bayesian modeling, the gamma distribution is used as a conjugate prior for the precision (inverse variance) of the normal distribution. It is also the conjugate prior for the exponential distribution, which is a special case of the gamma distribution with $\alpha = 1$. +The gamma distribution is used as a conjugate prior for the exponential distribution and for the precision of the normal distribution. It can also used as prior for scale parameters, in particular when there is interest in avoiding values close to zero or one wants to include more information than with other prior commonly used for scale parameter like the halfnormal or the exponential. ## Probability Density Function (PDF): @@ -26,7 +26,6 @@ mystnb: alt: Gamma Distribution PDF --- -import numpy as np import arviz as az from preliz import Gamma az.style.use('arviz-doc')