question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting NaNs when running plot_trace

See original GitHub issue

Describe the bug I’m getting a ValueError: cannot convert float NaN to integer when running plot_trace on my NumPyro sampled data. As far as I can neither my data nor samples contain any NaNs.

To Reproduce

I’m not able to share any code and data however I’ve done some debugging that could lead to where the error is coming from:

Full Stacktrace:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<timed exec> in <module>

/project/venv/lib/python3.9/site-packages/arviz/plots/traceplot.py in plot_trace(data, var_names, filter_vars, transform, coords, divergences, kind, figsize, rug, lines, circ_var_names, circ_var_units, compact, compact_prop, combined, chain_prop, legend, plot_kwargs, fill_kwargs, rug_kwargs, hist_kwargs, trace_kwargs, rank_kwargs, labeller, axes, backend, backend_config, backend_kwargs, show)
    252 
    253     plot = get_plotting_function("plot_trace", "traceplot", backend)
--> 254     axes = plot(**trace_plot_args)
    255 
    256     return axes

/project/venv/lib/python3.9/site-packages/arviz/plots/backends/matplotlib/traceplot.py in plot_trace(data, var_names, divergences, kind, figsize, rug, lines, circ_var_names, circ_var_units, compact, compact_prop, combined, chain_prop, legend, labeller, plot_kwargs, fill_kwargs, rug_kwargs, hist_kwargs, trace_kwargs, rank_kwargs, plotters, divergence_data, axes, backend_kwargs, backend_config, show)
    286                     aux_plot_kwargs = dealiase_sel_kwargs(plot_kwargs, compact_prop_iter, sub_idx)
    287                     aux_trace_kwargs = dealiase_sel_kwargs(trace_kwargs, compact_prop_iter, sub_idx)
--> 288                     ax = _plot_chains_mpl(
    289                         ax,
    290                         idy,

/project/venv/lib/python3.9/site-packages/arviz/plots/backends/matplotlib/traceplot.py in _plot_chains_mpl(axes, idy, value, data, chain_prop, combined, xt_labelsize, rug, kind, trace_kwargs, hist_kwargs, plot_kwargs, fill_kwargs, rug_kwargs, rank_kwargs, circular, circ_var_units, circ_units_trace)
    486             aux_kwargs = dealiase_sel_kwargs(plot_kwargs, chain_prop, chain_idx)
    487             if not idy:
--> 488                 axes = plot_dist(
    489                     values=row,
    490                     textsize=xt_labelsize,

/project/venv/lib/python3.9/site-packages/arviz/plots/distplot.py in plot_dist(values, values2, color, kind, cumulative, label, rotated, rug, bw, quantiles, contour, fill_last, figsize, textsize, plot_kwargs, fill_kwargs, rug_kwargs, contour_kwargs, contourf_kwargs, pcolormesh_kwargs, hist_kwargs, is_circular, ax, backend, backend_kwargs, show, **kwargs)
    210 
    211     plot = get_plotting_function("plot_dist", "distplot", backend)
--> 212     ax = plot(**dist_plot_args)
    213     return ax

/project/venv/lib/python3.9/site-packages/arviz/plots/backends/matplotlib/distplot.py in plot_dist(values, values2, color, kind, cumulative, label, rotated, rug, bw, quantiles, contour, fill_last, figsize, textsize, plot_kwargs, fill_kwargs, rug_kwargs, contour_kwargs, contourf_kwargs, pcolormesh_kwargs, hist_kwargs, is_circular, ax, backend_kwargs, show)
     91         legend = label is not None
     92 
---> 93         ax = plot_kde(
     94             values,
     95             values2,

/project/venv/lib/python3.9/site-packages/arviz/plots/kdeplot.py in plot_kde(values, values2, cumulative, rug, label, bw, adaptive, quantiles, rotated, contour, hdi_probs, fill_last, figsize, textsize, plot_kwargs, fill_kwargs, rug_kwargs, contour_kwargs, contourf_kwargs, pcolormesh_kwargs, is_circular, ax, legend, backend, backend_kwargs, show, return_glyph, **kwargs)
    259                 bw = "experimental"
    260 
--> 261         grid, density = kde(values, is_circular, bw=bw, adaptive=adaptive, cumulative=cumulative)
    262         lower, upper = grid[0], grid[-1]
    263 

/project/venv/lib/python3.9/site-packages/arviz/stats/density_utils.py in kde(x, circular, **kwargs)
    490         kde_fun = _kde_linear
    491 
--> 492     return kde_fun(x, **kwargs)
    493 
    494 

/project/venv/lib/python3.9/site-packages/arviz/stats/density_utils.py in _kde_linear(x, bw, adaptive, extend, bound_correction, extend_fct, bw_fct, bw_return, custom_lims, cumulative, grid_len, **kwargs)
    583         grid, pdf = _kde_adaptive(x, bw, grid_edges, grid_counts, grid_len, bound_correction)
    584     else:
--> 585         grid, pdf = _kde_convolution(x, bw, grid_edges, grid_counts, grid_len, bound_correction)
    586 
    587     if cumulative:

/project/venv/lib/python3.9/site-packages/arviz/stats/density_utils.py in _kde_convolution(x, bw, grid_edges, grid_counts, grid_len, bound_correction, **kwargs)
    704     grid = (grid_edges[1:] + grid_edges[:-1]) / 2
    705 
--> 706     kernel_n = int(bw * 2 * np.pi)
    707     if kernel_n == 0:
    708         kernel_n = 1

ValueError: cannot convert float NaN to integer

The bw parameter from computing the density seems to become NaN at a certain point. From the debugger:

> /project/venv/lib/python3.9/site-packages/arviz/stats/density_utils.py(585)_kde_linear()
    583         grid, pdf = _kde_adaptive(x, bw, grid_edges, grid_counts, grid_len, bound_correction)
    584     else:
--> 585         grid, pdf = _kde_convolution(x, bw, grid_edges, grid_counts, grid_len, bound_correction)
    586 
    587     if cumulative:

ipdb>  bw
nan

It seems that the error comes from the kde_fun call, at this point there are no NaNs anywhere in the arguments:

> /project/venv/lib/python3.9/site-packages/arviz/stats/density_utils.py(492)kde()
    490         kde_fun = _kde_linear
    491 
--> 492     return kde_fun(x, **kwargs)
    493 
    494 

ipdb>  x
array([0.06682166, 0.06682151, 0.0668215 , 0.06682149, 0.06682152,
       0.06682113, 0.0668212 , 0.06682108, 0.06682114, 0.06682119,
       0.06682099, 0.06682099, 0.06682114, 0.06682115, 0.06682112,
       0.06682058, 0.0668207 , 0.06682093, 0.06682101, 0.06682117,
       0.06682105, 0.06682053, 0.06682035, 0.06682046, 0.06682055,
       0.06682087, 0.06682237, 0.06682225, 0.06682158, 0.06682166,
       0.06682136, 0.06682222, 0.06682229, 0.06682215, 0.06682245,
       0.06682227, 0.06682238, 0.06682224, 0.06682215, 0.06682251,
       0.0668226 , 0.06682282, 0.06682256, 0.06682279, 0.06682241,
       0.06682275, 0.06682276, 0.06682317, 0.0668232 , 0.06682297],
      dtype=float32)
ipdb>  kwargs
{'bw': 'experimental', 'adaptive': False, 'cumulative': False}

Expected behavior No NaNs to appear in plot_trace when there are no NaNs in the input data.

Additional context Installed the Arviz development version via GitHub pip install git+git://github.com/arviz-devs/arviz.git

numpyro   : 0.7.2
matplotlib: 3.4.3
sys       : 3.9.7 | packaged by conda-forge | (default, Sep  2 2021, 17:58:34) 
[GCC 9.4.0]
xarray    : 0.19.0
seaborn   : 0.11.2
arviz     : 0.11.3
jax       : 0.2.20

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ahartikainencommented, Oct 2, 2021

https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance

Because SumSq and (Sum×Sum)/n can be very similar numbers, cancellation can lead to the precision of the result to be much less than the inherent precision of the floating-point arithmetic used to perform the computation. Thus this algorithm should not be used in practice

0reactions
peterroelantscommented, Oct 4, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

NaN loss when training regression network - Stack Overflow
I was running into my loss function suddenly returning a nan after it go so far ... If you're performing textual analysis and...
Read more >
How to use plot_trace to draw individuals results in dependet ...
I want to use az.plot_trace() to draw trace for all subjects. However, I just got a long picture which contains 10 of subjects'...
Read more >
Cost function turning into nan after a certain number of iterations
Well, if you get NaN values in your cost function, it means that the input is outside of the function domain. E.g. the...
Read more >
Using a custom BoTorch model in Ax
In this tutorial, we illustrate how to use a custom BoTorch model within Ax's botorch_modular API. This allows us to harness the convenience...
Read more >
plotTrace: Plot MCMC Traces - RDocumentation
Plot Markov chain Monte Carlo traces. This is a diagnostic plot for deciding whether a chain shows unwanted trends.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found