-
Notifications
You must be signed in to change notification settings - Fork 9
/
HIGHR2.DOG
executable file
·268 lines (227 loc) · 6.7 KB
/
HIGHR2.DOG
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
function [ikey,isurr,rsurr,nsurr,errflg]=highr2(R,j1,j2,j3,mlap,rmin)
% highr2: highest correlation between tree-ring series; surrogate series
% CALL: [ikey,isurr,rsurr,nsurr,errflg]=highr2(R,j1,j2,j3,mlap,rmin);
%
% Meko 4-19-97
%
% Subfunction written for use with surrgt?.m functions that
% identify surrogate tree-ring series
%
%******************* IN *********
%
% R (mR x 4)r correlation, sample size, series 1 index, series 2 index
% as obtained from r4sov.m
% j1 (1 x nsers)L pointer to completed series
% j2 (1 x nsers)L pointer to yet to be completed series
% j3 (1 x nsers)L pointer to special (specified series)
% mlap (1 x 1)i minimum number of acceptable overlap years for
% a correlation coefficient
% rmin (1 x 1)r minimum acceptable correlation
%
%j
%****************** OUT *********************
%
% ikey (1 x 1)i index of newly completed series
% isurr (1 x 1)i index of its surrogate
% rsurr (1 x 1)r correlation coefficient between key and surrogate
% nsurr (1 x 1)i sample size (# obs) for rsurr
% errflg (1 x 1)i error flag
% 0 = no problem
% 1 = no pairs of j1,j2 series in cols 3,4 of R, after
% ruling out j3 series
% 2 suitable pairs exist, but none with at least mlap observations
% overlap for the computed r
% 3 suitable pairs exist, and overlap OK for at least 1, but
% none have high enough correl coef, as specified by rmin
%
%
%***************** NOTES **********************************
%
% The correlations and associated info, R, are assumed to
% have been generated by r4sov.m. R is therefore assumed not
% to have any NaN elements. highr2.m checks anyway and reports an
% error if R contains any NaNs
%
% Typically, the j1,j2,j3 logical series correspond to pointers
% to time series:
% j1 -- 'completed' series. Those either covering whole
% calibration period, or if not, those previously filled in
% for the calibration period
% j2 -- 'waiting' series. Those not yet with surrogate data
% generated for the calibration period
% j3 -- 'specified' series. Series that the user does not want
% correlations to determine surrogate for. The user instead
% has specified the surrogates based on other knowledge.
errflg=[]; %initialize error flag
rsurr=[];
nsurr=[];
isurr=[];
ikey=[];
% Check inputs
[mR,nR]=size(R);
if nR~=4,
error('R must be 4-col matrix');
end
if any(any(isnan(R)));
disp('R, as generated by r4sov.m, should not contain any NaNs')
error('NaN elements in R');
end
Ltemp=[size(j1,1)==1 size(j2,1)==1 size(j3,1)==1];
if ~all(Ltemp);
error('j1, j2, j3 must all be row vectors');
end
ntemp=size(j1,2);
Ltemp=[ntemp==size(j2,2) ntemp==size(j3,2)];
if ~all(Ltemp);
error('j1,j2,j3 not all same col size');
end
Ltemp=[islogical(j1) islogical(j2) islogical(j3)];
if ~all(Ltemp);
error('j1,j2,j3 not all logical');
end
% Check that j1, j2 mutually exclusive
Ltemp=j1 & j2;
if any(Ltemp),
error('Some element is turned on in both j1 and j2');
end
% Check that have at least one j2 series and at least one
% j1 series, and that those series are not j3 series
Ltemp=j1 & ~j3;
if ~any(Ltemp);
error('No j1 series, or none that are not also j3');
end
Ltemp=j2 & ~j3;
if ~any(Ltemp);
error('No j2 series or none that are not also j3');
end
%**************************************************************
% Identify rows of R corresponding to pairs of series such
% that (1) one series is a j1 series but not a j3 series, and
% (2) the other series is a j2 series but not a j3 series, (3)
% the sample size for correlation is at least mlap observations,
% and (4) the correlation is at least rmin
% Acceptable sample size?
L1=R(:,2)>=mlap;
% Accepable minimum r
L2=R(:,1)>=rmin;
% Store cols 3 and 4 of R in a and b
a=R(:,3); % cv of indices, first of pair
b=R(:,4); % cv of indices, second of pair
% First or second series (col 3 or 4 of R) not a j3 series
if ~any(j3);
L3a=logical(zeros(mR,1));
L3b=L3a;
else
numj3=sum(j3);
j3f=find(j3); % a rv of indices of j3 series
j3f=repmat(j3f,mR,1); % expand to a matrix, same row-size as R
% Handle col 3 of R
A=repmat(a,1,numj3); % expand to same size as j3f
Ltemp=A==j3f;
if numj3>1;
L3a=(any(Ltemp'))';
else
L3a=Ltemp; % special case for vector instead of matrix
end
% Handle col 4 of R
B=repmat(b,1,numj3); % expand to same size as j3f
Ltemp=B==j3f;
if numj3>1
L3b=(any(Ltemp'))';
else
L3b=Ltemp;
end
end
% Col 3 of R is a j1 series
numj1=sum(j1);
j1f=find(j1);
j1f=repmat(j1f,mR,1);
A=repmat(a,1,numj1);
Ltemp=A==j1f;
if numj1>1;
L4a=(any(Ltemp'))';
else
L4a=Ltemp;
end
% Col 4 of R is a j1 series
B=repmat(b,1,numj1);
Ltemp=B==j1f;
if numj1>1;
L4b=(any(Ltemp'))';
else
L4b=Ltemp;
end
% Col 3 of R is a j2 series
numj2=sum(j2);
j2f=find(j2);
j2f=repmat(j2f,mR,1);
A=repmat(a,1,numj2);
Ltemp=A==j2f;
if numj2>1;
L5a=(any(Ltemp'))';
else
L5a=Ltemp;
end
% Col 4 of R is a j2 series
B=repmat(b,1,numj2);
Ltemp=B==j2f;
if numj2>1;
L5b=(any(Ltemp'))';
else
L5b=Ltemp;
end
% Now we have these pointers to rows of R:
% L1 -- at least mlap observations for computation of R
% L2 -- r at least as large as rmin
% L3a -- j3 series in col 3
% L3b -- j3 series in col 4
% L4a -- j1 series in col 3
% L4b -- j1 series in col 4
% L5a -- j2 series in col 3
% L5b -- j2 series in col 4
% One a j1 series, one a j2 series, and neither a j3 series
L6=((L4a | L4b) & (L5a | L5b)) & ~L3a & ~L3b;
if sum(L6)==0;
disp('No j1,j2 pairs in R after excluding j3 series');
pause(2);
errflg=1;
return
end
% Constraint L6 and: minimum overlap of mlap observations
L7=L6 & L1;
if sum(L7)==0;
disp('Acceptable j1,j2 pairs, but not enought overlap in yr');
pause(2);
errflg=2;
return
end
% Constraint L7 and: correlation at least rmin
L8=L7 & L2;
if sum(L2)==0;
disp('Bombed out because correlation coef lower than rmin');
pause(2)
errflg=3
return
end
%***************************************************
%
% L8 is the pointer to rows of R for which to find the max correl
f8=find(L8); % row index in R of candidate rows
r1=R(L8,1); % cv of correlation coefficients
[rmax, imax]=max(r1); % highest correlation coef, and row index to r1
irow = f8(imax); % row of R containing highest correlation
rsurr=R(irow,1);
nsurr=R(irow,2);
var3=R(irow,3);
var4=R(irow,4);
% Know that var3 and var4 are a j1,j2 pair, but do not know
% which is the j1 and which is the j2
if any(find(j1)==var3);
ikey=var4;
isurr=var3;
else
ikey=var3;
isurr=var4;
end
errflg=0;