######################################################################
# Bayesian hierarchical Poisson model for performing nowcasting stated
# in the BUGS/JAGS modelling language. This is a template file -
# based on the control options, the nowcasttrunc function removes
# tags or inserts extra code - i.e. this file can not run with JAGS
# without extra modification
#
# Author: Michael Höhle <http://www.math.su.se/~hoehle>
######################################################################

model {
    ##################################################
    #Prior for the TPS
    ##################################################

    #<tps> #Priors for the regression coefficients. A joint distribution
    #<tps> #forces the updated to be MV metropolis hastings (might not be better as slice sampler)
    #<tps> beta ~ dmnorm( beta.mu, beta.prec)

    #<tps> #Random effects with automatic smoothing
    #<tps> for (k in 1:nknots){
    #<tps>  	  b[k] ~ dnorm(0, tau.b)
    #<tps> }
    #<tps> tau.b ~ dgamma(0.001, 0.001)

    #<rw> #1st order random walk prior for lambda[t]
    #<rw> logLambda[1] ~ dnorm(-10, tau.logLambda)
    #<rw> for (t in 2:T) {
    #<rw>	logLambda[t] ~ dnorm( logLambda[t-1], tau.logLambda)
    #<rw>    }
    #<rw> tau.logLambda ~ dgamma(0.001,0.001)

    #<rw2> #2nd order random walk prior for lambda[t]
    #<rw2> logLambda[1] ~ dnorm(-10, tau.logLambda)
    #<rw2> logLambda[2] ~ dnorm(-10, tau.logLambda)
    #<rw2> for (t in 3:T) {
    #<rw2>	logLambda[t] ~ dnorm( 2*logLambda[t-1] - logLambda[t-2], tau.logLambda)
    #<rw2> }
    #<rw2> tau.logLambda ~ dgamma(0.001,0.001)

    #<iidLogGa> #iid lambda, which are Ga-distributed
    #<iidLogGa> for (t in 1:T) {
    #<iidLogGa>	lambda[t] ~ dgamma( alpha.lambda, beta.lambda)
    #<iidLogGa>	logLambda[t] <- log(lambda[t])
    #<iidLogGa> }

    #<negbin> # Prior for r parameter in negbin
    #<negbin> r ~ dunif(0,50)

    ######################################
    #Priors for discrete time hazard model
    ######################################
    #for (d in 1:(maxDelay)) { }
    for (d in 1:(round( (maxDelay-1)/2-0.4)+1)) {
        #coefs for logit @ delay 0,..,maxDelay-1
    	gamma[d] ~ dnorm( mu.gamma[d], tau.gamma[d])
    }
    # #Prior for change point effects (now as vector)
    eta ~ dmnorm( eta.mu, eta.prec)

    # #Alternative: Separate random walks for each baseline
    # for (d in 1:(maxDelay)) {
    #     tau.gamma[d] ~ dgamma(0.001,0.001)
    # 	gamma[1,d] ~ dnorm( ifelse(maxDelay < 3/4*maxDelay, -3,-0.1), tau.gamma[d])
    # }
    # for (t in 2:T) {
    #     for (d in 1:(maxDelay)) {
    # 	    #coefs for logit @ delay 0,..,maxDelay-1
    # 	    gamma[t,d] ~ dnorm( gamma[t-1,d], tau.gamma[d])
    # 	}
    # }

    ####################################################
    #Loop over all time points in the reporting triangle
    ####################################################
    for (t in max(1,T-m):T) {
      #Time dependent delay distribution
	    logit(p[t,1]) <- gamma[1] + eta %*% W[t,,1]
	    #haz[t,1] <- p[t,1] -- not needed
    	for (d in 1:(maxDelay-1)) {
      # gamma's are unique
      #	logit(haz[t,d+1]) <- gamma[d+1] + eta %*% W[t,,d+1]
      # Two subsequent gamma's are the same
	    logit(haz[t,d+1]) <- gamma[ round(d/2-0.4)+1] + eta %*% W[t,,d+1]
	    p[t,d+1] <- (1-sum(p[t,1:d]))*haz[t,d+1]
	}
        p[t,maxDelay+1] <- (1-sum(p[t,1:maxDelay]))*1 #since haz[maxDelay+1]=1

	#Observations -- loop over all delays.
    	for (d in 0:maxDelay) {
	    mu[t,d+1] <- exp(logLambda[t])*p[t,d+1]
	    #<poisson> # Poisson distributed response
	    #<poisson> rT[t,d+1] ~ dpois(mu[t,d+1])

	    #<negbin> # Negative binomial distributed response
	    #<negbin> rT[t,d+1] ~ dnegbin(r/(r+mu[t, d+1]), r)
	}
    }

    #Loop over entire triangle not just the moving window
    for (t in 1:T) {
        #Curve for the expected number \lambda_t of cases
	#Spline model for the curve
	#<tps> logLambda[t] <- inprod(beta[],X[t,]) + inprod(b[],Z[t,])
        #count the total number of observations at time t.
	NtInf[t] <- sum(rT[t,])
    }
}


