Generalising Gaussian Process (GP) model

Hi, I use Stan to construct GP models, I model f(x)
as GP with mean
[ m(x) = h(x)^T \beta ]
and covariance function
[ \text{Cov}(x, \tilde{x}) = \sigma^2 C(x, \tilde{x}; \delta)]

Currently I use FitGP.stan to fit my model and deal with p (number of inputs) and Np (number of regression functions) greater than one. My question is could I specify my model where p=1 and Np=1 and how should I specify my beta and delta_par in parameters block. Currently, if I am just running my model as it is, I keep getting a nasty error message about the mismatch of dimensions.

Here is my short model code in R and the stan file enclosed:

StanEmulator <- stan(file=“FitGP.stan”,
data=list(X1 = Design, y1= tF, H1 = H, N1 = dim(Design)[1], p = dim(Design)[2],
SigSq = sigsq, SigSqV = sigsqvar, nugget=nugget),
iter = 2000,warmup=1000, chains = 2, cores = 4, init=init.list, …)

Thank you for your help in advance!FitGP.stan (1.2 KB)

What does the error message actually say? Could you copy and paste it here?

Apologies for not posting the error message. Here is my error message when I am trying to specify a constant mean function, i.e. Np = 1.

SAMPLING FOR MODEL ‘FitGP’ NOW (CHAIN 1).
[1] “Initialization from source failed.”
[2] “mismatch in number dimensions declared and found in context; processing stage=initialization; variable name=beta; dims declared=(1,1); dims found=()”
[3] "Error in sampler$call_sampler(args_list[[i]]) : "
error occurred during calling the sampler; sampling not done
SAMPLING FOR MODEL ‘FitGP’ NOW (CHAIN 2).
[1] “Initialization from source failed.”
[2] “mismatch in number dimensions declared and found in context; processing stage=initialization; variable name=beta; dims declared=(1,1); dims found=()”
[3] "Error in sampler$call_sampler(args_list[[i]]) : "
error occurred during calling the sampler; sampling not done
here are whatever error messages were returned
[[1]]
Stan model ‘FitGP’ does not contain samples.
[[2]]
Stan model ‘FitGP’ does not contain samples.
Stan model ‘FitGP’ does not contain samples.
Error in matrix(ParameterSamples$beta, ncol = 1) :
‘data’ must be of a vector type, was 'NULL’
In addition: Warning message:
In .local(object, …) :
some chains had errors; consider specifying chains = 1 to debug

That error message is saying that the data you’re providing has a beta variable that doesn’t match the declaration within the Stan program.

You’ll either need to fix your data or your Stan program.

I think I do understand this error. When I have Np greater than 1, everything works fine. However, if I specify Np=1, I keep getting this error.

Does it mean that I cannot have beta as a vector of length 1? Is there exist a universal container that I could specify for beta so that it works for any positive integer Np?

In R, explicitly make beta a matrix. There is an ambiguity with R types when dimensions collapse like when Np=1. Stan can handle length 1 and length 0 without a problem.

This is an issue with R which automatically converts single element arrays into scalars which are then no longer recognized as arrays. When creating your initialization list you’ll have to replace beta with something like

array(beta, dims = c(1,1))

Still, don’t have any luck with the initialization step:

beta <- array(meanResponse$linModel$coefficients, dim = c(1, 1)

and include it in my init argument of stan function. And I am getting this error:

SAMPLING FOR MODEL ‘FitGP’ NOW (CHAIN 1).
[1] “Initialization from source failed.”
[2] “mismatch in number dimensions declared and found in context; processing stage=initialization; variable name=beta; dims declared=(1); dims found=(1,1)”
[3] "Error in sampler$call_sampler(args_list[[i]]) : "
error occurred during calling the sampler; sampling not done
SAMPLING FOR MODEL ‘FitGP’ NOW (CHAIN 2).
[1] “Initialization from source failed.”
[2] “mismatch in number dimensions declared and found in context; processing stage=initialization; variable name=beta; dims declared=(1); dims found=(1,1)”
[3] "Error in sampler$call_sampler(args_list[[i]]) : "
error occurred during calling the sampler; sampling not done
here are whatever error messages were returned
[[1]]
Stan model ‘FitGP’ does not contain samples.
[[2]]
Stan model ‘FitGP’ does not contain samples.
Stan model ‘FitGP’ does not contain samples.
Stan model ‘FitGP’ does not contain samples.

What happens if you try this?:

beta <- as.vector(meanResponse$linModel$coefficients)

Not sure if that will work, but it may be worth a shot.

This is an option that I started with and the result of my first error message.

Please try to parse the error message:

This states that the model declares beta as a 1-dimensional vector (dims declared=(1)) where as you defined it as a 1x1 matrix in the initialization file (dims found=(1,1)) which is consistent with the call I gave you. This suggests that you instead use

beta <- array(meanResponse$linModel$coefficients, dim = 1)

Why did I have dim = c(1, 1) before? Because your original error message was

[2] “mismatch in number dimensions declared and found in context; processing stage=initialization; variable name=beta; dims declared=(1,1); dims found=()”

which states that the model originally requested a 1x1 matrix, so it looks like you’ve changed your model since you first asked the question!

1 Like

Yes, you are right. I did mess around with my model. But I got what you meant with array declaration for beta and now it is working! Thank you all for help!

Awesome! Could you mark one of the posts as the “Solution” so that others know that the thread is complete? Thanks.

1 Like