df_prior = dfwhole[(dfwhole.attacktype1 == 3) & (dfwhole.region == 6) & (dfwhole.iyear <= 2000)].
groupby(['iyear', 'country_txt']).attacktype1.count().values

## Confirming it worked.
df_prior

df_prior_mean = df_prior.mean()
print df_prior_mean

df_prior.groupby('country_txt').attacktype1.count()

dfwhole.groupby(['iyear', 'country_txt']).attacktype1.count()

"""
Explanation: Setting up my prior which are bombings/explosions in the South Asia region from 1970 up to and including 2000. I will use Pakistan and Afghanistan and bombings/explosions from 2001 till 2015 to measure the amount of difference of these types of attacks.
End of explanation
"""


df_prior_std = df_prior.std()
print df_prior_std

mean_prior_mean = df_prior_mean
mean_prior_std = df_prior_std

"""
Explanation: Setting up prior std on my df_prior
End of explanation
"""


afp = dfwhole[(dfwhole.attacktype1 == 3) & (dfwhole.country == 4) & (dfwhole.iyear > 2000)].
groupby(['iyear']).attacktype1.count().values

afp

"""
Explanation: Setting up Afghanistan to be one of my populations from 2001 through 2015.
End of explanation
"""


afp_mean = afp.mean()
print afp_mean

"""
Explanation: Getting mean for Afghanistan.
End of explanation
"""


afp_std = afp.std()
print afp_std

"""
Explanation: Settinp up the standard deviation for Afghanistan.
End of explanation
"""


pakp = dfwhole[(dfwhole.attacktype1 == 3) & (dfwhole.country == 153) & (dfwhole.iyear > 2000)].
groupby(['iyear']).attacktype1.count().values

"""
Explanation: Setting up Pakistan to be my second population from 2001 through 2015.
End of explanation
"""


pakp_mean = pakp.mean()
print pakp_mean

"""
Explanation: Getting mean for Pakistan.
End of explanation
"""


## setting up std for Pakistan
pakp_std = pakp.std()
print pakp_std

with pm.Model() as model:
    
    groupPk_mean = pm.Normal('Bombings_Pak_mean', mean_prior_mean, sd=mean_prior_std)
    groupAfg_mean = pm.Normal('Bombings_Afg_mean', mean_prior_mean, sd=mean_prior_std)

std_prior_lower = 0.01
std_prior_upper = 100.0

with model:
    
    groupPak_std = pm.Uniform('Bombings_Pak_std', lower=std_prior_lower, upper=std_prior_upper)
    groupAfg_std = pm.Uniform('Bombings_Afg_std', lower=std_prior_lower, upper=std_prior_upper)

with model:

    groupPak = pm.Normal('Bombings_Pak', mu=groupPk_mean, sd=groupPak_std, observed=pakp)
    groupAfg = pm.Normal('Bombings_Afg', mu=groupAfg_mean, sd=groupAfg_std, observed=afp)


with model:

    diff_of_means = pm.Deterministic('difference of means',groupPk_mean - groupAfg_mean)
    diff_of_stds = pm.Deterministic('difference of stds',groupPak_std - groupAfg_std)
    effect_size = pm.Deterministic('effect size',
                                   diff_of_means / np.sqrt((groupPak_std**2 + groupAfg_std**2) / 2))

with model:
    trace = pm.sample(25000, njobs=4)

pm.plot_posterior(trace[3000:],
                  varnames=['Bombings_Pak_mean', 'Bombings_Afg_mean', 'Bombings_Pak_std', 'Bombings_Afg_std'],
                  color='#87ceeb')

pm.plot_posterior(trace[3000:],
                  varnames=['difference of means', 'difference of stds', 'effect size'],
                  ref_val=0,
                  color='#87ceeb')

pm.summary(trace[3000:],
           varnames=['difference of means', 'difference of stds', 'effect size'])

afgtd = dfwhole[dfwhole.country_txt=='Afghanistan']

"""
Explanation: Setting up the standard deviation for Pakistan.
End of explanation
"""


plt.hist(afgtd['iyear'])

pkgtd = dfwhole[dfwhole.country_txt=='Pakistan']

pkgtd.tail(2)

"""
Explanation: Attacks in Afghanistan by Year
End of explanation
"""


pkgtd['attacktype1_txt'].value_counts().head(5)

"""
Explanation: Looking at attack types in Pakistan
End of explanation
"""


plt.hist(pkgtd['iyear'])

"""
Explanation: Attacks in Pakistan by Year
End of explanation
"""


