|
1 | 1 | clear; close all;
|
2 |
| -figure; |
3 |
| -drawnow; |
| 2 | +% figure; |
| 3 | +% drawnow; |
4 | 4 |
|
5 | 5 | % time steps
|
6 | 6 | Delta_t = 0.12;
|
7 | 7 |
|
8 |
| -% Time Stepping Methods |
9 |
| -solvermodel = @(f, t, y) datools.utils.rk4(f, t, y, 50); |
10 |
| -solvernature = @(f, t, y) datools.utils.rk4(f, t, y, 50); |
| 8 | +% Time Stepping Methods (Use ode45 or write your own) |
| 9 | +% solvermodel = @(f, t, y) datools.utils.rk4(f, t, y, 50); |
| 10 | +% solvernature = @(f, t, y) datools.utils.rk4(f, t, y, 50); |
| 11 | +solvermodel = @(f, t, y) ode45(f, t, y); |
| 12 | +solvernature = @(f, t, y) ode45(f, t, y); |
11 | 13 |
|
12 |
| -% ODE |
| 14 | +% Define ODE |
13 | 15 | natureODE = otp.lorenz63.presets.Canonical;
|
14 | 16 | nature0 = randn(natureODE.NumVars, 1);
|
15 | 17 | natureODE.TimeSpan = [0, Delta_t];
|
|
21 | 23 | [tt, yy] = ode45(natureODE.Rhs.F, [0 10], nature0);
|
22 | 24 | natureODE.Y0 = yy(end, :).';
|
23 | 25 |
|
| 26 | +% initialize model |
24 | 27 | model = datools.Model('Solver', solvermodel, 'ODEModel', modelODE);
|
25 | 28 | nature = datools.Model('Solver', solvernature, 'ODEModel', natureODE);
|
26 | 29 |
|
| 30 | +% Observation Model |
27 | 31 | naturetomodel = datools.observation.Linear(numel(nature0), 'H',...
|
28 | 32 | speye(natureODE.NumVars));
|
29 | 33 |
|
30 |
| -%observeindicies = 1:natureODE.NumVars; |
| 34 | + |
| 35 | +% observe these variables |
31 | 36 | observeindicies = 1:1:natureODE.NumVars;
|
32 | 37 |
|
33 | 38 | nobsvars = numel(observeindicies);
|
34 | 39 |
|
35 | 40 | R = (1/1)*speye(nobsvars);
|
36 | 41 |
|
| 42 | +% Observaton model (Gaussian here) |
37 | 43 | obserrormodel = datools.error.Gaussian('CovarianceSqrt', sqrtm(R));
|
38 | 44 | %obserrormodel = datools.error.Tent;
|
39 | 45 | observation = datools.observation.Indexed(model.NumVars, ...
|
|
45 | 51 |
|
46 | 52 | ensembleGenerator = @(N) randn(natureODE.NumVars, N);
|
47 | 53 |
|
48 |
| -ensNs = 20:5:25; |
49 |
| -infs = 1.05:.01:1.05; |
50 |
| -histvar = 1:1:1; |
| 54 | +% number of ensembles and inflation |
| 55 | +% ensNs = 10:5:25; |
| 56 | +% infs = 1.01:0.01:1.04; |
| 57 | + |
| 58 | +%ensNs = [5 15 25 50]; |
| 59 | +ensNs = [50 100 150 200]; |
| 60 | +infs = [1.01 1.02 1.05 1.10]; |
| 61 | +rejs = [0.10 0.12 0.15 0.20]; |
| 62 | + |
| 63 | +% variables for which you need the rank histogram plot |
| 64 | +histvar = 1:1:3; |
51 | 65 | serveindicies = 1:1:natureODE.NumVars;
|
52 | 66 | rmses = inf*ones(numel(ensNs), numel(infs));
|
| 67 | +rhplotval = inf*ones(numel(ensNs), numel(infs)); |
53 | 68 |
|
54 |
| -maxallowerr = 2; |
| 69 | +maxallowerr = 10; |
55 | 70 |
|
56 | 71 | mm = min(rmses(:));
|
57 | 72 |
|
58 | 73 | if mm >= maxallowerr
|
59 | 74 | mm = 0;
|
60 | 75 | end
|
61 | 76 |
|
62 |
| -imagesc(ensNs, infs, rmses.'); caxis([mm, 1]); colorbar; set(gca,'YDir','normal'); |
63 |
| -axis square; title('EnKF_l63'); colormap('hot'); |
64 |
| -xlabel('Ensemble Size'); ylabel('Inflation'); |
| 77 | +% imagesc(ensNs, infs, rmses.'); caxis([mm, 1]); colorbar; set(gca,'YDir','normal'); |
| 78 | +% axis square; title('EnKF_l63'); colormap('hot'); |
| 79 | +% xlabel('Ensemble Size'); ylabel('Inflation'); |
65 | 80 |
|
66 | 81 | runsleft = find(rmses == inf);
|
67 | 82 |
|
| 83 | +f1 = figure; f2 = figure; f3 = figure; f4 = figure; |
| 84 | + |
68 | 85 | for runn = runsleft.'
|
69 | 86 | [ensNi, infi] = ind2sub([numel(ensNs), numel(infs)], runn);
|
| 87 | + [ensNi, reji] = ind2sub([numel(ensNs), numel(rejs)], runn); |
70 | 88 |
|
71 | 89 | fprintf('N: %d, inf: %.3f\n', ensNs(ensNi), infs(infi));
|
72 | 90 |
|
73 | 91 | ns = 1;
|
74 | 92 | sE = zeros(ns, 1);
|
75 | 93 |
|
76 | 94 | inflationAll = infs(infi);
|
| 95 | + rejAll = rejs(reji); |
77 | 96 | ensN = ensNs(ensNi);
|
78 | 97 |
|
79 | 98 | for sample = 1:ns
|
80 | 99 | % Set rng for standard experiments
|
81 | 100 | rng(17 + sample - 1);
|
82 | 101 |
|
83 | 102 | inflation = inflationAll;
|
| 103 | + rejuvenation = rejAll; |
84 | 104 |
|
85 |
| - % No localization |
86 |
| -% r = 5; |
87 |
| -% d = @(t, y, i, j) modelODE.DistanceFunction(t, y, i, j); |
88 |
| - %localization = []; |
89 |
| - |
90 |
| - %localization = @(t, y, H) datools.tapering.gc(t, y, r, d, H); |
91 |
| - |
92 |
| -% localization = @(t, y, Hi, k) datools.tapering.gcCTilde(t, y, Hi, r, d, k); |
93 |
| - %localization = @(t, y, Hi, k) datools.tapering.cutoffCTilde(t, y, r, d, Hi, k); |
94 |
| - |
95 |
| - enkf = datools.statistical.ensemble.EnKF(model, ... |
| 105 | + % define the statistical/variational model here |
| 106 | + enkf = datools.statistical.ensemble.SIR(model, ... |
96 | 107 | 'Observation', observation, ...
|
97 | 108 | 'NumEnsemble', ensN, ...
|
98 | 109 | 'ModelError', modelerror, ...
|
99 | 110 | 'EnsembleGenerator', ensembleGenerator, ...
|
100 | 111 | 'Inflation', inflation, ...
|
101 | 112 | 'Parallel', false, ...
|
102 |
| - 'RankHistogram', histvar); |
| 113 | + 'RankHistogram', histvar, ... |
| 114 | + 'Rejuvenation', rejuvenation); |
103 | 115 |
|
104 | 116 | enkf.setMean(natureODE.Y0);
|
105 | 117 | enkf.scaleAnomalies(1/10);
|
106 | 118 |
|
| 119 | + % define steps and spinups |
107 | 120 | spinup = 100;
|
108 | 121 | times = 11*spinup;
|
109 | 122 |
|
110 | 123 | mses = zeros(times - spinup, 1);
|
111 | 124 |
|
112 |
| - rmse = nan; |
| 125 | + % imagesc(ensNs, infs, rmses.'); caxis([mm, 1]); colorbar; set(gca,'YDir','normal'); |
| 126 | + % axis square; title('EnKF'); colormap('pink'); |
| 127 | + % xlabel('Ensemble Size'); ylabel('Inflation'); |
113 | 128 |
|
| 129 | + rmse = nan; |
114 | 130 | ps = '';
|
115 |
| - |
116 | 131 | do_enkf = true;
|
117 | 132 |
|
| 133 | + rmstempval = NaN * ones(1, times-spinup); |
118 | 134 |
|
119 |
| - |
| 135 | + % Assimilation |
120 | 136 | for i = 1:times
|
121 | 137 | % forecast
|
122 |
| - |
123 | 138 | nature.evolve();
|
124 | 139 |
|
125 | 140 | if do_enkf
|
126 | 141 | enkf.forecast();
|
127 | 142 | end
|
128 | 143 |
|
129 |
| - |
130 | 144 | % observe
|
131 | 145 | xt = naturetomodel.observeWithoutError(nature.TimeSpan(1), nature.State);
|
132 | 146 | y = enkf.Observation.observeWithError(model.TimeSpan(1), xt);
|
133 | 147 |
|
134 |
| - % try RH |
| 148 | + % Rank histogram (if needed) |
135 | 149 | datools.utils.stat.RH(enkf, xt);
|
136 | 150 |
|
137 | 151 | % analysis
|
|
153 | 167 | mses(i - spinup) = mean((xa - xt).^2);
|
154 | 168 | rmse = sqrt(mean(mses(1:(i - spinup))));
|
155 | 169 |
|
156 |
| - if rmse > maxallowerr || isnan(rmse) || mses(i - spinup) > 2*maxallowerr |
157 |
| - do_enkf = false; |
158 |
| - end |
| 170 | + rmstempval(i - spinup) = rmse; |
| 171 | + |
| 172 | +% if rmse > maxallowerr || isnan(rmse) || mses(i - spinup) > 2*maxallowerr |
| 173 | +% do_enkf = false; |
| 174 | +% end |
159 | 175 | end
|
160 | 176 |
|
161 |
| - |
162 | 177 | if ~do_enkf
|
163 | 178 | break;
|
164 | 179 | end
|
165 | 180 |
|
166 | 181 | end
|
| 182 | + hold off; |
167 | 183 |
|
168 | 184 | if isnan(rmse)
|
169 | 185 | rmse = 1000;
|
|
176 | 192 | end
|
177 | 193 |
|
178 | 194 | end
|
179 |
| - rmse |
180 | 195 | resE = mean(sE);
|
181 | 196 |
|
182 | 197 | if isnan(resE)
|
|
185 | 200 |
|
186 | 201 | rmses(ensNi, infi) = resE;
|
187 | 202 |
|
| 203 | + [xs, pval, rhplotval(ensNi, infi)] = datools.utils.stat.KLDiv(enkf.RankValue(1,1:end-1),... |
| 204 | + (1/ensN) * ones(1, ensN+1)); |
| 205 | + |
188 | 206 | mm = min(rmses(:));
|
189 | 207 | mm = 0;
|
190 | 208 |
|
191 | 209 | if mm >= maxallowerr
|
192 | 210 | mm = 0;
|
193 | 211 | end
|
194 | 212 |
|
195 |
| - imagesc(ensNs, infs, rmses.'); caxis([mm, 1]); colorbar; set(gca,'YDir','normal'); |
196 |
| - axis square; title('EnKF'); colormap('pink'); |
| 213 | + figure(f1); |
| 214 | + subplot(numel(infs), numel(ensNs), runn); |
| 215 | + hold all; |
| 216 | + z = enkf.RankValue(1,1:end-1); |
| 217 | + maxz = max(z); |
| 218 | + z = z/sum(z); |
| 219 | + NN = numel(z); |
| 220 | + z = NN*z; |
| 221 | + bar(xs, z); |
| 222 | + plot(xs, pval, '-*r'); |
| 223 | + set(gca,'XTick',[xs(1) xs(end)]); |
| 224 | + set(gca,'XTickLabel',[1, ensN+1]); |
| 225 | + xlabel('bins'); |
| 226 | + drawnow; |
| 227 | + |
| 228 | + figure(f2); |
| 229 | + %imagesc(ensNs.', infs.', flipud(rmses.')); caxis([0, 1]); colorbar; set(gca,'YDir','normal'); |
| 230 | + imagesc(ensNs.', rejs.', flipud(rmses.')); caxis([0, 1]); colorbar; set(gca,'YDir','normal'); |
| 231 | + axis square; title('ETPF'); colormap('pink'); |
197 | 232 | xlabel('Ensemble Size'); ylabel('Inflation');
|
| 233 | + %ytics = max(infs) - min(infs); |
| 234 | + ytics = max(rejs) - min(rejs); |
| 235 | + %ytics = min(infs):ytics/(length(infs) - 1):max(infs); |
| 236 | + ytics = min(rejs):ytics/(length(rejs) - 1):max(rejs); |
| 237 | + set(gca,'YTick', ytics); |
| 238 | + %set(gca,'YTickLabel', fliplr(infs)); |
| 239 | + set(gca,'YTickLabel', fliplr(rejs)); |
198 | 240 | drawnow;
|
| 241 | + |
| 242 | + figure(f3); |
| 243 | + %imagesc(ensNs.', infs.', flipud(rhplotval.')); caxis([-0.09 0.09]); colorbar; set(gca, 'YDir', 'normal'); |
| 244 | + imagesc(ensNs, rejs, flipud(rhplotval.')); caxis([-0.09 0.09]); colorbar; set(gca, 'YDir', 'normal'); |
| 245 | + axis square; title('KLDiv'); colormap('summer'); |
| 246 | + xlabel('Ensemble Size'); ylabel('Inflation'); |
| 247 | + set(gca,'YTick', ytics); |
| 248 | + %set(gca,'YTickLabel', fliplr(infs)); |
| 249 | + set(gca,'YTickLabel', fliplr(rejs)); |
| 250 | + drawnow; |
| 251 | + |
| 252 | + figure(f4); |
| 253 | + subplot( numel(infs), numel(ensNs), runn); |
| 254 | + plot(spinup+1:1:times, rmstempval); |
| 255 | + xlim([spinup+1 times]); ylim([0 1]); |
| 256 | + set(gca, 'XTick', [spinup+1 times]) |
| 257 | + set(gca, 'XTickLabel', [spinup+1 times]) |
| 258 | + han=axes(f4,'visible','off'); |
| 259 | + han.Title.Visible='on'; |
| 260 | + han.XLabel.Visible='on'; |
| 261 | + han.YLabel.Visible='on'; |
| 262 | + ylabel(han,'Value'); |
| 263 | + xlabel(han,'Time Step'); |
| 264 | + title(han,'RMSE'); |
| 265 | + drawnow; |
| 266 | + |
199 | 267 | end
|
| 268 | +% step = 0; |
| 269 | +% Rank histogram |
200 | 270 | % figure;
|
201 |
| -% bar(enkf.RankValue(1,1:end-1)); |
202 |
| -return; |
| 271 | +% for i = 1: length(enkf.RankValue(:,1)) |
| 272 | +% subplot(1,3,i); |
| 273 | +% bar(enkf.RankValue(i,1:end-1)); |
| 274 | +% end |
203 | 275 |
|
| 276 | +% Kldiv + poly approx (for the variable being observed) |
| 277 | +% figure; |
| 278 | +% for i = 1:length(histvar) |
| 279 | +% |
| 280 | +% end |
| 281 | + |
| 282 | + |
| 283 | +return; |
0 commit comments