I’ve always wondered whether there might be a way to specify the model without the string name on the right-hand side like we do now
with pm.Model() as linear_model:
weights = pm.Normal('weights', mu=0, sigma=1) # name 'weights' specified on RHS
Recently I’ve been playing with param (mostly in the Panel context, but it’s independent) and I realized that they know the label/name of a parameter without specifying it as a string in a class constructor call because they are using a smart metaclass which sets the names according to the attribute names.
In theory this should make it possible to specify the model like so
class LinearModel(pm.Model): # model definition as a class becomes mandatory though for this API
weights = pm.Normal(mu=0, sigma=1) # the name weights is encoded as the class-level attr name
if there is the following metaclass implementation (rough sketch) and names in variable constructors become optional
class ModelMetaclass(...):
def __init__(mcs, name, bases, dict_):
....
variables = ( (n, v) for (n,v) in dict_.items()
if isinstance(v, (Distribution, Deterministic))] # TODO some better baseclass?
for var_name, var in variables:
var._set_name(var_name) # probably has to do more things
In theory this might even be backwards compatible, it could be a next-gen API if people are interested. Personally, I encode models as a class quite often as it is convenient for stacking models, etc., so for me creating classes for models does not seems such a hassle.
One might argue that this is just syntax sugar and perhaps not that necessary. But I think that for quite a few people syntax sugar is an important decision factor (even though they may not admit it even to themselves).
I’m putting this idea here to gather feedback and see if there is any interest.