Statistical modeling with “Pomegranate” —fast and intuitive
By: Tirthajyoti Sarkar
Tags: Statistics, Python, Editors Pick, Programming, Data Science

Statistical modeling with “Pomegranate” —fast and intuitive

Pomegranate is a delicious fruit. It can also be a super useful Python library for statistical analysis. We will show how in this article. Tirthajyoti Sarkar Dec 6, 2020·8 min read

What is Pomegranate?

First and foremost, it is a delicious fruit.

Image source: Pixabay (Free for commercial use)

But there is a double delight for fruit-lover data scientists!

It is also a Python package that implements fast and flexible probabilistic models ranging from individual probability distributions to compositional models such as Bayesian networks and Hidden Markov Models.

The central idea behind this package is that all probabilistic models can be viewed as a probability distribution. That means they all yield probability estimates for samples and can be updated/fitted given samples and their associated weights. The primary consequence of this realization is that the implemented classes can be stacked and chained more flexibly than those available from other common packages.

What’s different from Scipy or Numpy?

This is a fair question. However, you will see that the implemented classes in the Pomegranate package are super intuitive and have uniform interfaces although they cover a wide range of statistical modeling aspects,

General distributions

Markov chains

Bayesian networks

Hidden Markov Models

Bayes classifier

It is like having useful methods from multiple Python libraries together with a uniform and intuitive API.

Let us see some cool usage of this nifty little package.

Probability distributions

Let is initialize with a NormalDistribution class.

Generate a few samples,

We can, now, easily check the probability of a sample data point (or an array of them) belonging to this distribution,

Fitting data

This is where it gets more interesting. Fitting with a data sample is super easy and fast.

As initialized above, we can check the parameters (mean and std. dev) of the n1 object. We expect them to be 5.0 and 2.0.

Now, let us create some synthetic data by adding random noise to a Gaussian.

Image generated by the author

We can fir this new data to the n1 object and then check the estimated parameters.

Phew! It looks like that n1 has updated its estimated mean and std.dev parameters to match with the input data now. The peak of the histogram is close to 4.0 from the plot and that’s what the estimated mean shows.

Create the distribution directly from a data sample

We showed how to fit data to a distribution class. Alternatively, one can create the object directly from the data,

Plotting (histogram) is natively supported

Plotting is easy on the distribution class with the `plot()` method, which also supports all the keywords for a Matplotlib histogram method. We illustrate by plotting a Beta distribution object.

Image generated by the author

Discrete distribution

This is where it gets more interesting. Instead of passing parameters to a known statistical distribution (e.g. Normal or Beta), you can pass in a dictionary where keys can be any objects and values are the corresponding probabilities.

Here is an illustration with some Hogwarts characters. Note, when we try to calculate the probability of ‘Hagrid’, we get a flat zero because the distribution does not have any finite probability for the ‘Hagrid’ object.

Fitting data to a discrete distribution

We can do much more interesting things by fitting data to a discrete distribution object. Here is an example with a fictitious DNA nucleic acid sequence. It is common to have this type of sequence data in a string, and we can read the data and calculate the probabilities of the four nucleic acids in the sequence with simple code.