# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Distribution Sampling
#
# This notebook explores lognormal and normally distributed data

import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline

rng = np.random.RandomState(seed=42)

# First, let's create a sample randomly drawn from a lognormal distribution

mu, sigma = 3., 1.
samps = rng.lognormal(mu, sigma, 1000)

# Visualize the samples

# +
fig = plt.figure(figsize=(12,4))

ax1 = fig.add_subplot(1,2,1)
count, bins, _ = ax1.hist(samps, 100, density=True, align='mid')
x = np.linspace(min(bins), max(bins), 10000)
pdf = (np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2)) / (x * sigma * np.sqrt(2 * np.pi)))
ax1.plot(x, pdf, linewidth=2, color='r')
ax1.set_title('Randomly sampled Lognormal Data')

ax2 = fig.add_subplot(1,2,2)
_, bins2, _ = ax2.hist(np.log1p(samps), 100, density=True, align='mid')
ax2.plot(bins2, 1/(sigma * np.sqrt(2 * np.pi)) * np.exp( - (bins2 - mu)**2 / (2 * sigma**2) ), linewidth=2, color='r')
ax2.set_title('Randomly sampled Lognormal Data, Log-Transformed')

plt.tight_layout()
plt.show()
# -

# Ok, we have created a lognormally distributed sample of size n = 1000.  Applying a log-transform to the data confirms it is lognormally distributed, meaning it is normally distributed in log-space.
#
# Something of interest from the numpy documentation is that np.random.RandomState().lognormal(mu, sigma) actually samples from an underlying normal distribution of mean, mu, and standard deviation, sigma.
#
# This seems to be a consequence (or maybe feature) of the Central Limit Theorem ... more on this later.
#
# Let's now measure the uncertainty in this distribution using random sampling with replacement, also known as *bootstrap*.



StarcoderdataJupyterScriptsDedupFiltered