sidx = np.where(sids == s + 1)[0] y[sidx] += b0[s] + np.random.normal(0, np.sqrt(s2h[s]), len(sidx)) # add the site specific intercepts to the design matrix for s in range(n_site): site = np.zeros((N, 1)) site[idx[s]] = 1 Phi = np.concatenate((Phi, site), axis=1) site_te = np.zeros((Xs.shape[0], 1)) site_te[idx_te[s]] = 1 Phis = np.concatenate((Phis, site_te), axis=1) hyp0 = np.zeros(4) Bh = BLR(hyp0, Phi, y, var_groups=sids) Bh.loglik(hyp0, Phi, y) Bh.dloglik(hyp0, Phi, y) hyp = Bh.estimate(hyp0, Phi, y) yhat, s2 = Bh.predict(hyp, Phi, y, Phis, var_groups_test=sids_te) for s in range(n_site): plt.scatter(X[idx[s]], y[idx[s]]) plt.plot(Xs[idx_te[s]], yhat[idx_te[s]], color=cols[s]) plt.fill_between(Xs[idx_te[s]], yhat[idx_te[s]] - 1.96 * np.sqrt(s2[idx_te[s]]), yhat[idx_te[s]] + 1.96 * np.sqrt(s2[idx_te[s]]), alpha=0.2, color=cols[s]) plt.show()
Omega = np.zeros((D, Nf)) for f in range(Nf): Omega[:, f] = np.sqrt(ell2_est) * np.random.randn(Omega.shape[0]) XO = X.dot(Omega) Phi = np.sqrt(sf2_est / Nf) * np.c_[np.cos(XO), np.sin(XO)] XsO = Xs.dot(Omega) Phis = np.sqrt(sf2_est / Nf) * np.c_[np.cos(XsO), np.sin(XsO)] # add linear component Phi = np.c_[Phi, X] Phis = np.c_[Phis, Xs] hyp_blr = np.asarray([np.log(1 / sn2_est), np.log(1)]) B = BLR(hyp_blr, Phi, y) B.loglik(hyp_blr, Phi, y) yhat_blr, s2_blr = B.predict(hyp_blr, Phi, y, Phis) #plt.plot(Xs[:,0],yhat_blr,'y') #plot_dist(Xs[:,0].ravel(), yhat_blr.ravel(), # yhat_blr.ravel() - 2*np.sqrt(s2_blr).ravel(), # yhat_blr.ravel() + 2*np.sqrt(s2_blr).ravel(),'y','y') print('running RFA ...') R = GPRRFA(hyp, X, y, n_feat=Nf) # find good starting hyperparameters lm = LinearRegression() lm.fit(create_poly_basis(X, 3), y) yhat = lm.predict(create_poly_basis(X, 3)) hyp0 = np.zeros(D + 2) hyp0[0] = np.log(np.sqrt(np.var(y - yhat)))
Phi[:, colid] = np.vstack(X**d) colid += 1 b = [0.5, -0.1, 0.005] # true regression coefficients s2 = 0.05 # noise variance y = Phi.dot(b) + np.random.normal(0, s2, N) yid = range(0, N, 1) hyp0 = np.zeros(2) hyp0 = np.zeros(4) B = BLR(hyp0, Phi[yid, :], y[yid]) #B = BLR() #B.post(hyp0,Phi[yid,:],y[yid]) B.loglik(hyp0, Phi[yid, :], y[yid]) B.dloglik(hyp0, Phi[yid, :], y[yid]) hyp = B.estimate(hyp0, Phi[yid, :], y[yid]) yhat, s2 = B.predict(hyp, Phi[yid, :], y[yid], Phi) plt.plot(X, y) plt.plot(X, yhat) plt.show() ## test GPR #y = y-y.mean() #hyp0 = np.zeros(3) #G = GPR(hyp0, covSqExp, Phi[yid,:], y[yid]) #G.loglik(hyp0, covSqExp, Phi[yid,:], y[yid]) #G.dloglik(hyp0, covSqExp, Phi[yid,:], y[yid])