-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path151_gev_p12_makederivs.R
More file actions
243 lines (226 loc) · 7.23 KB
/
Copy path151_gev_p12_makederivs.R
File metadata and controls
243 lines (226 loc) · 7.23 KB
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
243
#
# make derivative codes for fitdistcp, one model at a time
#
setwd(paste(Sys.getenv('HOME'),'/97 MyRpackages/fitdistcp/R',sep=""))
library(Deriv)
#
f=function(x,t1,t2,v1,v2,v3,v4,v5){
(exp(-v3-v4*t2))*((1+v5*((x-v1-v2*t1)*exp(-v3-v4*t2)))^(-1/v5-1))*exp(-((1+v5*(x-v1-v2*t1)*exp(-v3-v4*t2))^(-1/v5)))
}
compare("d",extraDistr::dgev(1,4+5*2,exp(.6+.7*3),0.1),f(1,2,3,4,5,.6,.7,0.1))
gev_p12_fd=Deriv(f,c("v1","v2","v3","v4","v5"),nderiv=1)
gev_p12_fdd=Deriv(f,c("v1","v2","v3","v4","v5"),nderiv=2)
#
p=function(x,t1,t2,v1,v2,v3,v4,v5){
exp(-((1+v5*(x-v1-v2*t1)*exp(-v3-v4*t2))^(-1/v5)))
}
compare("p",extraDistr::pgev(1,4+5*2,exp(.6+.7*3),0.1),p(1,2,3,4,5,.6,.7,0.1))
gev_p12_pd=Deriv(p,c("v1","v2","v3","v4","v5"),nderiv=1)
gev_p12_pdd=Deriv(p,c("v1","v2","v3","v4","v5"),nderiv=2)
#
logf=function(x,t1,t2,v1,v2,v3,v4,v5){
-v3-v4*t2-(1+1/v5)*log(1+v5*(x-v1-v2*t1)*exp(-v3-v4*t2))-(1+v5*(x-v1-v2*t1)*exp(-v3-v4*t2))^(-1/v5)
}
compare("l",extraDistr::dgev(1,4+5*2,exp(.6+.7*3),0.1,log=TRUE),logf(1,2,3,4,5,.6,.7,0.1))
gev_p12_logfdd=Deriv(logf,c("v1","v2","v3","v4","v5"),nderiv=2)
gev_p12_logfddd=Deriv(logf,c("v1","v2","v3","v4","v5"),nderiv=3)
#
sink("151c_gev_p12_derivs.R")
#
cat("######################################################################\n")
cat("#' First derivative of the density\n")
cat("#' Created by Stephen Jewson\n")
cat("#' using Deriv() by Andrew Clausen and Serguei Sokol\n")
cat("#' @returns Vector\n")
cat("#' @inheritParams manf\n")
cat("gev_p12_fd=")
print.function(gev_p12_fd)
cat("######################################################################\n")
cat("#' Second derivative of the density\n")
cat("#' Created by Stephen Jewson\n")
cat("#' using Deriv() by Andrew Clausen and Serguei Sokol\n")
cat("#' @returns Matrix\n")
cat("#' @inheritParams manf\n")
cat("gev_p12_fdd=")
print.function(gev_p12_fdd)
cat("######################################################################\n")
cat("#' First derivative of the cdf\n")
cat("#' Created by Stephen Jewson\n")
cat("#' using Deriv() by Andrew Clausen and Serguei Sokol\n")
cat("#' @returns Vector\n")
cat("#' @inheritParams manf\n")
cat("gev_p12_pd=")
print.function(gev_p12_pd)
cat("######################################################################\n")
cat("#' Second derivative of the cdf\n")
cat("#' Created by Stephen Jewson\n")
cat("#' using Deriv() by Andrew Clausen and Serguei Sokol\n")
cat("#' @returns Matrix\n")
cat("#' @inheritParams manf\n")
cat("gev_p12_pdd=")
print.function(gev_p12_pdd)
cat("############################################################\n")
cat("#' Second derivative of the log density\n")
cat("#' Created by Stephen Jewson\n")
cat("#' using Deriv() by Andrew Clausen and Serguei Sokol\n")
cat("#' @returns Matrix\n")
cat("#' @inheritParams manf\n")
cat("gev_p12_logfdd=")
print.function(gev_p12_logfdd)
cat("############################################################\n")
cat("#' Third derivative of the log density\n")
cat("#' Created by Stephen Jewson\n")
cat("#' using Deriv() by Andrew Clausen and Serguei Sokol\n")
cat("#' @returns 3d array\n")
cat("#' @inheritParams manf\n")
cat("gev_p12_logfddd=")
print.function(gev_p12_logfddd)
cat("############################################################\n")
#
cat("#' The first derivative of the density for DMGS\n")
cat("#' @returns Vector\n")
cat("#' @inheritParams manf\n")
cat(
"gev_p12_f1fa=function(x,t01,t02,v1,v2,v3,v4,v5){
v3=movexiawayfromzero(v3)
vf=Vectorize(gev_p12_fd,\"x\")
f1=vf(x,t01,t02,v1,v2,v3,v4,v5)
return(f1)
}\n"
)
cat("############################################################\n")
#
cat("#' The first derivative of the density for WAIC\n")
cat("#' @returns Vector\n")
cat("#' @inheritParams manf\n")
cat(
"gev_p12_f1fw=function(x,t1,t2,v1,v2,v3,v4,v5){
v3=movexiawayfromzero(v3)
vf=Vectorize(gev_p12_fd,c(\"x\",\"t1\",\"t2\"))
f1=vf(x,t1,t2,v1,v2,v3,v4,v5)
return(f1)
}\n"
)
cat("############################################################\n")
#
cat("#' The second derivative of the density for DMGS\n")
cat("#' @returns Matrix\n")
cat("#' @inheritParams manf\n")
cat(
"gev_p12_f2fa=function(x,t01,t02,v1,v2,v3,v4,v5){
nx=length(x)
v3=movexiawayfromzero(v3)
vf=Vectorize(gev_p12_fdd,\"x\")
temp1=vf(x,t01,t02,v1,v2,v3,v4,v5)
f2=deriv_copyfdd(temp1,nx,dim=5)
return(f2)
}\n"
)
cat("############################################################\n")
#
cat("#' The second derivative of the density for WAIC\n")
cat("#' @returns Matrix\n")
cat("#' @inheritParams manf\n")
cat(
"gev_p12_f2fw=function(x,t1,t2,v1,v2,v3,v4,v5){
nx=length(x)
v3=movexiawayfromzero(v3)
vf=Vectorize(gev_p12_fdd,c(\"x\",\"t1\",\"t2\"))
temp1=vf(x,t1,t2,v1,v2,v3,v4,v5)
f2=deriv_copyfdd(temp1,nx,dim=5)
return(f2)
}\n"
)
###cat("############################################################\n")
####
###cat("#' The first derivative of the cdf\n")
###cat("#' @inheritParams manf\n")
###cat(
###"gev_p12_p1fa=function(x,t01,t02,v1,v2,v3,v4,v5){
###
### v3=movexiawayfromzero(v3)
###
### vf=Vectorize(gev_p12_pd,\"x\")
### p1=vf(x,t01,t02,v1,v2,v3,v4,v5)
### return(p1)
###}\n"
###)
###cat("############################################################\n")
####
###cat("#' The second derivative of the cdf\n")
###cat("#' @inheritParams manf\n")
###cat(
###"gev_p12_p2fa=function(x,t01,t02,v1,v2,v3,v4,v5){
### nx=length(x)
###
### v3=movexiawayfromzero(v3)
###
### vf=Vectorize(gev_p12_pdd,\"x\")
### temp1=vf(x,t01,t02,v1,v2,v3,v4,v5)
### p2=deriv_copyfdd(temp1,nx,dim=5)
### return(p2)
###}\n"
###)
cat("############################################################\n")
#
cat("#' Minus the first derivative of the cdf, at alpha\n")
cat("#' @returns Vector\n")
cat("#' @inheritParams manf\n")
cat(
"gev_p12_mu1fa=function(alpha,t01,t02,v1,v2,v3,v4,v5){
x=qgev((1-alpha),mu=v1+v2*t01,sigma=exp(v3+v4*t02),xi=v5)
v3=movexiawayfromzero(v3)
vf=Vectorize(gev_p12_pd,\"x\")
mu1=-vf(x,t01,t02,v1,v2,v3,v4,v5)
return(mu1)
}\n"
)
cat("############################################################\n")
#
cat("#' Minus the second derivative of the cdf, at alpha\n")
cat("#' @returns Matrix\n")
cat("#' @inheritParams manf\n")
cat(
"gev_p12_mu2fa=function(alpha,t01,t02,v1,v2,v3,v4,v5){
x=qgev((1-alpha),mu=v1+v2*t01,sigma=exp(v3+v4*t02),xi=v5)
nx=length(x)
v3=movexiawayfromzero(v3)
vf=Vectorize(gev_p12_pdd,\"x\")
temp1=vf(x,t01,t02,v1,v2,v3,v4,v5)
mu2=-deriv_copyfdd(temp1,nx,dim=5)
return(mu2)
}\n"
)
cat("############################################################\n")
#
cat("#' The second derivative of the normalized log-likelihood\n")
cat("#' @returns Matrix\n")
cat("#' @inheritParams manf\n")
cat(
"gev_p12_ldda=function(x,t1,t2,v1,v2,v3,v4,v5){
nx=length(x)
v3=movexiawayfromzero(v3)
vf=Vectorize(gev_p12_logfdd,c(\"x\",\"t1\",\"t2\"))
temp1=vf(x,t1,t2,v1,v2,v3,v4,v5)
ldd=deriv_copyldd(temp1,nx,dim=5)
return(ldd)
}\n"
)
cat("############################################################\n")
#
cat("#' The third derivative of the normalized log-likelihood\n")
cat("#' @returns 3d array\n")
cat("#' @inheritParams manf\n")
cat(
"gev_p12_lddda=function(x,t1,t2,v1,v2,v3,v4,v5){
nx=length(x)
v3=movexiawayfromzero(v3)
vf=Vectorize(gev_p12_logfddd,c(\"x\",\"t1\",\"t2\"))
temp1=vf(x,t1,t2,v1,v2,v3,v4,v5)
lddd=deriv_copylddd(temp1,nx,dim=5)
return(lddd)
}\n"
)
#
closeAllConnections()
setwd(paste(Sys.getenv('HOME'),'/03 pn/05 statistics/fitdistcp/dmgsderivs/',sep=""))