<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Basic usage
Create an instance of PyFBU
Step1:  Supply the input distribution to be unfolded as a 1-dimensional list for N bins, with each entry corresponding to the bin content.
Step2:  Supply the response matrix where each row corresponds to a truth level bin.
The normalization of each row must be the acceptance efficiency of the corresponding bin (e.g. the normalization is 1 for resolution only unfolding).
N.B. For now, only squared response matrices are allowed.
Step3:  Define the boundaries of the hyperbox to be sampled for each bin.
Step4:  Run the MCMC sampling (this step might take up to several minutes for a large number of bins).
Step5:  Retrieve the N-dimensional posterior distribution in the form of a list of N arrays.
Step6:  Each array corresponds to the projection of the posterior distribution for a given bin.
Step7:  Background
One or more backgrounds, with the corresponding normalization uncertainties (gaussian prior), can be taken into account in the unfolding procedure.
Step8:  The background normalization is sampled from a gaussian with the given uncertainty. To fix the background normalization the uncertainty should be set to 0.
Systematics
Systematic uncertainties affecting signal and background can be taken into account as well with their per-bin relative magnitudes. The prior is gaussian. Each systematics needs to be provided for each background listed at the previous step.
Step9:  Each systematics is treated as fully correlated across signal and the various backgrounds.
Nuisance parameters
The posterior probability for the nuisance parameters is stored in a dictionary of arrays. The correlation among nuisance parameters and with the estimates for the unfolded distribution is preserved in the array ordering.
<ASSISTANT_TASK:>
Python Code:
import fbu
myfbu = fbu.PyFBU()
Explanation: Basic usage
Create an instance of PyFBU
End of explanation
myfbu.data = [100,150]
Explanation: Supply the input distribution to be unfolded as a 1-dimensional list for N bins, with each entry corresponding to the bin content.
End of explanation
myfbu.response = [[0.08,0.02], #first truth bin
                  [0.02,0.08]] #second truth bin
Explanation: Supply the response matrix where each row corresponds to a truth level bin.
The normalization of each row must be the acceptance efficiency of the corresponding bin (e.g. the normalization is 1 for resolution only unfolding).
N.B. For now, only squared response matrices are allowed.
End of explanation
myfbu.lower = [0,0]
myfbu.upper = [3000,3000]
Explanation: Define the boundaries of the hyperbox to be sampled for each bin.
End of explanation
myfbu.run()
Explanation: Run the MCMC sampling (this step might take up to several minutes for a large number of bins).
End of explanation
trace = myfbu.trace
print( trace )
Explanation: Retrieve the N-dimensional posterior distribution in the form of a list of N arrays.
End of explanation
%matplotlib inline
from matplotlib import pyplot as plt
plt.hist(trace[1],
         bins=20,alpha=0.85,
         normed=True)
plt.ylabel('probability')
Explanation: Each array corresponds to the projection of the posterior distribution for a given bin.
End of explanation
myfbu.background = {'bckg1':[20,30],'bckg2':[10,10]}
myfbu.backgroundsyst = {'bckg1':0.5,'bckg2':0.04} #50% normalization uncertainty for bckg1 and 4% normalization uncertainty for bckg2
Explanation: Background
One or more backgrounds, with the corresponding normalization uncertainties (gaussian prior), can be taken into account in the unfolding procedure.
End of explanation
myfbu.objsyst = {
    'signal':{'syst1':[0.,0.03],'syst2':[0.,0.01]},
    'background':{
            'syst1':{'bckg1':[0.,0.],'bckg2':[0.1,0.1]},
            'syst2':{'bckg1':[0.,0.01],'bckg2':[0.,0.]}
    }
}
Explanation: The background normalization is sampled from a gaussian with the given uncertainty. To fix the background normalization the uncertainty should be set to 0.
Systematics
Systematic uncertainties affecting signal and background can be taken into account as well with their per-bin relative magnitudes. The prior is gaussian. Each systematics needs to be provided for each background listed at the previous step.
End of explanation
myfbu.run() #rerun sampling with backgrounds and systematics
unfolded_bin1 = myfbu.trace[1]
bckg1 = myfbu.nuisancestrace['bckg1']
plt.hexbin(bckg1,unfolded_bin1,cmap=plt.cm.YlOrRd)
Explanation: Each systematics is treated as fully correlated across signal and the various backgrounds.
Nuisance parameters
The posterior probability for the nuisance parameters is stored in a dictionary of arrays. The correlation among nuisance parameters and with the estimates for the unfolded distribution is preserved in the array ordering.
End of explanation
<END_TASK>