Skip to content
This repository has been archived by the owner on Jul 29, 2020. It is now read-only.

Commit

Permalink
added Week 1 materials
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Thorson committed Mar 26, 2018
1 parent e21d84c commit 808a1b0
Show file tree
Hide file tree
Showing 28 changed files with 601 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.o
*.dll
*.so
36 changes: 36 additions & 0 deletions TMB examples/map argument/.Rapp.history
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Simulate predictors#
Factor = rep( 1:10, each=10)#
Z = rnorm( length(unique(Factor)), mean=0, sd=1)#
X0 = 0#
#
# Simulate response#
Y = Z[Factor] + X0 + rnorm( length(Factor), mean=0, sd=1)
library(TMB)#
#
# Compile model#
Version = "linear_mixed_model"#
compile( paste0(Version,".cpp") )#
#
# Build inputs#
Data = list( "n_data"=length(Y), "n_factors"=length(unique(Factor)), "Factor"=Factor-1, "Y"=Y)#
Parameters = list( "X0"=-10, "log_SD0"=2, "log_SDZ"=2, "Z"=rep(0,Data$n_factor) )
Data
Parameters
# Turn off random effects#
Map = list()#
Map[["log_SDZ"]] = factor(NA)#
Map[["Z"]] = factor( rep(NA,Data$n_factor) )
Map
# Build object#
dyn.load( dynlib("linear_mixed_model") )#
Obj = MakeADFun(data=Data, parameters=Parameters, map=Map)
Obj
Obj$par
# Turn off random effects#
Map = list()#
Map[["log_SDZ"]] = factor(NA)
Map
# Build object#
dyn.load( dynlib("linear_mixed_model") )#
Obj = MakeADFun(data=Data, parameters=Parameters, map=Map)
Obj$par
50 changes: 50 additions & 0 deletions TMB examples/map argument/linear_mixed_model.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

setwd( "C:/Users/James.Thorson/Desktop/Project_git/2016_Spatio-temporal_models/TMB examples/map argument" )

######################
# Simulate data
######################

# Simulate predictors
Factor = rep( 1:10, each=10)
Z = rnorm( length(unique(Factor)), mean=0, sd=1)
X0 = 0

# Simulate response
Y = Z[Factor] + X0 + rnorm( length(Factor), mean=0, sd=1)

######################
# Run in TMB
######################

library(TMB)

# Compile model
Version = "linear_mixed_model"
compile( paste0(Version,".cpp") )

# Build inputs
Data = list( "n_data"=length(Y), "n_factors"=length(unique(Factor)), "Factor"=Factor-1, "Y"=Y)
Parameters = list( "X0"=-10, "log_SD0"=2, "log_SDZ"=2, "Z"=rep(0,Data$n_factor) )

# Turn off random effects
Map = list()
Map[["log_SDZ"]] = factor(NA)
Map[["Z"]] = factor( rep(NA,Data$n_factor) )

# Build object
dyn.load( dynlib("linear_mixed_model") )
Obj = MakeADFun(data=Data, parameters=Parameters, map=Map) #

# Prove that function and gradient calls work
Obj$fn( Obj$par )
Obj$gr( Obj$par )

# Optimize
start_time = Sys.time()
Opt = nlminb( start=Obj$par, objective=Obj$fn, gradient=Obj$gr, control=list("trace"=1) )

# Get reporting and SEs
Report = Obj$report()
SD = sdreport( Obj )

45 changes: 45 additions & 0 deletions TMB examples/map argument/linear_mixed_model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() ()
{
// Data
DATA_INTEGER( n_data );
DATA_INTEGER( n_factors );
DATA_IVECTOR( Factor );
DATA_VECTOR( Y );

// Parameters
PARAMETER( X0 );
PARAMETER( log_SD0 );
PARAMETER( log_SDZ );
PARAMETER_VECTOR( Z );

// Objective funcction
Type jnll = 0;

// Probability of data conditional on fixed and random effect values
for( int i=0; i<n_data; i++){
jnll -= dnorm( Y(i), X0 + Z(Factor(i)), exp(log_SD0), true );
}

// Probability of random coefficients
for( int i=0; i<n_factors; i++){
jnll -= dnorm( Z(i), Type(0.0), exp(log_SDZ), true );
}

// Reporting
Type SDZ = exp(log_SDZ);
Type SD0 = exp(log_SD0);

REPORT( SDZ );
REPORT( SD0 );
REPORT( Z );
REPORT( X0 );

ADREPORT( SDZ );
ADREPORT( SD0 );
ADREPORT( Z );
ADREPORT( X0 );

return jnll;
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
156 changes: 156 additions & 0 deletions Week 1 -- Likelihoods and linear models/Lab 1/Lab_1_code_V1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@

setwd( "C:/Users/James.Thorson/Desktop/Project_git/2018_FSH556/Week 1 -- Likelihoods and linear models/Lab 1" )
library(TMB)

###########
# Nonlinear optimization
###########

library("animation") # Requires v2.12 or higher, also need to download: http://www.imagemagick.org/script/binary-releases.php

# Nonlinear function
RosenbrookFn = function(Params, Write=TRUE){
Dev = (1-Params[1])^2 + 100*(Params[2]-Params[1]^2)^2
if(Write==TRUE){
if("Trace.txt" %in% list.files()){
write.table(matrix(c(Dev,Params),nrow=1), file="Trace.txt", append=TRUE,col.names=FALSE,row.names=FALSE)
}else{
write.table(matrix(c(Dev,Params),nrow=1), file="Trace.txt", append=FALSE,col.names=FALSE,row.names=FALSE)
}
}
return(Dev)
}

# Generate contour
X = seq(-10,20,length.out=1e3)
Y = seq(-10,20,length.out=1e3)
Z = matrix(NA, ncol=length(X), nrow=length(Y))
for(xI in 1:length(X)){
for(yI in 1:length(Y)){
Z[xI,yI] = RosenbrookFn(c(X[xI],Y[yI]), Write=FALSE)
}}

# Rosenbrook function
png( file="Rosenbrook.png", width=6, height=6, res=200, units="in")
par( mar=c(3,3,2,0) )
contour(x=X, y=Y, z=Z, levels=seq(0,1000,by=2)^5+1)
points( x=1, y=1, cex=2, col="red", lwd=2)
dev.off()

# Plot path -- Nelder Mead
if("Trace.txt" %in% list.files()) file.remove("Trace.txt")
Opt = nlminb( start=c(4,4), objective=RosenbrookFn, Write=TRUE )
Trace = read.table("Trace.txt" )
contour(x=X, y=Y, z=Z, levels=seq(0,1000,by=10)^3+1)
lines(x=Trace[,2], y=Trace[,3],col="red")
ani.options(interval=0.10, nmax=1e4)
saveVideo(expr={for(i in 1:nrow(Trace)){contour(x=X, y=Y, z=Z, levels=seq(0,1000,by=10)^3+1); lines(x=Trace[max(1,i-10):i,2], y=Trace[max(1,i-10):i,3],col="red",lwd=3); points(x=c(1,Trace[i,2]),y=c(1,Trace[i,3]), col=c("black","red"),cex=2,lwd=3)}}, video.name="Nelder-Mead.mp4", convert = 'gm convert', clean=TRUE) #

# Plot path -- BFGS
if("Trace.txt" %in% list.files()) file.remove("Trace.txt")
Opt = optim( par=c(4,4), fn=RosenbrookFn, Write=TRUE )
Trace = read.table("Trace.txt" )
contour(x=X, y=Y, z=Z, levels=seq(0,1000,by=10)^3+1)
lines(x=Trace[,2], y=Trace[,3],col="red")
ani.options(interval=0.10, nmax=1e4)
saveVideo(expr={for(i in 1:nrow(Trace)){contour(x=X, y=Y, z=Z, levels=seq(0,1000,by=10)^3+1); lines(x=Trace[max(1,i-10):i,2], y=Trace[max(1,i-10):i,3],col="red",lwd=3); points(x=c(1,Trace[i,2]),y=c(1,Trace[i,3]), col=c("black","red"),cex=2,lwd=3)}}, video.name="BFGS.mp4", convert = 'gm convert', clean=TRUE) #

# Plot path -- TMB
# Step 1 -- make and compile template file
compile( "Rosenbrook.cpp" )

# Step 2 -- build inputs and object
dyn.load( dynlib("Rosenbrook") )
Params = list( "Params"=c(4,4) )
Data = list( "dummy"=0 )
Obj = MakeADFun( data=Data, parameters=Params, DLL="Rosenbrook")

# Redefine functions
Obj$fn_trace <- function( Params, ...){
Dev = Obj$fn( Params, ... )
if("Trace.txt" %in% list.files()){
write.table(matrix(c(Dev,Params),nrow=1), file="Trace.txt", append=TRUE,col.names=FALSE,row.names=FALSE)
}else{
write.table(matrix(c(Dev,Params),nrow=1), file="Trace.txt", append=FALSE,col.names=FALSE,row.names=FALSE)
}
return( Dev )
}

# Step 3 -- test and optimize
if("Trace.txt" %in% list.files()) file.remove("Trace.txt")
Opt = nlminb( start=Obj$par, objective=Obj$fn_trace, gradient=Obj$gr )
Trace = read.table("Trace.txt" )
contour(x=X, y=Y, z=Z, levels=seq(0,1000,by=10)^3+1)
lines(x=Trace[,2], y=Trace[,3],col="red")
ani.options(interval=0.10, nmax=1e4)
saveVideo(expr={for(i in 1:nrow(Trace)){contour(x=X, y=Y, z=Z, levels=seq(0,1000,by=10)^3+1); lines(x=Trace[max(1,i-10):i,2], y=Trace[max(1,i-10):i,3],col="red",lwd=3); points(x=c(1,Trace[i,2]),y=c(1,Trace[i,3]), col=c("black","red"),cex=2,lwd=3)}}, video.name="TMB.mp4", convert = 'gm convert', clean=TRUE) #

###########
# Delta-model for canary rockfish
###########

devtools::install_github("nwfsc-assess/geostatistical_delta-GLMM")
library( SpatialDeltaGLMM )

#
data(WCGBTS_Canary_example)
CPUE = WCGBTS_Canary_example$HAUL_WT_KG
X = cbind( "Intercept"=rep(1,length(CPUE)) )

# Step 1 -- make and compile template file
compile( "delta_model_v1.cpp" )

# Step 2 -- build inputs and object
dyn.load( dynlib("delta_model_v1") )
Params = list("b_j"=rep(0,ncol(X)), "theta_z"=c(0,0))
Data = list( "y_i"=CPUE, "X_ij"=X )
Obj = MakeADFun( data=Data, parameters=Params, DLL="delta_model_v1")

# Step 3 -- test and optimize
Obj$fn( Obj$par )
Obj$gr( Obj$par )
Opt = nlminb( start=Obj$par, objective=Obj$fn, gradient=Obj$gr )
Opt$diagnostics = data.frame( "name"=names(Obj$par), "Est"=Opt$par, "final_gradient"=as.vector(Obj$gr(Opt$par)))
Opt$par # estimated parameters
SD = sdreport( Obj ) # standard errors

# Extract stuff
Report = Obj$report()

# Visualize fit
png( file="Canary_histogram--with_fit.png", width=4, height=4, res=200, units="in")
par( mar=c(3,3,2,0), mgp=c(2,0.5,0), tck=-0.02)
hist( log(1+CPUE), freq=FALSE, col=rgb(1,0,0,0.2) )
Sim_CPUE = (1-rbinom(1e5, size=1, prob=Report$zero_prob)) * rlnorm(1e5, meanlog=Report$linpred_i, sdlog=Report$logsd)
hist( log(1+Sim_CPUE), freq=FALSE, add=TRUE, col=rgb(0,0,1,0.2) )
legend( "topright", bty="n", legend=c("Observed","Predicted"), fill=c("red","blue"))
dev.off()

###########
# Crossvalidation experiment using delta-model for canary rockfish
###########

# Step 0 -- make and compile template file
compile( "delta_model_v2.cpp" )
dyn.load( dynlib("delta_model_v2") )

# Step 1 -- divide into partitions
K = 10
Partition_i = sample( x=1:K, size=length(CPUE), replace=TRUE )
PredNLL_k = rep(NA, K)

# Step 2 --Loop through partitions
for(k in 1:K){
Params = list("b_j"=rep(0,ncol(X)), "theta_z"=c(0,0))
Data = list( "y_i"=CPUE, "X_ij"=X, predTF_i=ifelse(Partition_i==k,1,0) )
Obj = MakeADFun( data=Data, parameters=Params, DLL="delta_model_v2")
Opt = nlminb( start=Obj$par, objective=Obj$fn, gradient=Obj$gr )
Report = Obj$report()
PredNLL_k[k] = Report$pred_jnll
}

# log-Predictive probability per datum
mean( PredNLL_k / table(Partition_i) )



Binary file not shown.
17 changes: 17 additions & 0 deletions Week 1 -- Likelihoods and linear models/Lab 1/Rosenbrook.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Space time
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() ()
{
// Data
DATA_SCALAR( dummy );

// Parameters
PARAMETER_VECTOR( Params );

// Objective funcction
Type jnll = pow(1-Params(0),2) + 100*pow(Params(1)-pow(Params(0),2),2);

// Reporting
return jnll;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
42 changes: 42 additions & 0 deletions Week 1 -- Likelihoods and linear models/Lab 1/Trace.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
14409 4 4
2427.00072099677 3.00771735492052 4.12399658170315
405.567414727713 2.65858999238928 5.06107186402148
18.85016210181 2.54167027391276 6.05421323332969
2.84052160663671 2.66012128811671 7.04717313042224
2.48500715651973 2.57408553778493 6.6344380201405
2.23172687072318 2.49322944264862 6.22065702314602
3.18801765974419 2.16815385655468 4.56585654912003
2.00271594240845 2.41024401150956 5.79747459749452
1.87488605284059 2.3256970972798 5.37460137008961
1.77194653364814 2.28315403564269 5.17737169908532
1.35689715514021 2.14013048534639 4.55628389870529
1.09787003107786 2.03695670644768 4.13416238108617
1.00957541887104 1.85503830706771 3.38839545098624
0.568839706248912 1.74096609143522 3.04503736138855
0.434842044388779 1.64595911534794 2.69592287792184
0.407023062869276 1.56884793522114 2.43239871108632
0.330273705858859 1.5552667427509 2.40403824807302
0.281940582356929 1.52955414047999 2.33564614545925
0.236492322508975 1.48383012387979 2.19685210823917
0.191985011699856 1.39169261567786 1.91717115075547
0.145941492546445 1.37338807097366 1.87811838277943
0.119799804021527 1.34216179988057 1.79617804158212
0.105536983836215 1.25267970672711 1.54878833077767
0.0647292580087596 1.25422104341677 1.5720658407961
0.0601678332615207 1.24528231446488 1.55051781730963
0.0484329717987803 1.2194670526587 1.48546531429903
0.0683766725485752 1.07912909721802 1.13959667554488
0.0311203129994762 1.16671083829298 1.35544546335281
0.0204533108352537 1.11149011793839 1.22645301459011
0.00854578852597585 1.0884630049999 1.18206827278944
0.00524697907085572 1.06741031796208 1.13671369635572
0.00323015633269444 1.04285120562733 1.08380509919993
0.000662638916923775 1.02572915358576 1.05203970044365
0.000156811462375771 1.01008182154083 1.01952253229918
1.18957012499119e-05 1.00216171569509 1.00405935384206
2.97447697019197e-07 1.00053469810139 1.0010804271676
9.18162300049358e-09 0.999928188914863 0.999850038862464
3.69301837436315e-13 1.00000039517812 1.00000083652307
3.99639202020881e-19 1.00000000039871 1.00000000084648
2.36046904364757e-27 0.999999999999961 0.999999999999925
2.36046904364757e-27 0.999999999999961 0.999999999999925
Loading

0 comments on commit 808a1b0

Please sign in to comment.