function sig = bdssig(w, n, m, eps)
%BDSSIG Significance level of the BDS statistic in small samples
%
% SIG = BDSSIG (W, N, M, EPS) evaluates the significance of BDS statistics W under the
%    null hypothesis of iidness, using Kanzler's (1999) finite-sample quantile values.
%
%    The significance levels can only assume 0.005, 0.010, 0.025, 0.050 or 1 (indicates
%    failure to reject the null hypothesis) in value. These levels must be doubled to
%    conduct what is normally a two-sided BDS test.
%
%    N is the size of the sample on which the BDS statistics were computed.
%
%    M can be either a scalar or a vector (corresponding to W) and represents
%    the embedding dimension(s) for which the BDS statistics were computed.
%    Only integers between 2 and 15 inclusive are permitted.
%
%    EPS is the dimensional distance for which the BDS statistics were calculated.
%    This is given in units of the standard deviation of approximately normally
%    distributed samples, and only values 0.5, 1.0, 1.5 and 2.0 are allowed.
%
%    See Kanzler (1999) on how estimation of the BDS statistic is to be correctly
%    sized in normally as well as non-normally distributed samples. As the paper
%    shows, EPS = 0.5 or 1.0 yield in many cases BDS distributions which are badly
%    shaped, and while it is in principle possible to use this function to evaluate
%    the significance of BDS statistics computed for either of these values, the
%    results may not be very reliable, in particular if the number of observations
%    is not close to one of the sample sizes for which the BDS distribution is
%    tabulated in Kanzler (1999).
%
%    Note also that estimation of any BDS statistics evaluated through this function
%    must be based on an algorithm which makes use of the most efficient estimators
%    of the various correlation integrals on which the BDS statistic is based. The
%    author's own function BDS.M may be the only function for which this is true.
%    See Kanzler (1999) on why this issue can be crucial to correctly sized estimation
%    of the BDS statistic.
%
%    Requires the MATLAB Statistics Toolbox (for n>2500).
%
% The author assumes no responsibility for errors or damage resulting from usage. All
% rights reserved. Usage of the programme in applications and alterations of the code
% should be referenced. This script may be redistributed if nothing has been added or
% removed and nothing is charged. Positive or negative feedback would be appreciated.

%                     Copyright (c) 15 Sept. 1998 by Ludwig Kanzler
%                     E-mail:                 kanzler@lsealumni.com
%                     Homepage:   http://www2.gol.com/users/kanzler
%                     $ Revision: 1.1 $ $ Date: 29 September 2002 $

% Revision history:   Revision 1.1:  fixed bug causing SIG to assume incorrect values
%                                    for case 2500<n<5000 (thanks to Deepak Goel for
%                                    helping me find this error)

%%%%%%%%%%%%%%%%%%%%%%%%%%% Check validity of input arguments %%%%%%%%%%%%%%%%%%%%%%%%%%%

mcases    = 2 : 15;
epscases  = [0.5 1.0 1.5 2.0];
ncases    = [50 100 250 500 750 1000 2500];
siglevels = [0.005 0.010 0.025 0.050 1 0.050 0.025 0.010 0.005];

if nargin < 1
   error('This function needs some argument input!')
elseif length(w) ~= length(w(:))
   error('Cannot evaluate a matrix of BDS statistics; input must be a scalar or vector.')
elseif nargin < 2
   n = inf;
end

if nargin < 3
   m = 2 : length(w);
elseif unique([mcases, m(:)']) ~= 14
   error('Cannot handle embedding dimension other than integers between 2 and 15.')
elseif length(m) ~= length(w) & length(m) ~= 1
   error('Cannot handle a vector of embedding dimensions which is not as long as W.')
elseif length(m) == 1
   m = m * ones(1, length(w));
end

if nargin < 4
   eps = 1.5;
elseif ~sum(eps == epscases)
   error('Cannot handle a dimensional distance other than 0.5, 1.0, 1.5 or 2.0.')
end

if n <= 5000

%%%%%%%%%%%%%%%%%%%%%%%%%% Setup of the quantile look-up table %%%%%%%%%%%%%%%%%%%%%%%%%%

% The quantile values are taken from Kanzler (1998) and are found along dimension 1
% (with the corresponding values for N(0,1) in parenthesis):
%                                                            <  0.5%  (-2.58)
%                                                            <  1.0%  (-2.33)
%                                                            <  2.5%  (-1.96)
%                                                            <  5.0%  (-1.65)
%                                                            > 95.0%  ( 1.65)
%                                                            > 97.5%  ( 1.96)
%                                                            > 99.0%  ( 2.33)
%                                                            > 99.5%  ( 2.58)
%
% Embedding dimensions m = [2 3 4 5 6 7 8 9 10 11 12 13 14 15] are along dimension 2.
%
% Sample sizes n = [50 100 250 500 750 1000 2500] are along dimension 3.
%
% Dimensional distances in units of the standard deviation of a normally distributed
% sample eps = [0.5 1.0 1.5 2.0] are along dimension 4.

quants = NaN * ones(8, 14, 7, 4);

% n = 50, eps = 0.5 (c1 ~ 0.27)
quants(1:8, 1:12, 1, 1) = [...
  -22.66 -27.66 -31.41 -40.37 -54.26 -48.20 -40.93 -34.41 -28.99 -26.20 -22.25 -22.19
  -13.87 -16.90 -19.92 -24.97 -28.85 -25.56 -21.97 -18.59 -16.45 -15.29 -14.53 -14.65
   -8.01  -9.87 -12.09 -14.93 -15.79 -13.73 -11.88 -10.34  -9.30  -8.68  -8.71  -8.58
   -5.75  -6.94  -8.74 -10.85 -10.85  -9.39  -7.98  -6.89  -6.33  -5.95  -5.81  -5.80
    5.66   6.88   9.14  13.14  18.81  15.41  -0.68  -0.53  -0.40  -0.29  -0.21  -0.15
    8.27  10.05  13.92  20.89  31.30  36.99  -0.44  -0.41  -0.30  -0.22  -0.15  -0.10
   13.99  16.09  23.76  34.03  56.16  74.44  73.59  -0.29  -0.22  -0.15  -0.10  -0.07
   21.47  27.66  37.27  57.80  89.30 118.37 147.67  45.58  -0.18  -0.12  -0.08  -0.05];
 
quants(1:8, 13:14, 1, 1) = [...
  -22.79 -24.73
  -14.99 -16.43
   -8.98  -9.75
   -5.89  -6.29
   -0.11  -0.07
   -0.07  -0.05
   -0.04  -0.03
   -0.03  -0.02];

% n = 50, eps = 1.0 (c1 ~ 0.51)
quants(1:8, 1:14, 1, 2) = [...
  -4.66 -5.12 -5.55 -6.05 -6.45 -6.99 -7.46 -7.65 -7.89 -7.72 -8.09 -8.60 -9.37 -10.87
  -4.10 -4.42 -4.67 -5.18 -5.49 -5.89 -6.27 -6.45 -6.57 -6.65 -6.69 -6.95 -7.70  -8.25
  -3.35 -3.55 -3.78 -4.10 -4.37 -4.58 -4.92 -5.06 -5.13 -5.13 -5.06 -5.25 -5.45  -5.83
  -2.84 -2.99 -3.12 -3.32 -3.54 -3.75 -4.01 -4.12 -4.20 -4.16 -4.03 -4.06 -4.17  -4.28
   2.76  2.89  3.03  3.27  3.64  4.11  4.81  5.66  6.60  7.15  6.60  2.59 -0.18  -0.15
   3.50  3.70  3.94  4.35  4.98  5.74  6.81  8.33 10.21 11.97 13.38 12.07  4.40  -0.10
   4.52  4.90  5.25  5.97  6.78  8.21  9.89 12.32 15.44 20.26 24.66 27.84 26.77  16.38
   5.43  5.82  6.46  7.24  8.60 10.65 12.56 16.11 21.04 26.66 34.22 40.93 46.54  46.35];

% n = 50, eps = 1.5 (c1 ~ 0.71)
quants(1:8, 1:14, 1, 3) = [...
  -4.09 -4.01 -4.14 -4.08 -4.15 -4.30 -4.38 -4.53 -4.98 -5.32 -5.64 -6.19 -7.05 -7.52
  -3.69 -3.65 -3.75 -3.69 -3.75 -3.92 -3.98 -4.12 -4.37 -4.64 -4.85 -5.19 -5.76 -6.35
  -3.08 -3.12 -3.19 -3.19 -3.25 -3.32 -3.36 -3.49 -3.63 -3.74 -3.94 -4.21 -4.46 -4.74
  -2.63 -2.67 -2.73 -2.75 -2.80 -2.84 -2.88 -2.97 -3.05 -3.14 -3.24 -3.40 -3.55 -3.74
   2.46  2.46  2.47  2.46  2.44  2.47  2.53  2.60  2.67  2.76  2.85  2.95  3.07  3.20
   3.01  3.04  3.07  3.07  3.14  3.27  3.34  3.48  3.64  3.78  3.96  4.25  4.54  4.87
   3.64  3.69  3.76  3.91  4.05  4.26  4.43  4.62  4.92  5.22  5.65  6.13  6.54  7.09
   4.15  4.11  4.26  4.57  4.70  4.96  5.22  5.56  6.05  6.40  6.90  7.41  8.29  9.19];

% n = 50, eps = 2.0 (c1 ~ 0.84)
quants(1:8, 1:14, 1, 4) = [...
  -4.86 -4.77 -4.74 -4.67 -4.83 -4.89 -4.86 -5.14 -5.33 -5.48 -5.73 -6.05 -6.45 -6.89
  -4.42 -4.34 -4.35 -4.28 -4.40 -4.48 -4.52 -4.62 -4.78 -4.88 -5.11 -5.32 -5.65 -5.94
  -3.69 -3.68 -3.67 -3.69 -3.78 -3.79 -3.86 -3.96 -4.02 -4.09 -4.26 -4.41 -4.63 -4.87
  -3.02 -3.09 -3.11 -3.15 -3.23 -3.25 -3.33 -3.38 -3.44 -3.53 -3.60 -3.69 -3.82 -3.98
   2.83  2.80  2.79  2.76  2.72  2.72  2.71  2.71  2.67  2.68  2.65  2.62  2.61  2.62
   3.50  3.42  3.46  3.44  3.44  3.42  3.40  3.40  3.41  3.39  3.44  3.46  3.48  3.52
   4.23  4.26  4.19  4.22  4.27  4.22  4.25  4.24  4.30  4.40  4.48  4.53  4.55  4.74
   4.70  4.77  4.71  4.72  4.81  4.82  4.91  4.93  5.08  5.16  5.21  5.30  5.44  5.53];

% n = 100, eps = 0.5 (c1 ~ 0.27)
quants(1:8, 1:14, 2, 1) = [...
  -5.33 -6.39 -7.91 -9.88 -11.20 -9.92 -8.48  -7.07  -6.10 -5.59 -5.07 -4.83 -4.58 -4.41
  -4.66 -5.51 -6.74 -8.49  -9.70 -8.72 -7.42  -6.29  -5.49 -4.88 -4.48 -4.14 -3.90 -3.83
  -3.78 -4.50 -5.56 -6.90  -8.11 -7.44 -6.27  -5.35  -4.59 -4.04 -3.66 -3.39 -3.20 -3.03
  -3.18 -3.71 -4.57 -5.67  -6.93 -6.44 -5.45  -4.62  -3.98 -3.50 -3.14 -2.86 -2.64 -2.50
   3.24  3.83  4.77  6.61   9.80 14.90 20.07  -1.13  -0.96 -0.78 -0.63 -0.50 -0.41 -0.33
   4.14  4.90  6.21  8.75  13.58 21.82 34.20  19.50  -0.82 -0.67 -0.53 -0.43 -0.34 -0.27
   5.49  6.35  8.22 11.79  19.23 33.76 58.23  87.29  -0.61 -0.56 -0.45 -0.35 -0.28 -0.21
   6.49  7.74  9.72 14.34  23.76 43.12 79.06 130.73 137.15 -0.47 -0.39 -0.30 -0.24 -0.18];

% n = 100, eps = 1.0 (c1 ~ 0.52)
quants(1:8, 1:14, 2, 2) = [...
  -3.16 -3.23 -3.32 -3.42 -3.66 -3.90 -4.20 -4.41 -4.57 -4.57 -4.40 -4.27 -4.14 -4.03
  -2.88 -2.93 -3.00 -3.12 -3.27 -3.50 -3.72 -3.97 -4.10 -4.12 -4.01 -3.85 -3.73 -3.63
  -2.47 -2.52 -2.58 -2.66 -2.80 -2.93 -3.14 -3.34 -3.49 -3.52 -3.42 -3.30 -3.18 -3.06
  -2.12 -2.16 -2.21 -2.29 -2.40 -2.53 -2.69 -2.87 -3.03 -3.07 -3.03 -2.90 -2.76 -2.64
   2.16  2.20  2.28  2.41  2.60  2.80  3.13  3.61  4.18  5.01  6.01  7.12  7.81  7.05
   2.64  2.73  2.85  3.05  3.28  3.67  4.16  4.84  5.68  6.96  8.84 10.88 12.77 14.23
   3.25  3.37  3.56  3.87  4.18  4.77  5.42  6.40  7.74  9.92 12.57 16.04 21.06 26.29
   3.72  3.81  4.06  4.39  4.94  5.58  6.37  7.58  9.39 12.14 15.70 20.87 27.11 35.25];

% n = 100, eps = 1.5 (c1 ~ 0.71)
quants(1:8, 1:14, 2, 3) = [...
  -3.15 -3.12 -3.15 -3.14 -3.10 -3.14 -3.08 -3.13 -3.15 -3.23 -3.21 -3.27 -3.38 -3.49
  -2.88 -2.88 -2.87 -2.87 -2.86 -2.86 -2.86 -2.85 -2.89 -2.95 -2.95 -2.99 -3.03 -3.15
  -2.45 -2.48 -2.44 -2.47 -2.49 -2.49 -2.48 -2.48 -2.50 -2.50 -2.53 -2.56 -2.60 -2.63
  -2.09 -2.11 -2.13 -2.12 -2.15 -2.16 -2.17 -2.16 -2.16 -2.17 -2.19 -2.22 -2.25 -2.28
   2.02  2.02  2.00  2.03  2.02  2.05  2.06  2.13  2.18  2.24  2.31  2.39  2.50  2.62
   2.47  2.48  2.45  2.49  2.54  2.59  2.65  2.72  2.82  2.94  3.09  3.25  3.41  3.60
   2.98  3.01  3.03  3.07  3.16  3.23  3.31  3.48  3.64  3.87  4.10  4.35  4.64  5.02
   3.27  3.41  3.45  3.50  3.61  3.72  3.86  4.05  4.26  4.46  4.84  5.16  5.56  6.10];

% n = 100, eps = 2.0 (c1 ~0.84)
quants(1:8, 1:14, 2, 4) = [...
  -3.66 -3.60 -3.56 -3.52 -3.43 -3.44 -3.43 -3.47 -3.46 -3.48 -3.48 -3.56 -3.56 -3.62
  -3.28 -3.24 -3.25 -3.25 -3.17 -3.14 -3.15 -3.16 -3.18 -3.20 -3.16 -3.24 -3.26 -3.31
  -2.73 -2.75 -2.77 -2.81 -2.76 -2.73 -2.74 -2.72 -2.74 -2.76 -2.76 -2.80 -2.81 -2.82
  -2.28 -2.33 -2.37 -2.38 -2.37 -2.36 -2.37 -2.37 -2.38 -2.37 -2.39 -2.41 -2.43 -2.44
   2.26  2.22  2.17  2.17  2.17  2.15  2.14  2.14  2.14  2.13  2.13  2.13  2.14  2.14
   2.77  2.71  2.69  2.67  2.66  2.67  2.67  2.72  2.71  2.72  2.75  2.77  2.77  2.78
   3.34  3.35  3.32  3.32  3.32  3.34  3.37  3.39  3.40  3.44  3.48  3.56  3.59  3.60
   3.73  3.74  3.76  3.78  3.74  3.78  3.84  3.92  3.93  4.00  4.04  4.07  4.10  4.23];

% n = 250, eps = 0.5 (c1 ~ 0.27)
quants(1:8, 1:14, 3, 1) = [...
  -3.25 -3.64 -4.32 -5.44 -6.76 -8.03 -7.35 -6.27  -5.29  -4.57 -4.01 -3.58 -3.22 -2.90
  -2.94 -3.32 -3.91 -4.85 -6.19 -7.40 -6.95 -5.91  -5.02  -4.33 -3.77 -3.35 -3.02 -2.72
  -2.54 -2.86 -3.29 -4.11 -5.19 -6.55 -6.30 -5.40  -4.59  -3.95 -3.46 -3.05 -2.71 -2.45
  -2.18 -2.40 -2.79 -3.47 -4.45 -5.78 -5.83 -5.00  -4.25  -3.65 -3.17 -2.80 -2.48 -2.23
   2.27  2.50  2.98  3.79  5.19  7.68 12.18 18.51  13.10  -1.69 -1.44 -1.23 -1.05 -0.90
   2.81  3.13  3.75  4.78  6.63 10.09 16.49 28.55  43.94  -1.52 -1.34 -1.13 -0.96 -0.82
   3.49  3.90  4.70  6.00  8.37 13.11 22.93 42.19  73.31  98.79 -1.20 -1.03 -0.87 -0.74
   3.96  4.49  5.42  7.01  9.81 15.49 28.61 54.34 102.44 165.50 -1.08 -0.94 -0.82 -0.69];

% n = 250, eps = 1.0 (c1 ~ 0.52)
quants(1:8, 1:14, 3, 2) = [...
  -2.63 -2.57 -2.58 -2.61 -2.67 -2.70 -2.81 -2.96 -3.12 -3.32 -3.51 -3.53 -3.44 -3.24
  -2.42 -2.38 -2.39 -2.40 -2.44 -2.48 -2.59 -2.72 -2.87 -3.06 -3.28 -3.33 -3.24 -3.08
  -2.09 -2.11 -2.09 -2.10 -2.13 -2.18 -2.24 -2.37 -2.51 -2.71 -2.91 -3.01 -2.99 -2.83
  -1.81 -1.83 -1.83 -1.84 -1.86 -1.90 -1.97 -2.06 -2.20 -2.37 -2.57 -2.73 -2.76 -2.63
   1.83  1.88  1.88  1.95  2.03  2.15  2.25  2.45  2.71  3.05  3.56  4.21  5.05  6.33
   2.26  2.30  2.35  2.44  2.54  2.69  2.88  3.12  3.49  3.93  4.68  5.60  6.99  8.82
   2.76  2.82  2.93  3.01  3.18  3.40  3.78  4.11  4.58  5.21  6.22  7.76  9.79 12.75
   3.08  3.15  3.31  3.47  3.70  3.97  4.39  4.91  5.38  6.49  7.70  9.43 12.14 15.87];

% n = 250, eps = 1.5 (c1 ~ 0.71)
quants(1:8, 1:14, 3, 3) = [...
  -2.72 -2.70 -2.67 -2.63 -2.60 -2.55 -2.51 -2.49 -2.49 -2.45 -2.46 -2.44 -2.43 -2.43
  -2.48 -2.49 -2.46 -2.43 -2.40 -2.37 -2.32 -2.31 -2.29 -2.29 -2.27 -2.26 -2.25 -2.25
  -2.13 -2.12 -2.14 -2.13 -2.10 -2.08 -2.04 -2.02 -2.03 -2.01 -2.00 -1.98 -1.99 -1.99
  -1.81 -1.81 -1.85 -1.84 -1.82 -1.81 -1.79 -1.77 -1.77 -1.75 -1.76 -1.75 -1.75 -1.74
   1.82  1.82  1.82  1.84  1.85  1.87  1.89  1.92  1.94  1.98  2.02  2.06  2.11  2.16
   2.22  2.22  2.25  2.25  2.29  2.33  2.35  2.40  2.44  2.52  2.55  2.63  2.73  2.82
   2.68  2.67  2.74  2.77  2.84  2.90  2.94  3.02  3.09  3.15  3.28  3.46  3.59  3.73
   2.97  3.01  3.09  3.12  3.19  3.27  3.28  3.41  3.52  3.66  3.85  3.97  4.20  4.42];

% n = 250, eps = 2.0 (c1 ~ 0.84)
quants(1:8, 1:14, 3, 4) = [...
  -2.90 -2.88 -2.87 -2.86 -2.89 -2.83 -2.77 -2.74 -2.76 -2.73 -2.68 -2.68 -2.66 -2.62
  -2.62 -2.64 -2.63 -2.61 -2.59 -2.59 -2.56 -2.51 -2.52 -2.50 -2.49 -2.48 -2.45 -2.43
  -2.25 -2.26 -2.24 -2.24 -2.24 -2.23 -2.22 -2.22 -2.19 -2.18 -2.18 -2.17 -2.15 -2.13
  -1.91 -1.92 -1.93 -1.93 -1.93 -1.94 -1.93 -1.93 -1.91 -1.91 -1.89 -1.89 -1.89 -1.88
   1.88  1.86  1.86  1.86  1.85  1.83  1.83  1.84  1.85  1.85  1.85  1.87  1.88  1.88
   2.30  2.29  2.29  2.28  2.27  2.29  2.28  2.28  2.29  2.29  2.31  2.33  2.35  2.36
   2.79  2.78  2.75  2.78  2.80  2.79  2.81  2.83  2.83  2.81  2.84  2.88  2.92  2.93
   3.16  3.15  3.09  3.07  3.13  3.17  3.14  3.18  3.20  3.24  3.22  3.24  3.32  3.36];

% n = 500, eps = 0.5 (c1 ~ 0.28)
quants(1:8, 1:14, 4, 1) = [...
  -2.88 -3.01 -3.35 -4.01 -5.05 -6.40 -7.60 -6.95 -5.99  -5.12  -4.43  -3.88 -3.43 -3.07
  -2.63 -2.78 -3.04 -3.67 -4.52 -5.92 -7.22 -6.69 -5.74  -4.93  -4.28  -3.74 -3.31 -2.95
  -2.22 -2.38 -2.62 -3.09 -3.87 -5.15 -6.55 -6.33 -5.44  -4.66  -4.03  -3.54 -3.11 -2.77
  -1.89 -2.01 -2.24 -2.64 -3.32 -4.38 -5.78 -6.00 -5.20  -4.46  -3.85  -3.35 -2.95 -2.63
   2.01  2.13  2.37  2.87  3.68  5.25  8.05 13.57 22.54  31.55  -2.28  -2.00 -1.73 -1.51
   2.46  2.64  2.92  3.55  4.55  6.61 10.41 17.88 31.94  49.71  -2.08  -1.89 -1.64 -1.43
   2.95  3.27  3.72  4.41  5.74  8.50 13.55 23.17 44.46  84.82 133.43  -1.73 -1.53 -1.33
   3.32  3.61  4.14  5.03  6.58  9.75 15.80 27.76 55.17 109.55 198.94 264.86 -1.45 -1.27];

% n = 500, eps = 1.0 (c1 ~ 0.52)
quants(1:8, 1:14, 4, 2) = [...
  -2.55 -2.52 -2.53 -2.51 -2.46 -2.46 -2.48 -2.54 -2.65 -2.80 -3.01 -3.24 -3.43 -3.47
  -2.34 -2.33 -2.30 -2.30 -2.27 -2.27 -2.30 -2.36 -2.47 -2.61 -2.78 -3.01 -3.22 -3.31
  -2.01 -2.00 -1.99 -1.96 -1.99 -2.00 -2.03 -2.07 -2.16 -2.28 -2.46 -2.66 -2.91 -3.04
  -1.72 -1.72 -1.71 -1.70 -1.73 -1.75 -1.76 -1.81 -1.88 -1.98 -2.14 -2.35 -2.59 -2.80
   1.77  1.77  1.77  1.81  1.87  1.93  2.01  2.12  2.25  2.47  2.76  3.16  3.64  4.34
   2.14  2.15  2.20  2.26  2.34  2.43  2.55  2.71  2.95  3.21  3.57  4.06  4.74  5.71
   2.58  2.62  2.64  2.76  2.89  3.04  3.23  3.47  3.79  4.15  4.58  5.23  6.31  7.75
   2.89  2.95  3.00  3.10  3.28  3.50  3.68  4.02  4.41  4.84  5.51  6.24  7.34  9.28];

% n = 500, eps = 1.5 (c1 ~ 0.71)
quants(1:8, 1:14, 4, 3) = [...
  -2.61 -2.54 -2.54 -2.54 -2.48 -2.45 -2.44 -2.41 -2.37 -2.33 -2.29 -2.29 -2.28 -2.25
  -2.37 -2.35 -2.31 -2.30 -2.26 -2.25 -2.23 -2.22 -2.19 -2.17 -2.14 -2.12 -2.10 -2.09
  -2.01 -2.02 -2.00 -1.98 -1.98 -1.96 -1.94 -1.93 -1.91 -1.89 -1.89 -1.88 -1.87 -1.85
  -1.71 -1.71 -1.71 -1.71 -1.69 -1.69 -1.69 -1.67 -1.67 -1.66 -1.65 -1.65 -1.64 -1.63
   1.74  1.72  1.73  1.74  1.74  1.74  1.76  1.76  1.79  1.82  1.85  1.89  1.94  1.98
   2.10  2.09  2.12  2.11  2.13  2.15  2.17  2.20  2.23  2.28  2.33  2.40  2.45  2.51
   2.50  2.54  2.52  2.57  2.60  2.63  2.65  2.71  2.75  2.85  2.88  2.98  3.09  3.21
   2.80  2.86  2.86  2.94  2.93  2.99  3.04  3.05  3.14  3.25  3.33  3.46  3.53  3.68];

% n = 500, eps = 2.0 (c1 ~ 0.84)
quants(1:8, 1:14, 4, 4) = [...
  -2.65 -2.65 -2.70 -2.63 -2.62 -2.63 -2.63 -2.64 -2.60 -2.55 -2.52 -2.49 -2.46 -2.46
  -2.43 -2.43 -2.45 -2.43 -2.39 -2.40 -2.40 -2.39 -2.40 -2.37 -2.34 -2.31 -2.30 -2.28
  -2.09 -2.08 -2.08 -2.09 -2.08 -2.07 -2.07 -2.06 -2.07 -2.05 -2.04 -2.02 -2.01 -1.98
  -1.76 -1.78 -1.79 -1.79 -1.79 -1.78 -1.78 -1.78 -1.78 -1.78 -1.78 -1.76 -1.74 -1.73
   1.78  1.76  1.74  1.73  1.74  1.75  1.75  1.74  1.74  1.74  1.75  1.77  1.77  1.78
   2.13  2.14  2.14  2.13  2.12  2.11  2.10  2.10  2.12  2.15  2.16  2.18  2.19  2.20
   2.58  2.57  2.58  2.58  2.55  2.57  2.57  2.56  2.60  2.63  2.66  2.69  2.71  2.75
   2.91  2.87  2.88  2.87  2.88  2.87  2.88  2.92  2.92  2.95  2.99  2.99  3.06  3.10];

% n = 750, eps = 1.0 (c1 ~ 0.52)
quants(1:8, 1:14, 5, 2) = [...
  -2.52 -2.53 -2.49 -2.48 -2.49 -2.45 -2.44 -2.47 -2.51 -2.61 -2.78 -2.96 -3.24 -3.46
  -2.32 -2.31 -2.30 -2.28 -2.29 -2.25 -2.23 -2.28 -2.32 -2.40 -2.55 -2.76 -3.03 -3.24
  -1.98 -1.97 -1.98 -1.97 -1.96 -1.94 -1.95 -1.97 -2.04 -2.11 -2.23 -2.42 -2.66 -2.91
  -1.68 -1.69 -1.69 -1.69 -1.69 -1.69 -1.70 -1.73 -1.77 -1.83 -1.96 -2.13 -2.34 -2.59
   1.72  1.74  1.76  1.79  1.80  1.87  1.91  2.01  2.13  2.29  2.49  2.74  3.13  3.70
   2.08  2.13  2.15  2.21  2.27  2.35  2.43  2.52  2.68  2.90  3.17  3.55  4.03  4.71
   2.48  2.59  2.65  2.69  2.78  2.88  3.00  3.21  3.46  3.64  4.02  4.53  5.16  6.21
   2.78  2.91  2.99  3.09  3.16  3.27  3.50  3.73  3.94  4.22  4.70  5.22  6.06  7.30];

% n = 750, eps = 1.5 (c1 ~ 0.71)
quants(1:8, 1:14, 5, 3) = [...
  -2.57 -2.56 -2.54 -2.53 -2.50 -2.45 -2.40 -2.39 -2.36 -2.34 -2.31 -2.28 -2.26 -2.23
  -2.34 -2.32 -2.33 -2.30 -2.29 -2.27 -2.24 -2.21 -2.17 -2.16 -2.14 -2.10 -2.09 -2.07
  -2.01 -1.99 -1.98 -1.98 -1.97 -1.96 -1.95 -1.93 -1.91 -1.90 -1.87 -1.85 -1.84 -1.82
  -1.68 -1.69 -1.69 -1.69 -1.71 -1.69 -1.68 -1.67 -1.65 -1.65 -1.63 -1.61 -1.60 -1.60
   1.69  1.68  1.68  1.69  1.71  1.72  1.73  1.74  1.75  1.78  1.79  1.80  1.84  1.87
   2.02  2.03  2.03  2.06  2.07  2.11  2.12  2.17  2.19  2.23  2.25  2.28  2.32  2.39
   2.48  2.50  2.48  2.50  2.54  2.58  2.61  2.63  2.69  2.75  2.84  2.93  2.95  3.04
   2.79  2.78  2.77  2.80  2.88  2.91  2.97  3.02  3.08  3.16  3.29  3.33  3.43  3.56];

% n = 750, eps = 2.0 (c1 ~ 0.84)
quants(1:8, 1:14, 5, 4) = [...
  -2.66 -2.64 -2.61 -2.65 -2.58 -2.57 -2.55 -2.55 -2.53 -2.51 -2.51 -2.50 -2.49 -2.46
  -2.40 -2.40 -2.40 -2.40 -2.37 -2.36 -2.36 -2.36 -2.35 -2.34 -2.31 -2.30 -2.29 -2.25
  -2.03 -2.03 -2.05 -2.04 -2.03 -2.03 -2.03 -2.04 -2.04 -2.02 -2.00 -1.99 -1.97 -1.97
  -1.72 -1.72 -1.74 -1.74 -1.73 -1.73 -1.73 -1.73 -1.74 -1.72 -1.72 -1.72 -1.71 -1.70
   1.75  1.71  1.71  1.69  1.70  1.69  1.69  1.69  1.70  1.71  1.71  1.72  1.73  1.75
   2.09  2.09  2.09  2.05  2.05  2.04  2.04  2.06  2.08  2.08  2.09  2.12  2.13  2.14
   2.53  2.54  2.49  2.47  2.47  2.48  2.48  2.52  2.55  2.57  2.59  2.60  2.62  2.64
   2.86  2.81  2.77  2.78  2.75  2.73  2.77  2.80  2.88  2.88  2.91  2.95  2.97  2.99];

% n = 1000, eps = 0.5 (c1 ~ 0.28)
quants(1:8, 1:14, 6, 1) = [...
  -2.66 -2.72 -2.83 -3.17 -3.82 -4.92 -6.63 -8.03 -7.33 -6.36  -5.50  -4.79 -4.21 -3.73
  -2.44 -2.48 -2.59 -2.91 -3.51 -4.49 -6.10 -7.69 -7.15 -6.19  -5.35  -4.66 -4.10 -3.62
  -2.08 -2.11 -2.24 -2.50 -3.01 -3.90 -5.30 -7.04 -6.85 -5.97  -5.16  -4.49 -3.94 -3.49
  -1.77 -1.79 -1.92 -2.16 -2.58 -3.31 -4.59 -6.15 -6.60 -5.76  -4.99  -4.34 -3.81 -3.37
   1.87  1.95  2.10  2.36  2.83  3.81  5.53  8.69 14.19 24.40  36.34  -3.02 -2.66 -2.33
   2.26  2.37  2.54  2.88  3.46  4.69  6.81 10.86 18.53 32.76  49.56  -2.85 -2.56 -2.25
   2.72  2.86  3.12  3.54  4.31  5.75  8.59 13.50 24.27 46.74  89.25 137.59 -2.44 -2.16
   3.04  3.25  3.52  4.03  4.82  6.53  9.84 16.19 28.67 56.34 115.13 190.18 -2.25 -2.10];

% n = 1000, eps = 1.0 (c1 ~ 0.52)
quants(1:8, 1:14, 6, 2) = [...
  -2.52 -2.53 -2.47 -2.45 -2.43 -2.42 -2.40 -2.40 -2.44 -2.52 -2.65 -2.87 -3.07 -3.33
  -2.28 -2.29 -2.26 -2.22 -2.20 -2.22 -2.21 -2.21 -2.25 -2.34 -2.48 -2.63 -2.86 -3.11
  -1.96 -1.96 -1.93 -1.94 -1.92 -1.90 -1.92 -1.93 -1.97 -2.06 -2.15 -2.31 -2.51 -2.77
  -1.68 -1.67 -1.66 -1.65 -1.65 -1.65 -1.65 -1.68 -1.71 -1.80 -1.88 -2.00 -2.20 -2.44
   1.68  1.70  1.72  1.75  1.78  1.82  1.88  1.95  2.04  2.12  2.28  2.50  2.79  3.19
   2.03  2.07  2.11  2.15  2.19  2.25  2.31  2.40  2.54  2.66  2.87  3.13  3.52  4.07
   2.43  2.50  2.56  2.57  2.66  2.73  2.82  3.00  3.15  3.35  3.62  3.94  4.44  5.20
   2.74  2.78  2.86  2.89  2.99  3.13  3.25  3.39  3.64  3.84  4.25  4.63  5.21  5.94];

% n = 1000, eps = 1.5 (c1 ~ 0.71)
quants(1:8, 1:14, 6, 3) = [...
  -2.53 -2.54 -2.50 -2.50 -2.47 -2.46 -2.44 -2.41 -2.38 -2.34 -2.31 -2.27 -2.26 -2.23
  -2.33 -2.34 -2.30 -2.28 -2.27 -2.24 -2.22 -2.21 -2.19 -2.17 -2.15 -2.12 -2.09 -2.07
  -2.00 -1.99 -2.01 -1.99 -1.97 -1.95 -1.94 -1.92 -1.92 -1.91 -1.89 -1.87 -1.86 -1.83
  -1.69 -1.71 -1.71 -1.71 -1.71 -1.67 -1.67 -1.66 -1.66 -1.65 -1.63 -1.63 -1.62 -1.61
   1.68  1.69  1.68  1.68  1.70  1.71  1.73  1.73  1.74  1.75  1.77  1.79  1.81  1.83
   2.01  2.02  2.03  2.04  2.06  2.08  2.12  2.16  2.16  2.19  2.22  2.25  2.27  2.31
   2.43  2.46  2.45  2.48  2.49  2.53  2.55  2.58  2.65  2.69  2.73  2.78  2.84  2.90
   2.70  2.74  2.78  2.81  2.82  2.87  2.89  2.89  2.93  3.01  3.07  3.16  3.23  3.30];

% n = 1000, eps = 2.0 (c1 ~ 0.84)
quants(1:8, 1:14, 6, 4) = [...
  -2.70 -2.65 -2.62 -2.59 -2.58 -2.57 -2.58 -2.60 -2.56 -2.53 -2.51 -2.49 -2.47 -2.46
  -2.40 -2.41 -2.39 -2.37 -2.37 -2.37 -2.35 -2.36 -2.35 -2.33 -2.30 -2.28 -2.26 -2.25
  -2.05 -2.04 -2.04 -2.05 -2.04 -2.04 -2.05 -2.04 -2.03 -2.02 -2.00 -2.00 -1.98 -1.96
  -1.73 -1.72 -1.74 -1.74 -1.73 -1.74 -1.74 -1.74 -1.73 -1.72 -1.73 -1.73 -1.71 -1.71
   1.70  1.68  1.69  1.69  1.69  1.69  1.69  1.69  1.69  1.70  1.71  1.72  1.73  1.73
   2.04  2.04  2.04  2.05  2.04  2.04  2.06  2.07  2.07  2.07  2.08  2.08  2.09  2.11
   2.45  2.47  2.45  2.47  2.46  2.47  2.51  2.51  2.50  2.52  2.52  2.55  2.57  2.58
   2.77  2.76  2.80  2.79  2.77  2.76  2.80  2.82  2.83  2.85  2.88  2.87  2.90  2.93];

% n = 2500, eps = 0.5 (c1 ~ 0.28)
quants(1:8, 1:14, 7, 1) = [...
  -2.51 -2.53 -2.59 -2.74 -3.00 -3.67 -4.78 -6.51 -8.74 -8.85 -7.73  -6.77  -5.93  -5.24
  -2.31 -2.33 -2.37 -2.55 -2.77 -3.35 -4.41 -6.00 -8.28 -8.68 -7.63  -6.66  -5.84  -5.16
  -1.97 -1.99 -2.06 -2.16 -2.39 -2.89 -3.82 -5.23 -7.22 -8.41 -7.45  -6.51  -5.71  -5.05
  -1.69 -1.70 -1.74 -1.85 -2.04 -2.46 -3.21 -4.52 -6.35 -8.19 -7.29  -6.38  -5.60  -4.93
   1.73  1.75  1.79  1.94  2.18  2.66  3.53  5.18  8.20 13.75 24.50  32.59  -4.41  -3.95
   2.07  2.11  2.21  2.36  2.68  3.19  4.27  6.32 10.24 17.79 31.87  60.12  95.19  -3.85
   2.45  2.54  2.69  2.93  3.22  3.93  5.28  7.70 12.65 22.49 43.30  83.54 131.83  -3.66
   2.78  2.85  3.02  3.26  3.60  4.36  5.93  8.69 14.35 26.23 52.23 105.70 216.13 339.65];

% n = 2500, eps = 1.0 (c1 ~ 0.52)
quants(1:8, 1:14, 7, 2) = [...
  -2.58 -2.52 -2.47 -2.46 -2.42 -2.40 -2.40 -2.38 -2.42 -2.44 -2.47 -2.50 -2.61 -2.81
  -2.35 -2.27 -2.26 -2.24 -2.22 -2.20 -2.20 -2.22 -2.22 -2.22 -2.25 -2.31 -2.41 -2.63
  -1.97 -1.94 -1.91 -1.91 -1.90 -1.91 -1.90 -1.89 -1.90 -1.92 -1.97 -2.02 -2.11 -2.29
  -1.66 -1.66 -1.64 -1.63 -1.63 -1.64 -1.64 -1.63 -1.62 -1.65 -1.68 -1.74 -1.83 -1.99
   1.67  1.70  1.69  1.71  1.75  1.76  1.78  1.79  1.83  1.88  1.95  2.07  2.21  2.39
   2.02  2.06  2.08  2.10  2.12  2.16  2.18  2.20  2.24  2.31  2.39  2.53  2.72  2.97
   2.47  2.50  2.53  2.55  2.54  2.57  2.66  2.74  2.83  2.88  3.02  3.17  3.35  3.77
   2.78  2.77  2.81  2.88  2.89  2.91  3.04  3.13  3.23  3.25  3.43  3.68  3.99  4.31];

% n = 2500, eps = 1.5 (c1 ~ 0.71)
quants(1:8, 1:14, 7, 3) = [...
  -2.55 -2.54 -2.53 -2.49 -2.50 -2.45 -2.43 -2.42 -2.40 -2.40 -2.39 -2.36 -2.36 -2.32
  -2.34 -2.32 -2.31 -2.29 -2.27 -2.23 -2.21 -2.22 -2.20 -2.18 -2.16 -2.16 -2.15 -2.13
  -1.98 -1.96 -1.95 -1.94 -1.94 -1.92 -1.92 -1.92 -1.90 -1.89 -1.88 -1.88 -1.86 -1.84
  -1.65 -1.66 -1.66 -1.64 -1.64 -1.63 -1.64 -1.64 -1.63 -1.63 -1.63 -1.62 -1.61 -1.59
   1.60  1.60  1.61  1.63  1.64  1.64  1.65  1.67  1.67  1.68  1.69  1.71  1.72  1.75
   1.95  1.91  1.92  1.93  1.95  1.98  2.00  2.01  2.02  2.04  2.07  2.11  2.14  2.17
   2.36  2.34  2.34  2.34  2.32  2.35  2.36  2.38  2.38  2.42  2.47  2.54  2.58  2.63
   2.59  2.60  2.56  2.61  2.58  2.59  2.62  2.67  2.73  2.74  2.77  2.80  2.84  2.89];

% n = 2500, eps = 2.0 (c1 ~ 0.84)
quants(1:8, 1:14, 7, 4) = [...
  -2.56 -2.57 -2.55 -2.54 -2.56 -2.56 -2.54 -2.53 -2.51 -2.51 -2.50 -2.47 -2.46 -2.44
  -2.33 -2.33 -2.32 -2.30 -2.32 -2.33 -2.31 -2.29 -2.28 -2.25 -2.26 -2.26 -2.24 -2.24
  -1.96 -2.00 -2.00 -1.99 -1.98 -1.98 -1.98 -1.98 -1.96 -1.96 -1.95 -1.94 -1.92 -1.91
  -1.67 -1.70 -1.70 -1.70 -1.70 -1.70 -1.71 -1.71 -1.71 -1.69 -1.68 -1.67 -1.66 -1.65
   1.69  1.70  1.69  1.68  1.68  1.69  1.69  1.70  1.71  1.71  1.71  1.70  1.71  1.72
   2.06  2.08  2.03  2.01  2.03  2.03  2.04  2.07  2.07  2.07  2.06  2.07  2.07  2.07
   2.47  2.44  2.45  2.45  2.44  2.42  2.45  2.42  2.44  2.45  2.45  2.47  2.51  2.49
   2.72  2.71  2.76  2.74  2.74  2.69  2.70  2.71  2.70  2.72  2.76  2.81  2.78  2.79];

%%%%%%%%%%%%%%%%%%%%%%%%%% Look-up of the appropriate quantiles %%%%%%%%%%%%%%%%%%%%%%%%%%

% Determine in between which two sample sizes tabulated the current n lies:
lower =     sum(n >= ncases);
upper = 8 - sum(n <= ncases);

% Fix some special cases; for samples of less than 50, use the values for 50:
if lower == 0
   lower = 1;

% and since there are no tabulated values for n = 750, eps = 0.5, reference to the
% corresponding part of the quantile table must be avoided:
elseif eps == 0.5
   if lower == 5
      lower = 4;
   end
   if upper == 5
      upper = 6;
   end
end

% Determine the significance level in turn for each BDS statistic contained in W:
for i = 1 : length(w)

   % Find the eight quantile values each for the lower and upper sample sizes:
   lowerqus    = reshape(quants(1:8, m(i)-1, lower, eps*2), 8, 1);

   if n <= 2500
      upperqus = reshape(quants(1:8, m(i)-1, upper, eps*2), 8, 1);
         
   else % i.e. approaching standard normality:
      upperqus = [norminv(siglevels(1:4)) norminv(1 - siglevels(6:9))]';
      ncases   = [ncases 5000];
   end

   % Interpolate the quantile values for the actual sample size from the quantile
   % values of the surrounding sample sizes; note that this method may slightly
   % increase the size of a type I error for sample sizes which are not close to one
   % of the tabulated cases; this problem could be mitigated by a response surface
   % yet to be developed.
   if lower ~= upper
      qus = lowerqus + (upperqus - lowerqus) * (n - ncases(lower)) /...
                                                      (ncases(upper) - ncases(lower));
   else
      qus = lowerqus;
   end
      
   % Find the matching significance levels; at least one of the terms must be 1, or
   % both, so their product yields the overall one-sided significance level:
   sig(i) = siglevels(5 - sum(w(i)<=qus(1:4))) * siglevels(5 + sum(w(i)>=qus(5:8)));
end

%%%%%%%%%%%%%%%%%%%%%%%%% Otherwise use standard-normal look-up %%%%%%%%%%%%%%%%%%%%%%%%%

else
   qus = [norminv(siglevels(1:4)) norminv(1 - siglevels(6:9))]';
   for i = 1 : length(w)
      sig(i) = siglevels(5 - sum(w(i)<=qus(1:4))) * siglevels(5 + sum(w(i)>=qus(5:8)));
   end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% REFERENCES:
%
% Kanzler, Ludwig (1999), "Very Fast and Correctly Sized Estimation of the BDS Statistic",
%    Oxford University, Department of Economics, working paper, available on
%    http://www2.gol.com/users/kanzler


% End of file.
