Week 2, Wed, 4/9

Week 2, Wed, 4/9#

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
n = 1000000
x = np.random.uniform(0, 1, n)
plt.hist(x, bins=10, edgecolor='black', density=True)
(array([0.99501098, 0.99885098, 1.00212099, 1.00326099, 1.00070099,
        1.00075099, 1.00176099, 1.00128099, 0.99957098, 0.99670098]),
 array([3.20964129e-07, 1.00000222e-01, 2.00000124e-01, 3.00000025e-01,
        3.99999927e-01, 4.99999828e-01, 5.99999730e-01, 6.99999631e-01,
        7.99999533e-01, 8.99999434e-01, 9.99999336e-01]),
 <BarContainer object of 10 artists>)
../_images/fb060276dc13051c86084aa3574d972bdd4b39c9f1a43d5aff00025e4ea26cce.png
sns.histplot(x, bins=5, edgecolor='black', stat='probability')
<Axes: ylabel='Probability'>
../_images/a5b7477e5e389ca6abfe4175f6d89ef775862168a63f745b141c5028689c38fb.png
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# Generate data from a uniform distribution
np.random.seed(42)
N = 1000
data = np.random.uniform(0, 1, N)
nbins = 10

# Create a 1x3 figure to show different types of histograms
fig, axs = plt.subplots(1, 3, figsize=(18, 5))

# Plot histogram showing frequency
# axs[0].hist(data, bins=20, edgecolor='black')
sns.histplot(data, bins=nbins, edgecolor='black', ax=axs[0])
axs[0].set_title('Histogram: Frequency')
axs[0].set_xlabel('Value')
axs[0].set_ylabel('Frequency')

# show probability
#  bar heights sum to 1
sns.histplot(data, bins=nbins, edgecolor='black', stat='probability', ax=axs[1])
axs[1].set_title('Histogram: Probability')
axs[1].set_xlabel('Value')
axs[1].set_ylabel('Probability')

# Plot histogram showing probability density
#  area under the histogram sums to 1
sns.histplot(data, bins=nbins, edgecolor='black', stat='density', ax=axs[2])
axs[2].set_title('Histogram: Probability Density')
axs[2].set_xlabel('Value')
axs[2].set_ylabel('Probability Density')



# Adjust layout and display the plot
plt.tight_layout()
plt.show()
../_images/da22b16e09fe69e135c669b41ae197887d8f62ce6e136153367c4428ecdd27a3.png
n = 100
x = np.random.randint(1, 7, n)
x
array([4, 6, 3, 5, 1, 5, 6, 6, 1, 2, 1, 4, 3, 2, 1, 5, 2, 2, 1, 2, 5, 6,
       2, 6, 5, 1, 3, 2, 3, 5, 2, 6, 4, 3, 2, 3, 2, 1, 6, 3, 4, 4, 5, 2,
       3, 5, 3, 2, 4, 1, 4, 5, 6, 1, 5, 2, 1, 5, 2, 4, 6, 2, 3, 2, 2, 6,
       3, 5, 5, 2, 3, 3, 6, 4, 5, 6, 1, 5, 6, 5, 4, 1, 4, 1, 2, 6, 1, 4,
       5, 1, 6, 1, 3, 5, 6, 6, 2, 4, 2, 6])
plt.hist(x, bins=6, range=(0.5, 6.5),edgecolor='black')
(array([16., 21., 14., 13., 18., 18.]),
 array([0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5]),
 <BarContainer object of 6 artists>)
../_images/59176616165e1d8c8ce7e587e3c7556dd7e50db532b6c3e26d2cbd220d1ba3db.png
sns.histplot(x,discrete=True)
<Axes: ylabel='Count'>
../_images/d2c7264e512f17e8a3aeb8c28d7120a17001198f1e937804765aa8348887621c.png
N = 10000
x = np.random.randint(1,7, (N,3))
max_each_round = x.max(axis=1)
max_each_round>4
array([ True, False, False, ...,  True,  True, False])
np.sum(max_each_round>4)/N
0.7074
(x.max(axis=1)>4).sum()/N
0.7074
N = 1000
x = np.random.uniform(0,1,size=(N,1))
y = np.random.uniform(0,1,size=(N,1))
r = np.sqrt(x**2+y**2)
(r<1).sum()/N
0.763
inside = r <= 1
plt.scatter(x[inside], y[inside], color='blue', alpha=0.5)
plt.scatter(x[~inside], y[~inside], color='red', alpha=0.5)
plt.axis('equal')
(-0.04960800372068671,
 1.048686021119046,
 -0.04554278212617771,
 1.0453759840903212)
../_images/97ccd474f08ecd1e76c76f7c65bc22ae2eeb17d99da487170c3584761979e2bd.png
np.pi/4
0.7853981633974483
N = 100000
x = np.random.randint(1,7,(N,3))
x.max(axis=1)
array([6, 5, 4, ..., 4, 5, 6])
x.max(axis=1).mean()
4.95567