Ejemplo n.º 1
0
b = [0.5, -0.1, 0.005]  # true regression coefficients
s2 = 0.05  # noise variance
y = Phip.dot(b) + np.random.normal(0, s2, N)

# cubic B-spline basis (used for regression)
p = 3  # order of spline (3 = cubic)
nknots = 5  # number of knots (endpoints only counted once)
knots = np.linspace(0, 10, nknots)
k = splinelab.augknt(knots, p)  # pad the knot vector
B = bspline.Bspline(k, p)
Phi = np.array([B(i) for i in X])
Phis = np.array([B(i) for i in Xs])

hyp0 = np.zeros(2)
#hyp0 = np.zeros(4) # use ARD
B = BLR(hyp0, Phi, y)
hyp = B.estimate(hyp0, Phi, y, optimizer='powell')

yhat, s2 = B.predict(hyp, Phi, y, Phis)
plt.fill_between(Xs,
                 yhat - 1.96 * np.sqrt(s2),
                 yhat + 1.96 * np.sqrt(s2),
                 alpha=0.2)
plt.scatter(X, y)
plt.plot(Xs, yhat)
plt.show()

print(B.nlZ)
print(1 / hyp)
print(B.m)
Ejemplo n.º 2
0
D = X.shape[1]
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)
Ejemplo n.º 3
0
def main(filename, maskfile, outdir, basis, ard=False):
    """ :outputs: * yhat - predictive mean
                  * ys2 - predictive variance
                  * trendcoeff - coefficients from the trend surface model
                  * negloglik - Negative log marginal likelihood
                  * hyp - hyperparameters
                  * explainedvar - explained variance
                  * rmse - standardised mean squared error
                  * trendcoeffvar - marginal variances """

    from bayesreg import BLR
    np.seterr(invalid='ignore')

    # load data
    print("Processing data in", filename)
    Y, X, maskIndices = load_data(filename, maskfile)
    Y = np.round(
        10000 * Y) / 10000  # truncate precision to avoid numerical probs
    if len(Y.shape) == 1:
        Y = Y[:, np.newaxis]
    N = Y.shape[1]

    # standardize responses and covariates
    mY = np.mean(Y, axis=0)
    sY = np.std(Y, axis=0)
    Yz = (Y - mY) / sY
    mX = np.mean(X, axis=0)
    sX = np.std(X, axis=0)
    Xz = (X - mX) / sX

    # create basis set and set starting hyperparamters
    Phi = create_basis(Xz, basis)
    if ard is True:
        hyp0 = np.zeros(Phi.shape[1] + 1)
    else:
        hyp0 = np.zeros(2)

    # estimate the models
    yhat = np.zeros_like(Yz)
    ys2 = np.zeros_like(Yz)
    nlZ = np.zeros(N)
    hyp = np.zeros((N, len(hyp0)))
    rmse = np.zeros(N)
    ev = np.zeros(N)
    m = np.zeros((N, Phi.shape[1]))
    bs2 = np.zeros((N, Phi.shape[1]))

    for i in range(0, N):
        # print("Estimating model ",i+1,"of",N)
        breg = BLR()
        hyp[i, :] = breg.estimate(hyp0, Phi, Yz[:, i], 'powell')
        m[i, :] = breg.m
        nlZ[i] = breg.nlZ

        # compute marginal variances
        bs2[i] = np.sqrt(np.diag(np.linalg.inv(breg.A)))

        # compute predictions and errors
        yhat[:, i], ys2[:, i] = breg.predict(hyp[i, :], Phi, Yz[:, i], Phi)
        yhat[:, i] = yhat[:, i] * sY[i] + mY[i]
        rmse[i] = np.sqrt(np.mean((Y[:, i] - yhat[:, i])**2))
        ev[i] = 100 * (1 - (np.var(Y[:, i] - yhat[:, i]) / np.var(Y[:, i])))

        # print("Variance explained =",ev[i],"% RMSE =",rmse[i])

    print("Mean (std) variance explained =", ev.mean(), "(", ev.std(), ")")
    print("Mean (std) RMSE =", rmse.mean(), "(", rmse.std(), ")")

    # Write output
    print("Writing output ...")
    out_base_name = outdir + "/" + filename.split('/')[-1].split('.nii')[0]
    np.savetxt(out_base_name + ".tsm.trendcoeff.txt",
               m,
               delimiter='\t',
               fmt='%5.8f')
    np.savetxt(out_base_name + ".tsm.negloglik.txt",
               nlZ,
               delimiter='\t',
               fmt='%5.8f')
    np.savetxt(out_base_name + ".tsm.hyp.txt",
               hyp,
               delimiter='\t',
               fmt='%5.8f')
    np.savetxt(out_base_name + ".tsm.explainedvar.txt",
               ev,
               delimiter='\t',
               fmt='%5.8f')
    np.savetxt(out_base_name + ".tsm.rmse.txt",
               rmse,
               delimiter='\t',
               fmt='%5.8f')
    np.savetxt(out_base_name + ".tsm.trendcoeffvar.txt",
               bs2,
               delimiter='\t',
               fmt='%5.8f')
    save_nifti(yhat, out_base_name + '.tsm.yhat.nii.gz', filename, maskIndices)
    save_nifti(ys2, out_base_name + '.tsm.ys2.nii.gz', filename, maskIndices)
Ejemplo n.º 4
0
def estimate(filename, maskfile, basis):
    """ Estimate a trend surface model

    This will estimate a trend surface model, independently for each subject.
    This is currently fit using a polynomial model of a specified degree.
    The models are estimated on the basis of data stored on disk in ascii or
    neuroimaging data formats (currently nifti only). Ascii data should be in
    tab or space delimited format with the number of voxels in rows and the
    number of subjects in columns. Neuroimaging data will be reshaped
    into the appropriate format

    Basic usage::

        estimate(filename, maskfile, basis)

    where the variables are defined below. Note that either the cfolds
    parameter or (testcov, testresp) should be specified, but not both.

    :param filename: 4-d nifti file containing the images to be estimated
    :param maskfile: nifti mask used to apply to the data
    :param basis: model order for the interpolating polynomial

    All outputs are written to disk in the same format as the input. These are:

    :outputs: * yhat - predictive mean
              * ys2 - predictive variance
              * trendcoeff - coefficients from the trend surface model
              * negloglik - Negative log marginal likelihood
              * hyp - hyperparameters
              * explainedvar - explained variance
              * rmse - standardised mean squared error
    """

    # load data
    print("Processing data in", filename)
    Y, X, mask = load_data(filename, maskfile)
    Y = np.round(
        10000 * Y) / 10000  # truncate precision to avoid numerical probs
    if len(Y.shape) == 1:
        Y = Y[:, np.newaxis]
    N = Y.shape[1]

    # standardize responses and covariates
    mY = np.mean(Y, axis=0)
    sY = np.std(Y, axis=0)
    Yz = (Y - mY) / sY
    mX = np.mean(X, axis=0)
    sX = np.std(X, axis=0)
    Xz = (X - mX) / sX

    # create basis set and set starting hyperparamters
    Phi = create_basis(Xz, basis)
    hyp0 = np.zeros(2)

    # estimate the models for all subjects
    yhat = np.zeros_like(Yz)
    ys2 = np.zeros_like(Yz)
    nlZ = np.zeros(N)
    hyp = np.zeros((N, len(hyp0)))
    rmse = np.zeros(N)
    ev = np.zeros(N)
    m = np.zeros((N, Phi.shape[1]))
    for i in range(0, N):
        print("Estimating model ", i + 1, "of", N)
        breg = BLR()
        hyp[i, :] = breg.estimate(hyp0, Phi, Yz[:, i])
        m[i, :] = breg.m
        nlZ[i] = breg.nlZ

        # compute predictions and errors
        yhat[:, i], ys2[:, i] = breg.predict(hyp[i, :], Phi, Yz[:, i], Phi)
        yhat[:, i] = yhat[:, i] * sY[i] + mY[i]
        rmse[i] = np.sqrt(np.mean((Y[:, i] - yhat[:, i])**2))
        ev[i] = 100 * (1 - (np.var(yhat[:, i] - Y[:, i]) / np.var(Y[:, i])))

        print("Variance explained =", ev[i], "% RMSE =", rmse[i])

    print("Mean (std) variance explained =", ev.mean(), "(", ev.std(), ")")
    print("Mean (std) RMSE =", rmse.mean(), "(", rmse.std(), ")")

    # Write output
    print("Writing output ...")
    np.savetxt("trendcoeff.txt", m, delimiter='\t', fmt='%5.8f')
    np.savetxt("negloglik.txt", nlZ, delimiter='\t', fmt='%5.8f')
    np.savetxt("hyp.txt", hyp, delimiter='\t', fmt='%5.8f')
    np.savetxt("explainedvar.txt", ev, delimiter='\t', fmt='%5.8f')
    np.savetxt("rmse.txt", rmse, delimiter='\t', fmt='%5.8f')
    fileio.save_nifti(yhat, 'yhat.nii.gz', filename, mask)
    fileio.save_nifti(ys2, 'ys2.nii.gz', filename, mask)
Ejemplo n.º 5
0
    hyp0 = np.zeros(2)

# estimate the models
yhat = np.zeros_like(Yz)
ys2 = np.zeros_like(Yz)
nlZ = np.zeros(N)
hyp = np.zeros((N, len(hyp0)))
rmse = np.zeros(N)
ev = np.zeros(N)
m = np.zeros((N, Phi.shape[1]))
bs2 = np.zeros((N, Phi.shape[1]))

#for i in range(0, N):
i = 0
print("Estimating model ", i + 1, "of", N)
breg = BLR()
hyp[i, :] = breg.estimate(hyp0, Phi, Yz[:, i], 'powell')
m[i, :] = breg.m
nlZ[i] = breg.nlZ

# compute marginal variances
bs2[i] = np.sqrt(np.diag(np.linalg.inv(breg.A)))

# compute predictions and errors
yhat[:, i], ys2[:, i] = breg.predict(hyp[i, :], Phi, Yz[:, i], Phi)
yhat[:, i] = yhat[:, i] * sY[i] + mY[i]
rmse[i] = np.sqrt(np.mean((Y[:, i] - yhat[:, i])**2))
ev[i] = 100 * (1 - (np.var(Y[:, i] - yhat[:, i]) / np.var(Y[:, i])))

print("Variance explained =", ev[i], "% RMSE =", rmse[i])
Ejemplo n.º 6
0
Phi = np.zeros((X.shape[0], dimpoly))
colid = np.arange(0, 1)
for d in range(1, dimpoly + 1):
    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)
Ejemplo n.º 7
0
Phis = np.zeros((Xs.shape[0],dimpoly))
colid = np.arange(0,1)
for d in range(1,dimpoly+1):
    Phis[:,colid] = np.vstack(Xs ** d)
    colid += 1

# generative model
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) # use ARD
B = BLR(hyp0, Phi[yid,:], y[yid])#,var_groups=np.ones(N))
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, y, Phis)
plt.fill_between(Xs,yhat-1.96*np.sqrt(s2), yhat+1.96*np.sqrt(s2), alpha = 0.2)
plt.scatter(X,y)
plt.plot(Xs,yhat)
plt.show()

print(B.nlZ)
print(1/hyp)
print(B.m)

print("Estimating a model with heteroskedastic noise ...")