(gallery_bf)=

# Bayes_factor

Compute Bayes factor using Savage–Dickey ratio.

We can apply this function when the null model is nested within the alternative.
In other words when the null (``ref_val``) is a particular value of the model we are
building (see [here](https://statproofbook.github.io/P/bf-sddr.html)).

For others cases computing Bayes factor is not straightforward and requires more complex
methods. Instead, of Bayes factors, we usually recommend Pareto smoothed
importance sampling leave one out cross validation (PSIS-LOO-CV). In ArviZ, you will find
them as functions with ``loo`` in their names.



::::::{tab-set}
:class: full-width
:sync-group: backend

:::::{tab-item} Matplotlib
:sync: matplotlib

![Matplotlib version of plot_bf](_images/plot_bf.png)

:::::

:::::{tab-item} Bokeh
:sync: bokeh

```{bokeh-plot}
:source-position: none

from bokeh.plotting import show

from arviz_base import load_arviz_data

import arviz_plots as azp

azp.style.use("arviz-variat")

data = load_arviz_data("centered_eight")

pc = azp.plot_bf(
data,
backend="bokeh",  # change to preferred backend
var_names="mu"
)



# for some reason the bokeh plot extension needs explicit use of show
show(pc.viz["figure"].item() if pc.viz["figure"].item() is not None else pc.viz["plot"].item())
```

Link to this page with the [bokeh tab selected](https://arviz-plots.readthedocs.io/en/latest//gallery/plot_bf.html?backend=bokeh#synchronised-tabs)
:::::

:::::{tab-item} Plotly
:sync: plotly

```{jupyter-execute}
:hide-code:

from arviz_base import load_arviz_data

import arviz_plots as azp

azp.style.use("arviz-variat")

data = load_arviz_data("centered_eight")

pc = azp.plot_bf(
data,
backend="plotly",  # change to preferred backend
var_names="mu"
)

pc.show()
```

Link to this page with the [plotly tab selected](https://arviz-plots.readthedocs.io/en/latest//gallery/plot_bf.html?backend=plotly#synchronised-tabs)
:::::
::::::

```{literalinclude} _scripts/plot_bf.py
:emphasize-lines: 11
```


:::{seealso}
API Documentation: {func}`~arviz_plots.plot_bf`
:::



## Other examples with `plot_bf`

```{eval-rst}
.. minigallery:: plot_bf
```


:::{div} example-plot-download
{download}`Download Python Source Code: plot_bf.py<_scripts/plot_bf.py>`
:::
