# Buggy code
def _run_convergence_checks(self, trace, model):
    if trace.nchains == 1:
        msg = (
            "Only one chain was sampled, this makes it impossible to "
            "run some convergence checks"
        )
        warn = SamplerWarning(WarningType.BAD_PARAMS, msg, "info", None, None, None)
        self._add_warnings([warn])
        return

    from pymc3 import diagnostics

    valid_name = [rv.name for rv in model.free_RVs + model.deterministics]
    varnames = []
    for rv in model.free_RVs:
        rv_name = rv.name
        if is_transformed_name(rv_name):
            rv_name2 = get_untransformed_name(rv_name)
            rv_name = rv_name2 if rv_name2 in valid_name else rv_name
        varnames.append(rv_name)

    self._effective_n = effective_n = diagnostics.effective_n(trace, varnames)
    self._gelman_rubin = gelman_rubin = diagnostics.gelman_rubin(trace, varnames)

    warnings = []
    rhat_max = max(val.max() for val in gelman_rubin.values())
    if rhat_max > 1.4:
        msg = (
            "The gelman-rubin statistic is larger than 1.4 for some "
            "parameters. The sampler did not converge."
        )
        warn = SamplerWarning(
            WarningType.CONVERGENCE, msg, "error", None, None, gelman_rubin
        )
        warnings.append(warn)
    elif rhat_max > 1.2:
        msg = "The gelman-rubin statistic is larger than 1.2 for some parameters."
        warn = SamplerWarning(
            WarningType.CONVERGENCE, msg, "warn", None, None, gelman_rubin
        )
        warnings.append(warn)
    elif rhat_max > 1.05:
        msg = (
            "The gelman-rubin statistic is larger than 1.05 for some "
            "parameters. This indicates slight problems during "
            "sampling."
        )
        warn = SamplerWarning(
            WarningType.CONVERGENCE, msg, "info", None, None, gelman_rubin
        )
        warnings.append(warn)

    eff_min = min(val.min() for val in effective_n.values())
    n_samples = len(trace) * trace.nchains
    if eff_min < 200 and n_samples >= 500:
        msg = (
            "The estimated number of effective samples is smaller than "
            "200 for some parameters."
        )
        warn = SamplerWarning(
            WarningType.CONVERGENCE, msg, "error", None, None, effective_n
        )
        warnings.append(warn)
    elif eff_min / n_samples < 0.1:
        msg = "The number of effective samples is smaller than 10% for some parameters."
        warn = SamplerWarning(
            WarningType.CONVERGENCE, msg, "warn", None, None, effective_n
        )
        warnings.append(warn)
    elif eff_min / n_samples < 0.25:
        msg = "The number of effective samples is smaller than 25% for some parameters."
        warn = SamplerWarning(
            WarningType.CONVERGENCE, msg, "info", None, None, effective_n
        )
        warnings.append(warn)

    self._add_warnings(warnings)


# Traceback (KeyError)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-7-45e332f1b8ef> in <module>()
1 with model:
----> 2     trace = sample(1000, trace=[dL0])

~/Repos/pymc3/pymc3/sampling.py in sample(draws, step, init, n_init, start, trace, chain_idx, chains, cores, tune, nuts_kwargs, step_kwargs, progressbar, model, random_seed, live_plot, discard_tuned_samples, live_plot_kwargs, compute_convergence_checks, use_mmap, **kwargs)
471                 "convergence reliably.")
472         else:
--> 473             trace.report._run_convergence_checks(trace, model)
474
475     trace.report._log_summary()

~/Repos/pymc3/pymc3/backends/report.py in _run_convergence_checks(self, trace, model)
80             varnames.append(rv_name)
81
---> 82         self._effective_n = effective_n = diagnostics.effective_n(trace, varnames)
83         self._gelman_rubin = gelman_rubin = diagnostics.gelman_rubin(trace, varnames)
84

~/Repos/pymc3/pymc3/diagnostics.py in effective_n(mtrace, varnames, include_transformed)
298
299     for var in varnames:
--> 300         n_eff[var] = generate_neff(mtrace.get_values(var, combine=False))
301
302     return n_eff

~/Repos/pymc3/pymc3/backends/base.py in get_values(self, varname, burn, thin, combine, chains, squeeze)
426         try:
427             results = [self._straces[chain].get_values(varname, burn, thin)
--> 428                        for chain in chains]
429         except TypeError:  # Single chain passed.
430             results = [self._straces[chains].get_values(varname, burn, thin)]

~/Repos/pymc3/pymc3/backends/base.py in <listcomp>(.0)
426         try:
427             results = [self._straces[chain].get_values(varname, burn, thin)
--> 428                        for chain in chains]
429         except TypeError:  # Single chain passed.
430             results = [self._straces[chains].get_values(varname, burn, thin)]

~/Repos/pymc3/pymc3/backends/ndarray.py in get_values(self, varname, burn, thin)
141         A NumPy array
142         """
--> 143         return self.samples[varname][burn::thin]
144
145     def _slice(self, idx):

KeyError: 'beta0'

# Fixed code
def _run_convergence_checks(self, trace, model):
    if trace.nchains == 1:
        msg = (
            "Only one chain was sampled, this makes it impossible to "
            "run some convergence checks"
        )
        warn = SamplerWarning(WarningType.BAD_PARAMS, msg, "info", None, None, None)
        self._add_warnings([warn])
        return

    from pymc3 import diagnostics

    valid_name = [rv.name for rv in model.free_RVs + model.deterministics]
    varnames = []
    for rv in model.free_RVs:
        rv_name = rv.name
        if is_transformed_name(rv_name):
            rv_name2 = get_untransformed_name(rv_name)
            rv_name = rv_name2 if rv_name2 in valid_name else rv_name
        if rv_name in trace.varnames:
            varnames.append(rv_name)

    self._effective_n = effective_n = diagnostics.effective_n(trace, varnames)
    self._gelman_rubin = gelman_rubin = diagnostics.gelman_rubin(trace, varnames)

    warnings = []
    rhat_max = max(val.max() for val in gelman_rubin.values())
    if rhat_max > 1.4:
        msg = (
            "The gelman-rubin statistic is larger than 1.4 for some "
            "parameters. The sampler did not converge."
        )
        warn = SamplerWarning(
            WarningType.CONVERGENCE, msg, "error", None, None, gelman_rubin
        )
        warnings.append(warn)
    elif rhat_max > 1.2:
        msg = "The gelman-rubin statistic is larger than 1.2 for some parameters."
        warn = SamplerWarning(
            WarningType.CONVERGENCE, msg, "warn", None, None, gelman_rubin
        )
        warnings.append(warn)
    elif rhat_max > 1.05:
        msg = (
            "The gelman-rubin statistic is larger than 1.05 for some "
            "parameters. This indicates slight problems during "
            "sampling."
        )
        warn = SamplerWarning(
            WarningType.CONVERGENCE, msg, "info", None, None, gelman_rubin
        )
        warnings.append(warn)

    eff_min = min(val.min() for val in effective_n.values())
    n_samples = len(trace) * trace.nchains
    if eff_min < 200 and n_samples >= 500:
        msg = (
            "The estimated number of effective samples is smaller than "
            "200 for some parameters."
        )
        warn = SamplerWarning(
            WarningType.CONVERGENCE, msg, "error", None, None, effective_n
        )
        warnings.append(warn)
    elif eff_min / n_samples < 0.1:
        msg = "The number of effective samples is smaller than 10% for some parameters."
        warn = SamplerWarning(
            WarningType.CONVERGENCE, msg, "warn", None, None, effective_n
        )
        warnings.append(warn)
    elif eff_min / n_samples < 0.25:
        msg = "The number of effective samples is smaller than 25% for some parameters."
        warn = SamplerWarning(
            WarningType.CONVERGENCE, msg, "info", None, None, effective_n
        )
        warnings.append(warn)

    self._add_warnings(warnings)