Skip to content

Instantly share code, notes, and snippets.

@patricoferris
Created April 25, 2019 21:37
Show Gist options
  • Save patricoferris/c46e699c8afa8942d050a887b319ab20 to your computer and use it in GitHub Desktop.
Save patricoferris/c46e699c8afa8942d050a887b319ab20 to your computer and use it in GitHub Desktop.
# Generate the linear evenly spaced xs and ys
n = 50
three_xs = np.linspace(-5, 5, n)
three_ys = np.linspace(-5, 5, n)
# Generate the values for both the X and Y (two parts to the Z)
three_zs_a = np.array(list(map(lambda x: guass_func(x, 0, 1), three_xs)))
three_zs_b = np.array(list(map(lambda y: guass_func(y, 0, 2), three_ys)))
# Use the Kronecker product of the two https://docs.scipy.org/doc/numpy/reference/generated/numpy.kron.html
three_zs = np.kron(three_zs_a, three_zs_b)
fig = plt.figure(figsize=(15, 10))
ax = fig.add_subplot(111, projection='3d')
# Plot the data by forming into n x n matrices - using tile and repeat we can get the grid specified
ax.plot_surface(np.tile(three_xs, n).reshape((n, n)),
np.repeat(three_ys, n).reshape((n, n)),
three_zs.reshape((n, n)),
cmap=cm.coolwarm)
plt.title("Z the joint distribution of X~N(0, 1) and Y~N(0, 2)", y=1.03)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment