In [3]:
#!pip install statsmodels
In [1]:
import ipywidgets as widgets
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
from sklearn.linear_model import LinearRegression
import statsmodels.api as sm
from sklearn.metrics import mean_squared_error
from scipy.stats import gaussian_kde
%matplotlib inline
In [2]:
def regression_rmse(b = 0):
x = np.array([1.2, 3.4, 4.8, 2.4, 4.0, 2.3, 4.5, 0.4])
x_with_constant = sm.add_constant(x)
y = np.array([9.8, 15, 25.4, 10.5, 22.1, 19, 29,5.5])
mean_x = np.mean(x)
mean_y = np.mean(y)
bs = np.arange(0, 10,0.25)
RMSEs = []
for slope in bs:
intercept = slope*(0-mean_x)+mean_y
y_preds = x*slope+intercept
errors = y-y_preds
rmse = np.sqrt(((y - y_preds) ** 2).mean())
RMSEs.append(rmse)
x_line = np.linspace(min(x), max(x), 100)
y_line = b * (x_line - mean_x) + mean_y
intercept = b*(0-mean_x)+mean_y
y_pred = x*b+intercept
error = y-y_pred
rmse = np.sqrt(((y - y_pred) ** 2).mean())
kde = gaussian_kde(error)
xkde = np.linspace(min(error), max(error), 100)
fig, ax = plt.subplots(1, 3, figsize=(9,3))
ax[0].scatter(x, y, color='black', label='data',s=12, zorder=4)
ax[0].plot(x, y_pred, color='red', label='fitted line', zorder=2)
ax[0].scatter(mean_x,mean_y,color='green',marker='s', zorder=3)
ax[0].plot([x,x],[y,y_pred],color='gray',linestyle='dashed',linewidth = 1,zorder=1)
ax[0].set_xlabel('x')
ax[0].set_ylabel('y')
ax[0].set_ylim([0,35])
ax[0].set_xlim([0,6])
ax[0].legend()
ax[0].grid()
ax[1].plot(bs,RMSEs,color='black',zorder=1)
ax[1].scatter(b,rmse,color='red',zorder=2)
ax[1].set_title(f'RMSE: {rmse:.3f}')
ax[1].set_xlabel('b')
ax[1].set_ylabel('rmse')
ax[1].grid()
ax[1].set_xlim([-0.5,10.5])
ax[1].set_ylim([0,10])
ax[2].hist(error, bins=3, color='gray', alpha=0.5, zorder=1, edgecolor='black', density=True)
ax[2].set_ylim([0,0.2])
ax[2].set_xlim([-10,10])
ax[2].plot(xkde, kde(xkde), color='red', label="KDE",zorder=2)
ax[2].set_title('Residuals')
plt.tight_layout()
plt.show()
In [4]:
widgets.interact(regression_rmse, b = (0,10,0.1))
interactive(children=(FloatSlider(value=0.0, description='b', max=10.0), Output()), _dom_classes=('widget-inte…
Out[4]:
<function __main__.regression_rmse(b=0)>
In [6]:
def simulation_scatter(b = 0):
x = np.array([1.2, 3.4, 4.8, 2.4, 4.0, 2.3, 4.5, 0.4]).reshape(-1, 1)
y = np.array([9.8, 17.9, 25.4, 12.5, 22.1, 18, 29,5.5])
model = LinearRegression()
result = model.fit(x, y)
y_pred = model.predict(x)
errors = y - y_pred
rmse = np.sqrt(mean_squared_error(y, y_pred))
fig, ax = plt.subplots(1, 2, figsize=(8, 4))
# Left plot: Scatter plot with regression line
ax[0].scatter(x, y, color='blue', label='Original data')
ax[0].plot(x, y_pred, color='red', label='Regression line')
ax[0].set_title('Scatter Plot with Regression Line')
ax[0].set_xlabel('x')
ax[0].set_ylabel('y')
ax[0].legend()
# Right plot: Scatter plot of RMSE (errors)
ax[1].scatter(x, errors, color='green', label='Errors (y - y_pred)')
ax[1].axhline(0, color='black', linestyle='--') # Baseline for zero error
ax[1].set_title(f'Error Scatter (RMSE: {rmse:.2f})')
ax[1].set_xlabel('x')
ax[1].set_ylabel('Error')
ax[1].legend()
plt.tight_layout()
plt.show()
In [7]:
import matplotlib.pyplot as plt
from scipy.stats import binom
def Binomial_Distribution(p = 0, n=10):
#n = 10
#p = 0.2
# Range of outcomes
x = range(n + 1)
y = binom.pmf(x, n, p)
# Plot
fig, ax = plt.subplots(1, 1, figsize=(4,4))
plt.bar(x, y, color='lightblue')
plt.xlabel('Number of Successes')
plt.ylabel('Probability')
plt.title(f'Binomial Distribution (p={p:.1f}, n={n:.0f})')
plt.grid(linestyle='--')
plt.ylim([0,0.4])
plt.tight_layout()
plt.show()
In [8]:
widgets.interact(Binomial_Distribution, p = (0,1,0.1),n=(10,100,10))
interactive(children=(FloatSlider(value=0.0, description='p', max=1.0), IntSlider(value=10, description='n', m…
Out[8]:
<function __main__.Binomial_Distribution(p=0, n=10)>
In [ ]: