/
DIVAnd_GCVKiiobs.jl
85 lines (66 loc) · 2.6 KB
/
DIVAnd_GCVKiiobs.jl
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
"""
Kii = DIVAnd_GCVKiiobs(s)
Computes an estimate of the mean value of the diagonal of HK using GCV and the already solved analysis and it structure s
Only using real data locations.
"""
function DIVAnd_GCVKiiobs(s, nr = 30; FIELD = (), rng = Random.GLOBAL_RNG)
#the second, optional argument is the number of random vectors nr used for the estimate
H = s.obsconstrain.H
R = s.obsconstrain.R
# if nr <0 use the data and analysis itself as random vector and KZ. Allows to use the function when s.P is not available
#
if nr < 0
nrealdata = sum(1 .- s.obsout)
ndata = size(s.obsout)[1]
#@show nrealdata,ndata,size(s.obsconstrain.yo)
#@show ndata,nrealdata
#@show size(s.yo),size(((s.H)*statevector_pack(s.sv,(FIELD,))))
Kii =
s.obsconstrain.yo' * ((s.obsconstrain.H) * statevector_pack(s.sv, (FIELD,))) /
(s.obsconstrain.yo' * s.obsconstrain.yo)
if nrealdata == 0
Kii = 0.0
else
factorc = ndata / nrealdata
# Now take average of the nr different estimates,
Kii = factorc * Kii
end
return Kii
end
#if optimisation is to be used, make sure to use the same reference random points
Random.seed!(rng,nr)
Z = randn(size(R)[1], nr)
# re-seed
Random.seed!(rng,rand(rng,UInt32))
P = s.P
WW = P * (H' * (R \ Z))
ZtHKZ = Z' * (H * WW)
ZtZ = Z' * Z
# correction for points out of the domain:
nrealdata = sum(1 .- s.obsout)
ndata = size(s.obsout)[1]
if nrealdata == 0
Kii = 0.0
else
factorc = ndata / nrealdata
# Now take average of the nr different estimates,
Kii = factorc * mean(diag(ZtHKZ) ./ diag(ZtZ))
end
return Kii
end
# Copyright (C) 2008-2019 Alexander Barth <barth.alexander@gmail.com>
# Jean-Marie Beckers <JM.Beckers@ulg.ac.be>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <http://www.gnu.org/licenses/>.
# LocalWords: fi DIVAnd pmn len diag CovarParam vel ceil moddim fracdim