Fix/Freeze Parameter

This question was originally asked by @kpei on Github, but it looks like it never made it to this forum.

I have a similar situation where I’d like to fix a parameter so that the parameter’s values don’t change during fitting, yet I’m unsure how I can do that without making it an observed stochastic random variable. I looked into pm.Bound, but that’s a bit different than what I’m trying to do.

From my understanding, you can only do this by setting them as observed. You can either build a model factory function that allows you to flexibly create models with or without giving the variables as observed, or you have to write the models down separately

2 Likes

Ok thank you @lucianopaz, that’s what I figured after reading through the docs.

I believe this can be accomplished by using DiscreteUniform and setting the lower and upper bound to the same value. I’m using the DEMetropolisZ sampler and it works just fine when combined, e.g., with parameters on Normal or Uniform distributions.

with pm.Model() as model:
    param0 = pm.TruncatedNormal('param0', 0, [-1, 1])
    fixed = pm.DiscreteUniform('fixed', lower = 3.14, upper = 3.14)

For everyone still coming to this post, as me, after many years, since we’re beginners: a straightforward way, if I understood correctly, is to define your rv as you would normally do, and exploit the observed=<value> argument inside the definition of the rv.
E.g. Fix the parameter “A” to a value of 0 and don’t calibrate it

A = pm.Normal('A', 0, 1, observed=0) 

This is particularly useful if you, as me, have a model with many parameters and want to perform multiple calibrations by fixing different parameters every time.

1 Like

You can use pm.do these days

https://www.pymc.io/projects/docs/en/stable/api/model/generated/pymc.model.transform.conditioning.do.html

1 Like