Modifying `point` in-place in custom step method

Old development guide for pymc2 states:

8.4 A second warning: Don’t update stochastic variables’ values in-place

Is this still a bad idea for pymc3? Would that mess up the caching of the values of parameters in NUTS?

I try to update the old code here.


import pymc3 as pm
import matplotlib.pyplot as plt
import numpy as np
from pymc3.step_methods.arraystep import BlockedStep
from pymc3.model import modelcontext

class StandardNormalStep(BlockedStep):
    def __init__(self, vars, model=None):
        model = modelcontext(model)
        # Must name atribute as self.vars
        self.vars = vars
        self.m = model

    def step(self, point):
        # [()] is indexing a scalar np.ndarray
        # We are modifying the 0D np.ndarray in-place.
        #print(id(self))  #Does nuts instantiate multiple steps?
        point[self.vars[0].name][()] = np.random.normal(0, 1)
        return point

def main():
    with pm.Model() as model:
        A = pm.Flat('A')
        step_A = StandardNormalStep([A])
        trace = pm.sample(5000, step=[step_A])


You can certainly modify the value inplace, that’s essentially how the compound step operates. For example, you can do Laplace approximation on some random variable, update the value and pass it on to NUTS