In [1]:
# Configure Jupyter so figures appear in the notebook
%matplotlib inline
# Configure Jupyter to display the assigned value after an assignment
%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'
import numpy as np
import pandas as pd
from scipy.stats import poisson
# import classes from thinkbayes2
from thinkbayes2 import Pmf, Cdf, Suite, Joint
import thinkbayes2
import thinkplot
import pymc3 as pm
import theano.tensor as T
In [2]:
n = 60
t1 = 30
t2 = n-t1
lam1 = 4
lam2 = 2
Out[2]:
In [3]:
before = poisson(lam1).rvs(t1)
Out[3]:
In [4]:
after = poisson(lam2).rvs(t2)
Out[4]:
In [5]:
data = np.concatenate([before, after])
Out[5]:
In [6]:
class Change(Suite, Joint):
def Likelihood(self, data, hypo):
"""
data: array of counts
hypo: t, lam1, lam2
"""
# FILL THIS IN
return 1
In [ ]:
In [ ]:
To implement this model in PyMC, see Chapter 1 of Bayesian Methods for Hackers and this example from Computational Statistics in Python
In [7]:
stop
Some real data, based on this analysis from the Baltimore Sun
In [ ]:
# !wget https://raw.githubusercontent.com/baltimore-sun-data/2018-shootings-analysis/master/BPD_Part_1_Victim_Based_Crime_Data.csv
In [ ]:
df = pd.read_csv('BPD_Part_1_Victim_Based_Crime_Data.csv', parse_dates=[0])
df.head()
In [ ]:
df.shape
In [ ]:
shootings = df[df.Description.isin(['HOMICIDE', 'SHOOTING']) & (df.Weapon == 'FIREARM')]
shootings.shape
In [ ]:
grouped = shootings.groupby('CrimeDate')
In [ ]:
counts = grouped['Total Incidents'].sum()
counts.head()
In [ ]:
index = pd.date_range(counts.index[0], counts.index[-1])
In [ ]:
counts = counts.reindex(index, fill_value=0)
counts.head()
In [ ]:
counts.plot()
thinkplot.decorate(xlabel='Date',
ylabel='Number of shootings')