-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 17 commits
849667a
bfb00dd
e37e346
3370b77
8cc51af
58eed06
60da767
489ab39
9245667
357410c
6d15312
40f483b
54e1f60
d65955a
f83637b
905e506
cf6183e
631d0c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
numpy==1.26.4 | ||
matplotlib==3.8.4 | ||
scipy==1.13.0 | ||
python-ternary==1.0.8 |
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() |
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() |
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() |
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import matplotlib.tri as tri | ||
from math import gamma | ||
import ternary | ||
|
||
|
||
# Code source: https://blog.bogatron.net/blog/2014/02/02/visualizing-dirichlet-distributions/ | ||
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, alphas, nlevels=200, subdiv=8): | ||
refiner = tri.UniformTriRefiner(triangle) | ||
trimesh = refiner.refine_triangulation(subdiv=subdiv) | ||
pvals = [Dirichlet(alphas).pdf(xy2bc(xy)) for xy in zip(trimesh.x, trimesh.y)] | ||
|
||
contour = ax.tricontourf(trimesh, pvals, nlevels, cmap='plasma') | ||
ternary.plt.colorbar(contour, ax=ax, orientation='vertical', fraction=0.05, pad=0.05) | ||
|
||
ax.axis('equal') | ||
ax.spines['top'].set_visible(False) | ||
ax.spines['right'].set_visible(False) | ||
ax.spines['bottom'].set_visible(False) | ||
ax.spines['left'].set_visible(False) | ||
|
||
tax = ternary.TernaryAxesSubplot(ax=ax, scale=1.0) | ||
|
||
tax.boundary(linewidth=1.0) | ||
|
||
tax.ticks(axis='lbr', linewidth=1, multiple=0.2, tick_formats="%.1f", offset=0.02) | ||
|
||
fontsize = 13 | ||
tax.set_title(f"α = {alphas}", fontsize=fontsize, pad=20) | ||
tax.right_axis_label("$x_3$", fontsize=fontsize, offset=0.15) | ||
tax.left_axis_label("$x_1$", fontsize=fontsize, offset=0.15) | ||
tax.bottom_axis_label("$x_2$", fontsize=fontsize) | ||
tax._redraw_labels() # Won't do this automatically because of the way we are saving the plot | ||
|
||
tax.clear_matplotlib_ticks() | ||
|
||
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)) | ||
for ax, alphas in zip(axes.flatten(), inputs): | ||
draw_pdf_contours(ax, alphas) | ||
|
||
# Adding the main title and colorbar | ||
ternary.plt.suptitle('Dirichlet Distribution', fontsize=16) | ||
|
||
ternary.plt.savefig(f"{directory}/dirichlet.{extension}") | ||
plt.close() |
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() |
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() |
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() |
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() |
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice little article.