source: src/main/java/agents/anac/y2015/Phoenix/GP/CovLINone.java@ 346

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

Initial import : Genius 9.0.0

File size: 5.1 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 a single hyperparameter. The covariance
33 * function is parameterized as:
34 * <p>
35 * k(x^p,x^q) = x^p'*inv(P)*x^q + 1./t2;
36 * <p>
37 * where the P matrix is t2 times the unit matrix. The second term plays the
38 * role of the bias. The hyperparameter is:
39 * <p>
40 * [ log(sqrt(t2)) ]
41 */
42
43public class CovLINone implements CovarianceFunction{
44
45 public CovLINone(){}
46
47 /**
48 * Returns the number of hyperparameters of this<code>PhoenixAlpha.CovarianceFunction</code>
49 *
50 * @return number of hyperparameters
51 */
52 public int numParameters() {
53 return 1;
54 }
55
56 /**
57 * Compute covariance matrix of a dataset X
58 *
59 * @param loghyper column <code>Matrix</code> of hyperparameters
60 * @param X input dataset
61 * @return K covariance <code>Matrix</code>
62 */
63 public Matrix compute(Matrix loghyper, Matrix X) {
64 if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters())
65 throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters());
66
67 final double it2 = Math.exp(-2*loghyper.get(0,0));
68
69 Matrix A = X.times(X.transpose());
70 return addValue(A,1).times(it2);
71 }
72
73 /**
74 * Compute compute test set covariances
75 *
76 * @param loghyper column <code>Matrix</code> of hyperparameters
77 * @param X input dataset
78 * @param Xstar test set
79 * @return [K(Xstar,Xstar) K(X,Xstar)]
80 */
81 public Matrix[] compute(Matrix loghyper, Matrix X, Matrix Xstar) {
82 if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters())
83 throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters());
84
85 final double it2 = Math.exp(-2*loghyper.get(0,0));
86
87 Matrix A = sumRows(Xstar.arrayTimes(Xstar));
88
89 A= addValue(A,1).times(it2);
90
91 Matrix B = X.times(Xstar.transpose());
92 B = addValue(B,1).times(it2);
93
94 return new Matrix[]{A,B};
95 }
96
97 /**
98 * Coompute the derivatives of this <code>PhoenixAlpha.CovarianceFunction</code> with respect
99 * to the hyperparameter with index <code>idx</code>
100 *
101 * @param loghyper hyperparameters
102 * @param X input dataset
103 * @param index hyperparameter index
104 * @return <code>Matrix</code> of derivatives
105 */
106 public Matrix computeDerivatives(Matrix loghyper, Matrix X, int index) {
107
108 if(loghyper.getColumnDimension()!=1 || loghyper.getRowDimension()!=numParameters())
109 throw new IllegalArgumentException("Wrong number of hyperparameters, "+loghyper.getRowDimension()+" instead of "+numParameters());
110 if(index>numParameters()-1)
111 throw new IllegalArgumentException("Wrong hyperparameters index "+index+" it should be smaller or equal to "+(numParameters()-1));
112
113 final double it2 = Math.exp(-2*loghyper.get(0,0));
114 Matrix A = X.times(X.transpose());
115 return addValue(A,1).times(-2*it2);
116 }
117
118 public static void main(String[] args) {
119 CovLINone cf = new CovLINone();
120
121 Matrix X = Matrix.identity(6,6);
122 Matrix logtheta = new Matrix(new double[][]{{0.1}});
123
124 Matrix z = new Matrix(new double[][]{{1,2,3,4,5,6},{1,2,3,4,5,6}});
125
126 System.out.println("");
127 Matrix K = cf.compute(logtheta,X);
128 K.print(K.getColumnDimension(), 8);
129
130 Matrix[] res = cf.compute(logtheta,X,z);
131
132 res[0].print(res[0].getColumnDimension(), 8);
133 res[1].print(res[1].getColumnDimension(), 8);
134
135 Matrix d = cf.computeDerivatives(logtheta,X,0);
136
137 d.print(d.getColumnDimension(), 8);
138
139 }
140}
Note: See TracBrowser for help on using the repository browser.