#%% Import of packages import numpy as np #%% Data t_array = np.arange(2000, 2009+1) kpi_obs_array = np.array([75.5, 77.7, 78.7, 80.7, 81.0, 82.3, 84.2, 84.8, 88.0, 89.9]) #%% Regression model phi_0 = np.array(t_array) phi_1 = np.array(np.ones(len(kpi_obs_array))) Phi = np.array([phi_0, phi_1]).T Y = np.array([kpi_obs_array]).T #%% Calculating LS estimate theta = np.linalg.inv(Phi.T @ Phi) @ Phi.T @ Y a_opt = theta[0][0] b_opt = theta[1][0] #%% Calculating value of objective function kpi_pred_array = a_opt*t_array + b_opt pe = kpi_obs_array - kpi_pred_array # prediction error sspe_min = sum(pe*pe) # sum of elementwise products of pe #%% Presentation of result print(f'a_opt = {a_opt:.3e}') print(f'b_opt = {b_opt:.3e}') print(f'sspe_min = {sspe_min:.3e}')