data {
    int<lower=0> N;
    int<lower=0> nit;
    matrix[N,nit] z;
    }

  parameters {
    vector[N] theta;
    vector[nit] beta;
    vector[nit] ln_phi;
    vector[nit] ln_alpha;
  }

  transformed parameters{
    vector<lower=0>[nit] alpha = exp(ln_alpha);
  }

   model {
    vector[nit] pbl = beta + ln_phi;
    vector[nit] mbl = -beta + ln_phi;
    beta~normal(0,10) ;
    ln_phi~normal(0,10);
    ln_alpha~normal(0,10);
    theta~normal(0,1);

    for(p in 1:N){
       for(i in 1:nit){
         real at=alpha[i]*theta[p];

         if(z[p,i]!=-999) target+=beta_lpdf(z[p,i] |
                                            exp(.5* (at + pbl[i])),
                                            exp(.5* (-at + mbl[i])));
      }
    }
  }



