# Size operation error get mean of row_vector[] by user-defined functions

hello STAN community, I faced troubles with indexing

I want to get mean value and covariance matrix of GP posterior.

GP posterior is, \mathcal{L}(\theta)\vert\mathcal{D} \sim \mathcal{N}(\mu_c(\theta), \Sigma_c(\theta)) (\mathcal{D} is data and \vartheta is observations)

Only want to get \mu_c(\theta), \Sigma_c(\theta) where \theta is 2-dimension.

\mu_c(\theta)= m_c(\theta)+K_{\ast}^T\tilde{K}^{-1}(\mathcal{L}(\vartheta)-m_c(\vartheta))

\Sigma_c(\theta)=K_{\ast,\ast}-K_{\ast}^T\tilde{K}^{-1}K_{\ast}

here is my code.

functions {
vector getmean(row_vector x) {
int J=rows(x);
real m=mean(x);
vector[J] mu=rep_vector(m,J);
return mu;
}

vector getmc(vector mu, matrix k1, matrix k2, vector L, vector mc) {
int N =rows(mu);
vector[N] muc=mu-k1'*inverse(k2)*(L-mc);
return log(muc);
}

matrix getsigmac(matrix k1, matrix k2, matrix k3) {
return k1-k2'*inverse(k3)*k2;
}
}

data {
int<lower=1> J; // number of samples
int<lower=1> N; // number of data points
real<lower=0> alpha; // hyperparameter
real<lower=0> rho; // hyperparameter

// data
row_vector[2] vartheta1[J];
row_vector[2] theta1[N];
vector[J] l1;
}

transformed data {
vector[J] mu11=getmean(vartheta1); // I got error at this line
vector[N] mu12=getmean(theta1); // I got error at this line
matrix[N, N] k1 = getsigmac(k11,k12,k13);
vector[N] k10=diagonal(k1)
vector[N] mu1=getmc(mu12,k11,k13,l1,mu11);

}

generated quantities {
vector[J] mu0=mu;
matrix[N,N] cov=1/2*k0;
}


I got error with this messages.

No matches for:

getmean(row_vector[ ])

Available argument signatures for getmean:

getmean(row_vector)

## error in ‘model2e44a2d5469_fit1’ at line 33, column 35

31:
32: transformed data {
33:   vector[J] mu11=getmean(vartheta1);
^
34:   vector[N] mu12=getmean(theta1);


How can I fix it?

Thank you.

Hi,

row_vector[2] vartheta1[J];


is an array of row vectors and your getmean function accepts row_vector.

You need to loop over vartheta1’s and call get mean with each of them.

So something like

vector[J] mu11;
for (i in 1:J) {
mu11[i] = getmean(vartheta1[i];
}

1 Like

Thank you so much.

But I faced some troubles with error message s.

1. “Variable “real” does not exist.” or “Variable “matrix” does not exist.” after for loop.

2. dimension mismathed.

here is my code

functions {
vector getmuc(vector x, vector y,vector a, vector b,vector l) {
real alpha=0.1;
real rho=0.1;
int J=num_elements(x);
int N=num_elements(a);
row_vector[2] vartheta1[J];
row_vector[2] theta1[N];
for (ii in 1:J) vartheta1[ii,1] = x[ii];
for (ii in 1:J) vartheta1[ii,2] = y[ii];
for (jj in 1:N) theta1[jj,1] = a[jj];
for (jj in 1:N) theta1[jj,2] = b[jj];
real m1=mean(x+y);                   //Variable "real" does not exist.
real m2=mean(a+b);
vector[N] mu=rep_vector(m1,J);
vector[J] mc=rep_vector(m2,N);
vector[N] muc=mu-k2'*inverse(k1)*(l-mc);
return muc;
}

matrix getsigmac(vector x,vector y,vector a,vector b) {
real alpha=0.1;
real rho=0.1;
int J=num_elements(x);
int N=num_elements(a);
row_vector[2] vartheta1[J];
row_vector[2] theta1[N];
for (ii in 1:J) vartheta1[ii,1] = x[ii];
for (ii in 1:J) vartheta1[ii,2] = y[ii];
for (jj in 1:N) theta1[jj,1] = a[jj];
for (jj in 1:N) theta1[jj,2] = b[jj];
vector[N] k0=diagonal(k3-k2'*inverse(k1)*k2); //dimension mismatched.
return k0;
}
}

data {
int<lower=1> J;
// data
vector[J] varthetax;
vector[J] varthetay;
vector[J] l1;
}

parameters {
vector[J] theta1x;
vector[J] theta1y;
}

model {
vector[J] k1 = getsigmac(vartheta1x,vartheta1y,theta1x,theta1y);
vector[J] mu1=getmuc(vartheta1x,vartheta1y, theta1x,theta1y,l1);
target += mu1+k1;
for (ii in 1:J) {
theta1x~normal(mean(varthetax),100);
theta1y~normal(mean(varthetay),100);
}
}