SMC kernel index out of bounds

Hi there,

I came across an issue with the following function in smc/kernels.py. I’m trying to figure out a minimal example but what I can say for now is that I have an index out of bounds for idx for
weight_accu += weights[idx]

It happens only for some random seed values. I can always hard code a boundary test for idx but I’m wondering whether that error implies a model that is problematic in any way?

Cheers,

Vian

def systematic_resampling(weights, rng):“”"Systematic resampling.

Parameters
----------
weights :
    The weights should be probabilities and the total sum should be 1.

Returns
-------
new_indices: array
    A vector of indices in the interval 0, ..., len(normalized_weights)
"""
lnw = len(weights)
arange = np.arange(lnw)
uniform = (rng.random(1) + arange) / lnw

idx = 0
weight_accu = weights[0]
new_indices = np.empty(lnw, dtype=int)
for i in arange:
    while uniform[i] > weight_accu:
        idx += 1
        weight_accu += weights[idx]
    new_indices[i] = idx

return new_indices

Perhaps some precision issue. CC @aloctavodia

Nice call, indeed I was using float32 throughout but there’s no crash with float64 for the same random seed

Just to follow up on that, since I saw that you marked it as solved, does that mean float32 precision is effectively discouraged? Does that make any sense to define all RVs as float32 and the custom likelihood functions as float64?

I don’t know who marked it as solved, not me.

Precision is not an easy problem. Sometimes you need it, sometimes you don’t. You may also be able to rewrite your models in ways to retain more precision even using smaller floats. No easy answers AFAICT