One of the simplest examples of data is univariate data
Let's consider a timeseries example:
The Annual Canadian Lynx Trappings Dataset as described by Campbel and Walker 1977 contains the number of Lynx trapped near the McKenzie River in the Northwest Territories in Canada between 1821 and 1934.
In [1]:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
sns.set_context('talk')
sns.set_style('darkgrid')
In [2]:
lynx = pd.read_csv('https://vincentarelbundock.github.io/Rdatasets/csv/datasets/lynx.csv',
index_col=0)
In [3]:
lynx = lynx.set_index('time')
lynx.head()
Out[3]:
In [4]:
lynx.plot(legend=False)
plt.xlabel('Year')
plt.title('Annual Canadian Lynx Trappings 1821-1934')
plt.ylabel('Lynx')
Out[4]:
Let's plot the kernel density estimate of annual lynx trapping
In [5]:
sns.kdeplot(lynx['lynx'])
plt.title('Kernel Density Estimate of Annual Lynx Trapping')
plt.ylabel('Probability')
plt.xlabel('Number of Lynx')
Out[5]:
Our plot suggests there could be three modes in the Lynx data.
In modeling this timeseries, we could assume that the number of lynx trapped in a given year is falls into one of $k$ states, which are normally distributed with some unknown mean $\mu_i$ and variance $\sigma^2_i$ for each state
In the case of our Lynx data
$$\forall i \in [1,...,k] \hspace{2mm} p(\text{lynx trapped}| \text{state} = i) \sim \mathcal{N}(\mu_i, \sigma^2_i)$$Now let's consider demographics data from the Titanic Dataset
The Titanic Dataset contains information about passengers of the Titanic.
In [6]:
ti = sns.load_dataset('titanic')
ti.head()
Out[6]:
Passenger age and fare are both real valued. Are they related? Let's examine the correlation matrix
In [7]:
ti[['age','fare']].dropna().corr()
Out[7]:
Since the correlation is between the two variables is zero, we can model these two real valued columns independently.
Let's plot the kernel density estimate of each variable
In [8]:
sns.kdeplot(ti['age'])
plt.title('Kernel Density Estimate of Passenger Age in the Titanic Datset')
Out[8]:
In [9]:
sns.kdeplot(ti['fare'])
plt.title('Kernel Density Estimate of Passenger Fare in the Titanic Datset')
Out[9]:
Given the long tail in the fare price, we might want to model this variable on a log scale:
In [10]:
ti['logfare'] = np.log(ti['fare'])
ti[['age','logfare']].dropna().corr()
Out[10]:
Again, logfare
and age
have near zero correlation, so we can again model these two variables independently
Let's see what a kernel density estimate of log fare would look like
In [11]:
sns.kdeplot(ti['logfare'])
plt.title('Kernel Density Estimate of Log Passenger Fare in the Titanic Datset')
Out[11]:
In logspace, passenger fare is multimodal, suggesting that we could model this variable with a normal distirbution
If we were to model the passenger list using our Mixture Model, we would have separate likelihoods for logfare
and age
Often, real value data is assumed to be normally distributed.
To learn the latent variables, $\mu_i$ $\sigma^2_i$, we would use a normal inverse-chi-square likelihood
The normal inverse-chi-square likelihood is the conjugate univariate normal likelihood in data microscopes. We also have normal likelihood, the normal inverse-wishart likelihood, optimized for multivariate datasets.
It is important to model univariate normal data with this likelihood as it acheives superior performance on univariate data.
In both these examples, we found variables that were amenable to being modeled as univariate normal:
To import our univariate normal inverse-chi-squared likelihood, call:
In [12]:
from microscopes.models import nich as normal_inverse_chisquared