-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalize_opakapaka_nmfs_cleanup.sh
More file actions
executable file
·173 lines (123 loc) · 4.01 KB
/
Copy pathfinalize_opakapaka_nmfs_cleanup.sh
File metadata and controls
executable file
·173 lines (123 loc) · 4.01 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
#!/usr/bin/env bash
set -euo pipefail
DOC="docs/opakapaka_nmfs_reorg_and_huu_diagnostics.md"
mkdir -p docs
if [[ -f patch_opakapaka_reuse_final_huu_diagnostics.sh ]]; then
rm patch_opakapaka_reuse_final_huu_diagnostics.sh
echo "Removed temporary patch script: patch_opakapaka_reuse_final_huu_diagnostics.sh"
fi
cat > "$DOC" <<'EOF'
# Opakapaka NMFS Reorganization and Huu Diagnostic Cleanup
## Status
Completed: June 2026
The PIFSC Opakapaka assessment-style example was moved under the NMFS
assessment examples directory and its final random-effect Hessian diagnostics
were corrected.
## Directory Reorganization
The Opakapaka example was moved from:
```text
examples/pifsc_opakapaka
```
to:
```text
examples/NMFS/pifsc_opakapaka
```
This keeps fisheries assessment applications separate from smaller framework
examples.
The NMFS examples directory now contains assessment-oriented examples such as:
```text
examples/NMFS/sefsc_red_snapper
examples/NMFS/pifsc_opakapaka
```
## Build Path Updates
After the move, relative include paths were updated because the example is now
one directory deeper.
For example, includes of the form:
```cpp
#include "../../../core/..."
```
were updated to:
```cpp
#include "../../../../core/..."
```
The Opakapaka executable is built from:
```bash
clang++ -std=c++17 -g -I"external/eigen/" \
examples/NMFS/pifsc_opakapaka/quadra/opakapaka_projection.cpp \
examples/NMFS/pifsc_opakapaka/quadra/opakapaka_adgraph_global.cpp \
-o build/examples/pifsc_opakapaka
```
## Diagnostic Issue
After the move, the example built and ran, but the optimizer structure report
showed stale metadata:
```text
random effects 0
pattern available no
detected structure unknown
Hessian nonzeros 0
```
This was inconsistent with the actual Laplace evaluation, which reported:
```text
Quadra: Discovering Hessian pattern from AD graph for 20 random variables ...
Quadra: Model structure aware now => Hessian pattern has 58 entries.
```
## Root Cause
The Opakapaka example can fall back to a local safeguarded one-dimensional
`log_q` polish after an L-BFGS line-search stall. That fallback returned a valid
fit and valid random effects, but it did not preserve the optimizer pattern
metadata in `fit.pattern`.
As a result, the final report was reading stale metadata even though the fitted
random-effect vector was present.
## Fix
The example now reconstructs the final random-effect Hessian after fitting:
```cpp
const Eigen::SparseMatrix<double> Huu_final =
compute_final_random_effect_hessian(model, params, opts, fit);
```
That final Hessian is reused for:
- optimizer structure diagnostics
- Hessian nonzero reporting
- random-effect uncertainty output
This avoids relying on stale `fit.pattern` metadata when the fallback path was
used.
## Validation
After the fix, the Opakapaka example reported:
```text
random effects 20
pattern available yes
detected structure sparse
Laplace backend final Huu reconstruction
random solver Laplace mode solve
Hessian nonzeros 58
```
The example also completed the fit and projection workflow and wrote outputs to:
```text
examples/NMFS/pifsc_opakapaka/outputs
```
## Remaining Note
The example still uses a local safeguarded `log_q` fallback after an L-BFGS
line-search stall:
```text
L-BFGS line-search stall detected in Opakapaka example.
Using local safeguarded one-dimensional log_q fallback.
```
This is an optimizer robustness issue, not a structural diagnostics or
uncertainty-reporting issue. The final polished fit reports a near-zero gradient
and coherent output.
Future work can replace the local fallback with a more general optimizer
robustness improvement.
EOF
echo "Wrote:"
echo " $DOC"
echo
echo "Suggested verification:"
echo 'grep -R "examples/pifsc_opakapaka" -n . \'
echo ' --exclude-dir=.git \'
echo ' --exclude-dir=.quadra_patch_backups \'
echo ' --exclude-dir=build \'
echo ' --exclude="*.bak" \'
echo ' --exclude="*.txt"'
echo
echo "Suggested git review:"
echo " git status --short"
echo " git diff -- docs examples/NMFS core | less"