<jupyter_start><jupyter_text>Sample 5.3 Single parameter Bayesian model---A sample of Coma cluster<jupyter_code>%matplotlib inline
#posteior distribution of normal data
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
import astropy.io.fits as fits
import matplotlib
matplotlib.rc('xtick', labelsize=12) 
matplotlib.rc('ytick', labelsize=12) 
from astropy.table import Table

## the mean redshift of Coma cluster
filename = 'ComaCluster.fits'
coma = Table.read(filename)
cz = coma["czA"][(coma["czA"]<20000) & (coma["czA"]>0)]
sig_cz = np.var(cz)
print("sigma of cz=",np.sqrt(sig_cz))
czgrid =np.arange(2000.,12000.,750.)
h, xedge = np.histogram(cz, bins=czgrid)

fig = plt.figure(figsize=[4,4])
ax = fig.add_subplot(111)
ax.step(czgrid[:-1],h,'k',where='post')
ax.set_xlabel('redshift (km/s)')
# fig.show()
print(coma)
plt.plot(coma['_RAJ2000'],coma['_DEJ2000'],'ko')
plt.show()
#known sigma ask for mean redshift

n = len(cz)
sig = 1317.0
tao_0 = 1000.0
mu_0 = 6925.0
mu_n = (1./tao_0**2*mu_0+n/sig**2*np.mean(cz))/\
    (1./tao_0**2+n/sig**2)
tao_n = np.sqrt(1./(1./tao_0**2+n/sig**2))
print('n=%(n)d\ny_bar=%(yb).2f\nmu_0=%(m0).2f\nmu_n=%(m1).2f\nsigma=%(s).2f\ntao_0=%(t0).2f\ntao_n=%(tn).2f' %\
      {'n':n, 'yb':np.mean(cz), 'm1': mu_n, 'm0': mu_0,\
       's':sig,'t0':tao_0,'tn':tao_n})

#p(theta|sigma,y)
czgrid2 = np.arange(2000,11000,100)
y_0 = stats.norm(loc=mu_0,scale=tao_0).pdf(czgrid2)
y_n = stats.norm(loc=mu_n,scale=tao_n).pdf(czgrid2)
y_data = stats.norm(loc=np.mean(cz),scale=sig).pdf(czgrid2)


fig = plt.figure(figsize=[6,6])
ax = fig.add_subplot(111)
e0, = ax.step(czgrid[:-1],h,'b',where='post')
e1, = ax.plot(czgrid2,y_data/np.sum(y_data)*np.sum(h)/100*750,'k')
e2, = ax.plot(czgrid2,y_0/np.sum(y_0)*np.sum(h)/100*750,'k--')
e3, = ax.plot(czgrid2,y_n/np.sum(y_n)*np.sum(h)/100*750,'r')
plt.legend([e0,e1,e2,e3],['Data hist','Likelihood','Prior','Posterior'])
ax.set_xlabel('redshift (km/s)')
# fig.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.22802611752887997, 'fraction_numerical': 0.07081868407835258, 'mean_word_length': 6.16546762589928, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '282521', 'license': 'MIT', 'n_tokens_mistral': 1014, 'n_tokens_neox': 945, 'n_words': 130, 'path': 'sample5.3_Singlemodel_comacluster.ipynb', 'repo_name': 'liuchaonaoc/Astrostatistics2022', 'size': '1991'}