import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
def plot_f_distribution(df1, df2, alpha=0.05):
# Create x values for the plot
x = np.linspace(0, 5, 1000)
# Calculate F-distribution values
y = stats.f.pdf(x, df1, df2)
# Calculate critical F-value
f_crit = stats.f.ppf(1 - alpha, df1, df2)
# Create the plot
plt.figure(figsize=(10, 6))
# Add solid black line at y=0 (x-axis)
plt.axhline(y=0, color='black', linewidth=1.5)
# Plot F-distribution curve
plt.plot(x, y, 'b-', lw=2, label=f'F({df1}, {df2})')
# Fill the rejection region
x_fill = x[x >= f_crit]
y_fill = stats.f.pdf(x_fill, df1, df2)
plt.fill_between(x_fill, y_fill, color='red', alpha=0.3)
# Add vertical line at critical value
plt.axvline(x=f_crit, color='r', linestyle='--', label=f'F-critical = {f_crit:.2f}')
# Add arrow and annotation for alpha
max_y = max(y)
plt.annotate(f'α = {alpha}',
xy=(f_crit+1, stats.f.pdf(f_crit, df1, df2)),
xytext=(f_crit + 1.85, max_y * 0.8),
arrowprops=dict(facecolor='black', shrink=0.05))
# Add x=2.711 label on x-axis
plt.text(2.75, -max_y * 0.01, 'x = 2.71',
horizontalalignment='center',
verticalalignment='top')
plt.axvline(x=2.711, color='green', linestyle=':', alpha=0.5)
# Customize the plot
plt.title('F-Distribution')
plt.xlabel('F-value')
plt.ylabel('Probability Density')
plt.legend()
plt.grid(True, alpha=0.3)
# Show the plot
plt.show()
# Example usage
plot_f_distribution(df1=5, df2=20)