# Deleting / replacing RVs

**URL:** https://discourse.pymc.io/t/deleting-replacing-rvs/735
**Category:** Development
**Created:** [January 15, 2018, 7:23pm UTC](https://discourse.pymc.io/t/deleting-replacing-rvs/735 "2018-01-15T19:23:16Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![cvigoe](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cvigoe/32/351_2.png) [@cvigoe](https://discourse.pymc.io/u/cvigoe)
#### Post date: [January 15, 2018, 7:23pm UTC](https://discourse.pymc.io/t/deleting-replacing-rvs/735/1 "2018-01-15T19:23:16Z")

</div>

Is it possible to replace a RV after it has been created the usual way? e.g. `var = pm.Beta('var', 1, 1)`

Suppose later on, I wish to redefine `var = pm.Normal('var', 0, 1)`

Currently, doing so would invoke an error:

`>>>ValueError: Variable name var already exists.`

I can’t seem to see how to delete RV from PyMC3 either.

Is this currently possible in PyMC3? I suspect there may be an issue in implementing this sort of functionality due to the way in which PyMC3 deals with RVs, automatically building it into `pm.Model()`.

Thoughts?

---

<div class="post-metadata">

### Author: ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)
#### Post date: [January 15, 2018, 8:38pm UTC](https://discourse.pymc.io/t/deleting-replacing-rvs/735/2 "2018-01-15T20:38:32Z")

</div>

I think you can use `theano.clone` to modify the underly theano computational graph, but I dont know exactly how you can to do that as well.

---

<div class="post-metadata">

### Author: ![npschafer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/npschafer/32/3389_2.png) [@npschafer](https://discourse.pymc.io/u/npschafer)
#### Post date: [December 12, 2020, 3:06pm UTC](https://discourse.pymc.io/t/deleting-replacing-rvs/735/3 "2020-12-12T15:06:50Z")

</div>

@junpenglao @lucianopaz I’m interested in doing what @cvigoe described. Do we now know whether or not this is possible with `theano.clone`? If so, do you know of an example illustrating this or can you sketch the syntax?

---

<div class="post-metadata">

### Author: ![lucianopaz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/lucianopaz/32/2186_2.png) [@lucianopaz](https://discourse.pymc.io/u/lucianopaz)
#### Post date: [December 12, 2020, 8:06pm UTC](https://discourse.pymc.io/t/deleting-replacing-rvs/735/4 "2020-12-12T20:06:09Z")

</div>

@npschafer, no, it is not possible at the moment. You can clone a theano computational graph and replace a node in it with another one. The problem is that a pymc3 Model has a computational graph that only represents the calculations underlying the `logp`, but the distributions are almost completely disconnected from this graph after each RV is created. This means that even if you managed to change the models logp using clone, this would only have an effect when you try to do inference (`sample`), but it wouldn’t do anything when you do forward sampling (`sample_prior_predictive` or `sample_posterior_predictive`).

This will change once we move pymc random variables to use Random variable operators (this work was introduced in symbolic pymc and it is being refined in theano-pymc itself). Once we are able to do that, the pymc model will have the full computational graph that will be responsible for inference and forward sampling, so it will be possible to clone and replace nodes in it, as you would with any other theano graph. In fact, symbolic pymc showed that this could be used to implement automatic model reparametrizations that had a more robust sampling behavior.

---

<div class="post-metadata">

### Author: ![npschafer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/npschafer/32/3389_2.png) [@npschafer](https://discourse.pymc.io/u/npschafer)
#### Post date: [December 13, 2020, 3:06pm UTC](https://discourse.pymc.io/t/deleting-replacing-rvs/735/5 "2020-12-13T15:06:25Z")

</div>

Thanks for the thorough explanation, @lucianopaz. Much appreciated!

---

<div class="post-metadata">

### Author: ![perrette](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/perrette/32/4637_2.png) [@perrette](https://discourse.pymc.io/u/perrette)
#### Post date: [October 4, 2022, 10:33am UTC](https://discourse.pymc.io/t/deleting-replacing-rvs/735/6 "2022-10-04T10:33:34Z")

</div>

Hi @lucianopaz, any update on this in pymc v4 ?  
I’d find it convenient to write a code that looks like:

```auto
with pm.Model() as model:
    a = pm.Normal('a', 0, 1)
    trace = pm.sample()

with model:
    i = pm.Categorical('i', np.arange(1000)/1000)
    a_trace = pm.Data('a_trace', trace.posterior['a'].sel(chain=0).values)
    pm.update_RVs({'a': a_trace[i]})
    pm.sample()

```

or alternatively:

```auto
with model2:
    i = pm.Categorical('i', np.arange(1000)/1000)
    a_trace = pm.Data('a_trace', trace.posterior['a'].sel(chain=0).values)
    pm.clone_from(model, update={'a', a_trace})
    pm.sample()

```

I currently use a model factory function with if/then/else, which is fine, but I think something like the above would be more readable (if easily done internally of course – otherwise not worth the extra machinery).

---

<div class="post-metadata">

### Author: ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)
#### Post date: [October 4, 2022, 11:39am UTC](https://discourse.pymc.io/t/deleting-replacing-rvs/735/7 "2022-10-04T11:39:53Z")

</div>

Do you want to replace a free variable by a fixed value?

---

<div class="post-metadata">

### Author: ![karthur](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/karthur/32/4470_2.png) [@karthur](https://discourse.pymc.io/u/karthur)
#### Post date: [March 15, 2024, 5:53pm UTC](https://discourse.pymc.io/t/deleting-replacing-rvs/735/8 "2024-03-15T17:53:54Z")

</div>

I do! Is there a way to do this? I have tried variations of:

```auto
with pm.Model() as model:
    beta = pm.LogNormal(...)
    ...
    # If I find out, for a certain dataset, this should be a fixed value...
    del globals()["beta"] 
    beta = fixed_value # Didn't work--"beta" is still sampled

    setattr(model, name, fixed_value) # Also didn't work--"beta" is still sampled

```

---

<div class="post-metadata">

### Author: ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)
#### Post date: [March 15, 2024, 6:06pm UTC](https://discourse.pymc.io/t/deleting-replacing-rvs/735/9 "2024-03-15T18:06:06Z")

</div>

You can use `pm.do` to replace variables by constants: [pymc.model.transform.conditioning.do — PyMC 5.11.0 documentation](https://www.pymc.io/projects/docs/en/stable/api/model/generated/pymc.model.transform.conditioning.do.html#pymc.model.transform.conditioning.do)

---

<div class="post-metadata">

### Author: ![karthur](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/karthur/32/4470_2.png) [@karthur](https://discourse.pymc.io/u/karthur)
#### Post date: [March 15, 2024, 6:39pm UTC](https://discourse.pymc.io/t/deleting-replacing-rvs/735/10 "2024-03-15T18:39:33Z")

</div>

This is great, thanks!

If the random variable(s) to be fixed are not known until runtime (for example, given as a string, `rv_name`):

```auto
with pm.Model() as model:
    ...

fixed = dict()
fixed[getattr(model, rv_name)] = fixed_value
model2 = pm.do(model, fixed)

```

---

<div class="post-metadata">

### Author: ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)
#### Post date: [March 15, 2024, 8:12pm UTC](https://discourse.pymc.io/t/deleting-replacing-rvs/735/11 "2024-03-15T20:12:47Z")

</div>

I think the method also accepts string with the variable names as keys
