-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_tmb_manual_profiled_logdet_fd_diagnostic.sh
More file actions
executable file
·172 lines (138 loc) · 4.61 KB
/
Copy pathinstall_tmb_manual_profiled_logdet_fd_diagnostic.sh
File metadata and controls
executable file
·172 lines (138 loc) · 4.61 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
#!/usr/bin/env bash
set -euo pipefail
FILE="examples/NMFS/sefsc_red_snapper/tmb/evaluate_tmb_at_quadra_fit.R"
if [[ ! -f "$FILE" ]]; then
echo "ERROR: $FILE not found. Run from Quadra repo root."
exit 1
fi
STAMP="$(date +%Y%m%d_%H%M%S)"
BACKUP="${FILE}.before_manual_random_profiled_logdet_fd.${STAMP}"
cp "$FILE" "$BACKUP"
echo "Backed up $FILE to:"
echo " $BACKUP"
python3 - <<'PY'
from pathlib import Path
path = Path("examples/NMFS/sefsc_red_snapper/tmb/evaluate_tmb_at_quadra_fit.R")
text = path.read_text()
if "TMB manual-random-optimized profiled logdet FD" in text:
print("Manual profiled logdet FD diagnostic already installed.")
raise SystemExit(0)
append = r'''
cat("\nTMB manual-random-optimized profiled logdet FD:\n")
fixed_names <- c("log_r0", "log_fbar", "log_q", "logit_sel_a50", "log_sel_slope")
theta0 <- as.numeric(qval[fixed_names])
names(theta0) <- fixed_names
u0 <- qrec$log_rec_dev
make_joint_obj <- function(theta_vec, u_vec) {
pars <- list(
log_r0 = as.numeric(theta_vec["log_r0"]),
log_fbar = as.numeric(theta_vec["log_fbar"]),
log_q = as.numeric(theta_vec["log_q"]),
logit_sel_a50 = as.numeric(theta_vec["logit_sel_a50"]),
log_sel_slope = as.numeric(theta_vec["log_sel_slope"]),
log_rec_dev = u_vec
)
MakeADFun(
data = list(
catch_obs = catch_obs,
index_obs = index_obs,
age_comp_obs = age_comp_obs
),
parameters = pars,
DLL = "red_snapper_tmb",
silent = TRUE
)
}
profile_u_for_theta <- function(theta_vec, u_start) {
joint <- make_joint_obj(theta_vec, u_start)
full_start <- c(theta_vec, u_start)
ntheta <- length(theta_vec)
nu <- length(u_start)
fn_u <- function(u_vec) {
par <- c(theta_vec, u_vec)
joint$fn(par)
}
gr_u <- function(u_vec) {
par <- c(theta_vec, u_vec)
as.numeric(joint$gr(par)[(ntheta + 1):(ntheta + nu)])
}
opt <- nlminb(
start = u_start,
objective = fn_u,
gradient = gr_u,
control = list(
eval.max = 1000,
iter.max = 1000,
rel.tol = 1e-12,
x.tol = 1e-12
)
)
list(
u = opt$par,
objective = opt$objective,
convergence = opt$convergence,
message = opt$message,
grad_norm = sqrt(sum(gr_u(opt$par)^2)),
joint = joint
)
}
logdet_at_theta_u <- function(theta_vec, u_vec) {
# Use random-enabled TMB object only as a convenient Huu provider at fixed theta/u.
o <- make_obj_for_theta(theta_vec, u_vec)
invisible(o$fn())
H <- as.matrix(o$env$spHess(random = TRUE))
as.numeric(determinant(H, logarithm = TRUE)$modulus)
}
eps <- 1e-5
manual_profiled_logdet_fd <- numeric(length(theta0))
manual_u_fd_norm <- numeric(length(theta0))
manual_u_opt_grad_norm_plus <- numeric(length(theta0))
manual_u_opt_grad_norm_minus <- numeric(length(theta0))
manual_u_opt_conv_plus <- integer(length(theta0))
manual_u_opt_conv_minus <- integer(length(theta0))
names(manual_profiled_logdet_fd) <- fixed_names
names(manual_u_fd_norm) <- fixed_names
names(manual_u_opt_grad_norm_plus) <- fixed_names
names(manual_u_opt_grad_norm_minus) <- fixed_names
names(manual_u_opt_conv_plus) <- fixed_names
names(manual_u_opt_conv_minus) <- fixed_names
for (j in seq_along(theta0)) {
th_plus <- theta0
th_minus <- theta0
th_plus[j] <- th_plus[j] + eps
th_minus[j] <- th_minus[j] - eps
plus <- profile_u_for_theta(th_plus, u0)
minus <- profile_u_for_theta(th_minus, u0)
ld_plus <- logdet_at_theta_u(th_plus, plus$u)
ld_minus <- logdet_at_theta_u(th_minus, minus$u)
manual_profiled_logdet_fd[j] <- 0.5 * (ld_plus - ld_minus) / (2 * eps)
manual_u_fd <- (plus$u - minus$u) / (2 * eps)
manual_u_fd_norm[j] <- sqrt(sum(manual_u_fd * manual_u_fd))
manual_u_opt_grad_norm_plus[j] <- plus$grad_norm
manual_u_opt_grad_norm_minus[j] <- minus$grad_norm
manual_u_opt_conv_plus[j] <- plus$convergence
manual_u_opt_conv_minus[j] <- minus$convergence
}
cat("0.5 * manually profiled logdet FD gradient:\n")
print(manual_profiled_logdet_fd)
cat("manual profiled u FD column norms:\n")
print(manual_u_fd_norm)
cat("random optimizer convergence plus/minus:\n")
print(manual_u_opt_conv_plus)
print(manual_u_opt_conv_minus)
cat("random optimizer gradient norms plus:\n")
print(manual_u_opt_grad_norm_plus)
cat("random optimizer gradient norms minus:\n")
print(manual_u_opt_grad_norm_minus)
cat("TMB implied logdet contribution from obj$gr - joint_gr:\n")
print(implied_logdet_gr)
cat("difference: manual profiled FD - implied TMB logdet contribution:\n")
print(manual_profiled_logdet_fd - implied_logdet_gr)
'''
text = text + append
path.write_text(text)
print("Installed manual-random-optimized profiled logdet FD diagnostic.")
PY
echo
echo "Run:"
echo "Rscript $FILE"