import matplotlib.pyplot as plt
import numpy as np
import pints
import pints.toy

"""
Explanation: Fitting a hierarchical logistic model to German credit data
This notebook explains how to run the toy hierarchical logistic regression model example using the German credit data from [1]. In this example, we have predictors for 1000 individuals and an outcome variable indicating whether or not each individual should be given credit.
[1] "UCI machine learning repository", 2010. A. Frank and A. Asuncion. https://archive.ics.uci.edu/ml/datasets/statlog+(german+credit+data)
End of explanation
"""


logpdf = pints.toy.GermanCreditHierarchicalLogPDF(download=True)

"""
Explanation: To run this example, we need to first get the data from [1] and process it so we have dichtonomous $y\in{-1,1}$ outputs and the matrix of predictors has been standardised. In addition, we also add a column of 1s corresponding to a constant term in the regression.
If you are connected to the internet, by instantiating with x=None, Pints will fetch the data from the repo for you. If, instead, you have local copies of the x and y matrices, these can be supplied as arguments.
End of explanation
"""


x, y, z = logpdf.data()
print(z.shape)

"""
Explanation: Let's look at the data: x is a matrix of predictors and y is a vector of credit recommendations for 1000 individuals. Pints also handles processing of x into a design matrix z of all interactions between variables (including with themselves).
End of explanation
"""


xs = [
    np.random.uniform(0, 1, size=(logpdf.n_parameters())),
    np.random.uniform(0, 1, size=(logpdf.n_parameters())),
    np.random.uniform(0, 1, size=(logpdf.n_parameters())),
]

mcmc = pints.MCMCController(logpdf, len(xs), xs, method=pints.HamiltonianMCMC)
mcmc.set_max_iterations(400)

# Set up modest logging
mcmc.set_log_to_screen(True)
mcmc.set_log_interval(10)

for sampler in mcmc.samplers():
    sampler.set_leapfrog_step_size(0.02)
    sampler.set_leapfrog_steps(1)

# Run!
print('Running...')
chains = mcmc.run()
print('Done!')

"""
Explanation: Now we run HMC to fit the parameters of the model.
End of explanation
"""


results = pints.MCMCSummary(chains=chains, time=mcmc.time())
print(results)

"""
Explanation: This is clearly a much harder problem than the non-hierarchical version!
End of explanation
"""


