The uncorrelated version looks like this: import numpy as np sigma = np.random.uniform (.2. I'm trying to create two random variables which are correlated with one another, and I believe the best way is to draw from a bivariate normal distribution with given parameters (open to other ideas). Previous Post Next Post Sampling from bivariate normal in pythonĢ. pi, lw = 1, ec = 'k', fc = 'none' )) kwargs = dict ( ha = 'left', va = 'top', transform = ax. add_patch ( Ellipse ( mean, N * sigma_1, N * sigma_2, angle = alpha * 180. binary, interpolation = 'nearest', extent =, bins, bins, bins ]) # draw 1, 2, 3-sigma ellipses over the distribution for N in ( 1, 2, 3 ): ax. imshow ( H, origin = 'lower', cmap = plt. add_subplot ( 111 ) # plot a 2D histogram/hess diagram of the points H, bins = np. sqrt ( cov ) sigma_xy = cov #- # Plot the results fig = plt. seed ( 0 ) x, cov = bivariate_normal ( mean, sigma_1, sigma_2, alpha, size = 100000, return_cov = True ) sigma_x = np. # bivariate_normal() generates this covariance matrix for the # given inputs. A more # general function for this is _normal(), # which requires the user to specify the full covariance matrix. pi / 4 #- # Draw 10^5 points from a multivariate normal distribution # we use the bivariate_normal function from astroML. array () sigma_1 = 2 sigma_2 = 1 alpha = np. if "setup_text_plots" not in globals (): from otting import setup_text_plots setup_text_plots ( fontsize = 8, usetex = True ) #- # Define the mean, principal axes, and rotation of the ellipse mean = np. In that case, # you can set usetex to False. This may # result in an error if LaTeX is not installed on your system. # Note that with usetex=True, fonts are rendered with LaTeX. # Author: Jake VanderPlas # License: BSD # The figure produced by this code is published in the textbook # "Statistics, Data Mining, and Machine Learning in Astronomy" (2013) # For more information, see # To report a bug or issue, use the following forum: # !forum/astroml-general import numpy as np from matplotlib import pyplot as plt from matplotlib.patches import Ellipse from import bivariate_normal #- # This function adjusts matplotlib settings for a uniform feel in the textbook.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |