Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- pd.set_option('display.width', 120)
- pd.set_option('display.max_columns', 20)
- pd.set_option('display.precision', 4)
- from scipy.stats import t as t_dist, chi2 as chi2_dist
- x = np.array([0.3, 0.6, 1.1, 0.2, 0.8, 1.6, 1.5, 0.9, 0.9, 0.8], dtype=float)
- y = np.array([2.8, 4.3, 6.3, 2.9, 5.3, 8.3, 8.0, 5.7, 5.4, 5.2], dtype=float)
- x_new = 2.0
- i = np.arange(1, len(y) + 1, dtype=int)
- T1 = pd.DataFrame({
- 'i': i,
- 'x_i': x,
- 'y_i': y,
- 'x_i^2': x ** 2,
- 'x_i*y_i': x * y,
- 'y_i^2': y ** 2,
- })
- T1_sum = pd.DataFrame({
- 'i': ['Σ'],
- 'x_i': [x.sum()],
- 'y_i': [y.sum()],
- 'x_i^2': [(x ** 2).sum()],
- 'x_i*y_i': [(x * y).sum()],
- 'y_i^2': [(y ** 2).sum()],
- })
- T1_print = pd.concat([T1.round(6), T1_sum], ignore_index=True)
- print('\n=== Таблиця 1: ===')
- print(T1_print.to_string(index=False))
- x_bar = x.mean()
- y_bar = y.mean()
- Sxx = float(np.sum((x - x_bar) ** 2))
- Sxy = float(np.sum((x - x_bar) * (y - y_bar)))
- a1 = Sxy / Sxx
- a0 = y_bar - a1 * x_bar
- y_hat = a0 + a1 * x
- u = y - y_hat
- u2 = u ** 2
- T2 = pd.DataFrame({
- 'i': i,
- 'y_i': y,
- 'ŷ_i': y_hat,
- 'u_i = y_i - ŷ_i': u,
- 'u_i^2': u2,
- })
- T2_sum = pd.DataFrame({
- 'i': ['Σ'],
- 'y_i': [''],
- 'ŷ_i': [''],
- 'u_i = y_i - ŷ_i': [u.sum()],
- 'u_i^2': [u2.sum()],
- })
- T2_print = pd.concat([T2.round(6), T2_sum], ignore_index=True)
- print('\n=== Таблиця 2: ===')
- print(T2_print.to_string(index=False))
- RSS = float(u2.sum())
- n = len(x)
- k = n - 2
- s2 = RSS / k
- s = math.sqrt(s2)
- alpha_90 = 0.10
- chi2_low = float(chi2_dist.ppf(1 - alpha_90/2, k))
- chi2_high = float(chi2_dist.ppf(alpha_90/2, k))
- sigma2_CI_low = (k * s2) / chi2_low
- sigma2_CI_high = (k * s2) / chi2_high
- sigma2_df = pd.DataFrame({
- 's2 (оцінка σ²)': [s2],
- 's = sqrt(s2)': [s],
- 'df (k)': [k],
- 'CI90_low': [sigma2_CI_low],
- 'CI90_high': [sigma2_CI_high],
- }).round(6)
- print('\n=== Дисперсія збурень σ²: точкова та 90% ДІ ===')
- print(sigma2_df.to_string(index=False))
- SE_a1 = s / math.sqrt(Sxx)
- SE_a0 = s * math.sqrt(1/n + (x_bar**2)/Sxx)
- alpha_95 = 0.05
- t_crit = float(t_dist.ppf(1 - alpha_95/2, k))
- t_a0 = a0 / SE_a0
- t_a1 = a1 / SE_a1
- p_a0 = 2 * (1 - t_dist.cdf(abs(t_a0), df=k))
- p_a1 = 2 * (1 - t_dist.cdf(abs(t_a1), df=k))
- a0_CI_low = a0 - t_crit * SE_a0
- a0_CI_high = a0 + t_crit * SE_a0
- a1_CI_low = a1 - t_crit * SE_a1
- a1_CI_high = a1 + t_crit * SE_a1
- coef_df = pd.DataFrame({
- 'coef': ['a0', 'a1'],
- 'estimate': [a0, a1],
- 'SE': [SE_a0, SE_a1],
- 't': [t_a0, t_a1],
- 'p_value(two‑tailed)': [p_a0, p_a1],
- 'CI95_low': [a0_CI_low, a1_CI_low],
- 'CI95_high': [a0_CI_high, a1_CI_high],
- }).round(4)
- print('\n=== Коефіцієнти моделі: оцінки, SE, t, p, 95% ДІ ===')
- print(coef_df.to_string(index=False))
- TSS = float(np.sum((y - y_bar) ** 2))
- R2 = 1 - RSS / TSS
- r = math.copysign(math.sqrt(R2), a1)
- MAPE = float(np.mean(np.abs((y_hat - y) / y) * 100))
- MPE = float(np.mean(((y_hat - y) / y) * 100))
- metrics_df = pd.DataFrame({
- 'R2': [R2],
- 'r = sign(a1)*sqrt(R2)': [r],
- 'MAPE (%)': [MAPE],
- 'MPE (%)': [MPE],
- }).round(4)
- print('\n=== Метрики якості моделі (R², r, MAPE, MPE) ===')
- print(metrics_df.to_string(index=False))
- S_yhat_i = s * np.sqrt(1/n + (x - x_bar) ** 2 / (Sxx))
- upper_i = y_hat + t_crit * S_yhat_i
- lower_i = y_hat - t_crit * S_yhat_i
- order = np.argsort(x)
- plt.figure(figsize=(7, 5))
- plt.scatter(x, y, label='Дані', zorder=3)
- plt.plot(x[order], y_hat[order], label='OLS лінія', linewidth=2, zorder=4)
- plt.plot(x[order], upper_i[order], linestyle='--', label='Верхня межа 95%', zorder=2)
- plt.plot(x[order], lower_i[order], linestyle='--', label='Нижня межа 95%', zorder=2)
- plt.xlabel('x')
- plt.ylabel('y')
- plt.title('Дані, OLS та 95% інтервал довіри')
- plt.legend()
- plt.grid(alpha=0.25)
- plt.tight_layout()
- plt.show()
- SE_pred = s * math.sqrt(1 + 1/n + (x_new - x_bar) ** 2 / Sxx)
- y_new_hat = a0 + a1 * x_new
- PI_low = y_new_hat - t_crit * SE_pred
- PI_high = y_new_hat + t_crit * SE_pred
- pred_df = pd.DataFrame({
- 'x_new': [x_new],
- 'ŷ(x_new)': [y_new_hat],
- 'SE_pred': [SE_pred],
- 'PI95_low': [PI_low],
- 'PI95_high': [PI_high],
- }).round(4)
- print('\n=== Прогноз у x_new та 95% ДІ прогнозу (PI) ===')
- print(pred_df.to_string(index=False))
Advertisement
Add Comment
Please, Sign In to add comment