-
Notifications
You must be signed in to change notification settings - Fork 1
/
getLognMeanVar.m
97 lines (88 loc) · 2.78 KB
/
getLognMeanVar.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
function [Zout,varargout] = getLognMeanVar(Z,n_dim,varargin)
% This function calculations the mean and variances, and the corresponding derivatives
% of a multivariate log-normal distribution given the parameters
% \f$\boldsymbol{\mu}\f$ and \f$\boldsymbol{\Sigma}\f$.
%
% USAGE:
% [Zout] = getLognMeanVar(Z,n_dim) \n
% [Zout,dZdthetaout] = getLognMeanVar(Z,n_dim,dZdtheta)
%
% Parameters:
% Z: (n_dim + n_dim(n_dim+1)/2) x n_t vector with \f$\boldsymbol{\mu}\f$
% and \f$\boldsymbol{\Sigma}\f$
% n_dim: dimension of the multivariate log-normal distribution
% varargin:
% dZdtheta: ((n_dim + n_dim(n_dim+1)/2) x n_theta x n_t derivative)
%
% Return values:
% Zout: (n_dim + n_dim(n_dim+1)/2) x n_t vector with mean and coariance
% dZdthetaout: derivative of Zout
n_t = size(Z,1);
if nargin >=3
dZdtheta = varargin{1};
n_theta = size(dZdtheta,2);
end
Zout = nan(size(Z));
mu = Z(:,1:n_dim);
Sigma = nan(n_dim,n_dim,n_t);
n = n_dim+1;
for i = 1:n_dim
for j = i:n_dim
Sigma(i,j,:) = Z(:,n);
n = n+1;
end
end
if nargout >= 2
dZdthetaout = nan(size(dZdtheta));
dmudtheta = dZdtheta(1:n_dim,:,:);
dSigmadtheta = nan(n_dim,n_dim,n_theta,n_t);
n = n_dim+1;
for i = 1:n_dim
for j = i:n_dim
dSigmadtheta(i,j,:,:) = dZdtheta(n,:,:);
n = n+1;
end
end
end
for n=1:n_dim
Zout(:,n) = exp(mu(:,n)+0.5*squeeze(Sigma(n,n,:)));
if nargout >= 2
dmutemp = nan(n_theta,n_t);
dmutemp(:,:) = dmudtheta(n,:,:);
dSigmatemp = nan(n_theta,n_t);
dSigmatemp(:,:) = dSigmadtheta(n,n,:,:);
dZdthetaout(n,:,:) = bsxfun(@times, Zout(:,n)', bsxfun(@plus,dmutemp,0.5*dSigmatemp));
end
end
n=n_dim+1;
for i = 1:n_dim
for j = i:n_dim
Sigmai = nan(n_t,1);
Sigmaj = nan(n_t,1);
Sigmaij = nan(n_t,1);
Sigmai(:,1) = Sigma(i,i,:);
Sigmaj(:,1) = Sigma(j,j,:);
Sigmaij(:,1) = Sigma(i,j,:);
Zout(:,n) = exp(mu(:,i)+mu(:,j)+0.5*(Sigmai+Sigmaj)).*(exp(Sigmaij)-1); %n_t x 1
if nargout >=2
dmui = nan(n_theta,n_t);
dmuj = nan(n_theta,n_t);
dmui(:,:) = dmudtheta(i,:,:);
dmuj(:,:) = dmudtheta(j,:,:);
dSigmai = nan(n_theta,n_t);
dSigmai(:,:) = dSigmadtheta(i,i,:,:);
dSigmaj = nan(n_theta,n_t);
dSigmaj(:,:) = dSigmadtheta(j,j,:,:);
dSigmaij = nan(n_theta,n_t);
dSigmaij(:,:) = dSigmadtheta(i,j,:,:);
dZdthetaout(n,:,:) = bsxfun(@times,Zout(:,n)',...
dmui+dmuj+0.5*(dSigmai+dSigmaj))+...
bsxfun(@times,(exp(mu(:,i)+mu(:,j)+0.5*(Sigmai+Sigmaj)).*exp(Sigmaij))',...
dSigmaij);
end
n=n+1;
end
end
if nargout>=2
varargout{1} = dZdthetaout;
end