Created
January 1, 2020 23:48
-
-
Save aerinkim/1297c5eebdb1736caacb95e1922ba041 to your computer and use it in GitHub Desktop.
Plotting the beta distribution with different shapes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from scipy.stats import beta | |
import matplotlib.pyplot as plt | |
plt.rcParams['figure.figsize'] = [10, 7] | |
# Bell shape | |
x = np.linspace(0, 1, 10000) | |
y1 = beta.pdf(x, 2, 8) | |
y2 = beta.pdf(x, 5, 5) | |
y3 = beta.pdf(x, 8, 2) | |
plt.title("PDF of Beta (Bell-shape)", fontsize=20) | |
plt.xlabel("X", fontsize=16) | |
plt.ylabel("Probability Density", fontsize=16) | |
plt.plot(x, y1, linewidth=3, color='firebrick') | |
plt.annotate("Beta(2,8)", xy=(0.15, 3.7), size = 14, ha='center', va='center', color='firebrick') | |
plt.plot(x, y2, linewidth=3, color='burlywood') | |
plt.annotate("Beta(5,5)", xy=(0.5, 2.6), size = 14, ha='center', va='center', color='burlywood') | |
plt.plot(x, y3, linewidth=3, color='dodgerblue') | |
plt.annotate("Beta(8,2)", xy=(0.85, 3.7), size = 14, ha='center', va='center', color='dodgerblue') | |
plt.ylim([0, 4]) | |
plt.xlim([0, 1]) | |
plt.show() | |
# Straight lines | |
x = np.linspace(0, 1, 10000) | |
y1 = beta.pdf(x, 1, 2) | |
y2 = beta.pdf(x, 1, 1) | |
y3 = beta.pdf(x, 2, 1) | |
plt.title("PDF of Beta (Straight lines)", fontsize=20) | |
plt.xlabel("X", fontsize=16) | |
plt.ylabel("Probability Density", fontsize=16) | |
plt.plot(x, y1, linewidth=3, color='firebrick') | |
plt.annotate("Beta(1,2)", xy=(0.88, 0.45), size = 14, ha='center', va='center', color='firebrick') | |
plt.plot(x, y2, linewidth=3, color='burlywood') | |
plt.annotate("Beta(1,1)", xy=(0.88, 1.15), size = 14, ha='center', va='center', color='burlywood') | |
plt.plot(x, y3, linewidth=3, color='dodgerblue') | |
plt.annotate("Beta(2,1)", xy=(0.88, 2.0), size = 14, ha='center', va='center', color='dodgerblue') | |
plt.ylim([0, 4]) | |
plt.xlim([0, 1]) | |
plt.show() | |
# U-shape | |
x = np.linspace(0, 1, 10000) | |
y1 = beta.pdf(x, 0.2, 0.8) | |
y2 = beta.pdf(x, 0.5, 0.5) | |
y3 = beta.pdf(x, 0.8, 0.2) | |
plt.title("PDF of Beta (U-shape)", fontsize=20) | |
plt.xlabel("X", fontsize=16) | |
plt.ylabel("Probability Density", fontsize=16) | |
plt.plot(x, y1, linewidth=3, color='firebrick') | |
plt.annotate("Beta(0.2,0.8)", xy=(0.85, 0.45), size = 14, ha='center', va='center', color='firebrick') | |
plt.plot(x, y2, linewidth=3, color='burlywood') | |
plt.annotate("Beta(0.5,0.5)", xy=(0.5, 0.88), size = 14, ha='center', va='center', color='burlywood') | |
plt.plot(x, y3, linewidth=3, color='dodgerblue') | |
plt.annotate("Beta(0.8,0.2)", xy=(0.15, 0.45), size = 14, ha='center', va='center', color='dodgerblue') | |
plt.ylim([0, 4]) | |
plt.xlim([0, 1]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment