dirichlet distribution

181 views
Skip to first unread message

Andres Martinez

unread,
Sep 27, 2023, 8:16:20 AM9/27/23
to nimble-users
Hi 

I am working with the dirichlet and gamma distributions as priors for my work, but I am getting two errors, the first one is that when I put the distribution for the dirichlet I get this error

Defining model
Error in addMissingIndexingRecurse(code[[i]], dimensionsList) :
  inconsistent dimensionality provided for node 'alpha_beta' 

omega ~ dgamma(omega_star, 0.25)
 alpha_beta[ 1: 3] ~ ddirch(alpha_beta_star[1:3]) 

I do not what happened I just change the position of code and the started to function wrong.

Second before that I was trying to understand why omega and alpha_beta do not have samplers assigned.  It is like the code is no reading the distribution of each prior that I put in the code

conf <- configureMCMC(model, nodes = NULL)
===== Monitors =====
thin = 1: alpha_beta, omega
===== Samplers =====
(no samplers assigned)
===== Comments =====
  [Warning] No samplers assigned for 2 nodes, use conf$getUnsampledNodes() for node names.
> conf$getUnsampledNodes()
[1] "omega"           "alpha_beta[1:3]"

If I not have samplers, the logProb for each will be -inf and then I will get this warning 

warning: problem initializing stochastic node alpha_beta[1, 1:3]: logProb is -Inf.
|-------------|-------------|-------------|-------------|
|-------------------------------------------------------|
Warning message:
Could not calculate the full summary of posterior samples, possibly due to NA or NaN values present in the samples array 

Thanks a lot for your help

Andrés

Daniel Turek

unread,
Sep 27, 2023, 9:01:13 AM9/27/23
to Andres Martinez, nimble-users
Andrés, offhand I don't see anything wrong with your code (and indeed, it seems to work fine for me - see below).  Are you able to provide a reproducible example, including your full model code, and the call to nimbleModel ?

The code below worked for me:

library(nimble)

code <- nimbleCode({

    omega ~ dgamma(omega_star, 0.25)
    alpha_beta[ 1: 3] ~ ddirch(alpha_beta_star[1:3])
})

Rmodel <- nimbleModel(code)

## Defining model
## Building model
## Running calculate on model
##   [Note] Any error reports that follow may simply reflect missing values in model variables.
## Checking model sizes and dimensions
##   [Note] This model is not fully initialized. This is not an error.
##          To see which variables are not initialized, use model$initializeInfo().
##          For more information on model initialization, see help(modelInitialization).

conf <- configureMCMC(Rmodel)

## ===== Monitors =====
## thin = 1: alpha_beta, omega
## ===== Samplers =====
## posterior_predictive sampler (2)
##   - omega
##   - alpha_beta[1:3] 

Cheers,
Daniel


--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/1ea6e721-7f94-417b-8d74-50109ba25329n%40googlegroups.com.

Andres Martinez

unread,
Sep 27, 2023, 9:27:18 AM9/27/23
to nimble-users
Hi Daniel

Thanks for the answer, yes this is my code

library(readxl)
simgarchr <- read_excel("simgarchr.xlsx")
simulated_returns=simgarchr
plot(simulated_returns)


n<-length(as.numeric(simulated_returns$simulated_returns))
# Use R nimble library
library(nimble)
## BUGS code
# Model and prior
myCode <- nimbleCode({
 
  # Likelihood
  for (t in 1:1) {
    y[t] ~ dnorm(0, sigma_h[t]^2)
    sigma_h[t] <- exp(h[t])
  }
 

  ##Priors  

  omega ~ dgamma(omega_star, 0.25)
  alpha_beta[ 1: 3] ~ ddirch(alpha_beta_star[1:3])
 
 
 
  h[1] <- omega[1]/(1-alpha+beta) ### long run volatility
  for(t in 2:n) {
    h[t] <- sqrt(omega[1] + alpha* pow(y[t-1], 2)
                     + beta * pow(h[t-1], 2))
  }
 
  alpha <- alpha_beta[1,1]
  beta <- alpha_beta[1,2]
  alpha_beta_star[1:3]<-c(alpha_star[1],beta_star[1],phi[1])
 
 
})
# Constants, Data, Initial values for MCMC
myConstants <- list( n = n )
myData      <- list( y = as.numeric(simulated_returns$simulated_returns))
myInits     <- list( h = rnorm(n+1, 0, 1), alpha_star = runif(1),beta_star=runif(1),phi=1, omega = 1 , alpha_beta =c(0,0,0),alpha=0,beta=0,omega_star=rgamma(1,2,2))

dimensions <- list(

  h<-c(1510),
  y<-c(1509,1),
  alpha_star<-c(1),
  beta_star<-c(1),
  omega<-c(1),
  phi<-c(1),
  alpha_beta<-c(3),
  alpha<-c(1),
  beta<-c(1),
  mu<-c(1)
  )

model <- nimbleModel(myCode, data=myData, inits=myInits, dimensions = dimensions)

I am having problems now with the dimensions but as I understood alpha_beta is [1] which is the same as the result that I want in alpha_beta[ 1: 3] ~ ddirch(alpha_beta_star[1:3])
 
But this is the model that I want to run

Thanks for your help

Andrés



simgarchr.xlsx

Daniel Turek

unread,
Sep 27, 2023, 9:51:29 AM9/27/23
to Andres Martinez, nimble-users
Andres, I'm sorry this is brief, but I have to run.  See updated code and comments (in the code) below.


##n<-length(as.numeric(simulated_returns$simulated_returns))


# Use R nimble library
library(nimble)
## BUGS code
# Model and prior


myCode <- nimbleCode({
    # Likelihood
    for (t in 1:n) {   ## changed 1:1 to 1:n

        y[t] ~ dnorm(0, sigma_h[t]^2)
        sigma_h[t] <- exp(h[t])
    }
    ##Priors  
    omega ~ dgamma(omega_star, 0.25)
    alpha_beta[ 1: 3] ~ ddirch(alpha_beta_star[1:3])
    h[1] <- omega/(1-alpha+beta) ### change the [1] after omega should be removed
    for(t in 2:n) {
        h[t] <- sqrt(omega + alpha* pow(y[t-1], 2)  ### change the [1] after omega should be removed

                     + beta * pow(h[t-1], 2))
    }
    alpha <- alpha_beta[1]  ##[1,1]   ## change
    beta <- alpha_beta[2]   ##[1,2]   ## change

    alpha_beta_star[1:3]<-c(alpha_star[1],beta_star[1],phi[1])
})

n <- 1510   ## added this line


# Constants, Data, Initial values for MCMC
myConstants <- list( n = n )
myData      <- list( y = rnorm(n)) ##as.numeric(simulated_returns$simulated_returns))   ## changed the data

myInits     <- list( h = rnorm(n+1, 0, 1), alpha_star = runif(1),beta_star=runif(1),phi=1, omega = 1 , alpha_beta =c(0,0,0),alpha=0,beta=0,omega_star=rgamma(1,2,2))

##dimensions <- list(
##    h<-c(1510),     ## can't use "<-" here
##    y<-c(1509,1),   ## etc
##    alpha_star<-c(1),
##    beta_star<-c(1),
##    omega<-c(1),
##    phi<-c(1),
##    alpha_beta<-c(3),
##    alpha<-c(1),
##    beta<-c(1),
##    mu<-c(1)
##)

dimensions <- list(   ## change.  Need to use "=" inside list, rather than "<-"
    h = c(1510),
    y = 1510, ##c(1509,1),   ## change here
    ##alpha_star = c(1),
    ##beta_star = c(1),     ## don't include dimensions for scalars, all lines below
    ##omega = c(1),
    ##phi = c(1),
    alpha_beta = c(3)
    ##alpha = c(1),
    ##beta = c(1),
    ##mu = c(1)

)

model <- nimbleModel(myCode, data=myData, inits=myInits, dimensions = dimensions)

Andres Martinez

unread,
Sep 27, 2023, 10:02:20 AM9/27/23
to nimble-users
Thanks
Reply all
Reply to author
Forward
0 new messages