[1] | 1 | package agents.anac.y2015.Phoenix.GP;/* This file is part of the jgpml Project.
|
---|
| 2 | * http://github.com/renzodenardi/jgpml
|
---|
| 3 | *
|
---|
| 4 | * Copyright (c) 2011 Renzo De Nardi and Hugo Gravato-Marques
|
---|
| 5 | *
|
---|
| 6 | * Permission is hereby granted, free of charge, to any person
|
---|
| 7 | * obtaining a copy of this software and associated documentation
|
---|
| 8 | * files (the "Software"), to deal in the Software without
|
---|
| 9 | * restriction, including without limitation the rights to use,
|
---|
| 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
| 11 | * copies of the Software, and to permit persons to whom the
|
---|
| 12 | * Software is furnished to do so, subject to the following
|
---|
| 13 | * conditions:
|
---|
| 14 | *
|
---|
| 15 | * The above copyright notice and this permission notice shall be
|
---|
| 16 | * included in all copies or substantial portions of the Software.
|
---|
| 17 | *
|
---|
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
| 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
| 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
| 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
| 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
| 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
| 25 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
| 26 | */
|
---|
| 27 |
|
---|
| 28 | import java.util.Arrays;
|
---|
| 29 |
|
---|
| 30 | import agents.Jama.Matrix;
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * Independent covariance function, ie "white noise", with specified variance.
|
---|
| 34 | * The covariance function is specified as:
|
---|
| 35 | * <p>
|
---|
| 36 | * k(x^p,x^q) = s2 * \delta(p,q)
|
---|
| 37 | * <p>
|
---|
| 38 | * where s2 is the noise variance and \delta(p,q) is a Kronecker delta function
|
---|
| 39 | * which is 1 iff p=q and zero otherwise. The hyperparameter is
|
---|
| 40 | * <p>
|
---|
| 41 | * [ log(sqrt(s2)) ]
|
---|
| 42 | */
|
---|
| 43 | public class CovNoise implements CovarianceFunction {
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
| 46 | * Creates a new <code>PhoenixAlpha.CovNoise PhoenixAlpha.CovarianceFunction<code>
|
---|
| 47 | */
|
---|
| 48 | public CovNoise(){
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * Returns the number of hyperparameters of <code>PhoenixAlpha.CovSEard</code>
|
---|
| 53 | * @return number of hyperparameters
|
---|
| 54 | */
|
---|
| 55 | public int numParameters() {
|
---|
| 56 | return 1;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * Compute covariance matrix of a dataset X
|
---|
| 61 | * @param loghyper column <code>Matrix</code> of hyperparameters
|
---|
| 62 | * @param X input dataset
|
---|
| 63 | * @return K covariance <code>Matrix</code>
|
---|
| 64 | */
|
---|
| 65 | public Matrix compute(Matrix loghyper, Matrix X) {
|
---|
| 66 |
|
---|
| 67 | if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters())
|
---|
| 68 | throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters());
|
---|
| 69 |
|
---|
| 70 | final double s2 = Math.exp(2*loghyper.get(0,0)); // noise variance
|
---|
| 71 |
|
---|
| 72 | Matrix K = Matrix.identity(X.getRowDimension(),X.getRowDimension()).times(s2);
|
---|
| 73 |
|
---|
| 74 | return K;
|
---|
| 75 |
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * Compute compute test set covariances
|
---|
| 80 | * @param loghyper column <code>Matrix</code> of hyperparameters
|
---|
| 81 | * @param X input dataset
|
---|
| 82 | * @param Xstar test set
|
---|
| 83 | * @return [K(Xstar,Xstar) K(X,Xstar)]
|
---|
| 84 | */
|
---|
| 85 | public Matrix[] compute(Matrix loghyper, Matrix X, Matrix Xstar) {
|
---|
| 86 |
|
---|
| 87 | if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters())
|
---|
| 88 | throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters());
|
---|
| 89 |
|
---|
| 90 | final double s2 = Math.exp(2*loghyper.get(0,0)); // noise variance
|
---|
| 91 |
|
---|
| 92 | double[]a = new double[Xstar.getRowDimension()];
|
---|
| 93 | Arrays.fill(a,s2);
|
---|
| 94 | Matrix A =new Matrix(a,Xstar.getRowDimension()); // adding Gaussian
|
---|
| 95 |
|
---|
| 96 | Matrix B = new Matrix(X.getRowDimension(),Xstar.getRowDimension());
|
---|
| 97 |
|
---|
| 98 | return new Matrix[]{A,B};
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * Coompute the derivatives of this <code>PhoenixAlpha.CovarianceFunction</code> with respect
|
---|
| 103 | * to the hyperparameter with index <code>idx</code>
|
---|
| 104 | *
|
---|
| 105 | * @param loghyper hyperparameters
|
---|
| 106 | * @param X input dataset
|
---|
| 107 | * @param index hyperparameter index
|
---|
| 108 | * @return <code>Matrix</code> of derivatives
|
---|
| 109 | */
|
---|
| 110 | public Matrix computeDerivatives(Matrix loghyper, Matrix X, int index){
|
---|
| 111 |
|
---|
| 112 | if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters())
|
---|
| 113 | throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters());
|
---|
| 114 | if(index>numParameters()-1)
|
---|
| 115 | throw new IllegalArgumentException("Wrong hyperparameters index "+index+" it should be smaller or equal to "+(numParameters()-1));
|
---|
| 116 |
|
---|
| 117 | //noise parameter
|
---|
| 118 | final double s2 = Math.exp(2*loghyper.get(0,0));
|
---|
| 119 | Matrix A = Matrix.identity(X.getRowDimension(),X.getRowDimension()).times(2*s2);
|
---|
| 120 |
|
---|
| 121 | return A;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|