arviz-devs / arviz

Exploratory analysis of Bayesian models with Python
https://python.arviz.org
Apache License 2.0
1.56k stars 388 forks source link

How to calculate the mean of the specified chains and draws #2284

Open LevCarlo opened 8 months ago

LevCarlo commented 8 months ago

trace = az.from_netcdf(trace_filepath) selected_posterior = trace.posterior.sel(chain=selected_chains, draw=selected_draws) selected_posterior[list(trace.posterior.data_vars)[0]].mean(("chain", "draw")) why does the code get stuck when applying mean method to selected_posterior?

ahartikainen commented 8 months ago

What do you mean by stuck?

What do you get with .mean(["chain", "draw"])?

LevCarlo commented 8 months ago

What do you mean by stuck?

What do you get with .mean(["chain", "draw"])?

No output after ~20 minutes running. trace.posterior.mean(("chain", "draw")) works well, but not for selected_posterior.

ahartikainen commented 8 months ago

Before calling the mean make sure selected posterior looks the way you expect it to look.

Then take the first data_var and check again. Then call mean.

Also make sure you want to use .sel and not .isel.

Are you certain that selected draw and selected chain are good?

ahartikainen commented 8 months ago

Can you run this example normally?

import arviz as az
data = az.load_arviz_data("centered_eight")
selected_posterior = data.posterior.sel(chain=[1,2], draw=list(range(422)))
print("selected_posterior:", selected_posterior[list(data.posterior.data_vars)[0]])
print("\nmean:", selected_posterior[list(data.posterior.data_vars)[0]].mean(("chain", "draw")))