File: NelderMeadErrorTesting.R

package info (click to toggle)
r-cran-openmx 2.21.13%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,716 kB
  • sloc: cpp: 36,559; ansic: 13,821; fortran: 2,001; sh: 1,440; python: 350; perl: 21; makefile: 11
file content (228 lines) | stat: -rw-r--r-- 7,796 bytes parent folder | download
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
#
#   Copyright 2007-2019 by the individuals mentioned in the source code history
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
# 
#        http://www.apache.org/licenses/LICENSE-2.0
# 
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

library(OpenMx)
library(testthat)

#This test script doesn't use any of the GD optimizers, so there's no reason to run it when all 3 are the default:
if(mxOption(NULL,"Default optimizer")!="SLSQP"){stop("SKIP")}


expect_error(
	mxComputeNelderMead(unrecognizedArgument=3),
	"mxComputeNelderMead does not accept"
)

omxCheckError(
	mxComputeNelderMead(nudgeZeroStarts="foo"),
	"unrecognized character string provided as argument 'nudgeZeroStarts'"
)

omxCheckError(
	mxComputeNelderMead(alpha=-1),
	"reflection coefficient 'alpha' must be positive"
)

omxCheckError(
	mxComputeNelderMead(betao=-1),
	"contraction coefficients 'betao' and 'betai' must both be within unit interval (0,1)"
)
omxCheckError(
	mxComputeNelderMead(betao=2),
	"contraction coefficients 'betao' and 'betai' must both be within unit interval (0,1)"
)
omxCheckError(
	mxComputeNelderMead(betai=-1),
	"contraction coefficients 'betao' and 'betai' must both be within unit interval (0,1)"
)
omxCheckError(
	mxComputeNelderMead(betai=2),
	"contraction coefficients 'betao' and 'betai' must both be within unit interval (0,1)"
)

omxCheckError(
	mxComputeNelderMead(alpha=1,gamma=0.9),
	"if positive, expansion coefficient 'gamma' must be greater than reflection coefficient 'alpha'"
)

omxCheckError(
	mxComputeNelderMead(sigma=1),
	"shrink coefficient 'sigma' must be less than 1.0"
)

omxCheckError(
	mxComputeNelderMead(iniSimplexType="foo"),
	"'foo' should be one of 'regular', 'right', 'smartRight', and 'random'"
)

omxCheckError(
	mxComputeNelderMead(degenLimit=-1),
	"'degenLimit' must be within interval [0,pi]"
)
omxCheckError(
	mxComputeNelderMead(degenLimit=-1),
	"'degenLimit' must be within interval [0,pi]"
)

omxCheckError(
	mxComputeNelderMead(stagnCtrl=10),
	"'stagnCtrl' must be an integer vector of length 2"
)

omxCheckError(
	mxComputeNelderMead(ineqConstraintMthd="foo"),
	"'foo' should be one of 'soft' and 'eqMthd'"
)
omxCheckError(
	mxComputeNelderMead(eqConstraintMthd="foo"),
	"'foo' should be one of 'GDsearch', 'soft', 'backtrack', and 'l1p'"
)

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

set.seed(1611150)
x <- matrix(rnorm(1000,sd=2))
colnames(x) <- "x"
m <- mxModel(
	"mod",
	mxData(observed=x,type="raw"),
	mxMatrix(type="Full",nrow=1,ncol=1,free=T,values=0,labels="mu",name="Mu"),
	mxMatrix(type="Full",nrow=1,ncol=1,free=T,values=4,labels="sigma2",name="Sigma2",lbound=0.0001),
	mxExpectationNormal(covariance="Sigma2",means="Mu",dimnames=c("x")),
	mxAlgebra(sqrt(Sigma2),name="Sigma"),
	mxFitFunctionML()
)
plan <- omxDefaultComputePlan()
plan$steps$GD <- mxComputeNelderMead()
plan2 <- plan

plan$steps$GD$alpha <- -1
omxCheckError(mxRun(mxModel(m,plan)), "reflection coefficient 'alpha' must be positive")
plan <- plan2

plan$steps$GD$betao <- -1
omxCheckError(mxRun(mxModel(m,plan)), "contraction coefficients 'betao' and 'betai' must both be within unit interval (0,1)")
plan <- plan2

plan$steps$GD$betai <- 1
omxCheckError(mxRun(mxModel(m,plan)), "contraction coefficients 'betao' and 'betai' must both be within unit interval (0,1)")
plan <- plan2

plan$steps$GD$gamma <- 0.9
omxCheckError(mxRun(mxModel(m,plan)), "if positive, expansion coefficient 'gamma' must be greater than reflection coefficient 'alpha'")
plan <- plan2

plan$steps$GD$iniSimplexType <- "foo"
omxCheckError(mxRun(mxModel(m,plan)), "unrecognized character string provided for Nelder-Mead 'iniSimplexType'")
plan <- plan2

plan$steps$GD$degenLimit <- -1
omxCheckError(mxRun(mxModel(m,plan)), "'degenLimit' must ge within interval [0,pi]")
plan <- plan2

plan$steps$GD$stagnCtrl <- 10L
omxCheckError(mxRun(mxModel(m,plan)), "'stagnCtrl' must be an integer vector of length 2")
plan <- plan2

plan$steps$GD$xTolProx <- -1
plan$steps$GD$fTolProx <- -1
omxCheckWarning(mxRun(mxModel(m,plan)), "both 'xTolProx' and 'fTolProx' are non-positive; 'fTolProx' will be assigned a value of 1e-14")
plan <- plan2

plan$steps$GD$ineqConstraintMthd <- "foo"
omxCheckError(mxRun(mxModel(m,plan)), "unrecognized character string provided for Nelder-Mead 'ineqConstraintMthd'")
plan <- plan2

plan$steps$GD$eqConstraintMthd <- "foo"
omxCheckError(mxRun(mxModel(m,plan)), "unrecognized character string provided for Nelder-Mead 'eqConstraintMthd'")
plan <- plan2

ism <- matrix(0,1,1)
plan$steps$GD$iniSimplexMat <- ism
omxCheckError(mxRun(mxModel(m,plan)), "'iniSimplexMat' has 1 columns, but 2 columns expected")
plan <- plan2

ism <- matrix(c(0,4,-1,4,0,5,1,1),4,2)
colnames(ism) <- c("mu","sigma2")
plan$steps$GD$iniSimplexMat <- ism
plan$steps$GD$fTolProx <- 1e-8
plan$steps$GD$xTolProx <- 1e-8
omxCheckWarning(mxRun(mxModel(m,plan)), "'iniSimplexMat' has 4 rows, but 3 rows expected; extraneous rows will be ignored")
plan <- plan2

ism <- matrix(c(0,4,-1,4,0,5),3,2,byrow=T)
colnames(ism) <- c("um","sigma2")
plan$steps$GD$iniSimplexMat <- ism
omxCheckError(mxRun(mxModel(m,plan)), "error in mapping column names of 'iniSimplexMat' to free-parameter labels")
plan <- plan2

ism <- matrix(c(0,4,-1,5),2,2,byrow=T)
colnames(ism) <- c("mu","sigma2")
plan$steps$GD$iniSimplexMat <- ism
plan$steps$GD$fTolProx <- 1e-8
plan$steps$GD$xTolProx <- 1e-8
omxCheckWarning(mxRun(mxModel(m,plan)), "'iniSimplexMat' has 2 rows, but 3 rows expected; omitted rows will be generated randomly")
plan <- plan2

ism <- matrix(c(2,4,1,4,3,5),3,2,byrow=T)
colnames(ism) <- c("mu","sigma2")
plan$steps$GD$iniSimplexMat <- ism
plan$steps$GD$validationRestart <- FALSE
plan$steps <- list(GD=plan$steps$GD)
omxCheckWarning(
	mxRun(mxModel(m,plan,mxConstraint(Mu<0))),
	"In model 'mod' Optimizer returned a non-zero status code 10. Starting values are not feasible. Consider mxTryHard()"
)
plan <- plan2

ism <- matrix(c(-2,4,-1,4,-3,5),3,2,byrow=T)
colnames(ism) <- c("mu","sigma2")
plan$steps$GD$iniSimplexMat <- ism
plan$steps$GD$validationRestart <- FALSE
plan$steps <- list(GD=plan$steps$GD)
omxCheckWarning(
	mxRun(mxModel(m,plan,mxConstraint(Mu>0))),
	"In model 'mod' Optimizer returned a non-zero status code 10. Starting values are not feasible. Consider mxTryHard()"
)
plan <- plan2

#Note: status code 3 is tested in models/passing/UselessConstraint.R; status code 4 is tested in models/nightly/NelderMeadUnitTesting.R.

ism <- matrix(c(0,4,-1,4,0,5),3,2,byrow=T)
plan$steps$GD$iniSimplexMat <- ism
omxCheckError(mxRun(mxModel(m,plan)), "'iniSimplexMat' has 0 column names, but 2 column names expected")
plan <- plan2

#Test that the l1p method can tolerate every point of the initial simplex violating a constraint:

ism <- matrix(c(2,4,1,4,3,5),3,2,byrow=T)
colnames(ism) <- c("mu","sigma2")
plan$steps$GD$iniSimplexMat <- ism
plan$steps$GD$validationRestart <- FALSE
plan$steps$GD$ineqConstraintMthd <- "eqMthd"
plan$steps$GD$eqConstraintMthd <- "l1p"
plan$steps <- list(GD=plan$steps$GD)
mxRun(mxModel(m,plan,mxConstraint(Mu<0)))
plan <- plan2

ism <- matrix(c(-2,4,-1,4,-3,5),3,2,byrow=T)
colnames(ism) <- c("mu","sigma2")
plan$steps$GD$iniSimplexMat <- ism
plan$steps$GD$validationRestart <- FALSE
plan$steps$GD$ineqConstraintMthd <- "eqMthd"
plan$steps$GD$eqConstraintMthd <- "l1p"
plan$steps <- list(GD=plan$steps$GD)
mxRun(mxModel(m,plan,mxConstraint(Mu>0)))
plan <- plan2