<jupyter_start><jupyter_text>AboutThis notebook is intended to introduce you to using python to simulate random processes and visualizing results.  Gaussian DistributionLet's say we want to simulate random values drawn from a Gaussian distribution. Well this is actually built right into the `numpy.random` module.<jupyter_code># Ask numpy for 1000 random numbers, drawn from a gaussian (normal) distribution
samples = np.random.randn(1000)
plt.figure()
plt.plot(samples, '.')
plt.xlabel('Sample Number')
plt.ylabel('Sample Value')<jupyter_output><empty_output><jupyter_text>Sure enough they look random. Are they gaussian? Let's plot a histogram.And we will also plot the theoretical gaussian curve on top of it.<jupyter_code>plt.figure()
plt.hist(samples, bins=30, density=True, histtype='step')

xaxis = np.linspace(-4, 4, num=100)
yvals = np.exp(-xaxis**2 / 2) / np.sqrt(2 * np.pi)
plt.plot(xaxis, yvals)

plt.xlabel('Sample Value')
plt.ylabel('Probability Density')<jupyter_output><empty_output><jupyter_text>Shifting the distributionA (normalized) gaussian has two free parameters - the width, $\sigma$, and the central value, $x_0$.Let's draw samples from a gaussian with $\sigma=1.5$ and $x_0 = -2.0$To do this, we scale the samples by $\sigma$ and offset them by $x_0$.<jupyter_code>sigma = 1.5
x0 = -2.0
samples = sigma * np.random.randn(1000) + x0<jupyter_output><empty_output><jupyter_text>Did we do it right? Let's check graphically and by calculating the standard deviation and average.<jupyter_code>plt.figure()
plt.hist(samples, bins=30, density=True, histtype='step')

xaxis = np.linspace(-10, 5, num=100)
yvals = np.exp(-(xaxis - x0)**2 / (2 * sigma**2)) / np.sqrt(2 * np.pi * sigma**2)
plt.plot(xaxis, yvals)

plt.xlabel('Sample Value')
plt.ylabel('Probability Density')

print(f'Average value = {np.mean(samples)}')
print(f'Standard dev. = {np.std(samples)}')<jupyter_output><empty_output><jupyter_text>Expectation ValuesNow that we can grab samples from an arbitrary distribution, how can we calculate an expectation value?Remember an expectation value is an average (assuming you can repeat the experiment exactly the same each time).We can calculate an expectation value for a continuous probability function using$$\left = \int_{-\infty}^{\infty}f(x)\rho(x)dx$$But to simulate it, we can use our samples as a proxy for the probability density. In other words, we can apply our function to each of our samples and average that.$$\left \approx \frac{1}{N}\sum_i f(x_i)$$Let's use that to calculate the standard deviation manually. Recall that $\sigma^2 \equiv \left = \left - \left^2$.<jupyter_code>exp_x_sq = np.sum(samples**2) / len(samples)
exp_x = np.sum(samples) / len(samples)
std = np.sqrt(exp_x_sq - exp_x**2)
print(f'Standard dev. = {std}')<jupyter_output>Standard dev. = 1.5143440719472412<jupyter_text>Uniform DistributionWe can use these ideas to check out answers for HW 1, problem 9!I strongly recommend you attempt the problem first, but this is a nice way to check your work.<jupyter_code># Random samples between zero and pi
samples = np.pi * np.random.rand(1000)

print(f'<theta> = {np.mean(samples)}')
print(f'<theta^2> = {np.mean(samples**2)}')
print(f'sigma = {np.std(samples)}')
print('')
print(f'<sin> = {np.mean(np.sin(samples))}')
print(f'<cos> = {np.mean(np.cos(samples))}')
print(f'<cos^2> = {np.mean(np.cos(samples)**2)}')<jupyter_output><theta> = 1.566217758007713
<theta^2> = 3.2812116109349363
sigma = 0.9100404086831688

<sin> = 0.634774213429035
<cos> = 0.0030290821338488706
<cos^2> = 0.4993726607049345
{'fraction_non_alphanumeric': 0.1348693453217196, 'fraction_numerical': 0.052261871312166336, 'mean_word_length': 5.729678638941399, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '292640', 'license': 'MIT', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1195, 'n_words': 364, 'path': 'notebooks/basic_random_simulations.ipynb', 'repo_name': 'WSU-Physics/phys451', 'size': '3559'}