<jupyter_start><jupyter_text>Sample 4.1 random drawing points from a uniform distribution<jupyter_code>%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import time
matplotlib.rc('xtick', labelsize=12) 
matplotlib.rc('ytick', labelsize=12) 

#generate random number between 1 and 3 following an uniform density
a = 1.
b = 3.
np.random.seed(np.int(time.time()))
x1 = np.random.uniform(a,b,size=1000)
x2 = np.random.uniform(a,b,size=100000)
x3 = np.random.uniform(a,b,size=10000000)

#analyze the random samples with a histogram
xgrid = np.arange(1,3,0.1)
xcenter = (xgrid[1:]+xgrid[:-1])/2.         
hx1,xedge = np.histogram(x1,xgrid)
hx2,xedge = np.histogram(x2,xgrid)
hx3,xedge = np.histogram(x3,xgrid)

#draw the histogram
fig = plt.figure(figsize=[10,5])
ax = fig.add_subplot(111)
ax.plot(xcenter,hx1/np.sum(hx1),'ko-')
ax.plot(xcenter,hx2/np.sum(hx2),'ro-')
ax.plot(xcenter,hx3/np.sum(hx3),'b^-',markerfacecolor='none')

#fig.show()
fig.savefig('unifrand_hist.png',bbox_inches='tight')

print('Uniform distribution')
print('a=%(a).2f, b=%(b).2f' %{'a':a,'b':b})
print('E(x)=%(m).3f\tD(x)=%(d).3f' % {'m':np.mean(x1), 'd':np.var(x1)})
print('E(x)=%(m).3f\tD(x)=%(d).3f' % {'m':np.mean(x2), 'd':np.var(x2)})
print('E(x)=%(m).3f\tD(x)=%(d).3f' % {'m':np.mean(x3), 'd':np.var(x3)})
'''
demonstrate how to draw a histogram aligning with different grids
'''
fig = plt.figure(figsize=[6,6])
x = np.array([0.2,0.3,1.2,1.6,1.7,1.8,2.4,2.5,2.9])
xgrid = np.arange(-1,5,1)
xcenter = (xgrid[1:]+xgrid[:-1])/2.
h,xedge = np.histogram(x,bins=xgrid)

ax = fig.add_subplot(111)
ax.step(xedge[1:],h,'k',where='pre')
ax.step(xedge[:-1],h,'r',where='post')
ax.step(xcenter,h,'b',where='mid')
#ax.step(xcenter,h,'r',where='mid')
ax.plot(xcenter,h,'k+-')

# fig.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.24189114898295766, 'fraction_numerical': 0.055525013743815285, 'mean_word_length': 6.428571428571429, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, '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': '500133', 'license': 'MIT', 'n_tokens_mistral': 890, 'n_tokens_neox': 849, 'n_words': 119, 'path': 'sample4.1_uniform_sample.ipynb', 'repo_name': 'liuchaonaoc/Astrostatistics2022', 'size': '1819'}