249
Minnesota AD Model Builder Short Course October 22-24, 2007 • Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous courses • QFC Supporting Partners – MSU, GLFC, Michigan DNR, Minnesota DNR, Ohio DNR, New York DEC, Illinois DNR, Ontario MNR

Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Embed Size (px)

Citation preview

Page 1: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Minnesota AD Model Builder Short CourseOctober 22-24, 2007

• Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous courses

• QFC Supporting Partners – MSU, GLFC, Michigan DNR, Minnesota DNR, Ohio DNR, New York DEC, Illinois DNR, Ontario MNR

Page 2: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Quantitative Fisheries Center (QFC)

• Created July 2005

• Co-directors: Jim Bence and Mike Jones

• Staffing: – Associate Director– Computer Programmer– Post-Docs (2)– Graduate students (3 - PhD; 3 - MS)

Page 3: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Quantitative Fisheries Center (QFC)

• Provide research, outreach, and educational services to supporting partners

• Outreach examples– Computer programming support to Michigan

DNR inland creel database– SCAA consultation for Lake Erie percid

assessments– River classifications in MI, WI, NY, PA– Power analysis for OhDNR Lake Erie gill net

surveys

Page 4: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Quantitative Fisheries Center (QFC)

• Education– AD Model Builder short courses taught in East

Lansing (2006, 2007) and Cornell Biological Field Station (2007)

– Online Maximum Likelihood Estimation course (launched October 16, 2007)

– Introduction to R short course (currently being converted to an online format)

– Online Resampling Approaches to Data Analysis course (planned for summer 2008)

Page 5: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

How this course will differ from previous offerings

• More emphasis on straightforward applications

• More hands on programming (coding the whole program rather than only bits and pieces)

• Less emphasis on coding efficiency (comes with practice)

Page 6: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

What is AD Model Builder and why should you use it?

• Auto Differentiation Model Builder• Software for creating computer programs

to estimate parameters of statistical models

Page 7: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

What are the advantages of using it?

• Fast and accurate• Flexible• Designed for general maximum likelihood

problems• Libraries for Bayesian and robust

estimation methods• Includes many advanced programming

options (estimation in phases)• Multi-dimensional arrays

Page 8: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

How fast is it? • Evaluation by Schnute and Olsen• 100 parameter catch-at-age model from Schnute

and Richards (2005)

Package msec/

Function call

Number of function calls

Time to converge

ADMB 131 291 38 seconds

Gauss 167 23,365 1.08 hours

Matlab 639 18,360 3.25 hours

S-plus n/a n/a n/a

Page 9: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Why is it so fast?• Auto differentiation – a method for

approximating derivatives to within numerical precision

• Most other computer programs actually calculate derivatives with respect to every parameter (finite differences)– Newton-Raphson – requires first and second

order derivatives– Levenberg-Marquardt – requires first order

derivatives

Page 10: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

What are some of the most noticeable differences with other

software packages?

• Users must specify the objective function to be minimized (Note: ADMB only does minimization)

Page 11: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Parameter value

O

bje

ctiv

e fu

nct

ion

Page 12: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

ADMB Differences with SASdata lenweight;input length weight;datalines;358 212360 242382 402388 285394 325...1214 125421219 15909;Run; 0

2,000

4,000

6,000

8,000

10,000

12,000

14,000

16,000

18,000

20,000

0 200 400 600 800 1,000 1,200 1,400

Length (mm)

Wei

ght

(g)

Page 13: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

ADMB Differences with SAS

proc nlin data=lenweight;parameters a=0 b=3;model weight=a*length**b;run;

Proc NLIN estimates parameters by (weighted) least squares; minimize the sum of square errors

2

1

Objective Functionn

bi

i

y a Length

Page 14: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

ADMB Differences with SAS

proc nlmixed data=lenweight;ypred=alpha*length**beta;parms alpha=0.001, beta=3, sigma=1; model weight~normal(ypred,sigma);run;

Proc NLMIXED estimates parameters by maximum likelihood

2

21

Length1Objective Function exp

22

bni

i

y a

Page 15: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

ADMB Differences with SAS

proc nlp data=lenweight tech=newrap inest=par1 outest=opar1 maxiter=1000;

parms a, b, sigma;ypred=a*Length**b;nlogl = log(sigma)+0.5*((weight-ypred)/sigma)**2;min nlogl;run;

Proc NLP (NonLinear Programming) in SAS/OR is an estimation method similar in vein to that of ADMB in that analysts must specify their objective function

Page 16: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

What are the most striking differences with other packages?• Users specify the objective function to be

minimized• Steps to running

1. Create an ADMB template2. Convert template to C++ code3. Compile – convert from programming code to machine

code (creates an executable file)4. Link the executable file to C++ libraries5. Run your executable file

• Resulting executable can be run on similar datasets on any computer

Page 17: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

What are the difficulties associated with using ADMB?

• Requires a more intimate knowledge of statistical theory (probability distributions, likelihoods, Hessians)

• Some knowledge of C++ is required

• Code can be a little quirky (as you will soon see)

Page 18: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

ADMB Files

Input.tpl – make the model.dat – input data.pin – initial values (optional; need to specify for all parameters)

Output.par – parameters estimates.cor – correlation of parameters.std – parameter estimates with std. deviations.rep – user-defined outputs (optional)

Page 19: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

ADMB Files

InputADMB will expect .dat and .pin files to have same name as .tpl

e.g., MilleLacs.tpl, MilleLacs.dat (this can be overridden)

Output• By default, output files will have same file name

e.g., MilleLacs.rep, MilleLacs.par (this can be overridden)

Note: In the project folder, ignore the files with the extra ~ on the extension…

e.g., Oneida.tpl~they are temporary files (so be sure you open the right

file).

Page 20: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

.dat file• Simply contains the data you will use when fitting your model

#Simple linear regression example#For ADMB Short Course 1, August 2007#Created by D. Fournier, modified by B. Linton

#Any text after "#" is ignored

# number of observations 10# observed Y values 1.4 4.7 5.1 8.3 9.0 14.5 14.0 13.4 19.2 18 # observed x values -1 0 1 2 3 4 5 6 7 8

Simple.dat

Page 21: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

.tpl Sections

• PARAMETER_SECTION• INITIALIZATION_SECTION

• REPORT_SECTION

Other commonly used section• PRELIMINARY_CALCS_SECTION• LOCAL_CALCS

• DATA_SECTION

Each must be written just like that

• PROCEDURE_SECTION

Page 22: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Keep in mind

• Different sections use different programming languages

– Data, Parameter, Initialization sections used ADMB code

– Procedure, Report, Local Calcs, Preliminary Calcs sections use C++ code• Lines typically must end with ; • Not absolute as in SAS (loops, conditional

statements)

Page 23: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Keep in mind

• Comments in .dat file are specified with ‘#”

• Comments in .tpl are specified with ‘//’

Page 24: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Keep in mind

• Section heads (DATA_SECTION, PARAMETER_SECTION) must be left justified– Except LOCAL_CALCS section, requires one

space before typing LOCAL_CALCS

• All other lines should have two spaces before the text

Page 25: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

.tpl Sections

Identify values that will be read-in from .dat file

Need to consider the order of numbers in your .dat file

Can read your data in as integers, real numbers, matrices, arrays,…

DATA_SECTION init_int first_year init_int last_year init_int first_age init_int last_age init_number lambda init_matrix obs_length(first_year,last_year,first_age,last_age)

• DATA_SECTION

Page 26: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

.tpl Sections

Also where you can declare your looping variable; valid throughout your entire code

DATA_SECTION init_int first_year init_int last_year init_int first_age init_int last_age init_number lambda init_matrix obs_length(first_year,last_year,first_age,last_age) int i int j

• DATA_SECTION

Page 27: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

If .dat doesn’t have the same name as .tpl• Assume program is MyModel.tpl• Then, default search is for MyModel.dat• Code below will read-in a file named ControlFile.dat:• !!ad_comm::change_datafile_name("ControlFile.dat");• Can also go back:• !!ad_comm::change_datafile_name(“MyModel.dat");

!! – tells ADMB that what follows is C++ code

.tpl Sections• DATA_SECTION

Page 28: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Always a good idea to verify that your data have been read in correctly

In .dat file, have -8888 as your last entry

In Data_section, specify init_int test as the last read in variable and type!!cout << test << endl;!!exit(99);

.tpl Sections• DATA_SECTION

Page 29: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

DATA_SECTION//Read data in from simple.dat init_int nobs //number of observations init_vector Y(1,nobs) //observed Y values init_vector x(1,nobs) //observed x values init_int test //test variable !!cout << test << endl; !!exit(99);

.tpl Sections• DATA_SECTION

Page 30: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Define Parameters – the values to be estimated (must have at least 1)- use loge scale, if only interested in non-negative parameter space- Identified by the prefix init_

Intermediary Variables - quantities that will change as a result of parameter estimation

Can also declare index variables here.

Also, if “containers” are needed just for output and not for calculations, then put those here too.

Name your Objective Function – the quantity to be minimized

• PARAMETER_SECTION• DATA_SECTION

.tpl Sections

Page 31: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

PARAMETER_SECTION//Parameters to be estimated init_number a //slope parameter init_number b //intercept parameter

//Quantities calculated from parameters vector pred_Y(1,nobs) //predicted Y values

//Value to be minimized by ADMB objective_function_value rss //residual sum of squares

• PARAMETER_SECTION• DATA_SECTION

.tpl Sections

Page 32: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Keep in mind

• Init_ in DATA_SECTION indicates a value that will be read in from the .dat file

• Init_ in PARAMETER_SECTION specifies a variable that will be estimated

Page 33: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

.tpl Sections

Set Initial values for parameters- use in place of .pin filelog_F -1.0log_M -1.6

• PARAMETER_SECTION• DATA_SECTION

• INITIALIZATION_SECTION

Page 34: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

.tpl Sections

• PARAMETER_SECTION• INITIALIZATION_SECTION

• DATA_SECTION

• PROCEDURE_SECTION

Back transform parameters for use in functions (if needed)e.g., F = exp(log_F)

Construct Functions

Specify the equation for your Objective function

Must have a PROCEDURE_SECTION for model to compile

Page 35: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

.tpl Sections• PROCEDURE_SECTIONDATA_SECTION init_int nobs //number of observations init_vector Y(1,nobs) //observed Y values init_vector x(1,nobs) //observed x values

PARAMETER_SECTION init_number a //slope parameter init_number b //intercept parameter vector pred_Y(1,nobs) //predicted Y values objective_function_value rss //residual sum of squares

PROCEDURE_SECTION//Simple linear model gives predicted Y values pred_Y=a*x+b;

//Parameter estimates obtained by minimizing //objective function value (residual sum of squares) rss=norm2(Y-pred_Y); //norm2(x)=x1^2+x2^2+...+xn^2

Page 36: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

.tpl Sections

• PARAMETER_SECTION• INITIALIZATION_SECTION

• REPORT_SECTION

• DATA_SECTION

• PROCEDURE_SECTION

Specify output to go to .rep file

Be sure to end .tpl with an empty line (hard return)

Page 37: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

.tpl Sections

• Report section useful for reporting values not otherwise needed in the model

• Can be organized in many ways• Can still do calculations in REPORT_SECTION

– e.g., report<< “S: ” << exp(-Z) <<endl;

• Results (.rep file) can be read into other programs

Page 38: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Create an Output .dat file

• Use an output file streamofstream ofs(“MyOutput.dat”,ios::app); { ofs << “Output variable x: “ << x << endl;

ofs << “Output variable y: “ << y << endl; }• Also can delete a file

system(“del MyOutput.dat);

Append to file

Note: different system command for Linux

Page 39: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Other .tpl Sections

• PRELIMINARY_CALCS_SECTIONUses C++ codeCan do some preliminary calculations and manipulations with the data before getting into the model proper

e.g., pi = 3.14;

• RUNTIME_SECTION Change behavior of function minimizer

• TOP_OF_MAIN_SECTION Change AUTODIFF global variables

Page 40: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Compare with SAS code

DATA GROWTH;INPUT AGE LENGTH GENDER;DATALINES;

PROC NLIN DATA=GROWTH METHOD=MARQUARDT;PARAMETERS LINF1 = 1100 K1=0.4 T01=0.0;YPRED= LINF1*(1-EXP(-K1*(AGE-T01)));MODEL LENGTH = YPRED;OUTPUT OUT=DATA_OUT PRED=PP RES=RR;RUN;

Data Section

Initialization Section

Report Section

Runtime Section

Prelim Calcs Section

Procedure Section

Page 41: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

.tpl File• General rule: make .tpl file as general as possible (try

to avoid hard coding) – will allow you to analyze future datasets

• Must be “compiled” into C++ code

1) tpl2cpp (makes .cpp file)2) compile (makes .exe file)3) link (connects libraries)

• We’ll use Emacs (more later)

Page 42: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Compiling your .tpl

• Need a C++ compiler to run your code

• After it is compiled, model will be a .exe– (so can be run on machines without ADMB)

• If you change the .tpl file, it must be recompiled…

• If you change and save data (values, sometimes dimensions), the existing model will still be ready to go…– So, advantage to putting starting values, ect…, into .dat or .pin

files.

Page 43: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

How should I build my tpl

Suggestions• Keep projects in separate folder• Name, describe, and date each file at the top• Start with a simple working program• Be sure data get read in correctly• Use unique names for files and parameters (don’t use

“catch” as a variable name)• Avoid “hard coding” … make it flexible• Build it one step at a time• COMMENT, COMMENT, COMMENT

Page 44: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

About Emacs

• For this class, you will Emacs to construct your .tpl file

• A highly customizable text editor • We have modified Emacs so that an

ADMB .tpl file is automatically linked to a C++ compiler

• MINGW32 is a freeware C++ compiler – don’t need to buy both ADMB and Visual Studio

Page 45: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Using Emacs

• Refer to Emacs Basics handout

• Hotkeys are different– e.g., “control-v” will not paste

• Highlighting text will automatically copy it

• Remember to save files and recompile .tpl

Page 46: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Let’s Try an Example

2

0

( )

Y a bX

E

Var

Simple linear regression model

2

1

n

i ii

SSE y a bx

Estimation by least squares

0

5

10

15

20

25

-2 0 2 4 6 8 10

Independent variable

Dep

ende

nt v

aria

ble

Page 47: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Let’s Try an Example

• Start Emacs by double clicking the Emacs icon on the desktop

Page 48: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Let’s Try an Example

• Open the simple.tpl and simple.dat files in the MNADMB folder located on your desktop

Page 49: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

0.0

0.5

1.0

1.5

2.0

2.5

3.0

3.5

4.0

4.5

-0.5 0.0 0.5 1.0 1.5 2.0 2.5

Slope

Inte

rce

pt

0

200

400

600

800

1000

1200

1400

1600

0 2 4 6 8 10 12

Iteration

Ob

jec

tiv

e f

un

cti

on

(RS

S)

Page 50: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

0

5

10

15

20

25

-2 0 2 4 6 8 10

Independent variable

De

pe

nd

en

t v

ari

ab

le

Page 51: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Simple Diagnostics

• Types of error messages:– Compile– Run-time

• Modes of operation:– Safe mode– Optimization mode

Page 52: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Compile Errors

Error in line 48 while reading

r

• Line number refers to tpl file

• Need a space at start of each line of code– Except for comments and section headings

• Need a “return” after last line of tpl

Page 53: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Compile Errors

c:/…/simple.cpp:36: error: expected `;' before "rss“

c:/…/simple.cpp:35: error: `pred_Y' undeclared (first use this function)

• Check designated line in cpp file

• Make corrections to tpl file, not cpp file

Page 54: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Run-Time Errors

Error reading in dat file – no error message

• In DATA_SECTION, values made up for init_objects that are not assigned values from dat file

• Use “cout” command to make sure dat file reads in properly

Page 55: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Run-Time Errors

Var Value Gradient 1 10.00000 -1.#IND0e+000 Var Value Gradient2 0.00000 1.#QNANe+000

• IND0: infinity or division by zero• QNAN: not a real number• Use “cout” command to check

calculations

Page 56: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Run-Time Errors

Error in matrix inverse -- matrix singular in inv(dmatrix)

• Hessian cannot be inverted to obtain asymptotic standard errors

• Use different parameter starting values

• Reparameterize model

Page 57: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Run-Time Errors

array bound exceeded -- index too high in prevariable::operator[]

• Tried to assign value outside the defined range indices for vector or matrix – Define a vector to be 10 elements long– Write values to the vector using a loop with 11

steps

• Use “cout” command to locate error in tpl• Error message only appears when in safe

mode

Page 58: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Modes of Operation

• Safe mode: provides bounds checking on all array objects– ADModel > tpl2cpp > compile > link– ADModel > makeadms

• Optimization mode: provides faster execution– ADModel > makeadm

Page 59: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Essential theory

Page 60: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Goal – Model Building• Models are tools for evaluating hypotheses• Uses of models

– Improved understanding of a system– Prediction– Help in making decisions

• Likely to have several competing models that you will want to fit and compare

• Many different types of estimation procedures to consider – we will consider maximum likelihood

Page 61: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Additional Information

Online MLE Course Launched 16 Oct. 2007

Registration at www.shop.msu.edu

Normal Cost: $370 to QFC Supporting Partners

$300 if you contact me ([email protected]) by November 5

Page 62: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Background on maximum likelihood• Likelihood – a measure of how likely a set of parameters

are to have produced your data• Can be confusing. Often written as:

But not always –

• Think of as function of parameters. • Depends upon data.• Likelihoods are mathematically the same as probability

distributions; thus, you must consider what probability distribution gave rise to your data

)|( XL

)(

),(

L

XL

Page 63: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Probability density functions

• Functions describing the probability of obtaining a particular outcome for a random variable

• For discrete distributions, sometimes referred to as probability mass functions

• Typically denoted as (x) or as (x|θ)

Page 64: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Properties of probability distributions

• Probability density functions (pdf) for continuous distributions

• Probability mass functions (pmf) for discrete distributions

• Joint pdf/pmf for multiple independent observations

0.1)( dxxf

0.1)( xf

k

i

k

k

xf

xfxfxfxf

xxxxf

1

321

,321

)(

)()()()(

),,(

Page 65: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

A familiar example

• A single observation from a normal distribution

2

22

2

)(exp

2

1),|(

x

xf

0

0.05

0.1

0.15

0.2

0.25

0.3

0.35

0.4

0.45

5 7 9 11 13 15

x value

Pro

bab

ilit

y d

ensi

ty

1

102

Probability density function

Page 66: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

0

0.05

0.1

0.15

0.2

0.25

0.3

0.35

0.4

0.45

x value

Pro

ba

bil

ity

de

ns

ity

c1 c2

Page 67: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

The likelihood function

• Mathematically equal to the joint probability density function

• But NOT a probability density for parameters

)|()|( xfxL

Maximum likelihood estimates are values of parameters that maximize the likelihood

1.0 necessary NOT all

dL )(

Page 68: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Properties of maximum likelihood estimates

• Invariant to transformations • Asymptotically efficient (lowest possible variance) • Asymptotically normally distributed • Asymptotically unbiased (expected value of the

estimated parameter equals the true value) • If we assume independent, normally distributed

errors, ML methods provide the same estimates of structural parameters as least squares.

Summary – versatile and widely used with a number of desirable properties, but not perfect!

1

)(ˆ

)(ˆ

2

2

n

xx

n

xx

iUB

iML

E.g., can be biased for small n

For normal distribution

Page 69: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Back to really, really, really simple example: Normal, one observation, variance known

• Likelihood equal to the probability density function2

2 22

1 ( )( | , 1) ( | , 1) exp

22

xL x f x

x = 12

0

0.05

0.1

0.15

0.2

0.25

0.3

0.35

0.4

0.45

5 7 9 11 13 15

Parameter (mean)

Lik

elih

oo

d

Maximum likelihood estimates are values of parameters that maximize the likelihood

Page 70: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Slightly more complicated example– normal sample (multiple observations)

2 22

1

22

1 1( , ) exp ( )

22

1 1 exp ( )

22

n

ii

n

ii

L x

x

Page 71: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

To ease calculations, typically take the negative log of the likelihood

2 22

1log ( , ) ln 2 ln ( )

2 2 ii

nL n x

Parameter estimates that maximum the likelihood are the same values that will minimize the negative log

likelihood

Page 72: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

2

2

2

2 22

( , ) 0,

( , )

,

1log ( , ) ln 2 ln ( ( , ))

2 2

i i i i

i i i

i

i i

i ii

y g X N

E y g X

Var y

y N

nL n y g X

Slightly more complicated example – regression

Page 73: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Ignoring constants

• If you minimize negative log likelihood you can ignore constants because:

)()(log gICL Forsame will minimize left and right hand side

• Example of the reduced (ignored constants dropped) negative log likelihood (for normal). This depends on what you estimate.

2 2 22

2 22

1log ( , ) ln ( ) , estimated

2

1log ( , ) ( ) estimated

2

ii

ii

L n x

L x

Page 74: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Concentrated Likelihood

2

2

1

In -log likelihood for normal replace with

k

i ii

yRSS

n n

Reduce the number of parameters by writing some parameters as a function of other

parameters

Page 75: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Concentrated Likelihood

2

2 21

2 1

1

2

1

2

1

1

1log ( , ) ln ( )

2

= ln2 2

= ln ln2 2 2

= ln ln2 2 2

log ln2

i ii

ii

i ii

i ii

i ii

i

yL n y

ny

n

yn n

nn n n

y n

n n nRSS n

nConc L R

1i

SS IC

Page 76: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Combining normal data with different variances

• Data are: {y11, y12, …y1k1, y21, y22, …y2k2} Plus known predictors X

• First and second set of y have different distributions (variances)

• -logL= L1+L2+IC:

• Just special case of rule for getting joint pdf for independent data

22

1ln ( )

2i i i ij ijji

L n y

),(~ 2iijij Ny

Page 77: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Concentrated negative log likelihood when there is more than

one normal component

1 2

1 2 2

2 22 2 1 11 2

[ ] log[ ]2

ˆˆ ˆ, , (assumed known)

T

T T

i ii i

NConc LogL RSS IC

N n n n

RSS RSS RSS RSS

RSS

N

Lambda’s are a weighting factor – how strong a role should a particular data set play in influencing the overall fit of the model

Page 78: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Likelihood for Lognormal Distribution2 2

2

2 22

2 22

2 22

1 1 1( , ) exp (ln )

22

1 1log ( , ) ln( ) ln(2 ) ln( ) (ln )

2 21

log ( , ) ln( ) ln(2 ) ln( ) (ln )2 2

1log ( , ) ln( ) ln(2 ) ln( ) (ln )

2 2

L xx

L x x

nL x n x

nL x n x

0

0.0005

0.001

0.0015

0.002

0.0025

0.003

x value

Pro

ba

bil

ity

de

ns

ity

small"" ,

then

1)(

)1()(

2exp)(

),(~)ln(),(~

2

222

2

22

eXCV

eeXVar

XE

NXYLNX

Page 79: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Von Bertalanffy Growth Model

Welcome to nonlinearity

Page 80: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Example – Modeling size versus age

0200400600800

100012001400

0 5 10 15

Age (years)

Len

gth

(m

m)

Page 81: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Von Bertalanffy Model

0

0

2

( )

( )

2

( , ) 0,

( , ) (1 )

( ) (1 )

( )

i

i

i i i i

K a ti

K a ti

i

L g a N

g a L e

E L L e

Var L

Page 82: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Let’s write some code…..

• Check out your .dat file (number of observations, data for individual fish, dummy variable)

• Start out by reading in your data• Use the simple.tpl as an example• Use the concentrated likelihood for the

normal distribution as your obj. function• Initial values – Linf=1200 mm, t0 = 0, Growth

coefficient = 0.4

Page 83: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Hints

• Read age and length data in as a matrix e.g., init_matrix fish(1,nobs,1,2)

• Create an age and length vector e.g., vector ages(1,nobs)

• Extract age and length data from the matrix using the extract column command (column)

• e.g. ages = column(fish,1)

Page 84: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Let’s write some code…..

• Advanced things to try– Linf and Kappa must be positive so try

estimating on a log scale

– Use the full negative log likelihood as your objective function (sigma will be another parameter that will need to be estimated; also will need to be estimated on a log scale)

Page 85: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous
Page 86: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Mortality estimation

• Tag-recovery studies widely used to estimate fishing and natural mortality rates

• Expected number of tag recaptures generally considered to follow a multinomial distribution

Page 87: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Mortality model

• Expected number of tag recaptures generally considered to follow a multinomial distribution

31 2

31 2

1 2 3

1 2 3

1 1 2 2

( , | ) 1

( | , ) 1

log ( | , ) ln ln ln ln 1

ii

i

ii

i

N y

y yy yi i i i i

ii

N y

y yy yi i i i i

ii

i i i i i i ii i

Nf n y p p p p p p

y

NL p n y p p p p p

y

L p n y IC y p y p y p N y p

Page 88: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Mortality model• Expected numbers of recaptures

Year marked # Marked 1 2 3 4

1 N1 N1f1 N1S1f2 N1S1S2f3 N1S1S2S3f4

2 N2 N2f2 N2S2f3 N2S2S3f4

3 N3 N3f3 N3S3f4

K=4 N4 N4f4

Recovery Periods

S = probability a fish survives the year

f = probability a fish is harvested by angler and its tag is retrieved and reported

Page 89: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Instantaneous mortality formulation

1

1 2

2

111 1 1

1

212 1 2

2

2313 1 3

3

323 2 3

3

1 exp(

1 exp( exp

1 exp( exp

1 exp( exp

F M

F F M

F M

FE R N F M

F M

FE R N F M

F M

FE R N F M

F M

FE R N F M

F M

Prob. harvest during time periodProb. surviving previous time periods

Page 90: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Let’s write some code…..• Estimate Fs and Ms on the log scale

• Assume =0.18 for all years

• Use report section to calculate instantaneous total mortality for each year

• Use report section to calculate exploitation rate

/

1 exp Z

u FA Z

A

Page 91: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Advanced things to try…..

• Create an output vector of predicted tag recoveries to see how well model agrees with observed data

• Try different objective functions and see how results match with one another

Page 92: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous
Page 93: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Minnesota AD Model Builder Short CourseOctober 22-24, 2007

Day 2

Questions before proceeding???

Page 94: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Strategy for building your tpl from scratch

• Start with a working file from another problem, as memory aid on coding syntax etc (section names, define variables, loops…).

• First create a minimal program which consists of the required data, parameter, and procedure sections, has an objective function variable and one estimable parameter.

• Check that the program reads the data in correctly (use cout and exit)

Page 95: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Strategy for building your tpl from scratch

• Sequentially add calculations to the procedure section and check they work using cout and exit. Much easier to check things as you build them up rather than trying to find where the errors are after writing lots of code.

• Use small steps and do not worry about efficiency too much at this stage.

• Make sure the estimation procedure is working before investing time in defining derived variables in report section.

Page 96: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Quick Refresher

• In LN_Density folder is a dataset that was generated by random draw from a lognormal density (μ = 10, σ2 = 1.5)

• Use ADMB to find MLEs of μ and σ2

2 22

1log ( , ) ln( ) ln(2 ) ln( ) (ln )

2 2

nL x n x

Page 97: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Alewife stock recruitment in Lake Huron

• What effect would reduction in salmonid stocking by OMNR and MiDNR have on fish communities?

• Stock-recruitment relationships of prey species (alewife, rainbow smelt) recognized as major source of uncertainty

Page 98: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Alewife spawners vs. recruits

0

5000

10000

15000

20000

25000

30000

35000

40000

0 500 1000 1500 2000

Spaw ners

Rec

ruit

s

Page 99: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Ricker stock-recruitment curve

SR Se e

Multiplicative Error

SR Se

Additive Error

20,N

If X~N(μ,σ2) and Y = exp(X)

then Y~LN(μ,σ2)

Page 100: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Ricker stock-recruitment curve

2

log log log

log log log

log log

0,

R S S

R S S

RS

S

N

Linearized form of multiplicative error

To calculate R/S when R and S are vectors, use the command elem_div(R,S). Don’t forget to declare a vector where your predicted values will go

Page 101: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Ricker stock-recruitment curve

Linearized form

-6

-4

-2

0

2

4

6

8

10

12

0 500 1000 1500 2000

Spaw ners

Lo

g(R

ecru

its/

spaw

ner

s)

Page 102: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Things to try…..

• Estimate the linearized version of the multiplicative error form of the recruitment model

• Estimate the additive error form of the recruitment model (try using concentrated likelihood)

Page 103: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous
Page 104: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Inference using AD Model Builder

Page 105: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

An overview on inference

• By inference, I mean going beyond point estimates and saying something about the quality of the estimates. How likely is it that the estimate is close to the true value?

• Topics related to inference– Estimates of standard errors– Confidence intervals– Bayesian probability intervals

Page 106: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

• Inferences depend upon the variance-covariance matrix:

Diagonal elements are variances of parameter estimates, off-diagonals are covariances among parameter estimates.

21 1 2 1 1

22 1 2 2 2

21 2

21 2

... ...

... ...

... ... ... ... ... ...

... ...

... ... ... ... ... ...

... ...

i k

i k

i i i i k

k k k i k

Page 107: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

What is a variance and covariance?

• Recall definition of expected value

2])[()( XExEXVar

E X xf x dx( ) ( )

])[])([(),( YEyXExEYXCov

Page 108: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Cov(Y,X)=0

X value

Y v

alu

e

Cov(Y,X) Positive

X value

Y v

alu

e

Cov(Y,X) Negative

X value

Y v

alu

e

Page 109: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Covariances and parameter estimates

• The variances describe uncertainty in the parameter estimates.

• The square-root of the variances gives the standard errors

• The covariances describe how the estimation errors for two parameters are related. When parameter “a” is over-estimated does parameter “b” also tend to be over-estimated (+ cov), tend to be under-estimated (- cov) or is there no relationship (0 cov)?

Page 110: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Correlation matrix

• Diagonals are 1.0

• Off diagonals are correlations among parameter estimates:

2 2

i jij

i j

Page 111: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Asymptotic results for parameters

• Done automatically in ADMB (i.e., you don’t have to code anything)

• Results are in *.std and *.cor

• These are based on the Hessian matrix:

1

2

ˆ

log ( )ij

i j

H

Lh

Page 112: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Parameter value

Ne

ga

tiv

e lo

g

like

liho

od

iii

L

ˆ

2

2

measures how likelihood falls off away from best estimate

Page 113: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

-0.0

0068

-0.0

0053

-0.0

0038

-0.0

0023

-0.0

0008

0.00

007

-1.05

-0.85

-0.65

-0.45

-0.25

-0.05

-LogL

PAR2

PAR1

Neg log likelihood for stock recruitment model

10-15

5-10

0-5

-5-0

Cross derivatives “twist” the likelihood surface. Not counting for them would cause underestimation of uncertainty!

Page 114: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

index name value std dev

1 log_q -1.6219e+000 2.7145e+000

2 log_popscale 7.4954e+000 1.6715e-001

3 log_sel_par -6.0105e+000 2.7178e+000

4 log_sel_par -3.1105e+000 2.7089e+000

5 log_sel_par -1.3544e+000 2.7038e+000

6 log_sel_par -1.4792e-001 2.6779e+000

7 log_sel_par -4.7468e-002 2.5159e+000

8 log_sel_par -7.7288e-001 2.0588e+000

9 log_relpop 8.1995e-001 1.7816e-001

10 log_relpop 1.5404e+000 1.7094e-001

11 log_relpop 1.2639e+000 1.7262e-001

… …. … …

Example *.std output

Page 115: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

index name value std dev 1 2 3 4 5

1 log_q -1.6219e+0002.7145e+000 1.0000

2 log_popscale 7.4954e+0001.6715e-001 -0.6779 1.0000

3 log_sel_par -6.0105e+0002.7178e+000 -0.9971 0.6695 1.0000

4 log_sel_par -3.1105e+0002.7089e+000 -0.9997 0.6763 0.9970 1.0000

5 log_sel_par -1.3544e+0002.7038e+000 -0.9999 0.6771 0.9971 0.9997 1.0000

6 log_sel_pa …..

… … … … … … … … …

52 … … … … … … … ……

Example of *.cor file

Page 116: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

What happens if we use RSS instead of neg logL or neg logConc?

name value std value stdlog_Linf 3.382 0.030 3.382 0.019log_K -0.925 0.152 -0.925 0.098t0 0.037 0.216 0.037 0.140

Neg LogL or Conc RSS

// conc=(nobs/2.0)*log(rss); //concentrated likelihood //changed obj function to just be RSS for illustrative purposes //DO NOT DO THIS conc=rss;

Made this change to growth.tpl

Page 117: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Standard errors for derived quantities

• Often we are interested in assessing the uncertainty of derived quantities (quantities that are functions of one or model parameters)– biomass in last year of assessment, MSY for a logistic

surplus production model, ratio of abundance in 2002 to abundance in 1995, SSBR based on recent mortality schedule,…

• Calculated using the Delta method

Page 118: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Delta method• Method for approximating statistical

properties of nonlinear functions of random variables based on a Taylor series approximation of a function

2

2

Let be the random variable of interest with

E( ) = and Var( ) = .

Let ( ) be some function of . Then

2

Var

2

y

y y

g y y

yE g y E g y g g

yE g y g g

Page 119: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Delta method• Method for approximating statistical

properties of nonlinear functions of random variables based on a Taylor series approximation of a function

2

2

Let be the random variable of interest with

E( ) = and Var( ) = .

Let ( ) be some function of . Then

Var Var

Var Var

y

y y

g y y

g y g y g

g y y g

Page 120: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Delta method

• For functions of several random variables, method requires partial derivatives, covariances, …

• Approximation of variances less accurate then approximations of expectations (first versus second order Taylor series expansion)

• Also used to estimate standard errors of derived quantities in SAS (Proc NLMIXED, PROC GLIMMIX)

Page 121: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Standard error estimates for derived quantities (continued)

• Can be done for any type of variable (number, vector, matrix)

• Specified in PARAMETER_SECTION– sdreport_number Z– sdreport_vector predicted_N (2,nages)

• Results are included in *.std and *.cor files

Page 122: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Refer to simple.tpl

Page 123: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Asymptotic standard errors can produce misleading inferences

• When sample sizes are small

• The curvature of the likelihood surface changes substantially within the range of plausible estimates – i.e., “near to the maximum likelihood estimates

Page 124: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Profile Likelihood Method

• Typically, a method for constructing confidence intervals where analysts vary one or more parameters systematically and computes the values of the other parameters that maximize the likelihood

• Surface of the likelihood used to construct confidence intervals based on a chi-square distribution

Page 125: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Profile Likelihood Method (cont.)• Construct profile likelihood for growth coefficient

of von Bertalanffy growth model (MLE = 0.281)

Kappa (fixed) L (estimated) -logL

-2.0 SE = 0.11 34.19 2.45

-1.0 SE = 0.18 29.04 1.88

-0.5 SE = 0.22 27.22 1.64

+0.5 SE = 0.35 24.58 1.68

+1.0 SE = 0.44 23.66 2.15

+2.0 SE = 0.58 22.33 3.97

Page 126: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Profile Likelihood Method (cont.)

-2

0

2

4

6

8

10

12

14

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8

K

2(-l

og

L--

log

Lm

in)

Page 127: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Profile Likelihood Method

• This is NOT inverting a likelihood ratio test in ADMB land!

• This is Bayesian in philosophy (in the same way that MCMC is). Can also be motivated by likelihood theory (support intervals)

• Idea is to use the profile for g() to approximate the probability density function for g.

Page 128: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

How to use the profile method

• Declare a variable you would like to profile as type likeprof_number in the parameter section, and assign it the correct value in the procedure section.

• When you run your program use the lprof switch: run -lprof

• Results are saved in xxxxx.plt where xxxxx is the name of your likeprof_number variable

• Your variable is varied over a “profile” of values and the best fit constrained to match each value of your variable is found

Page 129: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

s

riiii yxx 1

PLT File contains list of point (x,y) x is value (say biomass) y is associated prob density

Plot of Y vs X gives picture of prob distribution

ADMB manual says estimate probability x in (xr,xs) by

Page 130: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Likelihood profile versus Normal approximation

012345678

5.9 6 6.1 6.2 6.3 6.4 6.5 6.6

log(Biomass)

Lik

elih

oo

d

Likeprof Normal approximation

Page 131: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Likelihood profile versus Normal approximation

-0.0020.0000.0020.0040.0060.0080.0100.0120.0140.016

400 450 500 550 600 650 700

Biomass

Lik

elih

oo

d

Likeprof Normal approximation

Page 132: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Profile likelihood optionsSwitch -prsave This saves the parameter values associated with each step of profile in myvar.pv

Options set in tpl (preliminary calcs section): e.g., for lprof var myvar:

PRELIMINARY_CALCS_SECTION myvar.set_stepnumber(10); // default is 8 myvar.set_stepsize(0.2); //default is 0.5

Note manuals says stepsize is in estimated standard deviations but this appears to be altered adaptively during the profile

WARNING -- LOTS OF STEPS CAN TAKE LOTS OF TIME!

Page 133: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Exercises

• Calculate asymptotic standard errors and likelihood profiles for– Growth model – parameters of growth model;

predictions of length at age (asymptotic only)– Mortality model – Z=M+F and u=FA/Z– Stock recruitment model (linear version of the

multiplicative error)

1max

2

MSY 0.5log 0.07 log

R e

Remember, with linear version and simple linear regression, β0 estimates log and β1 estimates β

Page 134: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous
Page 135: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Essential Programming Skills

• New ADMB concepts and techniques– Loops– Conditional statements– Bounded objects– User-defined functions– Random number generation

Page 136: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Loops

• Repeats code a specified number of times

for (i=m;i<=n;i++){ . . . . . . ;}

for (i=10, i>=0, i-=2)

looping variable

‘i’ goes from ‘m’ to ‘n’ in increments of 1

Code that is repeated for each increment of ‘i’

‘i’ goes from 10 to 0 in increments of -2

Page 137: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Refer to loop.tpl

Page 138: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Conditional Statements

• Runs code if conditions met

if (condition)

{

. . . . . . ;

}

if condition is true

then run this code

Page 139: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Common Conditional Statements

• (X==Y) X equal to Y • (X!=Y) X not equal to Y• (X<Y) X less than Y• (X<=Y) X less than or equal to Y• (X>Y) X greater than Y• (X>=Y) X greater than or equal to Y• Use && for compound statements

– e.g., if (iyear=1998 && iarea=north)

• Use || for or statement– e.g., if (iyear=1998 || iyear=2000)

Page 140: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Conditional Statements

if (condition){ . . . . . . ;}else{ . . . . . . ;}

if condition is true

then run this code

then run this code

if condition is false

Page 141: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Advanced Conditional Statements

• active(parameter)– Returns true if parameter is being estimated

• last_phase()– Returns true if in last phase of estimation

• mceval_phase()– Returns true if –mceval switch is used

• sd_phase()– Returns true if in SD report phase

Page 142: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Refer to conditional.tpl

Page 143: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Bounded Objects• Bounds constrain what values a parameter

can take

init_bounded_number x(-10,10)

init_bounded_vector y(1,nobs,-10,10)

Lower bound Upper bound

Page 144: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

User-Defined Functions• Organize code in

PROCEDURE_SECTION

function_name();

FUNCTION function_name

. . . . . . ;

Call function for use

Define function

Code for function

Page 145: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Functions that take arguments

• Functions that do not take arguments can be used to organize code

get_catch_at_age();

• Functions that take arguments can simplify calculations

rss=norm2(residuals);

• Beware of functions that take parameters as arguments

Page 146: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Refer to function.tpl

Page 147: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Random Number Generator

• Initialize random number generator (x) random_number_generator x(seed);

• Fill object (y) with random numbers y.fill_randn(x); // yi~Normal(0,1)

y.fill_randu(x); //yi~Uniform(0,1)

Random number seed

Page 148: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Random Number Generator

• Random number generator produces pseudo-random numbers

• Pseudo-random numbers are generated from an algorithm which is a function of the random number seed

• The same random number seed will always produce the same string of numbers

Page 149: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Exercises

• Modify the growth.tpl so that you use a loop rather than the norm2 command to calculate the residual sum of squares

Page 150: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Now to code it…

Page 151: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Piecewise regression catch curve

• Maceina (2007) recently proposed using piecewise regression to estimate size related mortality rates by catch curves

0.00

0.50

1.00

1.50

2.00

2.50

3.00

3.50

4.00

4.50

5.00

0 2 4 6 8 10 12

Age (Years)

LN

Cat

ch

ˆSlope Z

Page 152: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Piecewise regression catch curve

0.00

0.50

1.00

1.50

2.00

2.503.00

3.50

4.00

4.50

5.00

0 2 4 6 8 10 12

Age (Years)

LN

Cat

ch

1 1ˆSlope Z

2 2ˆSlope Z

Knot or joinpoint

Page 153: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Piecewise regression catch curve

0 1

0 1 2

for age knot

for age knot i i

ii i

agey

knot age knot

•4 parameters (β0, β1, β2, knot) [actually 5 with 1 linear constraint]

•Knot should be initialized as a bounded variable (minimum and maximum age)

•Use a loop and conditional statement to estimate predicted ln catch at age for different fish ages

•Use concentrated log likelihood (assume normal)

Page 154: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Caveat

• This estimation approach is similar to the one taken in Maceina (2007)

• However, this is not the generally recommended approach for piecewise models

• Grid search recommended– Search for regression parameters across a

grid of knots– Fix the knot, estimate the regression

parameters

Page 155: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Now to code it…

Page 156: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Convergence Issues

• Convergence criteria

• Diagnosing convergence problems– Convergence messages– Self diagnostics

• Fixing convergence problems– Convergence criteria problems– Code problems

Page 157: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Convergence Criteria

• Gradients close to zero– Maximum |gradient| < 1x10-4

• Obj. function value fails to decrease– Change < 1x10-6 for 10 iterations in a row

• Obj. function evaluated too many times– Maximum evaluations = 1,000

• Line search fails to find parameters with lower objective function value– Step size adjusted 30 times

Page 158: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Convergence Messages

ic > imax in fminim is answer attained ?

Function minimizer not making progress ... is minimum attained?

Minimprove criterion = 0.0000e+000

• Run-time messages indicating convergence problems

Page 159: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Self Diagnostics• Compare smallest and largest eigenvalues

of Hessian in eva file• Is logarithm of determinant of Hessian

small in cor file?• Are correlations large in cor file?• Are standard errors large compared to

parameter value in std file?• Examine trajectory of iterations including

objective function and key parameters

Page 160: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Convergence Criteria Problems

• Is convergence criteria too strict or too loose?– Does objective function value change

substantially as gradients approach convergence criterion?

– Are results sensitive to changes in convergence criterion?

– Try different parameter starting values

Page 161: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Changing Convergence Criteria• In tpl fileRUNTIME_SECTION

convergence_criteria 0.001

maximum_function_evaluations 500

• With runtime switches -crit 0.001 –maxfn 500

• Switch to restart (after rescaling) if function not improving but gradients not near zero

-rs

Page 162: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Code Problems

• Do predictions respond to parameter values?– If not possible can estimate parameters in

different phases– Parameterize the current function differently– Or use a different function

Page 163: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Improving Efficiency

• You do not need to worry about model efficiency in most cases

• In general, it is only important when:– Your model is very complex– You are running your model many times (e.g.,

mcmc, simulation study)

Page 164: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Efficiency rule number 1 – calculate something only once if you can!

• Quantities that do not change but are needed during estimation should be calculated in PRELIMINARY_CALCS_SECTION

• Quantities that are not needed for estimation but only for reporting should be calculated in REPORT_SECTION or if uncertainty estimates are needed conditional on phase too:

• If (sd_phase()){…

}

Page 165: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Efficiency rule number 2 – avoid unneeded loops

• Use admb built in functions (e.g., sum, rowsum, element by element multiplication and division, etc)

• Combine loops over the same index

Page 166: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Bayesian Inference in AD Model Builder

• Bayesian inference – a different philosophical approach to statistics than traditional inference

• Essential element is the use of observed data to transform numerical estimates of our degree of belief in a hypothesis into posterior distributions that take into account the evidence in the observed data

Page 167: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Bayesian Inference

( , ) ( )( | )

( , ) ( )

L X pp X

L X p d

Constant ))(ln()|(ln()|(ln( pXLXp

ADMB presumes we are going to start by finding the parameters that maximize the posterior density (called highest posterior or modal estimates), so just minimize the log posterior. Just like a negative log-likelihood but with new terms for priors

Page 168: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Markov Chain Monte Carlo• Calculation of posterior distributions can be

mathematically intractable accept for trivial scenarios• Markov Chain Monte Carlo method is an algorithmic

way to generate samples from a complex multivariate pdf (in practice, usually the posterior distribution).

• This is useful in looking at marginal distributions of derived quantities.

• These marginal distributions are the same thing the profile likelihood method was approximating.

Page 169: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

• If prior on M were log-normal with median of 0.2 and with sd for ln(M)=0.1, then just add to your likelihood:

• For special case of diffuse prior ln(p()) is constant inside the bounds, so a bounded diffuse prior can be specified just by setting bounds on parameters.

Specifying Priors

2

1.0

)2.0/ln(5.0

M

Page 170: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Doing an MCMC run

• Use -mcmc N switch to generate a chain of length N. Default N is 100,000.

• Summarized output for parameters, sdreport variables and likeprof_numbers is in *.hst

• This automatic summary is for the entire chain with no provision for discarding a burn-in and no built in diagnostics.

• Serious evaluation of the validity of the MCMC results requires you gain access to the chain values.

Page 171: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Gaining access to chain values

• When you do the MCMC run, add the switch -mcsave N, which saves in a binary file every Nth values from the chain.

• You can rerun your program to read in the saved results and make one run through your model for each saved set of parameters. Use the switch -mceval

• You can add code to your program to write out results (and do special calculations) during the mceval phase.

• You can modify your program to do this even after you generate your chain, provided your change does influence the posterior density.

Page 172: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Example of code to write results out when using mceval switch

if (mceval_phase()) cout << Blast << endl;

Important caution: this writes to standard output. Better redirect this to file or millions or numbers will go scrolling by!

Page 173: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Some basic MCMC diagnostics• Look at trace plot• Look at autocorrelation function for chain• Calculate “effective sample size”• Compare subchain CDFs (if the first and

second half differ substantially then chain may be too short

• Lots of other diagnostics and procedures– E.g., parallel chains and formal comparisons

Page 174: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Trace plot example

100,000 steps, sampled every 100

trace plot log scale init vs last biomass

1

1.2

1.4

1.6

1.8

2

2.2

0 200 400 600 800 1000

Page 175: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Frequency Histogram

0

1

2

3

4

5

6

1.25 1.35 1.45 1.55 1.65 1.75 1.85 1.95 2.05 2.15

Second halfFrequency Histogram

0

1

2

3

4

5

6

1.25 1.35 1.45 1.55 1.65 1.75 1.85 1.95

Frequency Histogram

0

1

2

3

4

5

6

1.25 1.35 1.45 1.55 1.65 1.75 1.85 1.95 2.05 2.15 burn-in excluded

First half

Entire chain

Page 176: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Autocorrelation example

AR(1) shown for comparison (curve)

Autocorrelation log-scale ratio of init to final biomass

-0.5

-0.3

-0.1

0.1

0.3

0.5

0.7

0.9

0 10 20

Lag

Co

rrel

atio

n c

oef

fici

ent

rho

AR(1)

Page 177: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

MCMC Chain Optionsto make changes to transition rule

• -mcgrope p p is the proportion of “fat” tail

• -mcrb N 1 to 9, smaller = weaker correlation

• -mcdiag Hessian replaced with Identity

• -mcmult N Scaler for Hessian• -mcnoscale No automatic adj to scaler

Page 178: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Starting and Restarting a Chain

• -mcr Restart from where it left off

• -mcpin fn Start chain at params in fn– The output obtained by running with the

switches -lprof -prsave (in *.prv) can be useful for this.

Page 179: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Getting MCMC Results into R

• To check chains, easiest to simply read the MCMC results into R and to use CODA functions

Page 180: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Getting MCMC Results into RSteps:1)Declare your parameters as sdreport_ objects (e.g., sdreport_number parameter1)

2) In procedure section, include the following codeif (mceval_phase())  {    cout << parameter1 << “ “ << parameter2 << “ “ << endl;  }

3) Use "Run ..." command from the ADModel menu.  Then in the mini-buffer, you enter the following switches "-mcmc XXX -mcsave YYY" where XXX is the number of MCMC cycles and YYY is how often the cycles are saved.

4) Run the model a second time using the "Run ..."  In the minibuffer, you enter the switch "-mceval >> filename" where filename is the name of the file to which you want to save the parameters you are interested in

Page 181: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

• Open the chain output file in Excel and copy the chain results to the clipboard

• Use the read.table command in R to copy the data into R

• Convert to an .mcmc object and use CODA functions

Getting MCMC Results into R

Page 182: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous
Page 183: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Huge literature on MCMC and Diagnostics

• Gelman et al. Bayesian Data Analysis good general source on all things Bayesian

• Cowles and Carlin, Markov Chain Monte Carlo convergence diagnostics: a comparative review. JASA 91:833-904

Page 184: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Exercises

• Using models that you have estimated previously (i.e., growth, mortality, recruitment, piecewise) practice using Bayesian methods with both informative and non-informative priors and getting resulting MCMC chains into R and plotting with CODA functions (trace plots, density plots)

Page 185: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Minnesota AD Model Builder Short CourseOctober 22-24, 2007

Day 3

Questions before proceeding???

Page 186: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Sensitivity to Parameter Starting Values

• Why do we care about sensitivity to parameter starting values?

• Methods for specifying starting values– Default values– In tpl file– In dat file– In pin file

• Precedence between the methods

Page 187: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Why do we care about sensitivity to starting values?

• Avoiding local minimums in the likelihood surface– If different starting values lead to solution with

lower obj. function value, then you were at a local minimum

• Identifying sensitive parameters– If small change to parameter starting value

causes large change in results, then you may want to reparameterize model

Page 188: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Default Starting Values

• Parameter with unspecified starting value has default starting value of zero

• Bounded parameter has default starting value which is midway between lower and upper bounds

Page 189: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Specify starting values in tpl file

INITIALIZATION_SECTION

log_q -1.0

• Must recompile tpl file everytime starting values are changed

Page 190: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Specify starting values in dat file

DATA_SECTION

init_number start_log_q

PRELIMINARY_CALCS_SECTION

log_q = start_log_q;

• Can change starting values without recompiling tpl file

Page 191: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Specify starting values in pin file

#Example pin file for model with 15 parameters

0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0

• Can change starting values without recompiling tpl file

• Must specify a starting value for each parameter

Page 192: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Precedence Between Methods

• Specifying starting values in dat file takes precedence over pin file and INITIALIZATION_SECTION

• Specifying starting values in pin file takes precedence over INITIALIZATION_SECTION

Page 193: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Matrix Algebra in ADMB• Usual rules of matrix algebra• Faster than loops• Also can do element-by-element tasks

– element-by-element product– element-by-element division

4,23,22,21,2

4,13,12,11,1)4,1,2,1(

#,1,#,1

aaaa

aaaaA

colrowAmatrix

Page 194: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Matrix Operations in ADMB

110

57

14

22

06

35

12

13

14

22

06

35

654

321A

Matrix Addition and Subtraction

Matrix Multiplication and Division

• Not commutative: A*N usually ≠ N*A• ADMB can do both traditional matrix multiplication and element-by-element operations

• Element-by-element calculations• Commutative: A+B = B+A • Associative: A±(B±C)=(A±B)±C

See Gotelli and Ellison (2004) for a good primer on matrix operations

Transpose

1 4

tran 2 5

3 6

A

Page 195: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Matrix Multiplication

ttANN 1

0

0

0

100

tN

037.00

0037.0

00037.

2.68.35.0

A

0

0

37

0

0*00*37.0*0100*0

0*00*00*37.100*0

0*00*00*0100*37.

0*2.60*8.30*5.100*0

1tN

Column dimension of A must =

Row dimension of N

Page 196: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Array and Matrix Functions

• Functions elem_prod and elem_div provide elementwise multiplication and division

• For vector objects x, y and zz=elem_prod(x,y); //returns zi=xiyi

z=elem_div(x,y); //returns zi=xi/yi

• For matrix objects x, y and zz=elem_prod(x,y); //returns zi,j=xi,jyi,j

z=elem_div(x,y); //returns zi,j=xi,j/yi,j

Page 197: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Let’s Try an Example(very) Simple simulation model- Leslie matrix calculator (following Gotelli 2001)

- No estimation (but dummy parameter still needed in ADMB)

0.025.00.00.0

0.00.050.00.0

0.00.00.080.0

025.05.16.1

A

Transition matrix

Fecundity

Survival

0

0

0

200

0N

Initial abundance-at-age

matrix

Page 198: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

ADMB Tasks:• Perform matrix projection• Use a for loop (8 years)

• Output– annual total abundance– age-specific abundance–

0

0

160

320

0

0

0

200

10NN

Population Projection

Time (t)

0 2 4 6 8 10

Pop

ulat

ion

Siz

e (N

)

1

10

100

1000

10000

100000

1000000Age 1Age 2Age 3

Age 4

e

t

t

r

N

N

log

1

r&

776.0

173.2

318,114

8

r

N

Year

tot

875.0

4.2

480

1

r

N

Year

tot

ttANN 1

Let’s Try an Example

Page 199: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Advanced Programming

• Phases

• Reparameterization to improve estimation

Page 200: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Phases

• Minimization of objective function can be carried out in phases

• Parameter remains fixed at starting value until its phase is reached, then it become active

• Allows difficult parameters to be estimated when other parameters are “almost” estimated

Page 201: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Phases

• Specified in PARAMETER_SECTION

init_number x //estimate in phase 1

init_number x(1) //estimate in phase 1

init_number x(-1) //remains fixed

init_vector x(1,n,2) //estimate in phase 2

init_matrix x(1,n,1,m,3) //estimate in phase 3

Page 202: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Parameterization Issues• How do you estimate highly correlated

parameters?– Catchabilities for multiple fisheries– Annual recruitments

• Deviation method

• Difference method

• Random walk

Page 203: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Deviation Method

• Estimate one free parameter X

• Estimate m parameters as bounded_dev_vector 1, . . . , m

• ThenlogX1 = logX + 1

. . . .

logXm = logX + m

Page 204: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

bounded_dev_vector

• Specified in PARAMETER_SECTIONinit_bounded_dev_vector x(1,m,-10,10)

• Each element must take value between lower and upper bounds-10 < xi < 10

• All elements must sum to zero

m

iix

1

0

Page 205: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Difference Method

• Estimate n free parameters:X1, 1, . . . , n-1

• ThenlogX1

logX2 = logX1 + 1

. . . . .

logXn = logXn-1 + n-1

Page 206: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Random Walk

• Estimate n parameters

X1, 1, . . . , n-1

• Then

21

,0~

loglog

N

XX

i

iii

Page 207: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Time-Varying Von Bertalanffy Growth Model

• Asymptotic length varies over time

• Mean length at age-1 (L1) and Brody growth coefficient (K) are constant over time

2,

,,,1,1

,0~

1ˆ ,

N

eeLLLL

ay

Kayyayay

ay

L

Page 208: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Random Walk

• Model time-varying asymptotic length

2

,1,

,1,

,0~

loglog

N

LL

eLL

y

yyy

yyy

Page 209: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Model Parameters

K – Brody growth coefficient

Lyr=1…10,age=1 – mean length at age 1 for all years

1, . . . , 9 – annual deviation of L

L,1 – initial asymptotic length

Lyr=1,age=2…9 – mean length at ages 2…9 for yr. 1

Page 210: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Negative Log Likelihood

2

22

1

1 ˆlog log log log2 2

n

i ii

nLike L L

222

1

1log 0

2 2

m

ii

m

Page 211: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Concentrated Negative Log Likelihood

1 2

1 2 2

2 22 2 1 11 2

[ ] log[ ]2

ˆˆ ˆ, , i i

i i

NConc LogL RSS

N n n

RSS RSS RSS

RSS

N

Page 212: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Now to look at the code…..

Page 213: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Overview of Catch-At-Age

CAA estimates of population dynamics

CAA predictions of observed data

Observed data Negative log likelihood

Page 214: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Basic Relationships in Catch-at-Age Models

• Catch in relation to current abundance at fishing and natural mortalities

• Number at any age in relation to initial cohort strength and cumulative fishing and natural mortality rates

• Relationship between fishing mortality and fishing effort (catchability)

Page 215: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Observed Data

• Total annual fishery catch

• Proportion of catch-at-age

• Auxiliary data– Fishing effort– Survey index of relative abundance– Tagging data (to estimate M)

Page 216: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Population Submodel

logR+1

logR+2

logR+m

. . . .

logN1,1+1 . . . . logN1,n-1+n-1

Recruitment

Initial numbers at age

Page 217: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Population Submodel

ayay

Zayay

FMZ

eNN ay

,,

,1,1,

Numbers of fish Survival

Total mortality Fishing mortality

Natural mortality

Page 218: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Population Submodel

2

,

,0~

N

eqEsF

y

yaayy

Selectivity Effort

Catchability Effort error

Page 219: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Observation Submodel

• Baranov’s catch equation

ayZ

ay

ayay Ne

Z

FC ay

,,

,,

,1

Page 220: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Observation Submodel

2

,

,0~

~

N

eCC

CC

y

yy

aayy

y

Total catch

Observation error

Observed total catch

Page 221: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Observation Submodel

E

ayay

Eayy

y

ayay

N

nP

NpMNOMPn

C

CP

,,

,E

,,

~

~N

Proportion of catch-at-age

Numbers sampled at age Proportions

Effective sample size

Obs. proportion of catch-at-age

Page 222: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Negative Log Likelihoodfor Multinomial

y aayayyE

y kyyy

yE

pPN

nnn

NL

,,,

,2,1,

,

log~

!!...!

!loglog

Page 223: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Model Parameters

q s1, . . . , sn-1

1, . . . , m

2

2

Ratio of relative variances (assumed known)

1, . . . , mR 1, . . . , n-1

Page 224: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Negative Log Likelihood (ignoring constants)

2

2

~log

2

1loglog

y y

y

C

CmL

y

ym 222

1log

y a

ayayE PPN ,, log~

Page 225: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Now to look at the code…..

Page 226: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Simulating Data

Page 227: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Simulating Data

• Data simulations are useful for testing models

• How well does model perform when processes underlying “reality” are known?– The “true” values of parameters and variables

can be compared to model estimates

• Make sure model works before using real world data

Page 228: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Simulating Data

• “Parameter” values are read in from dat file

• “Parameter” values used in estimation model equations to calculate true data

• Random number generator creates random errors

• Adding random error to true data gives observed data

Page 229: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Data Generating Model

• Recruitments generated from white noise process

2

1,

,0~

eR

N

N

y

yy

Input “parameters”

Page 230: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Data Generating Model

• Numbers at age in first year came from applying mortality to randomly generated recruitments

21,1

1,1,1

,~

1,0

NNa

Z

aa

LNN

eNN

a

jj

Page 231: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Input “Parameters”

q

s1, . . . , sn

Must also provide seed for random number generator

R

1, . . . , m

Page 232: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Tpl file must still contain:

• Data section

• Parameter section

• Procedure section

• objective_function_value

• One active parameter

Page 233: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Data Generating Model

• Most of work is done in preliminary calcs section or using local_calcs command– Operations involved only need to be run once

• Use “exit(0);” command at end of local_calcs since no parameters or asymptotic standard errors need to be estimate

Page 234: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Time for some code…..

Page 235: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Simulation Study

• Simulation study combines a data generating model with a control program to repeatedly fit an estimating model to many simulated data sets

• This provides replicate model runs to better evaluate an estimating model’s performance– Only one replicate normally is available in the

real world

Page 236: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Simulation Study

• Can evaluate how a model performs vs. different underlying “reality”– E.g., with different levels of observation error

• Can evaluate how well different models can fit the same data sets– E.g., fit Ricker and Beverton-Holt stock-

recruitment models to same data sets

• Or can use a combination of the two approaches

Page 237: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Simulation Study Practicum

• You have seen:– Catch-at-age model– Data generating model for catch-at-age model– Control program for data generating and

catch-at-age models

• Now you need to modify the three programs to run a Monte Carlo simulation study

Page 238: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Simulation Study Practicum

• Your simulation study will look at the effects of process error on performance of a catch-at-age model

• Data sets will be generated using two levels (low and high) of process error for catchability

Page 239: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Let’s code a simulation study…..

Page 240: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Miscellaneous Topics and Tricks

• Missing data

• Advanced ADMB functions

• Using dat file for flexibility

Page 241: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Missing Data

• It is not uncommon to have missing years of data in a time series of observed data

• One solution is to interpolate the missing years of data outside the model fitting process by some ad hoc method– E.g., averaging data from the adjacent years

• A better solution is to allow the model to predict values for the missing data– This takes advantage of all the available data

Page 242: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Missing DataImplementation

• Use special value to denote missing data in dat file– E.g., a value you wouldn’t normally see in real

data like -1

• Use loops and conditional statements to exclude missing data values from objective function value

• Otherwise, model will try to match predicted values to the missing data values

Page 243: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Missing DataMultinomial Case

• Replace missing data with 0 and it will not contribute to negative log likelihood value

y a

ayayE PPNL ,, log~

log

Page 244: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Advanced Functions

• Filling objects

• Obtaining shape information

• Extracting subobjects

• Sorting vectors and matrices

• Cumulative density functions

Page 245: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Filling Objects

v.fill(“{1,2,3,6}”); // v=[1,2,3,6]

v.fill_seqadd(1,0.5); // v=[1,1.5,2,2,5]

m.rowfill_seqadd(3,1,0.5); // fill row 3 with sequence

m.colfill_seqadd(2,1,0.5); // fill column 2 with sequence

m.rowfill(3,v); // fill row 3 with vector v

m.colfill(2,v); // fill column 2 with vector v

Page 246: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Obtaining Shape Information

i=v.indexmax(); // returns maximum index

i=v.indexmin(); // returns minimum index

i=m.rowmax(); // returns maximum row index

i=m.rowmin(); // returns minimum row index

i=m.colmax(); // returns maximum column index

i=m.colmin(); // returns minimum column index

Page 247: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Extracting Subobjects

v=column(m,2); // extract column 2 of m

v=extract_row(m,3); // extract row 3 of m

v=extract_diagonal(m); // extract diagonal elements of m

vector u(1,20)

vector v(1,19)

u(1,19)=v; // assign values of v to elements 1-19 of u

--u(2,20)=v; // assign values of v to elements 2-20 of u

u(2,20)=++v; // assign values of v to elements 2-20 of u

u.shift(5); // new min is 5 new max is 24

Page 248: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Sorting Objects

• Sorting vectorsw=sort(v); // sort elements of v in ascending order

• Sorting matricesx=sort(m,3); // sort columns of m, with column 3 in

ascending order

Page 249: Minnesota AD Model Builder Short Course October 22-24, 2007 Thanks to Jim Bence, Brian Linton, and Brian Irwin for providing materials used in previous

Cumulative Density Functions

• For standard normal distribution x=cumd_norm(z); // x=p(Z<=z), Z~N(0,1)

• Also have CDF for Cauchy distribution cumd_cauchy()