GaussianRandomWalk error 4.0.0b5

I have tried upgradinging to 4.0.0b5. But I get an error with the GaussianRandomWalk distribution. I have recoded to the new API. The error message is a bit cryptic: “.dist() missing 1 required positional argument: ‘dist_params’”.

My code …

def define_model(n_brands, n_days, poll_brand,
zero_centered_y,
measurement_error_sd):
“”“PyMC3 model for pooling/aggregating voter opinion polls”“”

model = pm.Model()
with  model:
    # priors 
    unanchored_house_bias = pm.Cauchy("unanchored_house_bias", 
                                      alpha=0, beta=10, shape=n_brands)
    zero_sum_house_bias = pm.Deterministic('zero_sum_house_bias', 
        var=(unanchored_house_bias - unanchored_house_bias.mean()))
    
    # temporal model
    EARLY_DATA_ITEMS = 5 
    educated_guess = zero_centered_y[:min(EARLY_DATA_ITEMS, len(zero_centered_y))].mean()
    SPAN = 2.0
    starting_distro = pm.Normal('starting_distro', mu=educated_guess, sigma=SPAN) 
    DRIFT = 0.0
    innovation = 0.15 # from experience ... day-to-day change distribution sigma
    grw = pm.GaussianRandomWalk('grw', mu=DRIFT, sigma=innovation, 
                                init=starting_distro, steps=n_days) ### FAILS HERE

    # the observational model
    observed = pm.Normal("observed", 
                         mu=grw[poll_day] 
                            + zero_sum_house_bias[poll_brand.to_list()],
                         sigma=measurement_error_sd, observed=zero_centered_y)
return model
1 Like

@RavinKumar, you have been working on this, no?

init has to be an “unnamed” distribution, created with the .dist API. In your code you should use this:

starting_distro = pm.Normal.dist(mu=educated_guess, sigma=SPAN) 

You don’t want this distribution registered in the model directly, because the logp is accounted for by the GaussianRandomWalk. Otherwise it would double count it.

Ricardo, thanks for your quick reply.

I have made the change you suggest, but still get the same error message.

The updated code follows.

def define_model(n_brands, n_days, poll_brand, 
             zero_centered_y, 
             measurement_error_sd):
    """PyMC model for pooling/aggregating voter opinion polls"""

    model = pm.Model()
    with  model:
        # priors 
        unanchored_house_bias = pm.Cauchy("unanchored_house_bias", 
                                          alpha=0, beta=10, shape=n_brands)
        zero_sum_house_bias = pm.Deterministic('zero_sum_house_bias', 
            var=(unanchored_house_bias - unanchored_house_bias.mean()))
    
        # temporal model
        DRIFT = 0.0
        INNOVATION = 0.15 # from experience ... day-to-day change distribution sigma
        EARLY_DATA_ITEMS = 5 
        SIGMA = 2.0
        educated_guess = zero_centered_y[:min(EARLY_DATA_ITEMS, 
                         len(zero_centered_y))].mean()
        start_dist = pm.Normal.dist(mu=educated_guess, sigma=SIGMA)
        print(f'DRIFT: {DRIFT}, INNOVATION: {INNOVATION}, '
              f'educated_guess: {educated_guess}, SIGMA: {SIGMA}'
              f'\ninit: {type(start_dist)}')
        grw = pm.GaussianRandomWalk('grw', mu=DRIFT, sigma=INNOVATION, 
                                    init=start_dist, steps=n_days) ### FAILS HERE

        # the observational model
        observed = pm.Normal("observed", 
                             mu=grw[poll_day] 
                                + zero_sum_house_bias[poll_brand.to_list()],
                             sigma=measurement_error_sd, observed=zero_centered_y)
    return model

You can see that I have added a diagnostic print statement, which tells me the type of the starting_dist is <class ‘aesara.tensor.var.TensorVariable’>

Any further thoughts or suggestions would be welcomed.

Can you share the error traceback?

Apologies, I just realized the GaussianRandomWalk is not yet refactored/ready in the latest beta release. It should be available on the next release, or if you install directly from github main branch

1 Like

TypeError Traceback (most recent call last)
Input In [22], in <cell line: 1>()
2 print(series[‘title’])
4 (zero_centered_y, centre_offset, n_polls, n_days, day_zero, poll_day,
5 poll_brand, brand_map, n_brands, measurement_error_sd) = (
6 prepare_data_for_analysis(tpp, series[‘column’]))
----> 8 model = define_model(n_brands, n_days, poll_brand, zero_centered_y,
9 measurement_error_sd)
11 trace, summary = draw_samples(model)
13 plot_aggregation(trace, tpp, series[‘column’], day_zero, n_days,
14 centre_offset, series[‘point_color’],
15 series[‘line_color’], series[‘title’])

Input In [16], in define_model(n_brands, n_days, poll_brand, zero_centered_y, measurement_error_sd)
20 start_dist = pm.Normal.dist(mu=educated_guess, sigma=SIGMA)
21 print(f’DRIFT: {DRIFT}, INNOVATION: {INNOVATION}, ’
22 f’educated_guess: {educated_guess}, SIGMA: {SIGMA}’
23 f’\ninit: {type(start_dist)}’)
—> 24 grw = pm.GaussianRandomWalk(‘grw’, mu=DRIFT, sigma=INNOVATION,
25 init=start_dist, steps=n_days) ### FAILS HERE
27 # the observational model
28 observed = pm.Normal(“observed”,
29 mu=grw[poll_day]
30 + zero_sum_house_bias[poll_brand.to_list()],
31 sigma=measurement_error_sd, observed=zero_centered_y)

File /opt/homebrew/Caskroom/miniforge/base/lib/python3.9/site-packages/pymc/distributions/distribution.py:267, in Distribution.new(cls, name, rng, dims, initval, observed, total_size, transform, *args, **kwargs)
263 rng = model.next_rng()
265 # Create the RV and process dims and observed to determine
266 # a shape by which the created RV may need to be resized.
→ 267 rv_out, dims, observed, resize_shape = _make_rv_and_resize_shape(
268 cls=cls, dims=dims, model=model, observed=observed, args=args, rng=rng, **kwargs
269 )
271 if resize_shape:
272 # A batch size was specified through dims, or implied by observed.
273 rv_out = change_rv_size(rv_var=rv_out, new_size=resize_shape, expand=True)

File /opt/homebrew/Caskroom/miniforge/base/lib/python3.9/site-packages/pymc/distributions/distribution.py:166, in _make_rv_and_resize_shape(cls, dims, model, observed, args, **kwargs)
163 “”“Creates the RV and processes dims or observed to determine a resize shape.”""
164 # Create the RV without dims information, because that’s not something tracked at the Aesara level.
165 # If necessary we’ll later replicate to a different size implied by already known dims.
→ 166 rv_out = cls.dist(*args, **kwargs)
167 ndim_actual = rv_out.ndim
168 resize_shape = None

TypeError: dist() missing 1 required positional argument: ‘dist_params’

How do I install directly from github?

You have instructions in here: Installation Guide (Linux) · pymc-devs/pymc Wiki · GitHub

That’s for Linux, you can check the respective wiki pages for Windows or MacOS, if you are using one of those

1 Like

Thanks, that worked a treat.

1 Like