<jupyter_start><jupyter_text>Monte Carlo piCharpter 2, page 38<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import seaborn as sns
import tqdm

%matplotlib inline
sns.set(font_scale=1.5)
N = 10000
x, y = np.random.uniform(-1, 1, size=(2, N))
inside = (x**2 + y**2) <= 1
pi = inside.sum()*4/N
error = abs((pi - np.pi)/pi)* 100
outside = np.invert(inside)
plt.plot(x[inside], y[inside], 'b.')
plt.plot(x[outside], y[outside], 'r.')
plt.plot(0, 0, label='$\hat \pi$ = {:4.3f}\nerror = {:4.3f}%'.
format(pi, error), alpha=0)
plt.axis('square')
plt.legend(frameon=True, framealpha=0.9, fontsize=16);
N = np.logspace(1, 7, 50)
 
ans = []
for nn in tqdm.tqdm_notebook(N):
    x, y = np.random.uniform(-1, 1, size=(2, int(nn)))
    inside = (x**2 + y**2) <= 1
    pi = inside.sum()*4/int(nn)
    error = abs((pi - np.pi)/pi)* 100
    outside = np.invert(inside)
    ans.append((nn, pi, error))
ans = np.asarray(ans)
plt.errorbar(ans[:,0], ans[:,1], yerr=ans[:,2])
plt.xscale('log')
plt.ylim((2.5, 3.5))
plt.axhline(np.pi, lw=1, c='r', ls='--')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.22312556458897922, 'fraction_numerical': 0.04787714543812105, 'mean_word_length': 4.404878048780488, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '449991', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 534, 'n_tokens_neox': 490, 'n_words': 107, 'path': 'BayesianAnalysisWithPython/PI.ipynb', 'repo_name': 'balarsen/pymc_learning', 'size': '1107'}