File: runitNonLinTests.R

package info (click to toggle)
fnonlinear 260.72-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 240 kB
  • ctags: 47
  • sloc: ansic: 529; makefile: 13
file content (172 lines) | stat: -rwxr-xr-x 5,450 bytes parent folder | download | duplicates (7)
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

# 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

# Copyrights (C)
# for this R-port: 
#   1999 - 2007, Diethelm Wuertz, GPL
#   Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
#   info@rmetrics.org
#   www.rmetrics.org
# for the code accessed (or partly included) from other R-ports:
#   see R's copyright and license files
# for the code accessed (or partly included) from contributed R-ports
# and other sources
#   see Rmetrics's copyright file


################################################################################
# FUNCTION:                 DESCRIPION:
#  tsTest                    Time Series Test Suite
# FUNCTION:                 DEPENDENCY TEST:
#  bdsTest                   Brock-Dechert-Scheinkman test for iid series
# FUNCTION:                 NONLINEARITY TESTS:
#  wnnTest                   White Neural Network Test for Nonlinearity
#  tnnTest                   Teraesvirta Neural Network Test for Nonlinearity
################################################################################


test.tsSuite = 
function()
{  
    # NA

    # Return Value:
    return()    
}


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


test.bdsTest = 
function()
{    
    # iid example:
    RNGkind(kind = "Marsaglia-Multicarry", normal.kind = "Inversion")
    set.seed(4711, kind = "Marsaglia-Multicarry")
    x = rnorm(100)
    plot(x, type = "l", col = "steelblue")
    test = bdsTest(x)
    print(test)
    p.value = as.vector(test@test$p.value)
    # Is each of the 8 p.values greater 0.1?
    checkEqualsNumeric(sum(p.value > 0.1), 8)
    
    # Not identically distributed:
    RNGkind(kind = "Marsaglia-Multicarry", normal.kind = "Inversion")
    set.seed(4711, kind = "Marsaglia-Multicarry")
    x = c(rnorm(50), runif(50))
    test = bdsTest(x)
    print(test)
    p.value = as.vector(test@test$p.value)
    # Is each of the 8 p.values smaller 1e-3?
    checkEqualsNumeric(sum(p.value < 1e-3), 8)
    
    # Not independent:
    RNGkind(kind = "Marsaglia-Multicarry", normal.kind = "Inversion")
    set.seed(4711, kind = "Marsaglia-Multicarry")
    n = 500
    x = rep(0, times = n)
    for(i in (2:n)) x[i] = 0.4*x[i-1] + tanh(x[i-1]) + rnorm(1, sd = 0.5)
    plot(x, type = "l", col = "steelblue")
    test = bdsTest(x)
    print(test)
    p.value = as.vector(test@test$p.value)
    # Is each of the 8 p.values smaller 1e-6?
    checkEqualsNumeric(sum(p.value < 1e-6), 8)
    

    # Return Value:
    return()    
}


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


test.wnnTest = 
function()
{    
    # White NN Test:

    # See tseries Package:
    RNGkind(kind = "Marsaglia-Multicarry", normal.kind = "Inversion")
    set.seed(4711, kind = "Marsaglia-Multicarry")
    x = runif(1000, -1, 1)   
    plot(x, type = "l", col = "steelblue")
    test = wnnTest(x)
    print(test)
    p.value = as.vector(test@test$p.value)
    # Is each of the two p.values greater 0.5?
    checkTrue(as.logical(mean(p.value > 0.5)))

    ## Generate time series which is nonlinear in ``mean''
    RNGkind(kind = "Marsaglia-Multicarry", normal.kind = "Inversion")
    set.seed(4711, kind = "Marsaglia-Multicarry")
    n = 1000
    x = rep(0, times = n)
    for(i in (2:n)) x[i] <- 0.4*x[i-1] + tanh(x[i-1]) + rnorm(1, sd = 0.5)
    plot(x, type = "l", col = "steelblue")
    test = wnnTest(x)
    print(test)
    p.value = as.vector(test@test$p.value)
    # Is each of the two p.values smaller than 1e-4?
    checkTrue(as.logical(mean(p.value < 1e-4)))
    
    # Return Value:
    return()    
}


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


test.tnnTest = 
function()
{   
    # Teraesvirta NN Test:

    # See example from tseries Package:
    RNGkind(kind = "Marsaglia-Multicarry", normal.kind = "Inversion")
    set.seed(4711, kind = "Marsaglia-Multicarry")
    x = runif(1000, -1, 1)   
    plot(x, type = "l", col = "steelblue")
    test = tnnTest(x)
    print(test)
    p.value = as.vector(test@test$p.value)
    # Is each of the two p.values greater 0.5?
    checkTrue(as.logical(mean(p.value > 0.5)))
   
    ## Generate time series which is nonlinear in ``mean''
    RNGkind(kind = "Marsaglia-Multicarry", normal.kind = "Inversion")
    set.seed(4711, kind = "Marsaglia-Multicarry")
    n = 1000
    x = rep(0, times = n)
    for(i in (2:n)) x[i] <- 0.4*x[i-1] + tanh(x[i-1]) + rnorm(1, sd = 0.5)
    plot(x, type = "l", col = "steelblue")
    test = tnnTest(x)
    print(test)
    p.value = as.vector(test@test$p.value)
    # Is each of the two p.values smaller than 1e-4?
    checkTrue(as.logical(mean(p.value < 1e-4)))
    
    # Return Value:
    return()    
}


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