In [11]:
import lifelines
import pymc as pm
from pyBMA.CoxPHFitter import CoxPHFitter
import matplotlib.pyplot as plt
import numpy as np
from numpy import log
from datetime import datetime
import pandas as pd
%matplotlib inline

The first step in any data analysis is acquiring and munging the data

Our starting data set can be found here: http://jakecoltman.com in the pyData post

It is designed to be roughly similar to the output from DCM's path to conversion

Download the file and transform it into something with the columns:

id,lifetime,age,male,event,search,brand

where lifetime is the total time that we observed someone not convert for and event should be 1 if we see a conversion and 0 if we don't. Note that all values should be converted into ints

It is useful to note that end_date = datetime.datetime(2016, 5, 3, 20, 36, 8, 92165)


In [ ]:
####Data munging here

In [ ]:
###Parametric Bayes
#Shout out to Cam Davidson-Pilon

In [14]:
## Example fully worked model using toy data
## Adapted from http://blog.yhat.com/posts/estimating-user-lifetimes-with-pymc.html
## Note that we've made some corrections 

N = 2500

##Generate some random data 
lifetime = pm.rweibull( 2, 5, size = N )
birth = pm.runiform(0, 10, N)
censor = ((birth + lifetime) >= 10)
lifetime_ = lifetime.copy()
lifetime_[censor] = 10 - birth[censor]


alpha = pm.Uniform('alpha', 0, 20)
beta = pm.Uniform('beta', 0, 20)

@pm.observed
def survival(value=lifetime_, alpha = alpha, beta = beta ):
    return sum( (1-censor)*(log( alpha/beta) + (alpha-1)*log(value/beta)) - (value/beta)**(alpha))

mcmc = pm.MCMC([alpha, beta, survival ] )
mcmc.sample(50000, 30000)


 [-----------------100%-----------------] 50000 of 50000 complete in 78.1 sec

In [ ]:
pm.Matplot.plot(mcmc)
mcmc.trace("alpha")[:]

Problems:

1 - Try to fit your data from section 1 
2 - Use the results to plot the distribution of the median

Note that the media of a Weibull distribution is: $$β(log 2)^{1/α}$$


In [1]:
#### Fit to your data here

In [ ]:
#### Plot the distribution of the median

Problems:

4 - Try adjusting the number of samples for burning and thinnning
5 - Try adjusting the prior and see how it affects the estimate    

In [ ]:
#### Adjust burn and thin, both paramters of the mcmc sample function

In [2]:
#### Narrow and broaden prior

Problems:

7 - Try testing whether the median is greater than a different values

In [ ]:
#### Hypothesis testing

If we want to look at covariates, we need a new approach.

We'll use Cox proprtional hazards, a very popular regression model.

To fit in python we use the module lifelines:

http://lifelines.readthedocs.io/en/latest/


In [3]:
### Fit a cox proprtional hazards model

Once we've fit the data, we need to do something useful with it. Try to do the following things:

1 - Plot the baseline survival function

2 - Predict the functions for a particular set of features

3 - Plot the survival function for two different set of features

4 - For your results in part 3 caculate how much more likely a death event is for one than the other for a given period of time

In [4]:
#### Plot baseline hazard function

In [ ]:
#### Predict

In [ ]:
#### Plot survival functions for different covariates

In [ ]:
#### Plot some odds

Model selection

Difficult to do with classic tools (here)

Problem:

1 - Calculate the BMA coefficient values

2 - Try running with different priors

In [ ]:
#### BMA Coefficient values

In [ ]:
#### Different priors