-
Notifications
You must be signed in to change notification settings - Fork 40
/
cd_transform.m
65 lines (55 loc) · 1.62 KB
/
cd_transform.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
function [mu,S,C] = cd_transform(m,P,g,g_param,tr_param)
%CD_TRANSFORM Central Difference transform of random variables
%
% Syntax:
% [mu,S,C,SX,W] = CD_TRANSFORM(M,P,g,n,param)
%
% In:
% M - Random variable mean (Nx1 column vector)
% P - Random variable covariance (NxN pos.def. matrix)
% g - Transformation function of the form g(x,param) as
% matrix, inline function, function name or function reference
% g_param - Parameters of g (optional, default empty)
% tr_param - Parameters of the transformation in form:
% h = tr_param{1}: Step length of central difference approximation
%
% Out:
% mu - Estimated mean of y
% S - Estimated covariance of y
% C - Estimated cross-covariance of x and y
% Copyright (c), 2009 Jouni Hartikainen
if nargin < 4
g_param = [];
end
if nargin < 5
tr_param = [];
end
if isempty(tr_param)
h = sqrt(3);
else
h = tr_param{1};
end
d = size(m,1);
cholP = chol(P)';
g_mu = feval(g,m,g_param);
s = size(g_mu,1);
% Compute the first and second order terms
a = zeros(s,d);
H = zeros(s,d);
for i = 1:d
e = zeros(d,1);
e(i) = 1;
f1 = feval(g,cholP*(h*e)+m,g_param);
f2 = feval(g,cholP*(-h*e)+m,g_param);
a(:,i) = (f1 - f2) / (2*h);
H(:,i) = (f1 + f2 - 2*g_mu) / h^2;
end
% Transformed mean
mu = g_mu + 0.5*sum(H,2);
% Covariance of y
S = zeros(s,s);
for i = 1:d
S = S + a(:,i)*a(:,i)' + 0.5*H(:,i)*H(:,i)';
end
% Cross-covariance of x and y
C = cholP*a';