InĀ [1]:
# Code attribution: Yiyin Shen, Tyler Caraza-Harter
# Imports
import numpy
import matplotlib.pyplot as plt
InĀ [2]:
# Generate normal distribution
rng = numpy.random.default_rng(seed = 42)
x = rng.normal(0, 1, 1000)
plt.hist(x, 50)
numpy.mean(x), numpy.var(x)
Out[2]:
(-0.028891550995946837, 0.9775718960608186)
InĀ [3]:
# Generate exponential distribution
x = rng.exponential(1, 1000)
plt.hist(x, 50)
numpy.mean(x), numpy.var(x)
Out[3]:
(1.0155822620264812, 1.0529200889478467)
InĀ [4]:
# Generate a random integer
x = -numpy.log(rng.random(1000))
plt.hist(x, 50)
numpy.mean(x), numpy.var(x)
Out[4]:
(1.0005849397717568, 0.9297203293894388)
InĀ [5]:
# Create two (independent) normal distributions
x = rng.normal(0, 1, (1000, 2))
plt.scatter(x[:, 0], x[:, 1])
Out[5]:
<matplotlib.collections.PathCollection at 0x150d2568b90>
InĀ [6]:
# Create two correlated normal distributions with a correlation of 0.5
x = rng.multivariate_normal([0, 0], [[1, 0.5], [0.5, 1]], 1000)
plt.scatter(x[:, 0], x[:, 1])
Out[6]:
<matplotlib.collections.PathCollection at 0x150d3673510>
InĀ [7]:
# Create two correlated normal distributions with a correlation of -0.5
x = rng.multivariate_normal([0, 0], [[1, -0.5], [-0.5, 1]], 1000)
plt.scatter(x[:, 0], x[:, 1])
Out[7]:
<matplotlib.collections.PathCollection at 0x150d36a9150>
InĀ [8]:
# Create two correlated normal distributions with correlation 1
x = rng.multivariate_normal([0, 0], [[1, 2], [2, 4]], 1000)
plt.scatter(x[:, 0], x[:, 1])
Out[8]:
<matplotlib.collections.PathCollection at 0x150d3725190>