source: src/main/java/agents/anac/y2015/Phoenix/GP/CovLINard.java@ 201

Last change on this file since 201 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 6.7 KB
Line 
1package 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
28import static agents.anac.y2015.Phoenix.GP.MatrixOperations.*;
29
30import agents.Jama.Matrix;
31
32/** Linear covariance function with Automatic Relevance Determination (ARD). The
33 * covariance function is parameterized as:
34 * <p>
35 * k(x^p,x^q) = x^p'*inv(P)*x^q
36 * <p>
37 * where the P matrix is diagonal with ARD parameters ell_1^2,...,ell_D^2, where
38 * D is the dimension of the input space. The hyperparameters are:
39 * <p>
40 * [ log(ell_1) <br>
41 * log(ell_2) <br>
42 * . <br>
43 * log(ell_D) ] <br>
44 * <p>
45 * Note that there is no bias term; use covConst to add a bias.
46 *
47 */
48
49public class CovLINard implements CovarianceFunction{
50
51 private int D;
52
53 /**
54 * Creates a new <code>PhoenixAlpha.CovSEard PhoenixAlpha.CovarianceFunction<code>
55 * @param inputDimension muber of dimension of the input
56 */
57 public CovLINard(int inputDimension){
58 this.D = inputDimension;
59 }
60
61 /**
62 * Returns the number of hyperparameters of this<code>PhoenixAlpha.CovarianceFunction</code>
63 *
64 * @return number of hyperparameters
65 */
66 public int numParameters() {
67 return D;
68 }
69
70 /**
71 * Compute covariance matrix of a dataset X
72 *
73 * @param loghyper column <code>Matrix</code> of hyperparameters
74 * @param X input dataset
75 * @return K covariance <code>Matrix</code>
76 */
77 public Matrix compute(Matrix loghyper, Matrix X) {
78
79 if(X.getColumnDimension()!=D)
80 throw new IllegalArgumentException("The number of dimensions specified on the covariance function "+D+" must agree with the size of the input vector"+X.getColumnDimension());
81 if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters())
82 throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters());
83
84 final Matrix ell = exp(loghyper.getMatrix(0,D-1,0,0)); // characteristic length scales
85 Matrix diag = new Matrix(D,D);
86 for(int i=0; i<D; i++)
87 diag.set(i,i,1/ell.get(i,0));
88
89 X = X.times(diag);
90
91 return X.times(X.transpose());
92 }
93
94 /**
95 * Compute compute test set covariances
96 *
97 * @param loghyper column <code>Matrix</code> of hyperparameters
98 * @param X input dataset
99 * @param Xstar test set
100 * @return [K(Xstar,Xstar) K(X,Xstar)]
101 */
102 public Matrix[] compute(Matrix loghyper, Matrix X, Matrix Xstar) {
103
104 if(X.getColumnDimension()!=D)
105 throw new IllegalArgumentException("The number of dimensions specified on the covariance function "+D+" must agree with the size of the input vector"+X.getColumnDimension());
106 if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters())
107 throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters());
108
109 final Matrix ell = exp(loghyper.getMatrix(0,D-1,0,0)); // characteristic length scales
110 Matrix diag = new Matrix(D,D);
111 for(int i=0; i<D; i++)
112 diag.set(i,i,1/ell.get(i,0));
113
114 X = X.times(diag);
115
116 Xstar = Xstar.times(diag);
117 Matrix A = sumRows(Xstar.arrayTimes(Xstar));
118
119 Matrix B = X.times(Xstar.transpose());
120 return new Matrix[]{A,B};
121 }
122
123 /**
124 * Coompute the derivatives of this <code>PhoenixAlpha.CovarianceFunction</code> with respect
125 * to the hyperparameter with index <code>idx</code>
126 *
127 * @param loghyper hyperparameters
128 * @param X input dataset
129 * @param index hyperparameter index
130 * @return <code>Matrix</code> of derivatives
131 */
132 public Matrix computeDerivatives(Matrix loghyper, Matrix X, int index) {
133 if(X.getColumnDimension()!=D)
134 throw new IllegalArgumentException("The number of dimensions specified on the covariance function "+D+" must agree with the size of the input vector"+X.getColumnDimension());
135 if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters())
136 throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters());
137 if(index>numParameters()-1)
138 throw new IllegalArgumentException("Wrong hyperparameters index "+index+" it should be smaller or equal to "+(numParameters()-1));
139
140 final Matrix ell = exp(loghyper.getMatrix(0,D-1,0,0)); // characteristic length scales
141 Matrix diag = new Matrix(D,D);
142 for(int i=0; i<D; i++)
143 diag.set(i,i,1/ell.get(i,0));
144
145 X = X.times(diag);
146
147 Matrix tmp = X.getMatrix(0,X.getRowDimension()-1,index,index);
148 return tmp.times(tmp.transpose()).times(-2);
149 }
150
151
152
153 public static void main(String[] args) {
154
155 CovLINard cf = new CovLINard(6);
156
157 Matrix X = Matrix.identity(6,6);
158 Matrix logtheta = new Matrix(new double[][]{{0.1},{0.2},{0.3},{0.4},{0.5},{0.6}});
159
160 Matrix z = new Matrix(new double[][]{{1,2,3,4,5,6},{1,2,3,4,5,6}});
161
162 System.out.println("");
163 //Matrix K = cf.compute(logtheta,X);
164 //K.print(K.getColumnDimension(), 8);
165
166 //Matrix[] res = cf.compute(logtheta,X,z);
167
168 //res[0].print(res[0].getColumnDimension(), 8);
169 //res[1].print(res[1].getColumnDimension(), 8);
170
171 Matrix d = cf.computeDerivatives(logtheta,X,5);
172
173 d.print(d.getColumnDimension(), 8);
174
175 }
176}
Note: See TracBrowser for help on using the repository browser.