How to modify the value after a judge

Hi, your issue is that you are using Python’s standard if statement rather than using one of Theano’s logical operators. See this collab notebook for a detailed explanation, but here’s my suggested fix (avoiding both an if and your loop altogether):

test_model = pm.Model()

with test_model:
    a_fit = pm.Categorical('a_fit', p=np.ones(10)/10)
    b_fit = pm.Categorical('b_fit', p=np.ones(20)/20)  
    
    z_fit = a_fit*x+b_fit*y
    
    # Get index values where entry is < 4
    idx_pts = (z_fit < 4).nonzero()

    # Set these entries from above to 0
    z_fit = tt.set_subtensor(z_fit[idx_pts], 0)
    
    z_obs_nor = pm.Normal("z_fit", mu=z_fit, sigma=1, observed=z)
    trace_nor=pm.sample(draws=4000,tune=1000,chains=4,cores=4)

This correctly estimates a and b. Hope this helps!

1 Like