# 'Unknown variable energy' error when calling \`pm.bfmi(trace)\`

**URL:** <https://discourse.pymc.io/t/unknown-variable-energy-error-when-calling-pm-bfmi-trace/1909>\
**Category:** Questions\
**Created:** [September 11, 2018, 10:08pm UTC](https://discourse.pymc.io/t/unknown-variable-energy-error-when-calling-pm-bfmi-trace/1909 "2018-09-11T22:08:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![clausherther](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/clausherther/32/414_2.png) [@clausherther](https://discourse.pymc.io/u/clausherther)\
**Post date:** [September 11, 2018, 10:08pm UTC](https://discourse.pymc.io/t/unknown-variable-energy-error-when-calling-pm-bfmi-trace/1909/1 "2018-09-11T22:08:02Z")

</div>

Hi, I get this error `'Unknown variable energy'` when calling `pm.bfmi(trace)` on a `trace` variable that I sampled with `trace=backend` where `backend` is a `Text()` backend. This is not the reloaded trace from disk, but the original object returned by `pm.sample`. Same error on the `trace` loaded from disk. Any ideas what I’m doing wrong here, or is this a bug?

```python
with my_model:
    backend = pm.backends.Text("traces")
    trace = pm.sample(**SAMPLE_KWARGS, trace=backend)

```

```python
pm.bfmi(trace)

```

```python
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-70-69aaad1a469d> in <module>()
----> 1 pm.bfmi(trace)

~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/pymc3/stats.py in bfmi(trace)
   1056 The Bayesian fraction of missing information of the model and trace.
   1057 """
-> 1058 energy = trace['energy']
   1059 
   1060 return np.square(np.diff(energy)).mean() / np.var(energy)

~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/pymc3/backends/base.py in __getitem__ (self, idx)
    317 if var in self.stat_names:
    318 return self.get_sampler_stats(var, burn=burn, thin=thin)
--> 319 raise KeyError("Unknown variable %s" % var)
    320 
    321 _attrs = set(['_straces', 'varnames', 'chains', 'stat_names',

KeyError: 'Unknown variable energy'

```

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [September 12, 2018, 5:06am UTC](https://discourse.pymc.io/t/unknown-variable-energy-error-when-calling-pm-bfmi-trace/1909/2 "2018-09-12T05:06:30Z")

</div>

Text backend does not have all the information saved. Text backend will be deprecated and replace by xarray backend soon, so things should improve in the future.

---

<div class="post-metadata">

**Author:** ![clausherther](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/clausherther/32/414_2.png) [@clausherther](https://discourse.pymc.io/u/clausherther)\
**Post date:** [September 12, 2018, 1:03pm UTC](https://discourse.pymc.io/t/unknown-variable-energy-error-when-calling-pm-bfmi-trace/1909/3 "2018-09-12T13:03:49Z")

</div>

Got it, thx!
