Array of ordered vectors?

I want to specify an N array of ordered 2 vectors. Something like

// doesn't work
vector[N] ordered[2] mu;
vector[N] ordered mu[2];

what is the best way to achieve this?

Playing around a bit, I was able to achieve what I wanted. Though I think it would be nice if we could specify an array of ordered vectors instead of what I did.

The problem I’m working on involved a mixture of two distributions where the intercept was allowed to vary within each mixture (ala hierarchical). However, I wanted one of the mixture intercepts to be always less than the other. I enforced this by creating two vectors where one is strictly negative and the other one is strictly positive.

Forgive the nested loop, I was doing some stuff above it and know that I could re-write this example a bit more efficiently.

parameters {
  vector<lower=-3, upper=0>[N] mu1;
  vector<lower=0, upper=3>[N] mu2;
}
transformed parameters {
  matrix[2, N] mu_ind;
  
  for (i in 1:2){
    //doin' some other stuff here also
     for (n in 1:N) {
         mu_ind[i, n] = i == 1 ? mu1[n] : mu2[n];
     }
   }
}

Have you tried

ordered[2] mu[N];

?

1 Like

This is hilarious, I tried that but got an error so abandoned it. Trying again, all I had to modify was to specify two priors (not one) as

  mu[, 1] ~ normal(-0.5, 1.0);
  mu[, 2] ~ normal(0.5, 1.0);

thanks!

Hi! Sorry for reopening this topic after many years from the original. I am also trying to create an array of ordered vectors. However, it is not working for me. I would appreciate any help with the code:

data {
  int<lower=0> n; // number of examinees
  int<lower=0> N; // number of data points
  int<lower=0> K;  // number of timepoints
  int<lower=0> J; // number of components
  vector[N] y; // response variable
  vector<lower=0>[N] t; // time data
  int<lower = 1> jj[N]; // timepoint id
  int<lower = 1> ii[N]; // person id
}

parameters {
  // individual parameters
  vector[n] M; // MESOR
  positive_ordered[J] A[n]; // Amplitude
  positive_ordered<upper=max(t)>[J] tau[n]; // Period
  ordered<lower=-2*pi(),upper=0>[J] phi[n]; // Acrophase
  
  // population location parameters
  real mu_M;
  vector<lower=0>[J] mu_A;
  vector<lower=0>[J] mu_tau;
  vector<lower=-2*pi(),upper=0>[J] mu_phi;
  
  // population scale parameters
  real<lower=0> sigma_m; 
  vector<lower=0>[J] sigma_A; 
  vector<lower=0>[J] sigma_tau; 
  vector<lower=0>[J] kappa_phi; 
  real<lower=0> sigma_epsilon; // Joint error variance
}

model {
  // lower level
  target += normal_lpdf(y | M[ii] + A[ii,J] .* cos((2*pi()*t[jj])./tau[ii,J] + phi[ii,J]), sigma_epsilon);
  
  // upper level
  M ~ normal(mu_M, sigma_m);
  A[J] ~ normal(mu_A, sigma_A);
  tau[J] ~ normal(mu_tau, sigma_tau);
  phi[J] ~ von_mises(mu_phi, kappa_phi);
  
  // priors locations
  mu_M ~ normal(0,100);
  mu_A ~ normal(0,100);
  mu_tau ~ normal(0, 20);
  mu_phi ~ von_mises(-pi(), pi());
  
  // priors scales
  sigma_m ~ cauchy(0,5);
  sigma_A ~ cauchy(0,5);
  sigma_tau ~ cauchy(0,5);
  kappa_phi ~ cauchy(0,5);
  sigma_epsilon ~ cauchy(0,5);
}