forked from cultpenguin/mGstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
krig_covar_lik.m
100 lines (85 loc) · 2.54 KB
/
krig_covar_lik.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
% krig_covar_lik : Calculates the likelihood tha V is consistent with data observations
%
% Call :
% L=krig_covar_lik(pos_known,val_known,V,options)
%
%
% Can be used to infer covariance properties (range, sill, anisotropy,...)
%
function [L,sigma2_est,Cm,Q]=krig_covar_lik(pos_known,val_known,V,options,method)
if exist('method','var')==0
method=1;
end
options.dummy='';
if isfield(options,'covar_method');
method=options.covar_method;
end
if isfield(options,'sk_mean')==0
m0=0;
else
m0=options.sk_mean;
end
if isfield(options,'mean')==1
m0=options.mean;
end
nknown=size(pos_known,1);
pos=1:1:nknown;
if isfield(options,'Cm');
Cm=options.Cm;
else
Cm=precal_cov(pos_known,pos_known,V,options);
%Cm=precal_cov(pos_known,pos_known,V);
end
dm=val_known(:,1)-m0;
if method==1
mgstat_verbose(sprintf('%s : Pardo-Iguzquiza likelihood',mfilename),1);
% FROM Pardo-Iguzquiza, 1998, Math Geol, 30(1), EQN 8.
% Kitanidis (1983), Water Res. Res. 19(4), EQN 29.
sample_size=nknown;
sill=sum([V.par1]);
Cd=eye(size(Cm)).*0.001*sill; % FOR BETTER PERFORMANCE
Cm=Cm+Cd;
Q=Cm./sill;
d_val=val_known(:,1)-mean(val_known(:,1));
iQ=inv(Q); %
try
logdetQ=logdet(Q);
catch
logdetQ=log(det(Q));
end
FAC1=(sample_size/2)*(log(2*pi)+1-log(sample_size));
dQd=d_val'*iQ*d_val;
L_nllf=FAC1+.5*logdetQ+(sample_size/2)*log(dQd);
%L_nllf=(sample_size/2)*(log(2*pi)+1-log(sample_size))+.5*log(detQ)+(sample_size/2)*log(d_val'*iQ*d_val);
if nargout > 1
sigma2_est=(1/sample_size)*(dQd);
end
L=-1.*L_nllf;
%HOO=FAC1+0.5*DET+0.5*NSUB*FAC2
elseif method==2
%%% NOT YET TESTED !!!!!!
mgstat_verbose(sprintf('%s : Pardo-Iguzquiza likelihood (known variance)',mfilename),1);
% FROM Pardo-Iguzquiza, 1998, Math Geol, 30(1), EQN 5.
sample_size=nknown;
sill=sum([V.par1]);
Q=Cm./sill;
d_val=val_known(:,1);
%iQ=inv(Q);
iQ=inv(Q+0.00000001*eye(size(Q,1)));% NEEDED FOR SOM UNSTABLE MATRIX INVERSION
L_nllf=(sample_size/2)*log(2*pi) + sample_size.*log(sqrt(sill)) + .5*log(det(Q))+(1/(2*sill))*(d_val'*iQ*d_val);
%L_nllf=log(det(Q))+sample_size*(d_val'*iQ*d_val);
L=-1.*L_nllf;
%if nargout > 1
% sigma2_est=(1/sample_size)*(d_val'*iQ*d_val);
% L=-1.*L_nllf;
%
%end
sigma2_est=0;
elseif method==3,
mgstat_verbose(sprintf('%s : gauss likelihood',mfilename),-1);
f1 = -.5*log(2*pi^nknown);
f2 = -0.5*logdet(Cm);
f3 = (-.5*dm'*inv(Cm)*dm);
L = f1 + f2 +f3;
sigma2_est=0;
end