Setting max treedepth in MATLABStan

I’m prototyping a model that’s performing pretty well so far — good mixing (although some autocorrelation), decent Rhat, good parameter recovery. However, it’s often hitting the hitting the maximum treedepth, which is concerning. I’d like to increase NUTS’ max_depth setting from the default value of 10.

Unfortunately, I’m primarily a MATLAB user, and I can’t figure out how to do this in MATLABStan (or it is not possible?).

If anyone knows how to set the max_depth parameter for NUTS in MATLABStan, I would greatly appreciate any instructions or other help you can offer.

(I’ve raised an issue on the MATLABStan github but no issues have been responded to on there since 1/2018. Hoping @brian-lau might see this post as he’s more recently been active here?)

Thanks everyone.

1 Like

Answering my own question. Digging in to the MATLABStan code, I finally saw that a default value (and value checking) for max treedepth is set in stan_params.m. So it is both possible and easy to hack in the functionality to MATLABStan.

In StanModel.m, after line 678, add:

case {'max_depth' 'hmc_max_depth'}
   set_hmc_max_depth(self,control.(fn{i}));

and after (what is now) line 1097, add:

function set_hmc_max_depth(self,val) 
    validateattributes(val,self.validators.sample.hmc.nuts.max_depth{1}, ...
    self.validators.sample.hmc.nuts.max_depth{2});
    self.params.sample.hmc.nuts.max_depth = val;
end

Now in your regular code, after compiling the model with a line like
sm = StanModel('file',stanfile);
you can change the max treedepth with:
sm.control.max_depth = 15;
then use the sampling method as usual.

Hope this helps others!

3 Likes