Bayesian Modelling in Python
Mark Regan
Section 0: Introduction
Welcome to "Bayesian Modelling in Python" - a tutorial for those interested in learning Bayesian statistics in Python. You can find a list of all tutorial sections on the project's homepage.
Statistics is a topic that never resonated with me throughout my years in university. The frequentist techniques that we were taught (p-values, etc.) felt contrived and ultimately, I turned my back on statistics as a topic that I wasn't interested in.
That was until I stumbled upon Bayesian statistics - a branch of statistics quite different from the traditional frequentist statistics that most universities teach. I was inspired by a number of different publications, blogs & videos that I would highly recommend any newbies to Bayesian stats to begin with. They include:
Doing Bayesian Data Analysis by John Kruschke
Python port of John Kruschke's examples by Osvaldo Martin
Bayesian Methods for Hackers provided me with a great source of inspiration to learn Bayesian stats. In recognition of this influence, I've adopted the same visual styles as BMH.
While My MCMC Gently Samples blog by Thomas Wiecki
Healthy Algorithms blog by Abraham Flaxman
Scipy Tutorial 2014 by Chris Fonnesbeck
I created this tutorial in the hope that others find it useful and it helps them learn Bayesian techniques just like the above resources helped me. I'd welcome any corrections/comments/contributions from the community.
Loading your Google Hangout chat data
Throughout this tutorial, we will use a dataset containing all of my Google Hangout chat messages. I've removed the messages content and anonymized my friends' names; the rest of the dataset is unaltered.
If you'd like to use your Hangout chat data whilst working through this tutorial, you can download your Google Hangout data from Google Takeout. The Hangout data is downloadable in JSON format. After downloading, you can replace the hangouts.json
file in the data folder.
The json file is heavily nested and contains a lot of redundant information. Some of the key fields are summarized below:
Field | Description | Example |
---|---|---|
conversation_id | Conversation id representing the chat thread | Ugw5Xrm3ZO5mzAfKB7V4AaABAQ |
participants | List of participants in the chat thread | [Mark, Peter, John] |
event_id | Id representing an event such as chat message or video hangout | 7-H0Z7-FkyB7-H0au2avdw |
timestamp | Timestamp | 2014-08-15 01:54:12 |
message | Content of the message sent | Went to the local wedding photographer today |
sender | Sender of the message | Mark Regan |
The below code loads the json data and parses each message into a single row in a pandas DataFrame.
Note: the data/ directory is missing the hangouts.json file. You must download and add your own JSON file as described above. Alternatively, you can skip to the next section where I import an anonymized dataset.
conversation_id | event_id | timestamp | sender | message | message_length | participants | participants_str | num_participants | message_type | prev_timestamp | prev_sender | time_delay_seconds | time_delay_mins | day_of_week | year_month | is_weekend | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
10 | Ugw5Xrm3ZO5mzAfKB7V4AaABAQ | 7-H0Z7-FkyB7-HDBYj4KKh | 2014-08-15 03:44:12.840015 | Mark Regan | Thanks guys!!! | 14 | [Keir Alexander, Louise Alexander Regan, Mark ... | Keir Alexander, Louise Alexander Regan, Mark R... | 3 | TEXT | 2014-08-15 03:44:00.781653 | Keir Alexander | 12.0 | 1.0 | 4 | 2014-08 | 0 |
We now have a data model that we can work with more easily. The above table shows a single row in the pandas DataFrame. I'm interested in how long it takes me to respond to messages. Let's create some plots that describe my typical response times.
The above plots give a monthly and an overall perspective of the length of time (in seconds) that it takes me to respond to messages. At this point I have a lot of questions that I want to ask of the data. For example:
Is my response time affected by who I am talking to?
Are there environmental factors that affect my response time (day of week, location, etc.)?
What is the best and worst day to get in touch with me?
Before we try and answer some of these questions, lets take some baby steps by estimating some parameters of a model that describes the above data. That'll make it easier for us to understand the data and inquire further.
In the next section, we'll estimate parameters that describe the above distribution.
>> Go to the Next Section
Export data for usage throughout tutorial
References
Hangout reader by Fabian Mueller