```{r}
library(dplyr)
mtcars$id <- 1:nrow(mtcars)
train <- dplyr::sample_frac(mtcars, 0.7) # training dataset
test <- dplyr::anti_join(mtcars, train, by = 'id') # testing dataset
y1 <- train$mpg
X1 <- as.matrix(train[, 3:7])
N1 <- nrow(X1)
D <- ncol(X1)
X2 <- as.matrix(test[, 3:7])
N2 <- nrow(X2)
my_data <- list(x1 = X1, N1 = N1, D = D, y1 = y1, x2 = X2, N2 = N2)
``````{r}
gp_joint <- "
data {
int<lower = 1> D; // dim of matrix
int<lower=1> N1;
vector[D] x1[N1]; // matrix
vector[N1] y1;
int<lower=1> N2;
vector[D] x2[N2]; // matrix
}
transformed data {
int<lower=1> N;
vector[D] x[N1+N2];
vector[N1+N2] mu;
# cov_matrix[N1+N2] Sigma;
N = N1 + N2;
for (n in 1:N1)
x[n] = x1[n];
for (n in 1:N2)
x[N1 + n] = x2[n];
for (i in 1:N)
mu[i] = 20;
}
parameters {
vector[N2] y2;
real<lower=0> eta_sq;
real<lower=0> inv_rho_sq;
real<lower=0> sigma_sq;
}
transformed parameters {
real<lower=0> rho_sq;
rho_sq = inv(inv_rho_sq);
}
model {
vector[N] y;
matrix[N, N] Sigma;
// off-diagonal elements
for (i in 1:(N-1)) {
for (j in (i+1):N) {
Sigma[i,j] = eta_sq * exp(-rho_sq * dot_self(x[i] - x[j]));
Sigma[j,i] = Sigma[i,j];
}
}
// diagonal elements
for (k in 1:N)
Sigma[k,k] = eta_sq + sigma_sq; // + jitter
eta_sq ~ cauchy(0,5); // prior on eta
inv_rho_sq ~ cauchy(0,5); // prior
sigma_sq ~ cauchy(0,5); // prior
for (n in 1:N1)
y[n] = y1[n];
for (n in 1:N2)
y[N1 + n] = y2[n];
y ~ multi_normal(mu, Sigma);
}
"
``````{r}
my_fit_joint <- stan(model_code = gp_joint, data = my_data, iter = 200, chains = 3)
```DIAGNOSTIC(S) FROM PARSER:
Warning (non-fatal):
Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable.
If it does, you need to include a target += statement with the log absolute determinant of the Jacobian of the transform.
Left-hand-side of sampling statement:
y ~ multi_normal(...)The following numerical problems occured the indicated number of times on chain 1
count
Exception thrown at line 58: multi_normal_log: LDLT_Factor of covariance parameter is not positive d 1
When a numerical problem occurs, the Hamiltonian proposal gets rejected.
See http://mc-stan.org/misc/warnings.html#exception-hamiltonian-proposal-rejected
If the number in the 'count' column is small, do not ask about this message on stan-users.On Feb 1, 2017, at 7:27 PM, Daniel Emaasit <daniel....@gmail.com> wrote:
Thanks Bob.Now I have an issue when I use a dataset containing 1 variable and 200 points, i.e X[200, 1]. The sampling takes really really LONG (t > 30 mins). I'd expected it to take seconds. I tried using the cholesky decomposition as the manual suggests but there's no significant speed improvements. My plan is to test this model first on small data, then use it for a much bigger dataset with N = 5000 and 15 or more variables. Is there another way of speeding up gps?
Thanks Bob.Now I have an issue when I use a dataset containing 1 variable and 200 points, i.e X[200, 1]. The sampling takes really really LONG (t > 30 mins). I'd expected it to take seconds. I tried using the cholesky decomposition as the manual suggests but there's no significant speed improvements. My plan is to test this model first on small data, then use it for a much bigger dataset with N = 5000 and 15 or more variables. Is there another way of speeding up gps?
for (n in 1:N1)
y[n] = y1[n];
for (n in 1:N2)
y[N1 + n] = y2[n];
y ~ multi_normal(mu, Sigma);
If that's possible for GPs, we should rewrite our manual
examples (which I first wrote when I was just learning GPs,
so sorry if it's inefficient).
- Bob
> On Feb 4, 2017, at 12:08 AM, andre.pfeuffer via Stan users mailing list <stan-...@googlegroups.com> wrote:
>
> Daniel,
>
> for (n in 1:N1)
> y[n] = y1[n];
> for (n in 1:N2)
> y[N1 + n] = y2[n];
>
> y ~ multi_normal(mu, Sigma);
>
> y is a parameter, y1 is data. So you assign data to a parameter. I would do the y2 in the generated_quantities, and put
> y1 ~ multi_normal(mu, Sigma) in the model block.
>
> The time is ok. I got ~ 3 hours runtime in Stan with my GP, which slows down my development process.
>
> One advanced idea you could do is to use cholesky decomposition take the inverse and map it to normal(0, 1), since the cholesky
> is a triangular the corresponding jacobi adjustment is only with the diagonals.
> I tried that onetime and it gives some speed.
>
> Andre
>
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.
Rob
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.
K_div_y = mdivide_left_tri_low(L_Sigma, y);
K_div_y = mdivide_right_tri_low(K_div_y',L_Sigma)';No problem.functions {vector gp_pred_rng(real[] x_pred,
vector[] x_pred,vector y,real[] x,
vector[] x,int<lower=1> N;
int<lower=1> D;
int<lower=1> N_pred;
vector[N] y;
vector[D] x[N];
vector[D] x_pred[N_pred]; count
Exception thrown at line 69: cholesky_decompose: Matrix m is not positive definite 3
Exception thrown at line 66: cov_exp_quad: sigma is 0, but must be > 0! 1
When a numerical problem occurs, the Hamiltonian proposal gets rejected.
See http://mc-stan.org/misc/warnings.html#exception-hamiltonian-proposal-rejected
If the number in the 'count' column is small, do not ask about this message on stan-users.Rob

K_div_y = mdivide_left_tri_low(L_Sigma, y);
K_div_y = mdivide_right_tri_low(K_div_y',L_Sigma)';Rob,
I have problems in understanding with the second line.I don't understand.
Thank you,your code is a good template,Andre