Skip to content

Instantly share code, notes, and snippets.

@galenseilis
Created October 16, 2022 02:34
Show Gist options
  • Select an option

  • Save galenseilis/e287626518ce47b1448e3a4f074959f6 to your computer and use it in GitHub Desktop.

Select an option

Save galenseilis/e287626518ce47b1448e3a4f074959f6 to your computer and use it in GitHub Desktop.
from scipy.stats import beta
import matplotlib.pyplot as plt
import numpy as np
params = (((8,2), (5,5), (2,8)), ((2,1), (1,1), (1,2)), ((0.8,0.2), (0.5,0.5), (0.2,0.8)))
title_suffixes = ('Bell-shape', 'Straight lines', 'U-shape')
colors = ('blue', 'yellow', 'red')
x = np.linspace(0, 1, 10000)
for i in range(3):
fig, ax = plt.subplots(1, 1)
ax.set_xlim(left=0, right=1)
ax.set_ylim(bottom=0, top=4)
ax.set_xlabel('X')
ax.set_ylabel('Probability density')
ax.set_title(f'PDF of Beta ({title_suffixes[i]})')
for j, (a,b) in enumerate(params[i]):
y = beta.pdf(x, a, b)
ax.plot(x, y, color=colors[j])
if i == 0:
text_x = (a - 1) / (a + b - 2)
text_y = beta.pdf(text_x, a, b) + 0.1
text_x -= 0.05
elif i == 1:
text_x = 0.81
text_y = beta.pdf(text_x, a, b) + 0.25
else:
text_x = x[np.argmin(y)] - 0.1
text_y = np.min(y) + 0.1
ax.text(text_x, text_y, s=f'Beta({a},{b})', color=colors[j])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment