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 2 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,462 changes: 1,462 additions & 0 deletions diagrams/beta.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,427 changes: 1,427 additions & 0 deletions diagrams/binomial.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,390 changes: 1,390 additions & 0 deletions diagrams/cauchy.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,336 changes: 1,336 additions & 0 deletions diagrams/chi_squared.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,112 changes: 1,112 additions & 0 deletions diagrams/exponential.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,014 changes: 1,014 additions & 0 deletions diagrams/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,345 changes: 1,345 additions & 0 deletions diagrams/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,453 changes: 1,453 additions & 0 deletions diagrams/gamma.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,476 changes: 1,476 additions & 0 deletions diagrams/geometric.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,389 changes: 1,389 additions & 0 deletions diagrams/gumbel.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,450 changes: 1,450 additions & 0 deletions diagrams/hypergeometric.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,365 changes: 1,365 additions & 0 deletions diagrams/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,235 changes: 1,235 additions & 0 deletions diagrams/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,432 changes: 1,432 additions & 0 deletions diagrams/normal.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,798 changes: 1,798 additions & 0 deletions diagrams/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,152 changes: 1,152 additions & 0 deletions diagrams/pareto.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
864 changes: 864 additions & 0 deletions diagrams/pert.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,473 changes: 1,473 additions & 0 deletions diagrams/poisson.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,513 changes: 1,513 additions & 0 deletions diagrams/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,271 changes: 1,271 additions & 0 deletions diagrams/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,109 changes: 1,109 additions & 0 deletions diagrams/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,350 changes: 1,350 additions & 0 deletions diagrams/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,147 changes: 1,147 additions & 0 deletions diagrams/triangular.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,053 changes: 1,053 additions & 0 deletions diagrams/weibull.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,387 changes: 1,387 additions & 0 deletions diagrams/zeta.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,434 changes: 1,434 additions & 0 deletions diagrams/zipf.svg

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions distr/beta.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 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()

plt.savefig(f"{directory}/beta.{extension}")
plt.close()
29 changes: 29 additions & 0 deletions distr/binomial.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 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()

plt.savefig(f"{directory}/binomial.{extension}")
plt.close()
27 changes: 27 additions & 0 deletions distr/cauchy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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, 0.5), (0, 1), (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('P(x)')

# Adding a legend
ax.legend()

plt.savefig(f"{directory}/cauchy.{extension}")
plt.close()
32 changes: 32 additions & 0 deletions distr/chi_squared.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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.0] = 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()

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

def save_to(directory: str, extension: str):
def plot_dirichlet(alpha, ax):
"""
Plots a Dirichlet distribution given alpha parameters and axis.
"""
# Create a 2D meshgrid of points
resolution = 200 # Resolution of the visualization
x = np.linspace(0, 1, resolution)
y = np.linspace(0, 1, resolution)
X, Y = np.meshgrid(x, y)

# Calculate remaining coordinate for the 3-simplex (3D Dirichlet is defined on a triangle in 2D)
Z = 1 - X - Y

# Filter out points outside the triangle
valid = Z >= 0

# Prepare coordinates and the probability density function (PDF) array
X = X[valid]
Y = Y[valid]
Z = Z[valid]
points = np.vstack((X, Y, Z)).T # Stack the valid points

# Calculate the Dirichlet PDF for valid points
PDF = dirichlet.pdf(points, alpha)

# Reshape PDF back into a 2D array for contour plotting
# Initialize full grid PDF to zero (areas outside the triangle remain zero)
full_PDF = np.zeros((resolution, resolution))
full_PDF[valid] = PDF # Place the calculated PDF in the valid grid positions

# Create a contour plot on the provided axis
contour = ax.contourf(np.linspace(0, 1, resolution), np.linspace(0, 1, resolution), full_PDF, levels=50, cmap='Blues')
plt.colorbar(contour, ax=ax, pad=0.05, aspect=10)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_xticks([])
ax.set_yticks([])
ax.set_xlabel(r'$x_1$', fontsize=12)
ax.set_ylabel(r'$x_2$', fontsize=12)
ax.set_title(r'$\alpha = {}$'.format(alpha), fontsize=14)

# Define alpha parameters for the Dirichlet distributions to be plotted
alpha_params = [
(1.5, 1.5, 1.5),
(5.0, 5.0, 5.0),
(1.0, 2.0, 2.0),
(2.0, 4.0, 8.0)
]

# Create a figure with subplots
fig, axes = plt.subplots(2, 2, figsize=(10, 8))

# Loop through the list of alpha parameters
for alpha, ax in zip(alpha_params, axes.flatten()):
plot_dirichlet(alpha, ax)

plt.savefig(f"{directory}/dirichlet.{extension}")
plt.close()
28 changes: 28 additions & 0 deletions distr/exponential.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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()

plt.savefig(f"{directory}/exponential.{extension}")
plt.close()
27 changes: 27 additions & 0 deletions distr/exponential_exp1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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()

plt.savefig(f"{directory}/exponential_exp1.{extension}")
plt.close()
33 changes: 33 additions & 0 deletions distr/fisher_f.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 f


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

# Degrees of freedom for the distribution
d1_d2 = [(1, 1), (2, 1), (5, 2), (10, 1), (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()

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


def save_to(directory: str, extension: str):
pass
27 changes: 27 additions & 0 deletions distr/gamma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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()

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

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

# Adding a legend
ax.legend()

plt.savefig(f"{directory}/gamma.{extension}")
plt.close()
30 changes: 30 additions & 0 deletions distr/geometric.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 geom


def save_to(directory: str, extension: str):
# Possible values of p for the distribution
p_values = [0.2, 0.5, 0.8]
# Possible outcomes for a Geometric distributed variable
outcomes = np.arange(1, 11)

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

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

# Adding title and labels
ax.set_title('Geometric distribution')
ax.set_xlabel('Number of trials until first success')
ax.set_ylabel('Probability')
ax.set_xticks(outcomes) # set the ticks to be the outcome values

# Adding a legend
ax.legend()

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


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

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

# Plotting the PDF for each value of mu and beta
for mu, beta in inputs:
ax.plot(x, gumbel_r.pdf(x, loc=mu, scale=beta), label=f'μ = {mu}, β = {beta}')

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

# Adding a legend
ax.legend()

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