Created
December 2, 2022 12:02
-
-
Save maxdevblock/96e414b71f829d6f4279bdee47f3faed to your computer and use it in GitHub Desktop.
This file contains hidden or 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
beta_hat_1 = sigma_xy / sigma2_x | |
beta_hat_0 = y_bar - beta_hat_1*x_bar | |
Y_hat = beta_hat_0 + beta_hat_1*X | |
sigma2_y_hat = ((Y_hat - y_bar)**2).sum()/Y.size | |
R2 = sigma2_y_hat / sigma2_y | |
r2 = r**2 | |
fig, ax = plt.subplots(facecolor="w") | |
plt.plot(X, Y, "o", label="observations") | |
plt.axvline(x_bar, ls="--", label=fr"$\bar{{x}}={x_bar:.2f}$", color="C1") | |
plt.axhline(y_bar, ls="--", label=fr"$\bar{{y}}={y_bar:.2f}$", color="C2") | |
X_continuous = np.linspace(min(X), max(X), 100) | |
sigma_x_1 = (X_continuous>=x_bar-sigma_x)&(X_continuous<=x_bar+sigma_x) | |
plt.fill_between( | |
X_continuous[sigma_x_1], | |
min(Y), max(Y), | |
color="C1", alpha=.15, | |
label=fr"$\sigma_{{x}}={sigma_x:.2f}$" | |
) | |
Y_continuous = np.linspace(min(Y), max(Y), 100) | |
sigma_y_1 = (Y_continuous>=y_bar-sigma_y)&(Y_continuous<=y_bar+sigma_y) | |
plt.fill_betweenx( | |
Y_continuous[sigma_y_1], | |
min(X), max(X), | |
color="C2", alpha=.15, | |
label=fr"$\sigma_{{y}}={sigma_y:.2f}$" | |
) | |
plt.plot(X, Y_hat, "k", label="regression line") | |
plt.legend(loc="upper left", bbox_to_anchor=(1,1)) | |
plt.text(1.05, .4, f"$r={r:.9f}$", ha="left", va="center", transform=ax.transAxes) | |
plt.text(1.05, .3, fr"$\hat{{\beta}}_0={beta_hat_0:.3f}$", ha="left", va="center", transform=ax.transAxes) | |
plt.text(1.05, .2, fr"$\hat{{\beta}}_1={beta_hat_1:.3f}$", ha="left", va="center", transform=ax.transAxes) | |
plt.text(1.05, .1, f"$R^2={R2:.9f}$", ha="left", va="center", transform=ax.transAxes) | |
plt.text(1.05, .0, f"$r^2={r2:.9f}$", ha="left", va="center", transform=ax.transAxes) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment