def check_affine_fit(x, y, x_name="", y_name="", force_reference=-1): alpha, beta, x_fit, y_fit, ref, mse = ccr.affine_fit(x, y, force_reference) x_fit = np.array(x_fit) y_fit = np.array(y_fit) # verify optimality main_title = "" if ref == 0: # fit y as a function of x main_title = y_name + " as a function of " + x_name assert_almost_equal(x.dot(x) * alpha + x.sum() * beta - x.dot(y), 0) assert_almost_equal(x.sum() * alpha + len(y) * beta - y.sum(), 0) figure(facecolor="white") scatter(x, y) plot(x_fit, y_fit) title(main_title) xlabel(x_name) ylabel(y_name) else: # Fit x as a function of y main_title = x_name + " as a function of " + y_name assert_almost_equal(y.dot(y) * alpha + y.sum() * beta - y.dot(x), 0) assert_almost_equal(y.sum() * alpha + len(x) * beta - x.sum(), 0) figure(facecolor="white") scatter(y, x) plot(y_fit, x_fit) title(main_title) xlabel(y_name) ylabel(x_name) print(main_title + ". MSE: " + str(mse) + ". alpha: " + str(alpha) + ". beta: " + str(beta))
def draw_affine_fit(ax, x, x_name, y, y_name): # Affine fit alpha, beta, x_fit, y_fit, ref, mse = ccr.affine_fit(x, y, 0) x_fit, y_fit = np.array(x_fit), np.array(y_fit) font = {"family": "serif", "color": "black", "weight": "normal", "size": 48} # main_title = y_name+" as a function of "+x_name main_title = "" ax.cla() ax.scatter(x, y) ax.plot(x_fit, y_fit) for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(32) tick.label.set_rotation("vertical") for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(32) ax.grid(True) ax.set_title(main_title, fontdict=font) ax.set_xlabel(x_name, fontdict=font) ax.set_ylabel(y_name, fontdict=font)