-
Notifications
You must be signed in to change notification settings - Fork 6
/
eofSigThreshold.m
72 lines (64 loc) · 2.32 KB
/
eofSigThreshold.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
function[varargout] = eofSigThreshold( randExpVar, p, expVar )
%% Gets the explained variance values required for significance at significance level p
%
% [sigExpVar, true_p] = getSigThreshold( randExpVar, p )
% Finds the significance threshold for explained variance values at a
% significance level p.
%
% [sigExpVar, true_p, nSig] = getSigThreshold( randExpVar, p, expVar )
% Also compares the significance threshold to a set of explained variances
% from an eof analysis and determines the number of significant leading
% modes.
%
%
% ----- Inputs -----
%
% randExpVar: Random explained variances generated by a "Rule N" process
%
% p: The significance level to use for the test. Must be on the open
% interval (0, 1).
%
% expVar: Explained variances of EOF modes.
%
%
% ----- Outputs -----
%
% sigExpVar: The explained variance significance threshold.
%
% true_p: The true significance level used in the test. (Depending on the
% number of Monte Carlo iterations, this may be slightly higher than the
% user-specified p value.)
%
% nSig: The number of significance leading EOF modes.
%
%
% ----- Author -----
%
% Jonathan King, 2017, University of Arizona, [email protected]
% Quick error checking
if ~ismatrix(randExpVar) || any(isnan(randExpVar(:)))
error('randExpVar must be a matrix and cannot contain NaN.');
elseif ~isfloat(p) || p<=0 || p>=1
error('p must be a number on the open interval (0,1)');
elseif exist('expVar','var') && ( ~isvector(expVar) || numel(expVar)~=size(randExpVar,2) || any(isnan(expVar(:))) )
error('expVar must be a vector with the same number of elements as columns in randExpVar. expVar may not contain NaN values.');
end
% Get the number of sets of random explained variance
nSets = size( randExpVar, 1);
% Sort the randomly generated explained variances
randExpVar = sort( randExpVar );
% Get the threshold index for significance <= p
threshold = ceil( nSets * (1-p) );
% Get the true significance of this threshold
true_p = 1 - threshold / nSets;
% Get the explained variances at this threshold
sigExpVar = randExpVar( threshold, : );
% If expVar was given, get the number of significant eof modes
if exist('expVar','var')
nSig = find( expVar' < sigExpVar, 1, 'first') - 1;
% Return the output
varargout = {sigExpVar, true_p, nSig};
else
varargout = {sigExpVar, true_p};
end
end