# nbi:hide_in
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (10, 6)
N=100 # total number of samples
# intsize the number of class intervals
class_num = 10
mean = 2
sigma = 3
mean0 = 0
sigma0 = 1
X = np.random.normal(mean, sigma, N)
def pdf_func(xdata, mu, sigma):
val = np.exp(-np.power(xdata-mu,2)/(2*sigma**2))/(sigma *np.sqrt(2*np.pi))
return val
def dens_hist(n, X=X, clas_num=class_num):
data = X[0:n]
# Add the histogram
plt.hist(data, bins="auto", density=True, color='#039be5', edgecolor='black', linewidth=1, zorder=1)
# plot the Bayesian estimator
datamean = np.mean(data)
tau2 = 1/(1/sigma0**2 + n/sigma**2)
Bmean = tau2 * ((1/sigma0**2) * mean0 + (n/sigma**2) * datamean)
maxim = np.max(data)
minim = np.min(data)
xvalues = np.linspace(minim,maxim, 1000)
plt.plot(xvalues, pdf_func(xvalues, Bmean, sigma ), linewidth=2, color="red", zorder=2)
plt.figtext(0.7,0.8, " mean = {}".format(mean)+"\n Bayes est. = {:.4f}".format(Bmean), ha="left", va="top",
backgroundcolor=(0.1, 0.1, 1, 0.15), fontsize="large")
dens_hist(50)