Using the user guide
[1]:
import polars as pl
import holoviews as hv
hv.extension('bokeh')
import bokeh.io
bokeh.io.output_notebook()
In the following pages, we demonstrate usage of the various modules of the bebi103 package. The viz module is always used in the context of statistical analysis, so its contents will be demonstrated within the bootstrap and stan sections.
In all graphics displays that use HoloViews, plots are rendered using Bokeh, like bokeh.io.show(hv.render(holoviews_graphic)). This is due to some issues with how Sphinx (which generates the documentation) renders HoloViews plots.
Sample data
In all of the demonstrations, we will use the same sample data set of \(x,y\) values taken from three different trials. The sample data were numerically generated using the following hierarchical generative model.
\begin{align} &n = (20, 25, 18)^\mathsf{T},\\[1em] &\theta = (3, 7)^\mathsf{T},\\[1em] &\mathsf{T} = \begin{pmatrix}1 & 0 \\ 0 & 16\end{pmatrix}, \\[1em] &\sigma = (2, 3)^\mathsf{T},\\[1em] &\rho = 0.6,\\[1em] &\mathsf{\Sigma} = \begin{pmatrix}\sigma_1^2 & \rho\sigma_1\sigma_2 \\ \rho\sigma_1\sigma_2 & \sigma_2^2\end{pmatrix}, \\[1em] &\theta_{1, i} \sim \text{Norm}(\theta, \mathsf{T}) \;\forall\,i\in\{1, 2, 3\},\\[1em] & \begin{pmatrix} x_{i, j} \\ y_{i,j} \end{pmatrix} \sim \text{Norm}(\theta_i, \mathsf{\Sigma})\;\forall\,i \in \{1, 2, 3\}, \; j \in \{1, \ldots, n_i \}. \end{align}
Unless doing Bayesian modeling with Stan, for simplicity we will ignore the hierarchical nature of the data set.
To get familiar with the data set, let’s load it in and take a look.
[2]:
df = pl.read_csv('sample_data.csv')
df
[2]:
| x | y | trial |
|---|---|---|
| f64 | f64 | i64 |
| 1.762886 | 11.955616 | 1 |
| 4.364957 | 11.136633 | 1 |
| 3.457626 | 12.301267 | 1 |
| -0.839319 | 10.401899 | 1 |
| 4.694602 | 11.925334 | 1 |
| … | … | … |
| 0.799388 | 3.618826 | 3 |
| -0.011088 | 4.408958 | 3 |
| 3.195386 | 11.213607 | 3 |
| 0.720947 | 6.987121 | 3 |
| 4.811848 | 8.647437 | 3 |
And we will also take a look at a scatter plot of the \(x, y\) values colored by trial number.
[3]:
plot = (
hv.Points(data=df, kdims=["x", "y"], vdims="trial")
.groupby("trial")
.overlay()
.opts(
frame_height=200,
frame_width=200,
legend_offset=(10, 60),
legend_position="right",
show_grid=True,
toolbar="above",
)
)
bokeh.io.show(hv.render(plot))
WARNING:bokeh.core.validation.check:W-1005 (FIXED_SIZING_MODE): 'fixed' sizing mode requires width and height to be set: figure(id='f351932d-6e4f-4919-91a0-ec3e1ac3c1a6', ...)
For much of the analysis, we will consider univariate x-data. It is useful to visualize the x-data as an ECDF.
[4]:
df = df.with_columns((pl.col("x").rank(method="ordinal") / len(df)).alias('ECDF'))
ecdf_plot = (
hv.Scatter(df, kdims="x", vdims=["ECDF", "trial"],)
.groupby("trial")
.overlay()
.opts(
frame_height=200,
frame_width=300,
legend_offset=(10, 60),
legend_position="right",
show_grid=True,
toolbar="above",
)
)
bokeh.io.show(hv.render(ecdf_plot))
WARNING:bokeh.core.validation.check:W-1005 (FIXED_SIZING_MODE): 'fixed' sizing mode requires width and height to be set: figure(id='c9988d0c-f666-4de9-854b-5954b22e581c', ...)