# Generate a labeled binary rooted tree in stan

Hi Everyone,

I am trying to generate a rooted tree in transformed parameters block of my stan. I have no idea how i can implement minspantree or diagraph or shortest path in stan file. I have a known F matrix that is the multiplication of two unknown matrices U and M F=UM. The matrix U = inv(I-T) that T is the unknown binary rooted tree. Does anyone have any suggestions?

Thank you,
Elaheh

Can you specify a function signature, like `matrix foo(matrix a);`? Is there a known algorithm for computing the function? If so, you can probably code it in Stan.

Thank you, Bob. I tried to implement it with function. But I have another problem. Every time, I run my algorithm, I face white space error at the beginning of my function line. Can you please help me how I can debug stan file or understand which line in my function has the problem? Other parts except function block working great. I copy my stan file here. Thank you.

data {
int<lower=0> T;
int<lower=0> N;
real<lower=0,upper=1> Fhat[N,T];
matrix [N,N] eye;
//matrix<lower=0,upper=1> [N,N] UTree;
//vector<lower=0> [N] tau;
}
parameters {
vector<lower=0,upper=1> [N] deltaTau;
real<lower= 0.0001,upper=0.3> mu[N];
matrix <lower=0.8,upper=1.2> [N,T] errorF;
real tmp;
}
functions {
matrix[N,N] Tree(real N){
vector<lower=1,upper=N> [N] temp_vect;
real<lower=1,upper=N-1> temp_vect_child;
vector[N] nodes_sequence;
matrix[N,N] A;
real root_node;
real num_child_root;
real remaining_node;
real num_passed_nodes;
real num_children_node_child;
real<lower=1,upper=remaining_node> tmp_vect_child_child;
for (n in 1:N) {
tmp_vect[n] ~ uniform(1,N);
}
nodes_sequence <- sort_indices_desc(temp_vect); // sequence of the nodes
root_node <- nodes_sequence; // root
temp_vect_child ~ uniform(1,N-1);
num_child_root <- ceil(temp_vect_child); // children of the root
remaining_node = N-1-num_child_root;
for (i in 1:num_child_root){
A[root_node,nodes_sequence[1+i]] <- 1;
}
num_passed_nodes <- 1+num_child_root;
for (j in 1:num_child_root) {
while (remaining_node ~= 0 ){
tmp_vect_child_child ~ uniform(1,remaining_node);
num_children_node_child <- ceil(tmp_vect_child_child);
for (i in 1:num_children_node_child){
A[nodes_sequence[1+j],nodes_sequence[num_passed_nodes+i]] <- 1;
}
num_passed_nodes <- num_passed_nodes + num_children_node_child;
remaining_node <- remaining_node -num_children_node_child;
}
}
return A;
}
}
transformed parameters {
matrix <lower=0> [N,T] numM;
matrix <lower=0,upper=1> [N,T] M;
matrix <lower=0> [N,T] F;
real<lower=0> den[T];
matrix <lower = 0, upper = 1> [N,N] UTree;
UTree <- inverse(eye-Tree(N));
//
// treeMat[i,j] = fujnjction(W[k]);
//
vector<lower=0> [N] tau;
tau = (UTree’)deltaTau; // UTree needs to be transposed in this operation
for(t in 1:T){
den[t] <- 0.0000001;//this should be a small value
for (n in 1:N){
numM[n,1] ~ 0.0000001;
if (tau[n] > t - 1){
numM[n,t] = 0.0000001;
}else{
numM[n,t] = (mu[n]
(t-1 - tau[n]));
}
den[t] = den[t] + numM[n,t];
}
for (n in 1:N){
M[n,t] = numM[n,t]/ den[t];
}
}
F = UTree*M;

``````}
model {
for(t in 1:T) {
for (n in 1:N){
mu[n] ~ uniform(0.0001, 0.3);
deltaTau[n] ~ uniform(0,1);
//  errorF[n,t] ~ uniform(0.8*Fhat[n,t]/(0.05+F[n,t]),1.2*Fhat[n,t]/(F[n,t]+0.05));
Fhat[n,t] ~ normal(F[n,t],0.05);
}
}
``````

}
generated quantities{
real<lower=0> err;
err = 0;
for (t in 1:T){
for (n in 1:N){
err = err+ (Fhat[n,t]-F[n,t])^2;
}
}
}

Best regards,
Elaheh

On Sun, Aug 6, 2017 at 5:38 PM, Bob Carpenter mc_stan@discoursemail.com wrote:

``````[Bob_Carpenter] 	Bob_Carpenter Developer
August 6

Can you specify a function signature, like matrix foo(matrix a);? Is there a known algorithm for computing the function? If so, you can probably code it in Stan.

Visit Topic or reply to this email to respond.