File: test-jbTable.R

package info (click to toggle)
fbasics 4041.97-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,016 kB
  • sloc: ansic: 740; makefile: 14
file content (242 lines) | stat: -rw-r--r-- 6,523 bytes parent folder | download | duplicates (8)
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

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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 Library General Public License for more details.
#
# You should have received A copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA


################################################################################
# FUNCTION:                 DESCRIPTION:
# .jbTable                   Finite sample p values for the Jarque Bera test
# .jbPlot                    Plots probabilities
# .pjb                       Returns probabilities for JB given quantiles
# .qjb                       Returns quantiles for JB given probabilities
################################################################################


.jbTable <- 
function(type = c("LM", "ALM"), size = c("mini", "small", "all"))
{   
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Finite sample p values for the Jarque Bera test

    # Details:
    #   The function jbTable() returns a data.frame with rows denoting
    #   size and columns denoting probabilities 0 < p < 1.

    # Note:
    #   x=-3:0; y=0:3; z=outer(x,y,"*"); rownames(z)=x; colnames(z)=y; z

    # Example:
    #   .jbTable()

    # FUNCTION:

    # Check Arguments:
    type = match.arg(type)
    size = match.arg(size)

    # Create Table:
    if (type == "LM") {
        Table = t(.jbLM())
    } else if (type == "ALM") {
        Table = t(.jbALM())
    }

    # Select Size:
    if (size == "small") {
        Table = t(Table)
        n = dim(Table)[1]
        Table = Table[c(matrix(1:(n-2), byrow = TRUE, ncol = 22)[, 1], n), ]
        Table = Table[-(1:17),]
        Table = t(Table)
    } else if (size == "mini") {
        p = c(
            "0.01", "0.0251189", "0.05", "0.1",
            "0.9", "0.95", "0.9751687", "0.99")
        Table = Table[, p]
        Table = rbind(Table, qchisq(as.numeric(p), df = 2))
        Table = round(Table, 3)
        colnames(Table) = substr(colnames(Table), 1, 5)
        nRows = dim(Table)[1]
        rownames(Table)[nRows] = "Inf"
    }
    ans = list(
        x = as.numeric(rownames(Table)),
        y = as.numeric(colnames(Table)),
        z = Table)

    # Add Control:
    attr(ans, "control") <-
        c(table = "jb", type = type, size = size)

    # Return Value:
    class(ans) = "gridData"
    ans
}


# ------------------------------------------------------------------------------


.jbPlot <- 
function(type = c("LM", "ALM"))
{   
    # A function implemented by Diethelm Wuertz

    # Match Arguments:
    type = match.arg(type)

    # Load Table:
    Y = .jbTable(size = "small")
    X = cbind(expand.grid(x = Y$x, y = Y$y), z = as.vector(Y$z))
    x = X[, 1] # N
    y = X[, 3] # q-Stat
    z = X[, 2] # p-Value

    # Interpolate:
    ans = akimaInterp(log(x), log(y), z, gridPoints = 101, extrap = FALSE)
    persp(ans, theta = 40, xlab = "log(N)", ylab = "log(q)", zlab = "p",
        main = paste(type, "Jarque Bera"))

    # Return Value:
    invisible(NULL)
}



# ------------------------------------------------------------------------------


.pjb <- 
function(q, N = Inf, type = c("LM", "ALM"))
{   
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Returns probabilities for the JB Test given quantiles

    # Arguments:
    #   q = quantiles (0.020 < q < 12)
    #   N = sample sizes

    # Details:
    #   Uses "small" table for interpolation.

    # Example:
    #   q=c(0.03,1,5,9); for (N in c(4,1000,50000,Inf)) print(.pjb(q, N))

    # FUNCTION:

    # Match Arguments:
    type = match.arg(type)

    # Check N:
    stopifnot(length(N) == 1)

    # Grid Points:
    Y = .jbTable(type = type, size = "small")
    if (N < 10000) {
        # Interpolate on log-Scale:
        X = cbind(expand.grid(x = Y$x, y = Y$y), z = as.vector(Y$z))
        x = log(X[, 1]) # N
        y = log(X[, 3]) # q-Stat
        z = X[, 2] # p-Value
        # Out Points:
        xo = rep(log(N), times = length(q))
        yo = log(q)
        # Interpolate:
        p = linearInterpp(x, y, z, xo = xo, yo = yo)[, 3]
    } else if (N > 10000) {
        p = pchisq(as.numeric(q), df = 2)
    }

    # Result:
    if (N < 5) controlN = "< 5"
    if (N >= 5 & N <= 10000) controlN = as.character(N)
    if (N > 10000) controlN = "> 10000"
    if (!is.finite(N)) controlN = "Inf"
    names(p) = as.character(q)
    attr(p, "control") <- c(N = controlN)

    # Return Value:
    p
}


# ------------------------------------------------------------------------------


.qjb <- 
function(p, N = Inf, type = c("LM", "ALM"))
{   
    # A function implemented by Diethelm Wuertz

    # Description:
    #   Returns quantiles for the ADF Test given probabilities

    # Arguments:
    #   p = probabilities (0.01 < p < 0.99)
    #   N = sample sizes

    # Details:
    #   Uses "small" table for interpolation.

    # Example:
    #   p=(1:9)/10; for (N in c(4,1000,50000,Inf)) print(.qjb(p, N))

    # FUNCTION:

    # Match Arguments:
    type = match.arg(type)
    statistic = match.arg(statistic)

    # Check N:
    stopifnot(length(N) == 1)

    # Grid Points:
    Y = .jbTable(type = type, size = "small")
    if (N < 10000) {
        # Interpolate on log-Scale:
        X = cbind(expand.grid(x = Y$x, y = Y$y), z = as.vector(Y$z))
        x = log(X[, 1]) # N
        y = X[, 2] # p-Value
        z = X[, 3] # q-Stat
        # Out Points:
        xo = rep(log(N), times = length(p))
        yo = p
        # Interpolate - Scaling Required:
        xStd = sqrt(var(x))
        q = linearInterpp(x/xStd, y, z, xo = xo/xStd, yo = yo)[, 3]
    } else {
        # Asymptotic Limit:
        q = qchisq(as.numeric(p), df = 2)
    }

    # Result:
    if (N < 5) controlN = "< 5"
    if (N >= 5 & N <= 10000) controlN = as.character(N)
    if (N > 10000) controlN = "> 10000"
    if (!is.finite(N)) controlN = "Inf"
    names(q) = as.character(p)
    attr(q, "control") <- c(N = controlN)

    # Return Value:
    q
}


################################################################################