Interactive online version:

# Bayesian Imputation¶

Real-world datasets often contain many missing values. In those situations, we have to either remove those missing data (also known as “complete case”) or replace them by some values. Though using complete case is pretty straightforward, it is only applicable when the number of missing entries is so small that throwing away those entries would not affect much the power of the analysis we are conducting on the data. The second strategy, also known as imputation, is more applicable and will be our focus in this tutorial.

Probably the most popular way to perform imputation is to fill a missing value with the mean, median, or mode of its corresponding feature. In that case, we implicitly assume that the feature containing missing values has no correlation with the remaining features of our dataset. This is a pretty strong assumption and might not be true in general. In addition, it does not encode any uncertainty that we might put on those values. Below, we will construct a Bayesian setting to resolve those issues. In particular, given a model on the dataset, we will

• create a generative model for the feature with missing value
• and consider missing values as unobserved latent variables.
[1]:

# first, we need some imports
import os

from IPython.display import set_matplotlib_formats
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

from jax import numpy as jnp
from jax import ops, random
from jax.scipy.special import expit

import numpyro
from numpyro import distributions as dist
from numpyro.distributions import constraints
from numpyro.infer import MCMC, NUTS, Predictive

plt.style.use("seaborn")
if "NUMPYRO_SPHINXBUILD" in os.environ:
set_matplotlib_formats("svg")

assert numpyro.__version__.startswith("0.5.0")


## Dataset¶

The data is taken from the competition Titanic: Machine Learning from Disaster hosted on kaggle. It contains information of passengers in the Titanic accident such as name, age, gender,… And our target is to predict if a person is more likely to survive.

[2]:

train_df = pd.read_csv(
"https://raw.githubusercontent.com/agconti/kaggle-titanic/master/data/train.csv"
)
train_df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
#   Column       Non-Null Count  Dtype
---  ------       --------------  -----
0   PassengerId  891 non-null    int64
1   Survived     891 non-null    int64
2   Pclass       891 non-null    int64
3   Name         891 non-null    object
4   Sex          891 non-null    object
5   Age          714 non-null    float64
6   SibSp        891 non-null    int64
7   Parch        891 non-null    int64
8   Ticket       891 non-null    object
9   Fare         891 non-null    float64
10  Cabin        204 non-null    object
11  Embarked     889 non-null    object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.7+ KB

[2]:

PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
0 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.2500 NaN S
1 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 0 PC 17599 71.2833 C85 C
2 3 1 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.9250 NaN S
3 4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 0 113803 53.1000 C123 S
4 5 0 3 Allen, Mr. William Henry male 35.0 0 0 373450 8.0500 NaN S

Look at the data info, we know that there are missing data at Age, Cabin, and Embarked columns. Although Cabin is an important feature (because the position of a cabin in the ship can affect the chance of people in that cabin to survive), we will skip it in this tutorial for simplicity. In the dataset, there are many categorical columns and two numerical columns Age and Fare. Let’s first look at the distribution of those categorical columns:

[3]:

for col in ["Survived", "Pclass", "Sex", "SibSp", "Parch", "Embarked"]:
print(train_df[col].value_counts(), end="\n\n")

0    549
1    342
Name: Survived, dtype: int64

3    491
1    216
2    184
Name: Pclass, dtype: int64

male      577
female    314
Name: Sex, dtype: int64

0    608
1    209
2     28
4     18
3     16
8      7
5      5
Name: SibSp, dtype: int64

0    678
1    118
2     80
5      5
3      5
4      4
6      1
Name: Parch, dtype: int64

S    644
C    168
Q     77
Name: Embarked, dtype: int64



## Prepare data¶

First, we will merge rare groups in SibSp and Parch columns together. In addition, we’ll fill 2 missing entries in Embarked by the mode S. Note that we can make a generative model for those missing entries in Embarked but let’s skip doing so for simplicity.

[4]:

train_df.SibSp.clip(0, 1, inplace=True)
train_df.Parch.clip(0, 2, inplace=True)
train_df.Embarked.fillna("S", inplace=True)


Looking closer at the data, we can observe that each name contains a title. We know that age is correlated with the title of the name: e.g. those with Mrs. would be older than those with Miss. (on average) so it might be good to create that feature. The distribution of titles is:

[5]:

train_df.Name.str.split(", ").str.get(1).str.split(" ").str.get(0).value_counts()

[5]:

Mr.          517
Miss.        182
Mrs.         125
Master.       40
Dr.            7
Rev.           6
Mlle.          2
Major.         2
Col.           2
Ms.            1
Don.           1
the            1
Sir.           1
Capt.          1
Jonkheer.      1
Mme.           1
Name: Name, dtype: int64


We will make a new column Title, where rare titles are merged into one group Misc..

[6]:

train_df["Title"] = (
train_df.Name.str.split(", ")
.str.get(1)
.str.split(" ")
.str.get(0)
.apply(lambda x: x if x in ["Mr.", "Miss.", "Mrs.", "Master."] else "Misc.")
)


Now, it is ready to turn the dataframe, which includes categorical values, into numpy arrays. We also perform standardization (a good practice for regression models) for Age column.

[7]:

title_cat = pd.CategoricalDtype(
categories=["Mr.", "Miss.", "Mrs.", "Master.", "Misc."], ordered=True
)
embarked_cat = pd.CategoricalDtype(categories=["S", "C", "Q"], ordered=True)
age_mean, age_std = train_df.Age.mean(), train_df.Age.std()
data = dict(
age=train_df.Age.pipe(lambda x: (x - age_mean) / age_std).values,
pclass=train_df.Pclass.values - 1,
title=train_df.Title.astype(title_cat).cat.codes.values,
sex=(train_df.Sex == "male").astype(int).values,
sibsp=train_df.SibSp.values,
parch=train_df.Parch.values,
embarked=train_df.Embarked.astype(embarked_cat).cat.codes.values,
)
survived = train_df.Survived.values
# compute the age mean for each title
age_notnan = data["age"][jnp.isfinite(data["age"])]
title_notnan = data["title"][jnp.isfinite(data["age"])]
age_mean_by_title = jnp.stack([age_notnan[title_notnan == i].mean() for i in range(5)])


## Modelling¶

First, we want to note that in NumPyro, the following models

def model1a():
x = numpyro.sample("x", dist.Normal(0, 1).expand([10])


and

def model1b():
numpyro.sample("x_obs", dist.Normal(0, 1).expand([10]), obs=x)


are equivalent in the sense that both of them have

• the same latent sites x drawn from dist.Normal(0, 1) prior,
• and the same log densities dist.Normal(0, 1).log_prob(x).

Now, assume that we observed the last 6 values of x (non-observed entries take value NaN), the typical model will be

def model2a(x):
x_impute = numpyro.sample("x_impute", dist.Normal(0, 1).expand([4]))
x_obs = numpyro.sample("x_obs", dist.Normal(0, 1).expand([6]), obs=x[4:])
x_imputed = jnp.concatenate([x_impute, x_obs])


or with the usage of mask,

def model2b(x):
x_imputed = jnp.concatenate([x_impute, x[4:]])
numpyro.sample("x", dist.Normal(0, 1).expand([10]), obs=x_imputed)


Both approaches to model the partial observed data x are equivalent. For the model below, we will use the latter method.

[8]:

def model(age, pclass, title, sex, sibsp, parch, embarked, survived=None, bayesian_impute=True):
b_pclass = numpyro.sample("b_Pclass", dist.Normal(0, 1).expand([3]))
b_title = numpyro.sample("b_Title", dist.Normal(0, 1).expand([5]))
b_sex = numpyro.sample("b_Sex", dist.Normal(0, 1).expand([2]))
b_sibsp = numpyro.sample("b_SibSp", dist.Normal(0, 1).expand([2]))
b_parch = numpyro.sample("b_Parch", dist.Normal(0, 1).expand([3]))
b_embarked = numpyro.sample("b_Embarked", dist.Normal(0, 1).expand([3]))

# impute age by Title
isnan = np.isnan(age)
age_nanidx = np.nonzero(isnan)[0]
if bayesian_impute:
age_mu = numpyro.sample("age_mu", dist.Normal(0, 1).expand([5]))
age_mu = age_mu[title]
age_sigma = numpyro.sample("age_sigma", dist.Normal(0, 1).expand([5]))
age_sigma = age_sigma[title]
age_impute = numpyro.sample(
)
age = ops.index_update(age, age_nanidx, age_impute)
numpyro.sample("age", dist.Normal(age_mu, age_sigma), obs=age)
else:
# fill missing data by the mean of ages for each title
age_impute = age_mean_by_title[title][age_nanidx]
age = ops.index_update(age, age_nanidx, age_impute)

a = numpyro.sample("a", dist.Normal(0, 1))
b_age = numpyro.sample("b_Age", dist.Normal(0, 1))
logits = a + b_age * age
logits = logits + b_title[title] + b_pclass[pclass] + b_sex[sex]
logits = logits + b_sibsp[sibsp] + b_parch[parch] + b_embarked[embarked]
numpyro.sample("survived", dist.Bernoulli(logits=logits), obs=survived)


Note that in the model, the prior for age is dist.Normal(age_mu, age_sigma), where the values of age_mu and age_sigma depend on title. Because there are missing values in age, we will encode those missing values in the latent parameter age_impute. Then we can replace NaN entries in age with the vector age_impute.

## Sampling¶

We will use MCMC with NUTS kernel to sample both regression coefficients and imputed values.

[9]:

mcmc = MCMC(NUTS(model), 1000, 1000)
mcmc.run(random.PRNGKey(0), **data, survived=survived)
mcmc.print_summary()

sample: 100%|██████████| 2000/2000 [00:18<00:00, 110.91it/s, 63 steps of size 6.48e-02. acc. prob=0.94]


mean       std    median      5.0%     95.0%     n_eff     r_hat
a      0.18      0.80      0.20     -1.14      1.50   1078.98      1.00
age_impute[0]      0.23      0.82      0.27     -1.09      1.54   2412.70      1.00
age_impute[1]     -0.09      0.83     -0.10     -1.34      1.39   1956.25      1.00
age_impute[2]      0.36      0.80      0.33     -1.01      1.60   1558.35      1.00
age_impute[3]      0.23      0.90      0.24     -1.23      1.70   2134.81      1.00
age_impute[4]     -0.65      0.89     -0.61     -2.06      0.82   2027.61      1.00
age_impute[5]      0.23      0.87      0.23     -1.26      1.50   1777.25      1.00
age_impute[6]      0.45      0.78      0.45     -0.78      1.66   1428.74      1.00
age_impute[7]     -0.66      0.91     -0.64     -2.01      0.91   2021.78      1.00
age_impute[8]     -0.09      0.87     -0.08     -1.41      1.42   1630.14      1.00
age_impute[9]      0.22      0.89      0.24     -1.42      1.56   1404.25      1.00
age_impute[10]      0.20      0.87      0.20     -1.16      1.66   2489.31      1.00
age_impute[11]      0.17      0.85      0.17     -1.28      1.51   2063.14      1.00
age_impute[12]     -0.66      0.89     -0.62     -2.18      0.72   1632.65      1.00
age_impute[13]      0.19      0.91      0.17     -1.34      1.64   2394.35      1.00
age_impute[14]     -0.02      0.85     -0.01     -1.38      1.33   1809.29      1.00
age_impute[15]      0.37      0.84      0.40     -1.04      1.66   1443.69      1.00
age_impute[16]     -1.73      0.25     -1.73     -2.11     -1.30   2062.38      1.00
age_impute[17]      0.22      0.86      0.22     -1.23      1.55   1565.91      1.00
age_impute[18]      0.23      0.90      0.23     -1.28      1.72   1483.35      1.00
age_impute[19]     -0.68      0.86     -0.66     -2.13      0.67   2069.45      1.00
age_impute[20]      0.21      0.89      0.27     -1.39      1.59   1675.83      1.00
age_impute[21]      0.19      0.89      0.22     -1.28      1.56   1984.23      1.00
age_impute[22]      0.19      0.92      0.20     -1.37      1.57   1608.50      1.00
age_impute[23]     -0.14      0.86     -0.13     -1.59      1.26   1890.99      1.00
age_impute[24]     -0.67      0.88     -0.66     -2.14      0.74   1530.94      1.00
age_impute[25]      0.17      0.89      0.19     -1.34      1.55   1767.90      1.00
age_impute[26]      0.19      0.84      0.22     -1.11      1.56   1617.58      1.00
age_impute[27]     -0.70      0.89     -0.68     -2.02      0.89   1761.35      1.00
age_impute[28]      0.60      0.76      0.62     -0.72      1.75   1645.46      1.00
age_impute[29]      0.24      0.85      0.24     -1.13      1.56   2126.36      1.00
age_impute[30]      0.22      0.84      0.23     -1.03      1.69   2796.85      1.00
age_impute[31]     -1.72      0.27     -1.72     -2.15     -1.28   2433.69      1.00
age_impute[32]      0.43      0.86      0.43     -0.93      1.81   1458.56      1.00
age_impute[33]      0.32      0.87      0.33     -1.08      1.70   1583.13      1.00
age_impute[34]     -1.72      0.28     -1.72     -2.19     -1.28   2429.56      1.00
age_impute[35]     -0.43      0.86     -0.42     -1.88      0.90   1451.65      1.00
age_impute[36]      0.30      0.87      0.29     -1.09      1.72   2084.66      1.00
age_impute[37]      0.30      0.83      0.33     -0.98      1.74   1924.58      1.00
age_impute[38]      0.35      0.79      0.33     -0.82      1.73   1980.63      1.00
age_impute[39]      0.19      0.93      0.19     -1.33      1.75   1463.15      1.00
age_impute[40]     -0.65      0.90     -0.67     -2.04      0.85   1548.78      1.00
age_impute[41]      0.20      0.86      0.21     -1.19      1.56   1889.65      1.00
age_impute[42]      0.22      0.88      0.22     -1.39      1.54   1785.76      1.00
age_impute[43]      0.21      0.82      0.23     -1.35      1.36   1663.29      1.00
age_impute[44]     -0.40      0.92     -0.40     -1.96      1.15   1745.40      1.00
age_impute[45]     -0.34      0.86     -0.33     -1.75      1.05   1380.72      1.00
age_impute[46]     -0.30      0.90     -0.30     -1.87      1.10   1237.61      1.00
age_impute[47]     -0.73      0.91     -0.73     -2.13      0.78   1467.82      1.00
age_impute[48]      0.22      0.89      0.22     -1.15      1.84   2169.66      1.00
age_impute[49]      0.43      0.77      0.43     -0.79      1.65   1839.23      1.00
age_impute[50]      0.23      0.86      0.24     -1.30      1.49   1579.39      1.00
age_impute[51]     -0.29      0.88     -0.35     -1.58      1.23   2247.95      1.00
age_impute[52]      0.36      0.82      0.38     -1.13      1.57   1606.92      1.00
age_impute[53]     -0.67      0.94     -0.65     -2.08      0.94   1587.69      1.00
age_impute[54]      0.25      0.90      0.25     -1.07      1.77   2455.98      1.00
age_impute[55]      0.33      0.88      0.33     -0.94      1.88   1593.76      1.00
age_impute[56]      0.39      0.78      0.39     -0.81      1.65   1101.71      1.00
age_impute[57]     -0.01      0.82     -0.03     -1.40      1.25   1532.71      1.00
age_impute[58]     -0.69      0.90     -0.68     -2.09      0.77   1614.68      1.00
age_impute[59]     -0.12      0.88     -0.13     -1.52      1.33   1384.61      1.00
age_impute[60]     -0.62      0.89     -0.61     -2.08      0.77   2116.31      1.00
age_impute[61]      0.22      0.83      0.24     -1.11      1.58   1581.98      1.00
age_impute[62]     -0.59      0.93     -0.60     -2.17      0.88   1528.18      1.00
age_impute[63]      0.20      0.87      0.20     -1.35      1.48   1677.96      1.00
age_impute[64]     -0.69      0.89     -0.67     -2.22      0.70   1877.18      1.00
age_impute[65]      0.41      0.75      0.42     -0.80      1.67   1501.98      1.00
age_impute[66]      0.24      0.96      0.25     -1.25      1.86   2558.45      1.00
age_impute[67]      0.32      0.73      0.34     -0.82      1.55   1678.01      1.00
age_impute[68]      0.34      0.88      0.34     -1.05      1.83   1712.65      1.00
age_impute[69]      0.24      0.92      0.24     -1.15      1.88   1170.47      1.00
age_impute[70]     -0.65      0.89     -0.64     -2.13      0.76   2018.98      1.00
age_impute[71]     -0.67      0.86     -0.69     -2.12      0.64   1625.09      1.00
age_impute[72]      0.18      0.84      0.17     -1.14      1.53   1929.52      1.00
age_impute[73]      0.38      0.77      0.37     -0.92      1.63   1696.03      1.00
age_impute[74]     -0.67      0.90     -0.66     -2.14      0.81   1683.08      1.00
age_impute[75]      0.44      0.84      0.40     -0.84      1.88   1215.51      1.00
age_impute[76]      0.20      0.86      0.22     -1.24      1.61   1899.12      1.00
age_impute[77]      0.18      0.86      0.16     -1.38      1.44   1809.40      1.00
age_impute[78]     -0.42      0.87     -0.44     -1.79      1.05   2265.80      1.00
age_impute[79]      0.20      0.84      0.20     -1.27      1.50   2009.37      1.00
age_impute[80]      0.25      0.84      0.23     -1.15      1.64   1561.96      1.00
age_impute[81]      0.26      0.88      0.29     -1.20      1.68   2251.52      1.00
age_impute[82]      0.62      0.83      0.62     -0.70      1.98   1502.80      1.00
age_impute[83]      0.21      0.86      0.20     -1.07      1.63   2044.48      1.00
age_impute[84]      0.20      0.86      0.18     -1.30      1.54   1497.56      1.00
age_impute[85]      0.25      0.88      0.24     -1.23      1.72   2379.66      1.00
age_impute[86]      0.31      0.75      0.32     -1.07      1.43   1543.45      1.00
age_impute[87]     -0.13      0.90     -0.10     -1.61      1.41   2132.30      1.00
age_impute[88]      0.21      0.90      0.25     -1.40      1.53   1824.99      1.00
age_impute[89]      0.23      0.93      0.23     -1.29      1.70   2859.52      1.00
age_impute[90]      0.40      0.82      0.39     -1.00      1.73   1416.46      1.00
age_impute[91]      0.24      0.92      0.24     -1.17      1.84   1843.64      1.00
age_impute[92]      0.20      0.83      0.19     -1.18      1.47   1970.14      1.00
age_impute[93]      0.24      0.89      0.26     -1.24      1.66   1831.49      1.00
age_impute[94]      0.21      0.90      0.17     -1.24      1.69   1975.06      1.00
age_impute[95]      0.21      0.88      0.22     -1.22      1.67   1904.83      1.00
age_impute[96]      0.34      0.90      0.33     -1.05      1.81   2090.87      1.00
age_impute[97]      0.27      0.88      0.24     -1.26      1.61   1768.14      1.00
age_impute[98]     -0.40      0.92     -0.40     -1.83      1.12   1787.86      1.00
age_impute[99]      0.16      0.91      0.14     -1.22      1.70   1518.86      1.00
age_impute[100]      0.24      0.86      0.22     -1.21      1.61   1856.56      1.00
age_impute[101]      0.20      0.88      0.21     -1.14      1.76   1967.24      1.00
age_impute[102]     -0.30      0.89     -0.28     -1.76      1.12   1795.47      1.00
age_impute[103]      0.01      0.86      0.01     -1.37      1.38   1416.70      1.00
age_impute[104]      0.25      0.92      0.26     -1.13      1.87   1643.05      1.00
age_impute[105]      0.25      0.85      0.29     -1.24      1.55   1831.86      1.00
age_impute[106]      0.25      0.87      0.24     -1.17      1.72   2185.41      1.00
age_impute[107]      0.21      0.87      0.21     -1.07      1.70   1945.87      1.00
age_impute[108]      0.34      0.86      0.35     -1.03      1.78   1666.87      1.00
age_impute[109]      0.27      0.88      0.22     -0.95      1.93   1470.86      1.00
age_impute[110]      0.32      0.76      0.35     -0.96      1.41   1737.45      1.00
age_impute[111]      0.22      0.86      0.22     -1.25      1.59   2439.65      1.00
age_impute[112]     -0.03      0.86     -0.03     -1.41      1.44   1737.27      1.00
age_impute[113]      0.22      0.87      0.23     -1.32      1.56   1211.30      1.00
age_impute[114]      0.38      0.79      0.36     -0.90      1.65   1416.21      1.00
age_impute[115]      0.20      0.88      0.19     -1.12      1.78   1437.63      1.00
age_impute[116]      0.25      0.86      0.20     -1.26      1.53   1336.80      1.00
age_impute[117]     -0.35      0.91     -0.36     -1.91      1.10   1737.09      1.00
age_impute[118]      0.23      0.94      0.23     -1.18      1.95   2347.76      1.00
age_impute[119]     -0.63      0.93     -0.66     -2.09      0.94   1681.58      1.00
age_impute[120]      0.60      0.80      0.59     -0.54      2.06   1437.89      1.00
age_impute[121]      0.21      0.85      0.23     -1.06      1.69   2152.47      1.00
age_impute[122]      0.22      0.82      0.20     -1.07      1.65   2296.29      1.00
age_impute[123]     -0.38      0.94     -0.39     -2.01      1.03   1557.61      1.00
age_impute[124]     -0.61      0.92     -0.63     -2.13      0.86   1450.41      1.00
age_impute[125]      0.24      0.93      0.23     -1.16      1.74   2397.23      1.00
age_impute[126]      0.21      0.84      0.21     -1.06      1.72   2248.42      1.00
age_impute[127]      0.36      0.87      0.36     -0.96      1.81   1663.20      1.00
age_impute[128]      0.24      0.90      0.25     -1.34      1.62   1790.40      1.00
age_impute[129]     -0.71      0.88     -0.67     -2.07      0.84   2155.52      1.00
age_impute[130]      0.19      0.85      0.17     -1.29      1.50   1766.80      1.00
age_impute[131]      0.25      0.88      0.25     -1.12      1.76   1891.41      1.00
age_impute[132]      0.32      0.88      0.32     -1.21      1.70   2209.15      1.00
age_impute[133]      0.22      0.89      0.20     -1.21      1.66   1656.09      1.00
age_impute[134]     -0.10      0.91     -0.14     -1.51      1.46   2255.33      1.00
age_impute[135]      0.22      0.85      0.23     -1.04      1.66   1534.46      1.00
age_impute[136]      0.18      0.84      0.17     -1.21      1.55   2292.26      1.00
age_impute[137]     -0.69      0.88     -0.68     -2.29      0.63   2473.01      1.00
age_impute[138]      0.19      0.93      0.18     -1.36      1.65   2256.20      1.00
age_impute[139]      0.20      0.85      0.19     -1.16      1.59   1589.73      1.00
age_impute[140]      0.40      0.79      0.41     -0.90      1.66   2200.96      1.00
age_impute[141]      0.24      0.90      0.22     -1.14      1.73   1805.79      1.00
age_impute[142]     -0.32      0.92     -0.32     -1.82      1.19   1755.92      1.00
age_impute[143]     -0.15      0.86     -0.14     -1.58      1.22   1850.64      1.00
age_impute[144]     -0.67      0.94     -0.66     -2.21      0.80   1812.97      1.00
age_impute[145]     -1.75      0.25     -1.75     -2.17     -1.36   1786.83      1.00
age_impute[146]      0.35      0.84      0.34     -1.02      1.66   2006.44      1.00
age_impute[147]      0.26      0.89      0.27     -1.27      1.61   1800.77      1.00
age_impute[148]     -0.67      0.88     -0.65     -2.13      0.68   1832.48      1.00
age_impute[149]      0.29      0.83      0.29     -1.07      1.59   2181.78      1.00
age_impute[150]      0.22      0.87      0.23     -1.20      1.63   1788.63      1.00
age_impute[151]      0.20      0.87      0.17     -1.19      1.62   1561.13      1.00
age_impute[152]      0.01      0.83     -0.01     -1.35      1.38   2966.42      1.00
age_impute[153]      0.19      0.91      0.22     -1.44      1.55   2302.81      1.00
age_impute[154]      1.06      0.96      1.07     -0.53      2.59   1612.33      1.00
age_impute[155]      0.22      0.81      0.22     -1.08      1.51   1460.31      1.00
age_impute[156]      0.27      0.94      0.25     -1.40      1.75   1409.05      1.00
age_impute[157]      0.22      0.92      0.21     -1.26      1.70   1705.55      1.00
age_impute[158]      0.22      0.87      0.22     -1.08      1.72   1561.10      1.00
age_impute[159]      0.21      0.90      0.22     -1.30      1.56   1366.89      1.00
age_impute[160]      0.18      0.84      0.14     -1.15      1.46   1437.73      1.00
age_impute[161]     -0.49      0.92     -0.52     -1.91      1.09   1357.29      1.00
age_impute[162]      0.37      0.84      0.37     -0.98      1.72   1971.90      1.00
age_impute[163]      0.31      0.84      0.28     -1.05      1.71   1541.32      1.00
age_impute[164]      0.22      0.85      0.21     -1.23      1.60   2056.93      1.00
age_impute[165]      0.23      0.88      0.22     -1.12      1.65   2037.07      1.00
age_impute[166]     -0.11      0.87     -0.11     -1.49      1.33   1851.82      1.00
age_impute[167]      0.21      0.89      0.21     -1.10      1.73   1777.15      1.00
age_impute[168]      0.20      0.83      0.21     -1.12      1.56   1793.28      1.00
age_impute[169]      0.02      0.88      0.01     -1.40      1.43   2410.48      1.00
age_impute[170]      0.16      0.88      0.16     -1.29      1.60   2230.68      1.00
age_impute[171]      0.41      0.83      0.40     -0.96      1.75   1846.52      1.00
age_impute[172]      0.24      0.89      0.22     -1.32      1.54   1852.66      1.00
age_impute[173]     -0.44      0.87     -0.45     -1.92      0.92   2089.83      1.00
age_impute[174]      0.22      0.82      0.23     -1.12      1.55   2427.19      1.00
age_impute[175]      0.22      0.96      0.26     -1.45      1.79   2380.77      1.00
age_impute[176]     -0.43      0.89     -0.41     -1.84      1.06   1803.21      1.00
age_mu[0]      0.19      0.04      0.19      0.12      0.27   1509.52      1.00
age_mu[1]     -0.55      0.08     -0.55     -0.67     -0.43   1224.77      1.00
age_mu[2]      0.42      0.08      0.42      0.30      0.56   1212.38      1.00
age_mu[3]     -1.73      0.04     -1.73     -1.79     -1.64   1274.40      1.00
age_mu[4]      0.85      0.19      0.85      0.55      1.16   1387.85      1.00
age_sigma[0]      0.88      0.03      0.88      0.83      0.93    646.28      1.00
age_sigma[1]      0.90      0.05      0.90      0.82      0.98   1120.20      1.00
age_sigma[2]      0.79      0.05      0.79      0.71      0.88   1113.02      1.00
age_sigma[3]      0.26      0.03      0.25      0.20      0.31   1443.42      1.00
age_sigma[4]      0.94      0.13      0.92      0.74      1.16   1106.42      1.00
b_Age     -0.44      0.13     -0.44     -0.66     -0.24    832.14      1.00
b_Embarked[0]     -0.30      0.53     -0.30     -1.14      0.59    525.77      1.00
b_Embarked[1]      0.27      0.54      0.28     -0.65      1.08    572.29      1.00
b_Embarked[2]      0.02      0.55      0.02     -1.00      0.78    524.41      1.00
b_Parch[0]      0.44      0.57      0.46     -0.52      1.35    484.44      1.00
b_Parch[1]      0.10      0.57      0.10     -0.76      1.11    494.90      1.00
b_Parch[2]     -0.49      0.56     -0.48     -1.42      0.38    498.59      1.00
b_Pclass[0]      1.16      0.56      1.17      0.32      2.15    475.02      1.00
b_Pclass[1]      0.02      0.55      0.04     -0.87      0.94    496.89      1.00
b_Pclass[2]     -1.23      0.56     -1.20     -2.20     -0.37    477.51      1.00
b_Sex[0]      1.18      0.70      1.16     -0.05      2.24    691.88      1.00
b_Sex[1]     -1.00      0.69     -1.01     -2.12      0.11    802.26      1.00
b_SibSp[0]      0.26      0.63      0.28     -0.78      1.25    779.73      1.00
b_SibSp[1]     -0.19      0.64     -0.18     -1.15      0.91    775.18      1.00
b_Title[0]     -0.96      0.57     -0.96     -1.85      0.02    521.30      1.00
b_Title[1]     -0.34      0.62     -0.33     -1.40      0.62    695.62      1.00
b_Title[2]      0.54      0.63      0.54     -0.42      1.58    672.71      1.00
b_Title[3]      1.46      0.64      1.46      0.32      2.39    708.52      1.00
b_Title[4]     -0.66      0.62     -0.68     -1.61      0.49    635.26      1.00

Number of divergences: 0


To double check that the assumption “age is correlated with title” is reasonable, let’s look at the infered age by title. Recall that we performed standarization on age, so here we need to scale back to original domain.

[10]:

age_by_title = age_mean + age_std * mcmc.get_samples()["age_mu"].mean(axis=0)
dict(zip(title_cat.categories, age_by_title))

[10]:

{'Mr.': 32.434227,
'Miss.': 21.763992,
'Mrs.': 35.852997,
'Master.': 4.6297398,
'Misc.': 42.081936}


The infered result confirms our assumption that Age is correlated with Title:

• those with Master. title has pretty small age (in other words, they are children in the ship) comparing to the other groups,
• those with Mrs. title have larger age than those with Miss. title (in average).

We can also see that the result is similar to the actual statistical mean of Age given Title in our training dataset:

[11]:

train_df.groupby("Title")["Age"].mean()

[11]:

Title
Master.     4.574167
Misc.      42.384615
Miss.      21.773973
Mr.        32.368090
Mrs.       35.898148
Name: Age, dtype: float64


So far so good, we have many information about the regression coefficients together with imputed values and their uncertainties. Let’s inspect those results a bit:

• The mean value -0.44 of b_Age implies that those with smaller ages have better chance to survive.
• The mean value (1.11, -1.07) of b_Sex implies that female passengers have higher chance to survive than male passengers.

## Prediction¶

In NumPyro, we can use Predictive utility for making predictions from posterior samples. Let’s check how well the model performs on the training dataset. For simplicity, we will get a survived prediction for each posterior sample and perform the majority rule on the predictions.

[12]:

posterior = mcmc.get_samples()
survived_pred = Predictive(model, posterior)(random.PRNGKey(1), **data)["survived"]
survived_pred = (survived_pred.mean(axis=0) >= 0.5).astype(jnp.uint8)
print("Accuracy:", (survived_pred == survived).sum() / survived.shape[0])
confusion_matrix = pd.crosstab(
pd.Series(survived, name="actual"), pd.Series(survived_pred, name="predict")
)
confusion_matrix / confusion_matrix.sum(axis=1)

Accuracy: 0.8271605

[12]:

predict 0 1
actual
0 0.876138 0.198830
1 0.156648 0.748538

This is a pretty good result using a simple logistic regression model. Let’s see how the model performs if we don’t use Bayesian imputation here.

[13]:

mcmc.run(random.PRNGKey(2), **data, survived=survived, bayesian_impute=False)
posterior_1 = mcmc.get_samples()
survived_pred_1 = Predictive(model, posterior_1)(random.PRNGKey(2), **data)["survived"]
survived_pred_1 = (survived_pred_1.mean(axis=0) >= 0.5).astype(jnp.uint8)
print("Accuracy:", (survived_pred_1 == survived).sum() / survived.shape[0])
confusion_matrix = pd.crosstab(
pd.Series(survived, name="actual"), pd.Series(survived_pred_1, name="predict")
)
confusion_matrix / confusion_matrix.sum(axis=1)
confusion_matrix = pd.crosstab(
pd.Series(survived, name="actual"), pd.Series(survived_pred_1, name="predict")
)
confusion_matrix / confusion_matrix.sum(axis=1)

sample: 100%|██████████| 2000/2000 [00:11<00:00, 166.79it/s, 63 steps of size 7.18e-02. acc. prob=0.93]

Accuracy: 0.82042646

[13]:

predict 0 1
actual
0 0.872495 0.204678
1 0.163934 0.736842

We can see that Bayesian imputation does a little bit better here.

Remark. When using posterior samples to perform prediction on the new data, we need to marginalize out age_impute because those imputing values are specific to the training data:

posterior.pop("age_impute")
survived_pred = Predictive(model, posterior)(random.PRNGKey(3), **new_data)


## References¶

1. McElreath, R. (2016). Statistical Rethinking: A Bayesian Course with Examples in R and Stan.
2. Kaggle competition: Titanic: Machine Learning from Disaster