
from threeML import *

%matplotlib inline
jtplot.style(context="talk", fscale=1, ticks=True, grid=False)
import matplotlib.pyplot as plt
plt.style.use("mike")


"""
Explanation: Random Variates
When we perform a fit or load and analysis result, the parmeters of our model become distributions in the AnalysisResults object. These are actaully instantiactions of the RandomVaraiates class. 
While we have covered most of the functionality of RandomVariates in the AnalysisResults section, we want to highlight a few of the details here.
End of explanation
"""


ar = load_analysis_results('test_mle.fits')

"""
Explanation: Let's load back our fit of the line + gaussian from the AnalysisResults section.
End of explanation
"""


ar.display()

"""
Explanation: When we display our fit, we can see the parameter paths of the model. What if we want specific information on a parameter(s)?
End of explanation
"""


norm = ar.get_variates('fake.spectrum.main.composite.F_2')

"""
Explanation: Let's take a look at the normalization of the gaussian. To access the parameter, we take the parameter path, and we want to get the variates:
End of explanation
"""


type(norm)

"""
Explanation: Now, norm is a RandomVariate.
End of explanation
"""


norm

"""
Explanation: This is essentially a wrapper around numpy NDArray with a few added properties. It is an array of samples. In the MLE case, they are samples from the covariance matrix (this is not at all a marginal distribution, but the parameter "knows" about the entire fit, i.e., it is not a profile) and in the Bayesian case, these are samples from the posterior (this is a marginal).
The output representation for an RV are its 68% equal-tail and HPD uncertainties.
End of explanation
"""


norm.equal_tail_interval(cl=0.95)

norm.highest_posterior_density_interval(cl=0.5)

"""
Explanation: We can access these directly, and to any desired confidence level.
End of explanation
"""


fig, ax = plt.subplots()

ax.hist(norm.samples,bins=50, ec='k', fc='w', lw=1.2);
ax.set_xlabel('norm');

"""
Explanation: As stated above, the RV is made from samples. We can histogram them to show this explicitly.
End of explanation
"""


log_norm = np.log10(norm)
log_norm

fig, ax = plt.subplots()

ax.hist(log_norm.samples,bins=50,ec='k', fc='w', lw=1.2);
ax.set_xlabel('log norm');

"""
Explanation: We can easily transform the RV through propagation.
End of explanation
"""


