Very Ragged Data Set in a Complex, Multi-Conditioned, BHM

Hey all,

I’m having slightly new coding woes trying to implement the conditional version of our model.

Version: cmdstan-2.30.1-mpi

I showed my working paralellized code for the single condition model here. However, this is only built to handle part of our data. Our real data has multiple conditions that the parameters depend on. Specifically, there are four contexts for our three confidence variables (meta, nconf, pconf), and six contrast levels that serve as contexts for our psychometric variables (sens, crit), and our lapse rate parameter spans all conditions (guess). The problem is that the conditions each have different numbers of contrast levels, which means that they also have a different numbers of trials. We also remove subjects with low variability in the confidence parameters. So we have a very ragged data set. To fix this, I pad all of the data sets: zero rows for missing trials, zero matrices for missing subjects, and entire arrays of zero matrices for missing contrasts. We do not want all of these zeros to be running through the model, greatly diminishing its efficiency. To combat this, I had hoped to implement conditionals at different check points to not run data sets that are entirely zero (there doesn’t seem to be much we can do about the padded trials, but that’s small fry compared to the entirely empty contrasts). I tried to implement as follows:

functions {

	//Custom function for lognormal inverse CDF
	matrix logncdfinv(vector x, vector m, vector s) {							

		int szx;
		int szm;
		szx = size(x);
		szm = size(m);
		matrix[szx,szm] y;
		y = exp( rep_matrix(s',szx) * sqrt(2) .* rep_matrix(inv_erfc( -2 .* x + 2 ),szm) + rep_matrix(m',szx));
		return y;

		}

	//Likelihood model for CASANDRE				
	real casandre_log(array[] matrix resps, vector guess, matrix sds, matrix se, vector nconf, vector pconf) {

		real ll;
		ll = 0.0;

		for (n in 1:size(resps)) {

			if (resps[n][:] == 0) {

			} else {

				matrix[size(se[:,n]),4] llhC;

				matrix[size(se[:,n]),size(sds[:,n])] avgs;
				avgs = rep_matrix(se[:,n],size(sds[:,n])).*rep_matrix(sds[:,n]',size(se[:,n]));
		
				for (tr in 1:size(se[:,n])){

					matrix[3,size(sds[:,n])] raws;

					for (rws in 1:size(sds[:,n])) {
					raws[1,rws] = normal_cdf(-nconf[n],avgs[tr,rws],sds[rws,n]);
						}
					for (rws in 1:size(sds[:,n])) {
					raws[2,rws] = normal_cdf(0,avgs[tr,rws],sds[rws,n]);
						}
					for (rws in 1:size(sds[:,n])) {
					raws[3,rws] = normal_cdf(pconf[n],avgs[tr,rws],sds[rws,n]);
						}

					vector[3] ratiodist;

					ratiodist[1] = mean(raws[1,:]);
					ratiodist[2] = mean(raws[2,:]);
					ratiodist[3] = mean(raws[3,:]);

					llhC[tr,1] = ((guess[n]/4) + ((1-guess[n])*ratiodist[1]));
					llhC[tr,2] = ((guess[n]/4) + ((1-guess[n])*(ratiodist[2]-ratiodist[1])));
					llhC[tr,3] = ((guess[n]/4) + ((1-guess[n])*(ratiodist[3]-ratiodist[2])));
					llhC[tr,4] = ((guess[n]/4) + ((1-guess[n])*(1-ratiodist[3])));
				}

 			}

			if (resps[n][:] == 0) {

				ll += 0;

			} else {

				ll += sum(columns_dot_product(resps[n], log(llhC)));

			}
		
		}

		return ll;
		
	}

	//Partial sum function
	real partial_sum_casandre_log(array[] matrix slice_n_resps, int start, int end, vector guess, matrix sds, matrix se, vector nconf, vector pconf) {

	return casandre_log(slice_n_resps, guess[start:end], sds[:,start:end], se[:,start:end], nconf[start:end], pconf[start:end]);

	}
		
}
data {

	int ncond;
	int ncont;
	int ns;
	int nt;
	array[ncond,ncont,ns] matrix[nt,4] resps;
	array[ncond,ncont] matrix[nt,ns] oris;
	int sampn;
	vector[sampn] sampx;

}
parameters {

	//Parameters
	vector<lower=0,upper=1>[ns] guess;
	matrix<lower=0>[ns,6] sens;
	matrix[ns,6] crit;
	matrix<lower=0>[ns,4] meta;
	matrix<lower=0>[ns,4] nconf;
	matrix<lower=0>[ns,4] pconf;

	//Hyperparameters
	real snm;
	real<lower=0> sns;
	real cm;
	real<lower=0> cs;
	real mm;
	real<lower=0> ms;
	real nccm;
	real<lower=0> nccs;
	real pccm;
	real<lower=0> pccs;

}
model {

	//Hyperpriors
	snm 	~	normal(0,1);
	sns 	~	lognormal(0,1);
	cm 	~	normal(0,1);
	cs 	~	lognormal(0,1);
	mm 	~	normal(0,1);
	ms 	~	lognormal(0,1);
	nccm 	~	normal(0,1);
	nccs 	~	lognormal(0,1);
	pccm	~	normal(0,1);
	pccs	~	lognormal(0,1);

	//Priors
	guess 	~	beta(1,193.0/3.0);
	sens	~	lognormal(snm,sns);
	crit	~	normal(cm,cs);
	meta  	~	lognormal(mm,ms);
	nconf  	~	lognormal(ccm,ccs);
	pconf	~	lognormal(pccm,pccs);

	//Likelihood model (with local variable calculations)
	for (cond in 1:4) {

		xtrans = logncdfinv(sampx,log(1/sqrt(meta[:,cond].^2+1)),sqrt(log(meta[:,cond].^2+1)));
		sds = 1./xtrans;

		for (cont in 1:6) {

			if (oris[cond][cont][:] == 0) {

			} else {

				sm = oris[cond][cont].*rep_matrix(sens[:,cont]',nt);
				sc = crit.*sens[:,cont];
				
				se = sm-rep_matrix(sc',nt);

				target += reduce_sum(partial_sum_casandre_log, resps[cond][cont], guess, sds, nconf[:,cond], pcond[:,cond]);

			}

		}

	}

}

However, I’m running into an error where the conditional is not allowed to compare a matrix to an integer. I tried using rep_matrix to create an equal-sized matrix of zeros to compare it to, but received an error that you cannot compare matrices to matrices either. I’m not sure how to procede and am hoping someone knows how I can wrangle with conditionals to prevent empty data sets from needlessly running through the likelihood function. Even better if someone has a better idea for how to deal with our very ragged data set. Thanks!

Can’t you just do

if (sum(oris[cond][cont[:]) == 0)

Also, for the logncdfinv, it’s a bit faster and more stable to do

matrix log_normal_qf (vector x, vector m, vector s) {
  int szx = size(x);
  return exp(rep_matrix(m', szx) + std_normal_qf(x) * s');
}

It also is easier to read

1 Like

Our model is running! When I have seen that it passes our sanity checks, I will supply the working code

1 Like

sum() is showing some very odd behavior. Below is a print out from a skipped data set, part of
the matrix being summed to make the decision, and the readout of sum()

I find it easier to construct the index beforehand. My advice is to get the index numbers that contain the non-zero elements and pass those in as data. Then the loop is over all the non-zero data sets.

If the index is array[nz] non_zero_index then you do

for  (j in non_zero_index) {
 // Only loop through the things with data
 container_of_things[j]
}

Part of the issue is the ways the data is ragged across multiple dimensions (missing contrast levels, missing subjects, missing trials), while mixing parameters (confidence parameters are condition-specific but cut across contrasts levels, while psychometric parameters are contrast-level-specific and cut across conditions), and also having the code parallelized so the amount of data being fed into the likelihood function is being dynamically handled by the back end. We really need a flexible way to deal with missing data points, to deal with these complications, keep the data aligned, and leave the code scrutible and flexible for data that might have different numbers of conditions, contrast levels, total subjects, and removed subjects. That’s why we’re hoping to make this conditional on what the program finds in the data, rather than using many hard-coded indices that we have to handle out of program. Summing matrics and vectors to find that they are all zero data points is ideal, but for some reason the sum of clearly non-zero matrices is coming back as 0.

Did you do

sum(oris[cond][cont]) == 0 or what I wrote before which is wrong sum(oris[cond][cont[:]) == 0?

I tried both, though I only investigated what sum was returning for sum(oris[cond][cont]) == 0 after finally honing in on sum() and not the JSON file or the way it was being read into Stan being the issue

Can you post either simulated data or a small snapshot of oris that has the issue for us to test?

We don’t yet have an simulation program for making Stan-compatible data for this conditioned model, but I’m attaching JSON formatted data of a real orientation “oris” matrix from our experiments:

[[-4.5,0,3,4.5,1.5,3.5,0,3,0,-3.5,2,0,-3.5,-4.5,-4.5,-3.5,5,-2.5,-3,5,1],[-4,0,-4.5,-2.5,-3,-0.5,0,2,0,5,-2,-2.5,-2,-3.5,1,-4,-4.5,1.5,-4,4,-2.5],[1.5,0,2.5,-3,2,0,0,-2.5,0,0.5,3.5,4,1,0,1,1,-3,-1.5,2,-1,-3],[-2,0,-2.5,0.5,-5,3.5,0,-3,0,2.5,-3.5,-2,2,1,-5,2.5,-1,-3.5,-3.5,5,3.5],[5,0,-5,3.5,-1,3,0,3,0,-3,-2.5,3.5,4,2.5,3.5,-0.5,1.5,-2,4,3.5,0.5],[-2.5,0,-3.5,1,4.5,-1,0,-1,0,-4,-3,-4.5,-2,-4.5,-5,-2.5,-3.5,3.5,-2,2,-4.5],[2.5,0,3.5,-5,2.5,1.5,0,3.5,0,-1,4.5,1.5,0,5,3,2.5,3.5,-2.5,-1,-2.5,-2],[-3,0,2.5,-3.5,1,-3,0,1.5,0,3,4.5,-5,5,2.5,5,2.5,-5,-3.5,2.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0,1.5,-4,2.5,-4.5,0,4.5,0,-4,0,2.5,-1,-5,-2.5,-3,3.5,3,1,3.5,-1],[1.5,0,0,-1,0.5,-3.5,0,2.5,0,-2,1,-3,1.5,2.5,-0.5,-5,0,1,1,-0.5,5],[-1.5,0,-0.5,3,2,-2,0,-1,0,5,-3.5,-0.5,3.5,-0.5,-3.5,4,4,-4,-2.5,-5,-5]]

Thanks, and to make sure I get everything correct what are the values for ncond, ncont, nt, ns for this?

1 Like

This is just one orientation matrix corresponding to oris[1][2] from the two dimensional array that’s read in from the real data file. If it were attached to the full data set, this particular matrix corresponds to an experiment with an ncond of 4, an ncont of 6, an nt of 100, and an ns of 21

1 Like

This is the same one above that shouldn’t sum to 0, right?

1 Like

Yes, that’s correct. The code that the conditional was based on reads if (sum(oris[cond][cont]) == 0) { where this is a matrix that was actually summed to 0 in the print out print("Sum: ",sum(oris[cond][cont]));

Well, this does sum to 0 so that method won’t work.

You can create a function that returns 1 when the first non-zero value is encountered and 0 otherwise.

int is_all_zero(data matrix x) {
 int N = rows(x);
 int M = cols(x);

  for (n in 1:N) {
   for (m in 1:M) {
    if (x[n, m] != 0.) return 0;
    }
   }
 return 1;
}

You can then do this in the transformed data block and create the index where this occurs so that it is dynamic within the Stan program.

1 Like

Thanks for the suggestion! I will play around with this soon and report back, but isn’t that odd behavior for sum()? The matrix is almost entirely non-zero, but the sum() function returns 0? Is it a bug? Or am I misusing the function?

Edit: Oh! I see! Is it because the orientations are on average equally positive and negative and at the same magnitudes? I didn’t even think of that even though it’s obvious

I can’t give you solution twice, but hopefully three likes makes up for it!

1 Like

No worries! Good luck with the model and I hope you get it running smoothly!

1 Like