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

Commit

Permalink
Adding Week 5 materials
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Thorson committed Apr 23, 2018
1 parent 00e33f6 commit 2447532
Show file tree
Hide file tree
Showing 13 changed files with 437 additions and 0 deletions.
Binary file not shown.
24 changes: 24 additions & 0 deletions Week 5 -- 1D spatial models/Homework/Homework_Week_5_simulator.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

Sim_Fn = function( n_i=1000, Scale=2, Sigma2=1, logSD_spatial=0.1, L0=10, Linf_0=100, beta_y=0.02, growth_rate=0.1, mortality_rate=growth_rate*1.6, logSD_resid=0.05){
require( RandomFields )

# Sample locations
n_i = 1000
y_i = runif( n=n_i, min=32, max=49 )

# Simulate spatial process
RMmodel = RMgauss(var=logSD_spatial^2, scale=Scale)
Linf_i = Linf_0 * exp( RFsimulate(model=RMmodel, x=rep(0,n_i), y=y_i)@data[,1] - Sigma2/2 ) * exp( beta_y*(y_i-40.5) )
plot( y=Linf_i, x=y_i )

# Simulate ages of samples
Survival_a = exp( -mortality_rate * 1:100 )
a_i = sample( size=n_i, x=1:100, prob=Survival_a, replace=TRUE)
l_i = Linf_i - (Linf_i-L0) * exp( -growth_rate * a_i ) * exp( rnorm(n_i, mean=-logSD_resid^2/2, sd=logSD_resid) )
plot( x=a_i, y=l_i )

# Bundle and return stuff
DF = data.frame( "y_i"=y_i, "a_i"=a_i, "l_i"=l_i, "Linf_i"=Linf_i)
return(DF)
}

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.
80 changes: 80 additions & 0 deletions Week 5 -- 1D spatial models/Lab/Lab 5.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@


library(TMB)
setwd( "C:/Users/James.Thorson/Desktop/Project_git/2018_FSH556/Week 5 -- 1D spatial models/Lab/" )

###################
# Visualize autocorrelation
###################

Rho = 0.8
X = rep(0,10)
for(s in 2:length(X)) X[s] = X[s-1] + rlnorm(1, meanlog=0, sdlog=1)
Corr = Rho^X
X_line = seq(0,max(X),length=1e4)
Corr_line = Rho^(X_line)

png( file="Autocorrelation.png", width=4, height=4, res=200, units='in')
plot( x=X, y=Corr, xlab="distance", ylab="Correlation", ylim=c(0,1) )
lines( x=X_line, y=Corr_line, xlab="distance", ylab="Correlation", ylim=c(0,1) )
dev.off()

###################
# Unequal distance autoregressive
###################

x = 1:100
Rho = 0.8
Sigma2 = (0.5) ^ 2
n_rep = 3
beta0 = 3

# Simulate locations
loc_s = rep(NA, length(x))
loc_s[1] = 0
for(s in 2:length(x)) loc_s[s] = loc_s[s-1] + rlnorm(1, meanlog=0, sdlog=1)

# Simulate spatial process
epsilon_s = rep(NA, length(x))
epsilon_s[1] = rnorm(1, mean=0, sd=sqrt(Sigma2))
for(s in 2:length(x)) epsilon_s[s] = Rho^abs(loc_s[s]-loc_s[s-1]) * epsilon_s[s-1] + rnorm(1, mean=0, sd=sqrt(Sigma2*(1-Rho^(2*abs(loc_s[s]-loc_s[s-1])))) )

# SImulate counts
c_si = matrix( nrow=length(x), ncol=n_rep)
for(s in 1:nrow(c_si)){
for(i in 1:ncol(c_si)){
c_si[s,i] = rpois(1, exp(beta0 + epsilon_s[s]) )
}}

# Compile
Params = list( "beta0"=0, "ln_sigma2"=0, "logit_rho"=0, "epsilon_s"=rnorm(length(x)) )
compile( "unequal_distance_autoregressive_V1.cpp" )
dyn.load( dynlib("unequal_distance_autoregressive_V1") )

######## Version 0 -- Stochastic process with automatic sparseness detection
# Build object
Data = list("Options_vec"=c(0), "c_si"=c_si, "loc_s"=loc_s )
Obj = MakeADFun( data=Data, parameters=Params, random="epsilon_s", DLL="unequal_distance_autoregressive_V1" )
# Optimize
Opt0 = TMBhelper::Optimize( obj=Obj, newtonsteps=1 )
par0 = Opt0$par
h0 = Obj$env$spHess(random=TRUE)

######## Version 2 -- Covariance and built-in function
# Build object
Data = list("Options_vec"=c(2), "c_si"=c_si, "loc_s"=loc_s )
Obj = MakeADFun( data=Data, parameters=Params, random="epsilon_s", DLL="unequal_distance_autoregressive_V1" )
# Optimize
Opt2 = TMBhelper::Optimize( obj=Obj, newtonsteps=1 )
par2 = Opt2$par
h2 = Obj$env$spHess(random=TRUE)

# Compare timing
Opt0$run_time
Opt2$run_time

# Compare separability
library(INLA)
image(h0, main="Version 0"); dev.new()
image(h2, main="Version 2")

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

#include <TMB.hpp>

// Space time
template<class Type>
Type objective_function<Type>::operator() ()
{
// Settings
DATA_IVECTOR( Options_vec );
// Slot 0: method for calculating probability of random effects

// Data
DATA_MATRIX( c_si );
DATA_VECTOR( loc_s );

// Parameters
PARAMETER( beta0 );
PARAMETER( ln_sigma2 );
PARAMETER( logit_rho );

// Random effects
PARAMETER_VECTOR( epsilon_s );

// Objective funcction
int n_s = c_si.col(0).size();
int n_i = c_si.row(0).size();
vector<Type> jnll_comp(2);
jnll_comp.setZero();
Type sigma2 = exp(ln_sigma2);
Type rho = 1 / (1 + exp(-logit_rho));

// Probability of random effects
using namespace density;
vector<Type> dist_s(n_s);
if( Options_vec(0)==0 ){
jnll_comp(1) -= dnorm( epsilon_s(0), Type(0.0), pow(sigma2,0.5), true );
for(int s=1; s<n_s; s++){
dist_s(s) = loc_s(s)-loc_s(s-1);
jnll_comp(1) -= dnorm( epsilon_s(s), pow(rho,dist_s(s))*epsilon_s(s-1), pow(sigma2*(1-pow(rho,2*dist_s(s))),0.5), true );
}
}
if( Options_vec(0)==1 ){
// NOT IMPLEMENTED //
}
if( Options_vec(0)==2 ){
matrix<Type> Cov_ss(n_s,n_s);
for(int s1=0; s1<n_s; s1++){
for(int s2=s1; s2<n_s; s2++){
Cov_ss(s1,s2) = sigma2 * pow( rho, loc_s(s2)-loc_s(s1) );
if(s1!=s2) Cov_ss(s2,s1) = Cov_ss(s1,s2);
}}
REPORT( Cov_ss );
jnll_comp(1) += MVNORM(Cov_ss)( epsilon_s );
}
if( Options_vec(0)==3 ){
// NOT IMPLEMENTED //
}

// Probability of data conditional on random effects
for( int s=0; s<n_s; s++){
for( int i=0; i<n_i; i++){
jnll_comp(0) -= dpois( c_si(s,i), exp(beta0 + epsilon_s(s)), true );
}}

// Reporting
Type jnll = jnll_comp.sum();
REPORT( jnll_comp );
REPORT( jnll );
REPORT( sigma2 );
REPORT( rho );

return jnll;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
150 changes: 150 additions & 0 deletions Week 5 -- 1D spatial models/Lecture/Lecture_5.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@


library(TMB)
setwd( "C:/Users/James.Thorson/Desktop/Project_git/2018_FSH556/Week 5 -- 1D spatial models/Lecture/" )

############################
# Visualize Gaussian process
############################

library(RandomFields)

png( "Gaussian_process.png", width=8, height=5, res=200, units="in")
par( mfrow=c(2,2), mar=c(2,2,2,0), mgp=c(2,0.5,0) )
for(i in 1:4){
Scale = c(10, 10, 25, 25)[i]
SD = c(0.5, 1, 0.5, 1)[i]
x_i = seq(0,100,length=100)
RMmodel = RMgauss(var=SD^2, scale=Scale)
plot( 1, type="n", xlim=range(x_i), ylim=c(-3,3), main=paste0("SD=",SD," Scale=",Scale), xlab="", ylab="")
for(j in 1:20){
gp_i = RFsimulate(model=RMmodel, x=x_i, y=rep(0,length(x_i)))@data[,1]
lines( x=x_i, y=gp_i )
}
}
dev.off()

###################
# Visualize autocorrelation
###################

Rho = 0.8
X = 0:10
Corr = Rho^X

png( file="Autocorrelation.png", width=4, height=4, res=200, units='in')
plot( x=X, y=Corr, xlab="distance", ylab="Correlation", ylim=c(0,1) )
dev.off()

###################
# Visualize autocorrelation vs. random-walk
###################

Rho = c(-0.5,0,0.5,0.99)
Marginal_Sigma = 1
Conditional_Sigma = sqrt( 1-Rho^2 ) * Marginal_Sigma

Y = X = 1:1e2
png( file="Autocorrelated_series.png", width=4, height=6, res=200, units='in')
par( mfrow=c(4,1), mgp=c(2,0.5,0), mar=c(2,2,1,0), xaxs="i")
for(i in 1:4){
Y[1] = 0
for(s in 2:length(X)) Y[s] = Y[s-1]*Rho[i] + rnorm(1, mean=0, sd=Conditional_Sigma[i])
plot( x=X, y=Y, xlab="location", ylab="value", ylim=c(-3,3), type="l", main=paste0("Rho = ",Rho[i]) )
}
dev.off()

###################
# Math check on inverse-covariance matrix
###################
Rho = 0.5
Sigma2 = 2 ^ 2
x = 1:10
Dist = outer(x, x, FUN=function(a,b){abs(a-b)})

# Calculate Q directly
Cov = Sigma2 / (1-Rho^2) * Rho^Dist
Q = solve(Cov)

# Calculate Q analytically
M = diag(length(x)) * (1+Rho^2)
M[ cbind(1:9,2:10) ] = -Rho
M[ cbind(2:10,1:9) ] = -Rho
Q2 = 1/Sigma2 * M

###################
# Equal distance autoregressive
###################

x = 1:100
Rho = 0.8
Sigma2 = (0.5) ^ 2
n_rep = 3
beta0 = 3

# Simulate spatial process
epsilon_s = rep(NA, length(x))
epsilon_s[1] = rnorm(1, mean=0, sd=sqrt(Sigma2))
for(s in 2:length(x)) epsilon_s[s] = Rho*epsilon_s[s-1] + rnorm(1, mean=0, sd=sqrt(Sigma2))

# SImulate counts
c_si = matrix( nrow=length(x), ncol=n_rep)
for(s in 1:nrow(c_si)){
for(i in 1:ncol(c_si)){
c_si[s,i] = rpois(1, exp(beta0 + epsilon_s[s]) )
}}

# Compile
Params = list( "beta0"=0, "ln_sigma2"=0, "logit_rho"=0, "epsilon_s"=rnorm(length(x)) )
compile( "autoregressive_V1.cpp" )
dyn.load( dynlib("autoregressive_V1") )

######## Version 0 -- Stochastic process with automatic sparseness detection
# Build object
Data = list("Options_vec"=c(0), "c_si"=c_si )
Obj = MakeADFun( data=Data, parameters=Params, random="epsilon_s", DLL="autoregressive_V1" )
# Optimize
Opt0 = TMBhelper::Optimize( obj=Obj, newtonsteps=1 )
par0 = Opt0$par
h0 = Obj$env$spHess( random=TRUE )

######## Version 1 -- Analytic precision matrix
# Build object
Data = list("Options_vec"=c(1), "c_si"=c_si )
Obj = MakeADFun( data=Data, parameters=Params, random="epsilon_s", DLL="autoregressive_V1" )
# Optimize
Opt1 = TMBhelper::Optimize( obj=Obj, newtonsteps=1 )
par1 = Opt1$par
h1 = Obj$env$spHess(random=TRUE)

######## Version 2 -- Covariance and built-in function
# Build object
Data = list("Options_vec"=c(2), "c_si"=c_si )
Obj = MakeADFun( data=Data, parameters=Params, random="epsilon_s", DLL="autoregressive_V1" )
# Optimize
Opt2 = TMBhelper::Optimize( obj=Obj, newtonsteps=1 )
par2 = Opt2$par
h2 = Obj$env$spHess(random=TRUE)

######## Version 3 -- Built-in function for AR process
# Build object
Data = list("Options_vec"=c(3), "c_si"=c_si )
Obj = MakeADFun( data=Data, parameters=Params, random="epsilon_s", DLL="autoregressive_V1" )
# Optimize
Opt3 = TMBhelper::Optimize( obj=Obj, newtonsteps=1 )
par3 = Opt3$par
h3 = Obj$env$spHess(random=TRUE)

# Compare timing
Opt0$run_time
Opt1$run_time
Opt2$run_time
Opt3$run_time

# Compare separability
library(INLA)
image(h0, main="Version 0"); dev.new()
image(h1, main="Version 1"); dev.new()
image(h2, main="Version 2"); dev.new()
image(h3, main="Version 3")

Loading

0 comments on commit 2447532

Please sign in to comment.