[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 static agents.anac.y2015.Phoenix.GP.MatrixOperations.*;
|
---|
| 29 |
|
---|
| 30 | import java.util.Arrays;
|
---|
| 31 |
|
---|
| 32 | import agents.Jama.Matrix;
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Squared Exponential covariance function with isotropic distance measure. The
|
---|
| 36 | * covariance function is parameterized as:
|
---|
| 37 | * <P><DD>
|
---|
| 38 | * k(x^p,x^q) = sf2 * exp(-(x^p - x^q)'*inv(P)*(x^p - x^q)/2)
|
---|
| 39 | * </DD>
|
---|
| 40 | * where the P matrix is ell^2 times the unit matrix and sf2 is the signal
|
---|
| 41 | * variance. The hyperparameters are:
|
---|
| 42 | * <P>
|
---|
| 43 | * [ log(ell)
|
---|
| 44 | * log(sqrt(sf2)) ]
|
---|
| 45 | */
|
---|
| 46 |
|
---|
| 47 | public class CovSEiso implements CovarianceFunction{
|
---|
| 48 |
|
---|
| 49 | public CovSEiso(){}
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * Returns the number of hyperparameters of this<code>PhoenixAlpha.CovarianceFunction</code>
|
---|
| 54 | *
|
---|
| 55 | * @return number of hyperparameters
|
---|
| 56 | */
|
---|
| 57 | public int numParameters() {
|
---|
| 58 | return 2;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * Compute covariance matrix of a dataset X
|
---|
| 63 | *
|
---|
| 64 | * @param loghyper column <code>Matrix</code> of hyperparameters
|
---|
| 65 | * @param X input dataset
|
---|
| 66 | * @return K covariance <code>Matrix</code>
|
---|
| 67 | */
|
---|
| 68 | public Matrix compute(Matrix loghyper, Matrix X) {
|
---|
| 69 |
|
---|
| 70 | if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters())
|
---|
| 71 | throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters());
|
---|
| 72 |
|
---|
| 73 | double ell = Math.exp(loghyper.get(0,0));
|
---|
| 74 | double sf2 = Math.exp(2*loghyper.get(1,0));
|
---|
| 75 |
|
---|
| 76 | Matrix K = exp(squareDist(X.transpose().times(1/ell)).times(-0.5)).times(sf2);
|
---|
| 77 |
|
---|
| 78 | return K;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * Compute compute test set covariances
|
---|
| 83 | *
|
---|
| 84 | * @param loghyper column <code>Matrix</code> of hyperparameters
|
---|
| 85 | * @param X input dataset
|
---|
| 86 | * @param Xstar test set
|
---|
| 87 | * @return [K(Xstar,Xstar) K(X,Xstar)]
|
---|
| 88 | */
|
---|
| 89 | public Matrix[] compute(Matrix loghyper, Matrix X, Matrix Xstar) {
|
---|
| 90 |
|
---|
| 91 | if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters())
|
---|
| 92 | throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters());
|
---|
| 93 |
|
---|
| 94 |
|
---|
| 95 | double ell = Math.exp(loghyper.get(0,0));
|
---|
| 96 | double sf2 = Math.exp(2*loghyper.get(1,0));
|
---|
| 97 | double[] a = new double[Xstar.getRowDimension()];
|
---|
| 98 | Arrays.fill(a,sf2);
|
---|
| 99 | Matrix A = new Matrix(a,a.length);
|
---|
| 100 |
|
---|
| 101 | Matrix B = exp(squareDist(X.transpose().times(1/ell),Xstar.transpose().times(1/ell)).times(-0.5)).times(sf2);
|
---|
| 102 |
|
---|
| 103 | return new Matrix[]{A,B};
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /**
|
---|
| 107 | * Coompute the derivatives of this <code>PhoenixAlpha.CovarianceFunction</code> with respect
|
---|
| 108 | * to the hyperparameter with index <code>idx</code>
|
---|
| 109 | *
|
---|
| 110 | * @param loghyper hyperparameters
|
---|
| 111 | * @param X input dataset
|
---|
| 112 | * @param index hyperparameter index
|
---|
| 113 | * @return <code>Matrix</code> of derivatives
|
---|
| 114 | */
|
---|
| 115 | public Matrix computeDerivatives(Matrix loghyper, Matrix X, int index) {
|
---|
| 116 |
|
---|
| 117 | if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters())
|
---|
| 118 | throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters());
|
---|
| 119 | if(index>numParameters()-1)
|
---|
| 120 | throw new IllegalArgumentException("Wrong hyperparameters index "+index+" it should be smaller or equal to "+(numParameters()-1));
|
---|
| 121 |
|
---|
| 122 | double ell = Math.exp(loghyper.get(0,0));
|
---|
| 123 | double sf2 = Math.exp(2*loghyper.get(1,0));
|
---|
| 124 |
|
---|
| 125 | Matrix tmp = squareDist(X.transpose().times(1/ell));
|
---|
| 126 | Matrix A = null;
|
---|
| 127 | if(index==0){
|
---|
| 128 | A = exp(tmp.times(-0.5)).arrayTimes(tmp).times(sf2);
|
---|
| 129 | } else {
|
---|
| 130 | A = exp(tmp.times(-0.5)).times(2*sf2);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | return A;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | private static Matrix squareDist(Matrix a){
|
---|
| 138 | return squareDist(a,a);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | private static Matrix squareDist(Matrix a, Matrix b){
|
---|
| 142 | Matrix C = new Matrix(a.getColumnDimension(),b.getColumnDimension());
|
---|
| 143 | final int m = a.getColumnDimension();
|
---|
| 144 | final int n = b.getColumnDimension();
|
---|
| 145 | final int d = a.getRowDimension();
|
---|
| 146 |
|
---|
| 147 | for (int i=0; i<m; i++){
|
---|
| 148 | for (int j=0; j<n; j++) {
|
---|
| 149 | double z = 0.0;
|
---|
| 150 | for (int k=0; k<d; k++) { double t = a.get(k,i) - b.get(k,j); z += t*t; }
|
---|
| 151 | C.set(i,j,z);
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | return C;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | public static void main(String[] args) {
|
---|
| 159 |
|
---|
| 160 | CovSEiso cf = new CovSEiso();
|
---|
| 161 |
|
---|
| 162 | Matrix X = Matrix.identity(6,6);
|
---|
| 163 | Matrix logtheta = new Matrix(new double[][]{{0.1},{0.2}});
|
---|
| 164 |
|
---|
| 165 | // Matrix z = new Matrix(new double[][]{{1,2,3,4,5,6},{1,2,3,4,5,6}});
|
---|
| 166 | //
|
---|
| 167 | // System.out.println("");
|
---|
| 168 | // Matrix K = cf.compute(logtheta,X);
|
---|
| 169 | // K.print(K.getColumnDimension(), 8);
|
---|
| 170 | //
|
---|
| 171 | // Matrix[] res = cf.compute(logtheta,X,z);
|
---|
| 172 | //
|
---|
| 173 | // res[0].print(res[0].getColumnDimension(), 20);
|
---|
| 174 | // res[1].print(res[1].getColumnDimension(), 20);
|
---|
| 175 |
|
---|
| 176 | Matrix d = cf.computeDerivatives(logtheta,X,1);
|
---|
| 177 |
|
---|
| 178 | d.print(d.getColumnDimension(), 8);
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | // private static Matrix squareDist(Matrix a, Matrix b, Matrix Q){
|
---|
| 183 | //
|
---|
| 184 | // if(a.getColumnDimension()!=Q.getRowDimension() || b.getColumnDimension()!=Q.getColumnDimension())
|
---|
| 185 | // throw new IllegalArgumentException("Wrong size of for Q "+Q.getRowDimension()+"x"+Q.getColumnDimension()+" instead of "+a.getColumnDimension()+"x"+b.getColumnDimension());
|
---|
| 186 | //
|
---|
| 187 | // Matrix C = new Matrix(D,1);
|
---|
| 188 | //
|
---|
| 189 | // for (int i=0; i<b.getColumnDimension(); i++) {
|
---|
| 190 | // for (int j=0; j<a.getColumnDimension(); j++) {
|
---|
| 191 | // double t = Q.get(i,j);
|
---|
| 192 | // for (int k=0; k<D; k++) {
|
---|
| 193 | // double z = a.get(i,k) - b.get(j,k);
|
---|
| 194 | // C.set(k,0,C.get(k,0)+ t*z*z);
|
---|
| 195 | // }
|
---|
| 196 | // }
|
---|
| 197 | // }
|
---|
| 198 | //
|
---|
| 199 | // return C;
|
---|
| 200 | // }
|
---|
| 201 |
|
---|
| 202 |
|
---|