4
$\begingroup$

i try to implement a simple gaussian process regression in java. I almost got every step from the book http://www.GaussianProcess.org/gpml .

With my implementation of algorithm 2.1 on page 19 i'm able to produce results but:

I have the strange behavior that all predicted datapoints where i do not have any target tend to be of zero value. Does anyone know this behavior or can explain where i could have taken a mistake?

In my plot the red line marks the predicted values, black are the targets.

Here is the prediction and covariance java code. The parameter array includes length scale and noise, I work with the idexes of the arrays as x inputs for the covaiance function:

private double generateCOV_RADIAL(int i, int j) {

    double covar = Math.pow(parameter[1], 2.0) * Math.exp(-1.0 * (Math.pow(i - j, 2.0)/(2 * Math.pow(parameter[0], 2.0))));

    if(i==j)
            covar += Math.pow(parameter[2], 2); 

    return covar;
}
public void predict(int predictFuture) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{

    RealMatrix y = MatrixUtils.createColumnRealMatrix(this.targets);

    double mean = StatUtils.mean(this.targets);

    for(int i = 0; i < this.targets.length;i++)
    {
        targets[i] -= mean;
    }


    RealMatrix K =  MatrixUtils.createRealMatrix(cov);


    //identity matrix for I
    RealMatrix k_eye = MatrixUtils.createRealIdentityMatrix(cov.length);


    //choleski(K + sigman^2*I)
    CholeskyDecomposition L = null;
    try {
        L = new CholeskyDecompositionImpl(
                K.add(
                        k_eye.scalarMultiply(Math.pow(parameter[2], 2))
                        )
                );
    } catch (NonSquareMatrixException e) {
        e.printStackTrace();
    } catch (NotSymmetricMatrixException e) {
        e.printStackTrace();
    } catch (NotPositiveDefiniteMatrixException e) {
        e.printStackTrace();
    }

    //inverse of Ltranspose for left devision
    RealMatrix L_transpose_1 = new LUDecompositionImpl(L.getLT()).getSolver().getInverse();
    //inverse of Ltranspose for left devision
    RealMatrix L_1 = new LUDecompositionImpl(L.getL()).getSolver().getInverse();



    //alpha = L'\(L\y)
    RealMatrix alpha = L_transpose_1.multiply(L_1).multiply(y);


    double L_diag = 0.0;

    for(int i = 0; i < L.getL().getColumnDimension();i++)
    {
        L_diag += Math.log(L.getL().getEntry(i, i)); 
    }

    double logpyX = - y.transpose().multiply(alpha).scalarMultiply(0.5).getData()[0][0]
                    - L_diag
                    - predictFuture * Math.log(2 * Math.PI) * 0.5;


    double[] fstar = new double[targets.length + predictFuture];
    double[] V = new double[targets.length + predictFuture];

    for(int i = 0;i < targets.length + predictFuture;i++)
    {

        double[] kstar = new double[targets.length];

        for(int j = 0; j < targets.length;j++)
        {
            double covar = (Double)covMethod.invoke(this,j,i);
            kstar[j] = covar;
        }

        //f*=k_*^T * alpha
        fstar[i] = MatrixUtils.createColumnRealMatrix(kstar).transpose().multiply(alpha).getData()[0][0];
        fstar[i] += mean;
        //v = L\k_*
        RealMatrix v = L_1.multiply(MatrixUtils.createColumnRealMatrix(kstar));

        //V[fstar] = k(x_*,x_*) - v^T*v
        double covar = (Double)covMethod.invoke(this,i,i);

        V[i] = covar - v.transpose().multiply(v).getData()[0][0] + Math.pow(parameter[2],2);
    }

    this.predicted_mean = fstar;
    this.predicted_variance = V;
}

Thank you

GP Example

$\endgroup$

1 Answer 1

4
$\begingroup$

I suspect this is because you are using a Gaussian process with a zero mean function, so that unless the covariance function is non-local, the output will go to zero away from the datapoints. If you are using a local covariance function, such as the squared exponential (RBF), it is a prior over functions that says that the function should be smooth, i.e. the value of the function should be similar to is value in nearby locations. If there are no nearby samples, then th prior says very little about the value of the function as there is no reference. Thus you get a smooth function (you can get no smoother than a straight line) and you just get the mean.

If you want to extrapolate from your model, you need to have a covariance function that tells you what to expect at least over the distance you are extrapolating. A polynomial kernel, or and RBF kernel with a broader length scale may help.

$\endgroup$
2
  • $\begingroup$ I just experimented with differnent length scales. The result is that i can influence how fast i decline to the zero values. What i expect though is a behavior like the java applet by Andreas Geiger rainsoft.de/projects/gausspro.html. When there is a target which is in the near of a local maximum the maximum is drawn by the process before its declining. I'm using the RBF function as well but in my implementation the prediction is always declining. Mybe my implementation of the left matrix devision is faulty. But i just invert and multiply which should give the right result. $\endgroup$
    – Andreas
    Commented Jan 4, 2012 at 16:58
  • $\begingroup$ Additionally my predicted curve isn't getting any smoother as i adjust lenght scale. The prediction of the target points always stays the same whereas the prediction of the unknown points gets smoother. But i want the "interpolation" of the given points to be smooth as well when i set a large length scale $\endgroup$
    – Andreas
    Commented Jan 4, 2012 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.