8
Introduction to AD Model Builder Anders Nielsen [email protected]

Anders Nielsen AD Model-Builder

Embed Size (px)

Citation preview

Page 1: Anders Nielsen AD Model-Builder

Introduction to AD Model Builder

Anders Nielsen

[email protected]

Page 2: Anders Nielsen AD Model-Builder

What is AD Model Builder

� Tool for development and optimization of nonlinear models

� Structured template for C++ programming

� Support library containing:

– A quasi-Newton minimizer aided by automatic differentiation

– Data objects (number, vector, matrix, 3darray,

ragged arrays, strings, ...)

– Model parameters (unbounded, bounded, fixed, summing to zero,

vectors of, matrix of, ...)

– A simple way to set up optimization in phases

– Standard ways to quantify uncertainty (Hessian based delta method, profile like-

lihood, MCMC sampling)

– Tools for random effects (AD aided Laplace approximation, sparse ma-

trix, importance sampling, ...)

– Lots of helper functions (gammaln, choleski decomp, inv, det, eigenvalues, RNG,

... )

Page 3: Anders Nielsen AD Model-Builder

What is it used for?

� Not enough

� Our web-site lists (at my last count):

— 110 peer reviewed publications based on AD Model Builder applications

— 18 Theses and Dissertations

— Countless reports and fish stock assessments

� Those are only the users who remembered to report back

� Models in fisheries science are typically:

— Non-standard

— Non-linear

— High dimensional

� Often they also contain random effects

� They should preferably run in minutes

Dave Fournier receiving the AFS Ricker award

Page 4: Anders Nielsen AD Model-Builder

Quick example

� Assume that these 15 numbers follow a negative binomial distribution:# Number of observations15

# Observations13 5 28 28 15 4 13 4 10 17 11 13 12 17 3

� Estimate the two unknown parameters.

� Entire AD Model Builder Program:

DATA_SECTIONinit_int Ninit_vector X(1,N);

PARAMETER_SECTIONinit_number logsize;init_bounded_number p(0,1);sdreport_number size;objective_function_value nll;

PROCEDURE_SECTIONsize=exp(logsize);nll=-sum(gammln(X+size))+N*gammln(size)+

sum(gammln(X+1.0))-N*size*log(p)-sum(X)*log(1.0-p);

� Compile: <filename>.tplmakeadm−→ <filename>.cpp

g++−→ binaryindex name value std dev

1 logsize 1.3017e+00 4.7101e-012 p 2.2218e-01 8.5571e-023 size 3.6754e+00 1.7312e+00

Page 5: Anders Nielsen AD Model-Builder

A fisheries catch-at-age model

� Data is the yearly catch in 7 age classes and 45 years Cay, and a yearly index of fishing

effort ey

� The model has 107 model parameters to be estimated (small for an assessment model)

� The model is non-linear

� In AD Model Builder that takes 0.3 seconds to optimize and compute and write all

output

Page 6: Anders Nielsen AD Model-Builder

Random effects — no problemDATA_SECTION

init_int Ninit_vector Y(1,N)

PARAMETER_SECTIONinit_number logr0init_number logthetainit_bounded_number logK(4.6,7.6)init_number logQinit_number logR

random_effects_vector X(1,N);

sdreport_number r0sdreport_number thetasdreport_number Ksdreport_number Qsdreport_number R

objective_function_value jnll

PROCEDURE_SECTIONfor(int i=2; i<=N; ++i){

step(X(i-1),X(i),logr0,logK,logtheta,logQ);}for(int i=1; i<=N; ++i){

obs(X(i),logR,i);}r0=exp(logr0); theta=exp(logtheta); K=exp(logK); Q=exp(logQ); R=exp(logR);

SEPARABLE_FUNCTION void step(const dvariable& x1, const dvariable& x2, const dvariable& logr0, const dvariable& logK, const dvariable& logtheta, const dvariable& logQ)dvariable var=exp(logQ);dvariable m=x1 + exp(logr0) * (1.0 - pow(exp(x1)/exp(logK),exp(logtheta)));jnll+=0.5*(log(2.0*M_PI*var)+square(x2-m)/var);

SEPARABLE_FUNCTION void obs(const dvariable& x, const dvariable& logR, int i)dvariable var=exp(logR);jnll+=0.5*(log(2.0*M_PI*var)+square(x-Y(i))/var);

A theta logistic population model is defined

for the log-transformed population size as a

nonlinear function of its previous size in the

following way:

Xt = Xt−1 + r0

(1−

(exp(Xt−1)

K

)θ)+ et,

Yt = Xt + ut,

where et ∼ N(0, Q) and ut ∼ N(0, R).