${\rm Pr}(\theta|d,H) = \frac{1}{{\rm Pr}(d|H)}\;{\rm Pr}(d|\theta,H)\;{\rm Pr}(\theta|H)$
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)$
Note the huge product over pixel values!
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\}$. This is just Bayes Theorem rearranged, with the normalizing denominator appearing on the left hand side instead.
In [2]:
%load_ext autoreload
%autoreload 2
In [3]:
from __future__ import print_function
import numpy as np
import cluster
In [4]:
lets = cluster.XrayData()
lets.read_in_data()
lets.set_up_maps()
In [5]:
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 [6]:
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 # HACK
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 [7]:
npix = 15
# Initial guess at the interesting range of cluster position parameters:
#xmin,xmax = 310,350
#ymin,ymax = 310,350
# Refinement, found by fiddling around a bit:
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='none', extent=[xmin,xmax,ymin,ymax])
plt.xlabel('x / pixels')
plt.ylabel('y / pixels')
Out[12]:
The posterior mean, $\langle x \rangle = \int\;{\rm Pr}(x|d,H)\;dx$
The posterior median, $x_{50}$ s.t. $\int_{x_{50}}^{\infty}\;{\rm Pr}(x|d,H)\;dx = 0.5$
The $N^{th}$ percentile, $x_{N}$ s.t. $\int_{x_{N}}^{\infty}\;{\rm Pr}(x|d,H)\;dx = N\%$
The posterior probability that $x > a$, $\int_{a}^{\infty}\;{\rm Pr}(x|d,H)\;dx$
In [13]:
# First, double-check that the posterior PDF sums
# (i.e. approximately integrates) to 1:
print (np.sum(prob))
In [16]:
# Now, sort the pixel value of the PDF map, and
# find the cumulative distribution:
sorted = np.sort(prob.flatten())
C = sorted.cumsum()
# Find the pixel values that lie at the levels that contain
# 68% and 95% of the probability:
lvl68 = np.min(sorted[C > (1.0 - 0.68)])
lvl95 = np.min(sorted[C > (1.0 - 0.95)])
plt.imshow(prob, origin='lower', cmap='Blues', interpolation='none', extent=[xmin,xmax,ymin,ymax])
plt.contour(prob.T,[lvl95,lvl68],colors='black',extent=[xmin,xmax,ymin,ymax])
plt.xlabel('x / pixels')
plt.ylabel('y / pixels')
plt.savefig("figures/cluster_2D-inferred-xy.png")
In the next example, we'll look at some 1D marginalised distributions.
In [ ]: