-
Notifications
You must be signed in to change notification settings - Fork 0
/
exact.m
134 lines (113 loc) · 4.63 KB
/
exact.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
function Kst = exact(hyp, x)
% EXACT Computes the Covariance Approximation Function Handle
%
% Syntax:
% Kst = exact(hyp, x)
%
% Inputs:
% hyp - Hyperparameters vector
% A numeric vector containing all hyperparameters required for covariance computation.
% x - Input data matrix (T x N)
% T: Number of time steps or observations
% N: Number of features or dimensions in the data
%
% Outputs:
% Kst - Function handle that computes ldB2_exact given a noise variance vector W
% This handle takes W as input and returns several outputs related to the
% log determinant of the covariance matrix and its derivatives.
%
% Description:
% The `exact` function computes the covariance matrix `K` using the provided
% hyperparameters and input data by calling the `covScale` function. It adds a
% small diagonal term (1e-4) to `K` to ensure numerical stability during matrix
% operations. The function then returns a handle `Kst`, which is a function that
% takes a noise variance vector `W` as input and computes various quantities
% related to the covariance matrix and its derivatives. These quantities include:
% This modular approach allows for efficient computation of covariance-related
% quantities necessary for probabilistic modeling and inference tasks.
% Compute covariance matrix and its derivative
[K, dK] = covScale(hyp, x);
% Add small diagonal for numerical stability
K = K + 1e-4 * eye(size(x,1));
% Return function handle for ldB2_exact
Kst = @(W) ldB2_exact(W, K, dK);
end
%% Sub-function: ldB2_exact
function [ldB2, solveKiW, dW, dldB2, L, triB] = ldB2_exact(W, K, dK)
% LDB2_EXACT Computes log determinant and related quantities
%
% Syntax:
% [ldB2, solveKiW, dW, dldB2, L, triB] = ldB2_exact(W, K, dK)
%
% Inputs:
% W - Noise variance vector (n x 1)
% K - Covariance matrix (n x n)
% dK - Function handle to compute derivative of K with respect to hyperparameters
%
% Outputs:
% ldB2 - Log determinant of B divided by 2
% solveKiW - Function handle to solve B^{-1} * r
% dW - Derivative of ldB2 with respect to W
% dldB2 - Function handle for derivative of ldB2
% L - Cholesky factor of B
% triB - Trace of B^{-1}
%
% Description:
% This function computes the Cholesky factor of B = I + S * K * S,
% where S is a diagonal matrix with sqrt(W) on the diagonal. It also
% computes the log determinant of B, a function handle to solve systems
% with B, the derivative of ldB2 with respect to W, and the trace of inv(B).
% Validate W
if ~isnumeric(W) || ~isvector(W) || any(W <= 0)
error('Input "W" must be a positive numeric vector.');
end
% Compute number of elements
n = numel(W);
% Compute S = diag(sqrt(W))
sW = sqrt(W);
% Perform Cholesky decomposition: B = L * L'
L = chol(eye(n) + sW *sW'.* K); % Lower triangular matrix
% Compute log determinant of B divided by 2
ldB2 = sum(log(diag(L)));
% Function handle to solve B^{-1} * r
% Equivalent to inv(B) * r, computed efficiently using Cholesky factors
solveKiW = @(r) bsxfun(@times,solve_chol(L,bsxfun(@times,r,sW)),sW);
% Compute inv(B) efficiently
invB = bsxfun(@times,1./sW,solve_chol(L,diag(sW)));
% Compute derivative dW = 0.5 * diag(inv(B) * K)
dW = sum(invB .* K, 2) / 2; % (n x 1)
% Compute trace of inv(B)
triB = trace(invB); % trace(inv(B))
% Function handle for derivative of ldB2
dldB2 = @(alpha) ldB2_deriv_exact(W, dK, invB, alpha);
end
%% Sub-function: ldB2_deriv_exact
function dhyp = ldB2_deriv_exact(W, dK, invB, alpha)
% LDB2_DERIV_EXACT Computes the derivative of ldB2 with respect to hyperparameters
%
% Syntax:
% dhyp = ldB2_deriv_exact(W, dK, invB, alpha)
%
% Inputs:
% W - Noise variance vector (n x 1)
% dK - Function handle to compute derivative of K with respect to hyperparameters
% invB - Inverse of matrix B (n x n)
% alpha - Vector involved in derivative computation (n x 1)
%
% Outputs:
% dhyp - Derivative of ldB2 with respect to hyperparameters (vector)
%
% Description:
% This function computes the derivative of ldB2 with respect to the hyperparameters.
% It uses the relationship between inv(B) and the derivative of the covariance matrix dK.
%
% Example:
% dhyp = ldB2_deriv_exact(W, dK, invB, alpha);
% Compute R = alpha * alpha'
R = alpha * alpha';
% Compute the term inside dK: Q .* W - R
% Q = invB
term = (invB .* W) - R; % Element-wise multiplication
% Compute derivative with respect to hyperparameters
dhyp = dK(term) / 2;
end