def _fit_rvr_cost(var, K, w, H): I = w.size H_inv = np.diag(1 / H) covariance = K @ H_inv @ K + var * np.eye(I) f = fitting.gaussian_pdf(w, np.zeros(I), covariance)[0, 0] f = -np.log(f) return f
def _fit_blr_cost(var, X, w, var_prior): I = X.shape[1] covariance = var_prior * (X.transpose() @ X) + \ np.sqrt(var) ** 2 * np.eye(I) f = fitting.gaussian_pdf(w, np.zeros(I), covariance)[0, 0] f = -np.log(f) return f
def _fit_slr_cost(var, X, w, H): I = X.shape[1] H_inv = np.diag(1 / H) covariance = X.transpose() @ H_inv @ X + var * np.eye(I) f = fitting.gaussian_pdf(w, np.zeros(I), covariance)[0, 0] f = -np.log(f) return f
def _fit_rvc_cost(psi, w, Hd, K): I = K.shape[0] L = I * (-np.log( fitting.gaussian_pdf(psi.reshape( (1, psi.size)), np.zeros(I), np.diag(1 / Hd))[0, 0])) predictions = sigmoid(psi @ K) for i in range(I): y = predictions[i] if w[i, 0] == 1: L -= np.log(y) else: L -= np.log(1 - y) return L
def _fit_klogr_cost(psi, X, w, var_prior, K): I = X.shape[1] L = I * (-np.log( fitting.gaussian_pdf(psi.reshape( (1, psi.size)), np.zeros(I), var_prior * np.eye(I))[0, 0])) predictions = sigmoid(psi @ K) for i in range(I): y = predictions[i] if w[i, 0] == 1: L -= np.log(y) else: L -= np.log(1 - y) return L
def _fit_logr_cost(phi, X, w, var_prior): I = X.shape[1] D = X.shape[0] - 1 L = I * (-np.log( fitting.gaussian_pdf(phi.reshape( (1, phi.size)), np.zeros(D + 1), var_prior * np.eye(D + 1))[0, 0])) predictions = sigmoid(phi.reshape((1, D + 1)) @ X) for i in range(I): y = predictions[0, i] if w[i, 0] == 1: L -= np.log(y) else: L -= np.log(1 - y) return L
elif argv[1] == "fit_by_linear": granularity = 500 a = -5 b = 5 domain = np.linspace(a, b, granularity) X, Y = np.meshgrid(domain, domain) x = X.reshape((X.size, 1)) y = Y.reshape((Y.size, 1)) xy_matrix = np.append(x, y, axis=1) # Compute the prior 2D normal distribution over phi mu_1 = np.array([0, 0]) var_prior = 6 covariance_1 = var_prior * np.eye(2) mvnpdf_1 = fitting.gaussian_pdf(xy_matrix, mu_1, covariance_1).reshape( granularity, granularity) plt.figure("Prior and posterior over phi") plt.subplot(1, 2, 1) plt.pcolor(X, Y, mvnpdf_1) # Generate the training and test data X_train = np.array([[1, 1, 1, 1, 1, 1], [-4, -1, -1, 0, 1, 3.5]]) w = np.array([4.5, 3, 2, 2.5, 2.5, 0]).reshape((6, 1)) X_test = domain.reshape((1, granularity)) X_test = np.append(np.ones((1, granularity)), X_test, axis=0) # Fit Bayesian linear regression model mu_test, var_test, var, A_inv = regression.fit_by_linear( X_train, w, var_prior, X_test)
def _fit_dgpr_cost(var, K, w, var_prior): I = K.shape[0] covariance = var_prior * K @ K + var * np.eye(I) f = fitting.gaussian_pdf(w, np.zeros(I), covariance)[0, 0] f = -np.log(f) return f
def _fit_gpr_cost(var, K, w, var_prior): I = w.size covariance = var_prior * K + (np.sqrt(var)**2) * np.eye(I) f = fitting.gaussian_pdf(w, np.zeros(I), covariance)[0, 0] f = -np.log(f) return f
argv = sys.argv if len(argv) != 2: print("Usage: python test_fitting_pdf.py t_pdf|gamma_pdf|mul_t_pdf") sys.exit(-1) if argv[1] == "t_pdf": plt.figure("T-distribution") x = np.arange(-5, 5, 0.01) mu = 0 sig = 1 px1 = fitting.t_pdf(x, mu, sig, 1) px2 = fitting.t_pdf(x, mu, sig, 2) px3 = fitting.t_pdf(x, mu, sig, 5) norm_pdf = fitting.gaussian_pdf(x.reshape((x.size, 1)), np.array([mu]), np.array([[sig]]))[:, 0] plt.subplot(121) line1, = plt.plot(x, px1, 'b') line2, = plt.plot(x, px2, 'g') line3, = plt.plot(x, px3, 'y') norm_line, = plt.plot(x, norm_pdf, 'r') plt.axis([-5, 5, 0, 0.6]) plt.legend([line1, line2, line3, norm_line], [ 't-distribution, nu = 1', 't-distribution, nu = 2', 't-distribution, nu = 5', 'normal distribution' ]) plt.subplot(122) line1, = plt.semilogy(x, px1, 'b') line2, = plt.semilogy(x, px2, 'g')
X1 = np.random.multivariate_normal(mu1, sig1, round(I * p_lambda1)) X2 = np.random.multivariate_normal(mu2, sig2, round(I * p_lambda2)) X = np.append(X1, X2, axis=0) I = X.shape[0] p_lambda, mu, sig = fitting.em_mog(X, K, 0.01) XX, YY = np.meshgrid(np.arange(-10, 10, 0.1), np.arange(-10, 10, 0.1)) x = XX.reshape((XX.size, 1)) y = YY.reshape((XX.size, 1)) xy_matrix = np.append(x, y, axis=1) mog = np.zeros((XX.size, 1)) for k in range(K): mog[:, 0] += p_lambda[k] * \ fitting.gaussian_pdf(xy_matrix, mu[k], sig[k])[:, 0] mog = mog.reshape((XX.shape[0], XX.shape[1])) plt.figure("EM MoG") plt.subplot(211) plt.scatter(X[:, 0], X[:, 1], marker=".") plt.axis([-6, 6, -1, 9]) plt.contour(XX, YY, mog) XX, YY = np.meshgrid(np.arange(-10, 10, 0.05), np.arange(-10, 10, 0.05)) x = XX.reshape((XX.size, 1)) y = YY.reshape((XX.size, 1)) xy_matrix = np.append(x, y, axis=1) mog = p_lambda1 * fitting.gaussian_pdf(xy_matrix, mu1, sig1)[:, 0] + \ p_lambda2 * fitting.gaussian_pdf(xy_matrix, mu2, sig2)[:, 0] mog = mog.reshape((XX.shape[0], XX.shape[1]))