Beispiel #1
0
np.set_printoptions(precision=1)

print "\nHow many locations per dimension?"
N = input("Enter:  ")

print "\nWhich dimension?"
dim = input("Enter:  ")

# Choose some hyperparameters for the Matern kernel
nu = 1.0
sigma = 1.0
rho = 1.0

# get and plot points
X = get_gppoints(N, dim)


def maternkernel(x, y, ORD=None, NU=nu, SIGMA=sigma, RHO=rho):
    r = norm_diff(x, y, ORD)
    return maternfunction(r, NU, SIGMA, RHO)


# build kernelmatrix
M = build_kernelmatrix(X, X, maternkernel)

# check condition number of matrix
print "\nCondition number of the kernel matrix:"
print "\tCond(K) =", np.linalg.cond(M)
print ""
Beispiel #2
0
def plotGP_mean(Minverse, data, kernelmatrix):

    # extract info from data
    approxpts = data[:, 0]
    approxpts = np.reshape(approxpts, (len(approxpts), 1))
    y = data[:, 1]
    y = np.reshape(y, (len(y), 1))

    # determine axis limits
    xmin = np.min(approxpts)
    xmax = np.max(approxpts)
    ymin = np.min(y)
    ymax = np.max(y)

    newpts = np.linspace(xmin, xmax, 250)
    newpts = np.reshape(newpts, (len(newpts), 1))

    # build kernel approximation matrices on new pts
    K = build_kernelmatrix(newpts, approxpts, kernelmatrix)
    K2 = build_kernelmatrix(newpts, newpts, kernelmatrix)

    # build approximation mean on newpts
    coeff = Minverse.dot(y - np.mean(y))
    approxmean = K.dot(coeff) + np.mean(y) * np.ones((len(newpts), 1))

    # build approximation covariance on newpts
    Knew = K2 - K.dot(Minv).dot(K.T)

    # construct deviations
    plusdev = approxmean[:, 0] + np.sqrt(Knew.diagonal())
    minusdev = approxmean[:, 0] - np.sqrt(Knew.diagonal())

    # plot mean
    fig = plt.figure()
    plt.plot(newpts[:, 0],
             approxmean[:, 0],
             '-',
             linewidth=3,
             color='darkblue')

    # plot data
    plt.plot(approxpts, y, 'o', markersize=10, color='darkslategray')

    # plot deviation
    #	plt.fill_between(newpts[:,0], minusdev, plusdev, color = 'burlywood', alpha = 0.5)

    plt.xlim((xmin - 0.1, xmax + 0.1))
    plt.ylim((ymin - 1, ymax + 1))

    # remove frame
    plt.gca().spines['top'].set_visible(False)
    plt.gca().spines['right'].set_visible(False)
    plt.gca().spines['bottom'].set_visible(False)
    plt.gca().spines['left'].set_visible(False)
    plt.tick_params(top='off',
                    bottom='off',
                    left='off',
                    right='off',
                    labelleft='off',
                    labelbottom='off')  #plt.title("Confidence intervals")

    # save figure
    plt.savefig("firstdeviation")

    return 0