${\rm Pr}(\theta|d,H) = \frac{1}{{\rm Pr}(d|H)}\;{\rm Pr}(d|\theta,H)\;{\rm Pr}(\theta|H)$
What you know about your model parameters given the data is what you knew about them before $\left[ {\rm Pr}(\theta|H) \right]$, combined with what the data are telling you $\left[ {\rm Pr}(d|\theta,H) \right]$.
In [1]:
# import cluster_pgm
# cluster_pgm.inverse()
from IPython.display import Image
Image(filename="cluster_pgm_inverse.png")
Out[1]:
This PGM illustrates the joint PDF for the parameters and the data, which can be factorised as:
$\prod_k \; {\rm Pr}(N_k\;|\;\mu_k(\theta),{\rm ex}_k,{\rm pb}_k,H) \; {\rm Pr}(\,\theta\,|H)$
It can also be factorised to:
${\rm Pr}(\,\theta\,|\{N_k\}\,H) \; {\rm Pr}(\{N_k\}\,|H)$
which is, up to the normalizing constant, the posterior PDF for the model parameters, given all the data $\{N_k\}$.
In [2]:
%load_ext autoreload
%autoreload 2
In [3]:
import cluster
lets = cluster.XrayData()
lets.read_in_data()
lets.set_up_maps()
In [4]:
x0,y0 = 328,328 # The center of the image is 328,328
S0,b = 0.001,1e-6 # Cluster and background surface brightness, arbitrary units
beta = 2.0/3.0 # Canonical value is beta = 2/3
rc = 12 # Core radius, in pixels
logprob = lets.evaluate_unnormalised_log_posterior(x0,y0,S0,rc,beta,b)
In [5]:
print logprob
Good. Here's the code that is being run, inside the "XrayData" class:
def evaluate_log_prior(self):
# Uniform in all parameters...
return 0.0
def evaluate_log_likelihood(self):
self.make_mean_image()
# Return un-normalized Poisson sampling distribution:
# log (\mu^N e^{-\mu} / N!) = N log \mu - \mu + constant
return np.sum(self.im * np.log(self.mu) - self.mu)
def evaluate_unnormalised_log_posterior(self,x0,y0,S0,rc,beta,b):
self.set_pars(x0,y0,S0,rc,beta,b)
return self.evaluate_log_likelihood() + self.evaluate_log_prior()
Now let's try evaluating the 2D posterior PDF for cluster position, conditioned on reasonable values of the cluster and background flux, cluster size and beta:
In [6]:
import numpy as np
In [7]:
npix = 15
xmin,xmax = 327.7,328.3
ymin,ymax = 346.4,347.0
x0grid = np.linspace(xmin,xmax,npix)
y0grid = np.linspace(ymin,ymax,npix)
logprob = np.zeros([npix,npix])
for i,x0 in enumerate(x0grid):
for j,y0 in enumerate(y0grid):
logprob[j,i] = lets.evaluate_unnormalised_log_posterior(x0,y0,S0,rc,beta,b)
print "Done column",i
In [8]:
print logprob[0:5,0]
To normalize this, we need to take care not to try and exponentiate any very large or small numbers...
In [9]:
Z = np.max(logprob)
prob = np.exp(logprob - Z)
norm = np.sum(prob)
prob /= norm
In [10]:
print prob[0:5,0]
Let's plot this as a 2D probability density map.
In [11]:
import astropy.visualization as viz
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 10.0)
In [12]:
plt.imshow(prob, origin='lower', cmap='Blues', interpolation='gaussian', extent=[xmin,xmax,ymin,ymax])
plt.xlabel('x / pixels')
plt.ylabel('y / pixels')
Out[12]: