Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add diagrams for rand_distr #1

Merged
merged 18 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1,531 changes: 1,531 additions & 0 deletions charts/beta.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,517 changes: 1,517 additions & 0 deletions charts/binomial.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,470 changes: 1,470 additions & 0 deletions charts/cauchy.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,402 changes: 1,402 additions & 0 deletions charts/chi_squared.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added charts/dirichlet.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,171 changes: 1,171 additions & 0 deletions charts/exponential.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,056 changes: 1,056 additions & 0 deletions charts/exponential_exp1.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,520 changes: 1,520 additions & 0 deletions charts/fisher_f.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,744 changes: 1,744 additions & 0 deletions charts/frechet.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,532 changes: 1,532 additions & 0 deletions charts/gamma.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,571 changes: 1,571 additions & 0 deletions charts/geometric.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,466 changes: 1,466 additions & 0 deletions charts/gumbel.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,581 changes: 1,581 additions & 0 deletions charts/hypergeometric.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,436 changes: 1,436 additions & 0 deletions charts/inverse_gaussian.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,575 changes: 1,575 additions & 0 deletions charts/log_normal.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,516 changes: 1,516 additions & 0 deletions charts/normal.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,740 changes: 1,740 additions & 0 deletions charts/normal_inverse_gaussian.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,228 changes: 1,228 additions & 0 deletions charts/pareto.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,744 changes: 1,744 additions & 0 deletions charts/pert.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,583 changes: 1,583 additions & 0 deletions charts/poisson.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,586 changes: 1,586 additions & 0 deletions charts/skew_normal.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,351 changes: 1,351 additions & 0 deletions charts/standard_geometric.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,159 changes: 1,159 additions & 0 deletions charts/standard_normal.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,615 changes: 1,615 additions & 0 deletions charts/student_t.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,237 changes: 1,237 additions & 0 deletions charts/triangular.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15,516 changes: 15,516 additions & 0 deletions charts/unit_ball.svg

Large diffs are not rendered by default.

907 changes: 907 additions & 0 deletions charts/unit_circle.svg

Large diffs are not rendered by default.

886 changes: 886 additions & 0 deletions charts/unit_disc.svg

Large diffs are not rendered by default.

6,218 changes: 6,218 additions & 0 deletions charts/unit_sphere.svg

Large diffs are not rendered by default.

1,500 changes: 1,500 additions & 0 deletions charts/weibull.svg

Large diffs are not rendered by default.

1,575 changes: 1,575 additions & 0 deletions charts/zeta.svg

Large diffs are not rendered by default.

1,556 changes: 1,556 additions & 0 deletions charts/zipf.svg

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
numpy==1.26.4
matplotlib==3.8.4
scipy==1.13.0
35 changes: 35 additions & 0 deletions src/distributions/beta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import beta


def save_to(directory: str, extension: str):
# Defining the Beta distribution PDF
def y(a, b, x):
y = beta.pdf(x, a, b)
y[y > 4] = np.nan
return y

inputs = [(0.5, 0.5), (5, 1), (1, 3), (2, 2), (2, 5)]
# Possible values for the distribution
x = np.linspace(0, 1, 1000)

# Creating the figure and the axis
fig, ax = plt.subplots()

# Plotting the PDF for each value of alpha and beta
for a, b in inputs:
ax.plot(x, y(a, b, x), label=f'α = {a}, β = {b}')

# Adding title and labels
ax.set_title('Beta distribution')
ax.set_xlabel('x')
ax.set_ylabel('Probability density')

# Adding a legend
ax.legend()
ax.grid()
ax.margins(x=0, y=0)

plt.savefig(f"{directory}/beta.{extension}")
plt.close()
33 changes: 33 additions & 0 deletions src/distributions/binomial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import binom


def save_to(directory: str, extension: str):
inputs = [(10, 0.2), (10, 0.6)]
# Possible outcomes for a Binomial distributed variable
outcomes = np.arange(0, 11)
width = 0.5

# Creating the figure and the axis
fig, ax = plt.subplots()

# Plotting the PMF for each value of n and p
for i, (n, p) in enumerate(inputs):
ax.bar(outcomes + i * width - width / 2, binom.pmf(outcomes, n, p), width=width, label=f'n = {n}, p = {p}')

# Adding title and labels
ax.set_title('Binomial distribution')
ax.set_xlabel('k (number of successes)')
ax.set_ylabel('Probability')
ax.set_xticks(outcomes) # set the ticks to be the outcome values

# Adding a legend
ax.legend()
ax.grid()
ax.margins(x=0, y=0)
ymin, ymax = ax.get_ylim()
ax.set_ylim(ymin, ymax * 1.05)

plt.savefig(f"{directory}/binomial.{extension}")
plt.close()
31 changes: 31 additions & 0 deletions src/distributions/cauchy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import numpy as np
import matplotlib.pyplot as plt


def save_to(directory: str, extension: str):
# Possible values for the distribution
x = np.linspace(-7, 7, 1000)

# Creating the figure and the axis
fig, ax = plt.subplots()

inputs = [(0, 1), (0, 0.5), (0, 2), (-2, 1)]

# Plotting the PDF for the Cauchy distribution
for x0, gamma in inputs:
ax.plot(x, 1 / (np.pi * gamma * (1 + ((x - x0) / gamma)**2)), label=f'x₀ = {x0}, γ = {gamma}')

# Adding title and labels
ax.set_title('Cauchy distribution')
ax.set_xlabel('x')
ax.set_ylabel('Probability density')

# Adding a legend
ax.legend()
ax.grid()
ax.margins(x=0, y=0)
ymin, ymax = ax.get_ylim()
ax.set_ylim(ymin, ymax * 1.05)

plt.savefig(f"{directory}/cauchy.{extension}")
plt.close()
34 changes: 34 additions & 0 deletions src/distributions/chi_squared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import chi2


def save_to(directory: str, extension: str):
def y(x, df):
y = chi2.pdf(x, df)
y[y > 1.05] = np.nan
return y
# Degrees of freedom for the distribution
df_values = [1, 2, 3, 5, 9]
# Possible values for the distribution
x = np.linspace(0, 10, 1000)

# Creating the figure and the axis
fig, ax = plt.subplots()

# Plotting the PDF for each value of the degrees of freedom
for df in df_values:
ax.plot(x, y(x, df), label=f'k = {df}')

# Adding title and labels
ax.set_title('Chi-squared distribution')
ax.set_xlabel('Chi-squared statistic')
ax.set_ylabel('Probability density')

# Adding a legend
ax.legend()
ax.grid()
ax.margins(x=0, y=0)

plt.savefig(f"{directory}/chi_squared.{extension}")
plt.close()
67 changes: 67 additions & 0 deletions src/distributions/dirichlet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri
from math import gamma
from matplotlib import ticker


# Code source: https://blog.bogatron.net/blog/2014/02/02/visualizing-dirichlet-distributions/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice little article.

class Dirichlet(object):
def __init__(self, alpha):
self._alpha = np.array(alpha)
self._coef = gamma(np.sum(self._alpha)) / np.multiply.reduce([gamma(a) for a in self._alpha])

def pdf(self, x):
"""Returns pdf value for `x`."""
return self._coef * np.multiply.reduce([xx ** (aa - 1) for (xx, aa) in zip(x, self._alpha)])


def save_to(directory: str, _: str):
extension = "png" # Hardcode png output format. SVG file size for this distribution is ~100x larger.
corners = np.array([[0, 0], [1, 0], [0.5, np.sqrt(0.75)]])
AREA = 0.5 * 1 * np.sqrt(0.75)
triangle = tri.Triangulation(corners[:, 0], corners[:, 1])

pairs = [corners[np.roll(range(3), -i)[1:]] for i in range(3)]
tri_area = lambda xy, pair: 0.5 * np.linalg.norm(np.cross(*(pair - xy)))

def xy2bc(xy, tol=1.e-4):
coords = np.array([tri_area(xy, p) for p in pairs]) / AREA
return np.clip(coords, tol, 1.0 - tol)

def draw_pdf_contours(ax, dist, alphas, nlevels=200, subdiv=8):
refiner = tri.UniformTriRefiner(triangle)
trimesh = refiner.refine_triangulation(subdiv=subdiv)
pvals = [dist.pdf(xy2bc(xy)) for xy in zip(trimesh.x, trimesh.y)]

contour = ax.tricontourf(trimesh, pvals, nlevels, cmap='plasma')
ax.axis('equal')
ax.set_xlim(0, 1)
ax.set_ylim(0, np.sqrt(0.75))
ax.axis('on')
ax.set_title(f"α = {alphas}", fontsize=10)

return contour

inputs = [
[1.5, 1.5, 1.5],
[5.0, 5.0, 5.0],
[1.0, 2.0, 2.0],
[2.0, 4.0, 8.0]
]

fig, axes = plt.subplots(2, 2, figsize=(12, 10))
contours = []
for ax, alphas in zip(axes.flatten(), inputs):
contour = draw_pdf_contours(ax, Dirichlet(alphas), alphas)
contours.append(contour)

# Adding the main title and colorbar
plt.suptitle('Dirichlet Distribution', fontsize=16)
cbar = fig.colorbar(contours[0], ax=axes.ravel().tolist(), orientation='horizontal', pad=0.75)
cbar.set_label('Probability Density')
cbar.ax.xaxis.set_major_locator(ticker.MaxNLocator(nbins=5)) # Set number of ticks on colorbar

plt.subplots_adjust(top=0.90, bottom=0.25) # Adjust the subplots to fit the title and colorbar
plt.savefig(f"{directory}/dirichlet.{extension}")
plt.close()
30 changes: 30 additions & 0 deletions src/distributions/exponential.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import expon


def save_to(directory: str, extension: str):
# Possible values of lambda for the distribution
lambda_values = [1, 0.5, 2]
# Possible values for the distribution
x = np.linspace(0, 5, 1000)

# Creating the figure and the axis
fig, ax = plt.subplots()

# Plotting the PDF for each value of lambda
for lmbda in lambda_values:
ax.plot(x, expon.pdf(x, scale=1 / lmbda), label=f'λ = {lmbda}')

# Adding title and labels
ax.set_title('Exponential distribution')
ax.set_xlabel('x')
ax.set_ylabel('Probability density')

# Adding a legend
ax.legend()
ax.grid()
ax.margins(x=0, y=0)

plt.savefig(f"{directory}/exponential.{extension}")
plt.close()
29 changes: 29 additions & 0 deletions src/distributions/exponential_exp1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import expon


def save_to(directory: str, extension: str):
# Fixed value for lambda
lmbda = 1
# Possible values for the distribution
x = np.linspace(0, 5, 1000)

# Creating the figure and the axis
fig, ax = plt.subplots()

# Plotting the PDF for each value of lambda
ax.plot(x, expon.pdf(x, scale=1 / lmbda), label=f'λ = {lmbda}')

# Adding title and labels
ax.set_title('Exponential distribution')
ax.set_xlabel('x')
ax.set_ylabel('Probability density')

# Adding a legend
ax.legend()
ax.grid()
ax.margins(x=0, y=0)

plt.savefig(f"{directory}/exponential_exp1.{extension}")
plt.close()
35 changes: 35 additions & 0 deletions src/distributions/fisher_f.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import f


def save_to(directory: str, extension: str):
def y(x, dfn, dfd):
y = f.pdf(x, dfn, dfd)
y[y > 2.3] = np.nan
return y

# Degrees of freedom for the distribution
d1_d2 = [(1, 1), (2, 1), (3, 1), (10, 1), (10, 10), (100, 100)]
# Possible values for the distribution
x = np.linspace(0, 5, 1000)

# Creating the figure and the axis
fig, ax = plt.subplots()

# Plotting the PDF for each value of the degrees of freedom
for m, n in d1_d2:
ax.plot(x, y(x, m, n), label=f'm = {m}, n = {n}')

# Adding title and labels
ax.set_title('F-distribution')
ax.set_xlabel('F-statistic')
ax.set_ylabel('Probability density')

# Adding a legend
ax.legend()
ax.grid()
ax.margins(x=0, y=0)

plt.savefig(f"{directory}/fisher_f.{extension}")
plt.close()
34 changes: 34 additions & 0 deletions src/distributions/frechet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import invweibull


def save_to(directory: str, extension: str):
def y(x, shape, loc, scale):
return invweibull.pdf(x, shape, loc=loc, scale=scale)
# Inputs for the distribution
inputs = [(0, 1, 1), (1, 1, 1), (0, 0.5, 1), (0, 2, 1), (0, 1, 0.35), (0, 1, 2)]
# x values for the distribution
x = np.linspace(0, 5, 1000)

# Creating the figure and the axis
fig, ax = plt.subplots()

# Plotting the PDF for each value of a
for loc, scale, shape in inputs:
ax.plot(x, y(x, shape, loc, scale), label=f'μ = {loc}, σ = {scale}, α = {shape}')

# Adding title and labels
ax.set_title('Fréchet distribution')
ax.set_xlabel('x')
ax.set_ylabel('Probability density')

# Adding a legend
ax.legend()
ax.grid()
ax.margins(x=0, y=0)
ymin, ymax = ax.get_ylim()
ax.set_ylim(ymin, ymax * 1.05)

plt.savefig(f"{directory}/frechet.{extension}")
plt.close()
31 changes: 31 additions & 0 deletions src/distributions/gamma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gamma


def save_to(directory: str, extension: str):
inputs = [(1, 1), (2, 1), (3, 1), (1, 2), (2, 2), (3, 2)]
# Possible values for the distribution
x = np.linspace(0, 7, 1000)

# Creating the figure and the axis
fig, ax = plt.subplots()
colors = ["blue", "red", "green", "blue", "red", "green"]
alphas = [1.0, 1.0, 1.0, 0.6, 0.6, 0.6]

# Plotting the PDF for each value of alpha and beta
for i, (k, theta) in enumerate(inputs):
ax.plot(x, gamma.pdf(x, k, scale=theta), label=f'k = {k}, θ = {theta}', color=colors[i], alpha=alphas[i])

# Adding title and labels
ax.set_title('Gamma distribution')
ax.set_xlabel('x')
ax.set_ylabel('Probability density')

# Adding a legend
ax.legend()
ax.grid()
ax.margins(x=0, y=0)

plt.savefig(f"{directory}/gamma.{extension}")
plt.close()