Why is the compiled model always anonymous in PyStan

When the model is compiled with StanModel, the model name is always anon_model_something, even when I’m compiling it with a .stan file and I’ve specified a model name.

It has always been like this (mildly irritating) but I never bothered to ask… I assume the same thing might be happening to everyone; if not I will provide further details and a replicable example.

kwarg obfuscate_model_name

Here is a simple explanation (there could be other reasons also):

Stan program is compiled to a python module. The first time python loads (imports) module, everything works fine. The second time, python checks if the module is already loaded and can skip the import (its probably more complicated than this).

So you compile a model, then you update it and compile it again. It could fail to load correctly.

See https://github.com/stan-dev/pystan/pull/58

Edit. If the anon_model is the problem, I have no idea. I will test this.

I can share you the real answer: PyStan doesn’t keep track of the
filename because it uses the full Stan program code as the “name”.

I can see how giving the program a short name would be convenient in
some cases. There is, however, a case where naming can be problematic:
two models which have the same filename (lm1.stan and lm1.stan) but
which have different program code.

PyStan (unlike, perhaps, CmdStan) is optimizing for the case where you
always have the full Stan program code available. When this is the case,
you can refer to a PyStan model unambiguously with (the hash of) the
program code.

2 Likes

But model name should be the same as the model_name?

self.model_name = stanc_ret['model_name']

msg = "COMPILING THE C++ CODE FOR MODEL {} NOW."
logger.info(msg.format(self.model_name))

I actually never find the model name of any use, just curious why it’s not behaving like it should (like in RStan, though my experience with RStan is very limited).

I can see things can be a little confusing: there is the model identifier living in python, there is the .stan file living in the system environment, and there is the model name living in the model namespace. But what do you mean by two models with the same filename?

  1. Are they two models compiled from the same .stan file? In that case aren’t they just the same model with two identifiers?
  2. Or are they two models compiled from two different .stan files? In that case I don’t see how they can have the same filename (lm1.stan and lm1.stan): they can’t be in the same directory, and when in different directories they will have different file path so the complete filenames will be different (folder1/lm1.stan and folder2/lm1.stan).
  3. If they are two different models with the same model_name, it’s confusing but I don’t see why it matters, because they are living in different namespaces.

You’re right. You can set the model name right now. The docs don’t
encourage the practice for the reasons I sketched.

In PyStan 3 I’ve dropped the support for model names entirely. Models
are identified by their Stan program code.